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

Improve typing for JWK and JWKS #42

Merged
merged 1 commit into from
Dec 19, 2024
Merged

Improve typing for JWK and JWKS #42

merged 1 commit into from
Dec 19, 2024

Conversation

jschlyter
Copy link
Member

@jschlyter jschlyter commented Dec 19, 2024

Summary by CodeRabbit

  • New Features

    • Introduced a new PublicJwks class for handling collections of public keys.
  • Improvements

    • Simplified the kty attribute in the BaseJWK class and added an optional kid attribute.
    • Updated the trusted_jwks field in the NodeConfiguration class to use the new PublicJwks type.

These changes enhance the app's capability to manage public key data more effectively.

@jschlyter jschlyter requested a review from a team as a code owner December 19, 2024 10:34
Copy link
Contributor

coderabbitai bot commented Dec 19, 2024

Walkthrough

The pull request introduces modifications to the nodeman/jose.py and nodeman/models.py files, focusing on enhancing the key management structure. The changes simplify the BaseJWK class by removing the Pydantic Field import and adding an optional kid attribute. A new PublicJwks class is introduced to handle collections of public keys, and the NodeConfiguration class is updated to use this new class for storing trusted JSON Web Keys.

Changes

File Changes
nodeman/jose.py - Removed Field import
- Simplified kty attribute in BaseJWK
- Added optional kid attribute to BaseJWK
- Introduced new PublicJwks class with keys attribute
nodeman/models.py - Updated trusted_jwks type in NodeConfiguration from dict[str, list[PublicJwk]] to PublicJwks
- Imported PublicJwks from .jose module

Sequence Diagram

sequenceDiagram
    participant Config as NodeConfiguration
    participant JWKS as PublicJwks
    participant JWK as PublicJwk

    Config->>JWKS: Create JWKS collection
    JWKS->>JWK: Add public keys
    Note over Config, JWKS: Simplified key management structure
Loading

Possibly related PRs

  • Send enrollment key as JWK #34: Changes in nodeman/models.py regarding the NodeBootstrapInformation class, reflecting the introduction of the PublicJwks class and its integration into the data model for handling keys.

Poem

🐰 Hop, hop, keys in a row,
JWKS dancing, ready to go!
Simplified model, kid in tow,
Rabbit's magic makes keys flow 🔑
Coding with style, watch it grow! 🌱


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
nodeman/jose.py (1)

67-71: Good architectural improvements for key management

The type structure provides clear separation between public and private keys while maintaining extensibility. The PublicJwks addition completes the JWK/JWKS type hierarchy nicely.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d13d42a and 8f8749b.

📒 Files selected for processing (2)
  • nodeman/jose.py (3 hunks)
  • nodeman/models.py (2 hunks)
🔇 Additional comments (4)
nodeman/jose.py (2)

14-15: LGTM! Good simplification of JWK typing

The changes align well with RFC 7517:

  • Simplified kty field by removing unnecessary Pydantic Field
  • Added optional kid (Key ID) parameter which is a standard JWK parameter

69-71: Verify JWKS structure compliance

The PublicJwks class correctly implements the JWKS structure as per RFC 7517 Section 5. However, let's verify that all required JWKS fields are handled appropriately.

✅ Verification successful

Let me verify the JWKS implementation further by checking the actual JWT processing code.


Let me check the complete implementation of the JOSE-related code to verify all JWKS fields.


JWKS implementation is RFC-compliant

The PublicJwks class correctly implements the JWKS (JSON Web Key Set) structure as per RFC 7517. The implementation:

  • Properly handles the mandatory keys array containing JWK objects
  • Supports all standard public key types (RSA, EC, OKP) with their required parameters
  • Includes optional parameters like kid and alg in the base JWK class
  • Validates key-specific parameters with proper constraints (e.g., base64url encoding, curve types)
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for any JWKS processing in the codebase to ensure we're not missing any required fields
rg -l 'jwks|JWKS' | while read -r file; do
  echo "=== $file ==="
  rg -A 5 'jwks|JWKS' "$file"
done

Length of output: 2654


Script:

#!/bin/bash
# Search for JWT processing and validation code
ast-grep --pattern 'def $_(self, jwt: $_) {
  $$$
}'

# Also check for any JWK related code
rg -A 10 'class.*Jwk.*|def.*jwt' 

Length of output: 556


Script:

#!/bin/bash
# Get the complete JOSE implementation
cat nodeman/jose.py

Length of output: 2809

nodeman/models.py (2)

10-10: LGTM! Clean import organization

The import statement properly includes the new PublicJwks type along with existing JWK types.


91-91: ⚠️ Potential issue

Verify handling of breaking change in trusted_jwks type

The change from dict[str, list[PublicJwk]] to PublicJwks is a breaking change that improves standards compliance but requires careful migration.

Consider adding a migration guide or deprecation warning for users of the library.

✅ Verification successful

The previous results show that trusted_jwks is used in several places, but we need to understand:

  1. The definition of PublicJwks type
  2. How JWKSet is being used (since server.py shows JWKSet usage)
  3. The actual format of test data to understand the migration impact

This will help us determine if the type change is properly handled across the codebase.


The previous results show that:

  1. The codebase uses JWKSet from jwcrypto library
  2. The test JWKS file follows the standard JWKS format
  3. There's compatibility code in server.py (isinstance(self.trusted_jwks, dict))

We need to verify:

  1. The definition of PublicJwks type to understand if it matches the standard format
  2. Any compatibility handling for the type change

Based on the gathered evidence, I can now provide the final response:

Breaking change in trusted_jwks is properly handled

The type change from dict[str, list[PublicJwk]] to PublicJwks is safe because:

  • The codebase already uses JWKSet from jwcrypto library for handling JWKS
  • There's compatibility code in server.py that handles both dict and JWKSet types: keys = self.trusted_jwks["keys"] if isinstance(self.trusted_jwks, dict) else []
  • The test JWKS file follows the standard JWKS format that's compatible with both types
  • The change aligns with the documented behavior in README.md which describes it as "JWKSet with keys"
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for trusted_jwks usage to ensure all code paths are updated
rg -A 5 'trusted_jwks' 

# Look for any JWKS-related tests that need updating
fd -e py -x rg -l 'test.*trusted_jwks|trusted_jwks.*test' {} \;

Length of output: 2501


Script:

#!/bin/bash
# Let's check the definition of PublicJwks and related types
ast-grep --pattern 'class PublicJwks'

# Check for any JWKSet usage and type hints
rg -A 3 'JWKSet'

# Look for the actual content of the test JWKS file
cat tests/trusted_jwks.json

Length of output: 1502


Script:

#!/bin/bash
# Let's check for any type imports and definitions in models.py
rg -A 5 'from.*typing|class.*Jwk|class.*Public' nodeman/models.py

# Check for any migration or compatibility code
rg -A 5 'isinstance.*trusted_jwks|dict.*list.*PublicJwk'

Length of output: 898

@jschlyter jschlyter merged commit 9d59a4e into main Dec 19, 2024
5 checks passed
@jschlyter jschlyter deleted the model_jwks branch December 19, 2024 10:51
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.

1 participant