Skip to content

feat: introduce jwks#499

Merged
chrisburr merged 3 commits intoDIRACGrid:mainfrom
aldbr:main_FEAT_jwks-rotation
May 8, 2025
Merged

feat: introduce jwks#499
chrisburr merged 3 commits intoDIRACGrid:mainfrom
aldbr:main_FEAT_jwks-rotation

Conversation

@aldbr
Copy link
Copy Markdown
Contributor

@aldbr aldbr commented Apr 30, 2025

Goes from using a single key to sign/verify tokens to setting up a jwks following RFC 7517.

Introduces 2 new diracx-logic commands:

  • generate a new key and rotate the JWKS: the new key can sign/verify tokens, the old one(s) can only verify tokens.
  • delete a given key from the JWKS

Note: I have switched from authlib to joserfc to manipulate JWK and JWT as recommended in the official documentation: https://docs.authlib.org/en/latest/jose/index.html#jose-guide

It comes with the following PRs:

Migration Plan (LHCb-only I think)

  1. Get the private key
kubectl exec -it diracx-<pod_id> -- cat /signing-key/rsa256.key > rsa256.key
  1. Import in a JWKS
import json
from joserfc.jwk import RSAKey, KeySet
from uuid_utils import uuid7


with open("rsa256.key") as f:
    key = RSAKey.import_key(
        f.read(),
        {
            "key_ops": ["sign", "verify"],
            "alg": "RS256",
            "kid": uuid7().hex, 
        }
    )
    
jwks = KeySet([key])

with open("jwks.json", "w") as f:
    json.dump(jwks.as_dict(private=True), f, indent=2)
  1. Rotate the key (generate a new one)
python -m diracx.logic rotate-jwk --jwks-path jwks.json
  1. Modify values.yaml to disable to keystore generation:
helm get values diracx -o yaml > values.yaml
initKeyStore:
  enabled: false
  1. Generate the secret
kubectl create secret generic diracx-jwks \
  --namespace=$namespace \
  --from-file=jwks.json \

Once the update is done, the content of the secret is expected to be copied in jwks.json and read by diracx.

@aldbr aldbr force-pushed the main_FEAT_jwks-rotation branch 5 times, most recently from e149e28 to a0a5180 Compare May 6, 2025 07:59
@aldbr aldbr force-pushed the main_FEAT_jwks-rotation branch 2 times, most recently from 2d4316b to 2bf6265 Compare May 6, 2025 11:11
@aldbr aldbr marked this pull request as ready for review May 6, 2025 12:51
@aldbr aldbr force-pushed the main_FEAT_jwks-rotation branch from 2bf6265 to a061ff6 Compare May 7, 2025 09:59
@aldbr
Copy link
Copy Markdown
Contributor Author

aldbr commented May 7, 2025

Of course we have a chicken-and-egg issue because the repos depend on each other.

  • I will make that PR compatible with the current approach (single key), this is only required for the tests
  • I will add authlib back to container-images, along with joserfc for now

From there, we can:

  • Merge feat: replace authlib with joserfc container-images#33
  • I make a quick test with the certification environment by applying this branch, just to make sure the migration plan works fine.
    • test on diracx-cert: get an access token using the single key, update the pod, try to interact with the instance with the access token (the instance is now using a jwks with the content of the key)
    • test locally: follow the migration plan with the new helm chart
  • I follow the same procedure to create the keystore secret in lhcb-diracx instances to be ready
    • secrets are in place
    • the keystore initialization is disabled in values.yaml
  • Merge this PR
  • Merge feat: add keystore init container diracx-charts#151 and feat: replace single key by a jwks in docker-compose DIRAC#8188 (they should pass the CI)
  • I will then remove authlib from the dependencies in container-images and the compatibility with the single key in diracx.

The deployed instances should fetch the keystore instead of the single key and that should work transparently.

@aldbr aldbr force-pushed the main_FEAT_jwks-rotation branch from 847f6f5 to 5342854 Compare May 7, 2025 13:48
@aldbr aldbr force-pushed the main_FEAT_jwks-rotation branch 10 times, most recently from a867f09 to d93d82c Compare May 8, 2025 09:33
@aldbr aldbr requested a review from chaen May 8, 2025 12:00
@chrisburr chrisburr force-pushed the main_FEAT_jwks-rotation branch from d93d82c to 3f7138b Compare May 8, 2025 20:58
@chrisburr chrisburr enabled auto-merge May 8, 2025 20:58
@chrisburr chrisburr merged commit b787d8d into DIRACGrid:main May 8, 2025
24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants