T13.9 follow-up: forgive paste damage in OKTA_SYNC_PRIVATE_KEY and name the failing step (D23)
This commit is contained in:
@@ -169,18 +169,48 @@ def auth_method() -> str:
|
||||
def _load_jwk() -> dict:
|
||||
"""OKTA_SYNC_PRIVATE_KEY is the private key as a JWK. Two forms are accepted:
|
||||
the JSON itself (it starts with "{"), or that JSON encoded as base64. Use base64
|
||||
in a Portainer environment field: it has no quote marks to be mangled. Raises
|
||||
OktaSyncError without ever putting the key, or any part of it, in the message."""
|
||||
in a Portainer environment field: it has no quote marks to be mangled.
|
||||
|
||||
The reader forgives the usual copy and paste damage: quote marks around the whole
|
||||
value, spaces and line breaks inside it, missing "=" padding, the URL-safe base64
|
||||
alphabet, a byte-order mark at the start of the file, and a JWK Set
|
||||
({"keys": [one key]}) in place of a single key. Raises OktaSyncError that says
|
||||
which step failed. A message never holds any text of the key, only counts."""
|
||||
raw = _key_raw()
|
||||
if not raw:
|
||||
raise _fail("OKTA_SYNC_PRIVATE_KEY is not set")
|
||||
if len(raw) >= 2 and raw[0] == raw[-1] and raw[0] in "\"'":
|
||||
raw = raw[1:-1].strip()
|
||||
if raw.startswith("{"):
|
||||
text = raw
|
||||
else:
|
||||
compact = re.sub(r"\s+", "", raw)
|
||||
compact += "=" * (-len(compact) % 4)
|
||||
decoded = None
|
||||
for decode in (base64.b64decode, base64.urlsafe_b64decode):
|
||||
try:
|
||||
decoded = decode(compact)
|
||||
break
|
||||
except Exception: # noqa: BLE001
|
||||
continue
|
||||
if decoded is None:
|
||||
raise _fail(f"OKTA_SYNC_PRIVATE_KEY is not valid base64 ({len(compact)} characters after "
|
||||
"removing spaces; it should be one long line of letters, digits, + / =)")
|
||||
try:
|
||||
text = decoded.decode("utf-8-sig")
|
||||
except UnicodeDecodeError:
|
||||
raise _fail("OKTA_SYNC_PRIVATE_KEY decodes from base64 but the result is not text") from None
|
||||
try:
|
||||
text = raw if raw.startswith("{") else base64.b64decode(raw, validate=True).decode("utf-8")
|
||||
jwk = json.loads(text)
|
||||
except Exception: # noqa: BLE001 - any failure here is "cannot read the key"; say nothing more
|
||||
raise _fail("OKTA_SYNC_PRIVATE_KEY is not a JWK as JSON or as base64 of JSON") from None
|
||||
jwk = json.loads(text.lstrip("\ufeff"))
|
||||
except ValueError:
|
||||
raise _fail("OKTA_SYNC_PRIVATE_KEY is not JSON"
|
||||
+ (" after decoding base64" if not raw.startswith("{") else "")) from None
|
||||
if isinstance(jwk, dict) and isinstance(jwk.get("keys"), list):
|
||||
if len(jwk["keys"]) != 1:
|
||||
raise _fail("OKTA_SYNC_PRIVATE_KEY is a key set with more than one key; paste one key")
|
||||
jwk = jwk["keys"][0]
|
||||
if not isinstance(jwk, dict) or jwk.get("kty") not in {"RSA", "EC"}:
|
||||
raise _fail("OKTA_SYNC_PRIVATE_KEY is not an RSA or EC key")
|
||||
raise _fail("OKTA_SYNC_PRIVATE_KEY is JSON but not an RSA or EC key")
|
||||
if "d" not in jwk:
|
||||
raise _fail("OKTA_SYNC_PRIVATE_KEY holds a PUBLIC key; the private key is needed to sign")
|
||||
return jwk
|
||||
|
||||
Reference in New Issue
Block a user