Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add a script to enhance JWKs in preparation for move from pyjwkest #32089

Merged
merged 2 commits into from
Apr 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions lms/envs/devstack.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,19 +320,23 @@ def should_show_debug_toolbar(request): # lint-amnesty, pylint: disable=missing
}],
'JWT_SECRET_KEY': 'lms-secret',
'JWT_SIGNING_ALGORITHM': 'RS512',
'JWT_PRIVATE_SIGNING_JWK': (
'{"e": "AQAB", "d": "RQ6k4NpRU3RB2lhwCbQ452W86bMMQiPsa7EJiFJUg-qBJthN0FMNQVbArtrCQ0xA1BdnQHThFiUnHcXfsTZUwmwvTu'
'iqEGR_MI6aI7h5D8vRj_5x-pxOz-0MCB8TY8dcuK9FkljmgtYvV9flVzCk_uUb3ZJIBVyIW8En7n7nV7JXpS9zey1yVLld2AbRG6W5--Pgqr9J'
'CI5-bLdc2otCLuen2sKyuUDHO5NIj30qGTaKUL-OW_PgVmxrwKwccF3w5uGNEvMQ-IcicosCOvzBwdIm1uhdm9rnHU1-fXz8VLRHNhGVv7z6mo'
'ghjNI0_u4smhUkEsYeshPv7RQEWTdkOQ", "n": "smKFSYowG6nNUAdeqH1jQQnH1PmIHphzBmwJ5vRf1vu48BUI5VcVtUWIPqzRK_LDSlZYh'
'9D0YFL0ZTxIrlb6Tn3Xz7pYvpIAeYuQv3_H5p8tbz7Fb8r63c1828wXPITVTv8f7oxx5W3lFFgpFAyYMmROC4Ee9qG5T38LFe8_oAuFCEntimW'
'xN9F3P-FJQy43TL7wG54WodgiM0EgzkeLr5K6cDnyckWjTuZbWI-4ffcTgTZsL_Kq1owa_J2ngEfxMCObnzGy5ZLcTUomo4rZLjghVpq6KZxfS'
'6I1Vz79ZsMVUWEdXOYePCKKsrQG20ogQEkmTf9FT_SouC6jPcHLXw", "q": "7KWj7l-ZkfCElyfvwsl7kiosvi-ppOO7Imsv90cribf88Dex'
'cO67xdMPesjM9Nh5X209IT-TzbsOtVTXSQyEsy42NY72WETnd1_nAGLAmfxGdo8VV4ZDnRsA8N8POnWjRDwYlVBUEEeuT_MtMWzwIKU94bzkWV'
'nHCY5vbhBYLeM", "p": "wPkfnjavNV1Hqb5Qqj2crBS9HQS6GDQIZ7WF9hlBb2ofDNe2K2dunddFqCOdvLXr7ydRcK51ZwSeHjcjgD1aJkHA'
'9i1zqyboxgd0uAbxVDo6ohnlVqYLtap2tXXcavKm4C9MTpob_rk6FBfEuq4uSsuxFvCER4yG3CYBBa4gZVU", "kid": "devstack_key", "'
'kty": "RSA"}'
),
'JWT_PRIVATE_SIGNING_JWK': """
{
"kid": "devstack_key",
"kty": "RSA",
"key_ops": [
"sign"
],
"n": "smKFSYowG6nNUAdeqH1jQQnH1PmIHphzBmwJ5vRf1vu48BUI5VcVtUWIPqzRK_LDSlZYh9D0YFL0ZTxIrlb6Tn3Xz7pYvpIAeYuQv3_H5p8tbz7Fb8r63c1828wXPITVTv8f7oxx5W3lFFgpFAyYMmROC4Ee9qG5T38LFe8_oAuFCEntimWxN9F3P-FJQy43TL7wG54WodgiM0EgzkeLr5K6cDnyckWjTuZbWI-4ffcTgTZsL_Kq1owa_J2ngEfxMCObnzGy5ZLcTUomo4rZLjghVpq6KZxfS6I1Vz79ZsMVUWEdXOYePCKKsrQG20ogQEkmTf9FT_SouC6jPcHLXw",
"e": "AQAB",
"d": "RQ6k4NpRU3RB2lhwCbQ452W86bMMQiPsa7EJiFJUg-qBJthN0FMNQVbArtrCQ0xA1BdnQHThFiUnHcXfsTZUwmwvTuiqEGR_MI6aI7h5D8vRj_5x-pxOz-0MCB8TY8dcuK9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9DMaKEkhTGy4Y3-8mMCBVqeQ"
}
""",
'JWT_PUBLIC_SIGNING_JWK_SET': (
'{"keys": [{"kid": "devstack_key", "e": "AQAB", "kty": "RSA", "n": "smKFSYowG6nNUAdeqH1jQQnH1PmIHphzBmwJ5vRf1vu'
'48BUI5VcVtUWIPqzRK_LDSlZYh9D0YFL0ZTxIrlb6Tn3Xz7pYvpIAeYuQv3_H5p8tbz7Fb8r63c1828wXPITVTv8f7oxx5W3lFFgpFAyYMmROC'
Expand Down
52 changes: 52 additions & 0 deletions scripts/jwk-precompute-params.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env python3
# Enhance a JWK to have all of its optional performance parameters.
# Intended to be used on an RSA `JWT_PRIVATE_SIGNING_JWK`.
#
# This is needed for the change from pyjwkest to PyJWT, since the
# former accepts a partial list of optional parameters but the latter
# requires that they are either all present or all absent. The optional
# parameters provide a performance boost.
#
# Usage: Key JSON accepted on stdin; enhanced key printed to stdout.

import json
import sys

from jwt.algorithms import RSAAlgorithm


print("Paste the key's JSON, followed by a new line and Ctrl-D:\n", file=sys.stderr)
old_jwk_data = json.loads(sys.stdin.read())

# Clear out all of the precomputed private numbers
for param_key in ['p', 'q', 'dp', 'dq', 'qi']:
if param_key in old_jwk_data:
del old_jwk_data[param_key]

# Ensure that there aren't any unexpected parameters
expected_remaining = {'kty', 'e', 'd', 'n', 'kid', 'key_ops'}
unexpected_params = set(old_jwk_data.keys()) - expected_remaining
if len(unexpected_params):
print(
f"Unexpected parameters {unexpected_params} would be lost. Aborting script. "
"If your key has additional parameters that are unrelated to the precomputed "
"private numbers, then please add them to the `expected_remaining` variable "
"and re-run the script. Please consider making a PR as well.",
file=sys.stderr
)
sys.exit(1)

# Recompute private numbers
new_jwk_data = json.loads(RSAAlgorithm.to_jwk(RSAAlgorithm.from_jwk(old_jwk_data)))

# Restore the kid (key ID) param, which gets lost in the process. This adds it
# to the front of the dict. The params are actually in a really nice order in
# the native ordering that comes out of the JWK, with metadata first, then the
# core params (n, e, d), and then the precomputed values.
for restore_param in ['kid']:
if restore_param in old_jwk_data:
new_jwk_data = {restore_param: old_jwk_data[restore_param], **new_jwk_data}

# Pretty-print so that the kid and modulus can be confirmed easily
print("\n\nEnhanced private key:\n", file=sys.stderr)
print(json.dumps(new_jwk_data, indent=4))