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

refactor(jans-cedarling): enhance schema parser and entity creation implementation #10549

Merged
merged 73 commits into from
Jan 16, 2025

Conversation

rmarinn
Copy link
Contributor

@rmarinn rmarinn commented Jan 4, 2025

Prepare


Description

This PR refactors the JSON schema struct and the Cedar entity-building logic to improve code readability and maintainability.

Target issue

target issue: #10513

closes #10513

Implementation Details

  1. Introduction of EntityBuilder Struct

  2. Refactoring CedarSchemaJson

    • The CedarSchemaJson struct has been redesigned for better usability.
    • Deserialization and JSON schema parsing logic is now self-contained, reducing complexity for other callers.
  3. Unit Tests

    • Added unit tests for the refactored components.
    • Future test cases will be easier to implement due to reduced coupling and improved struct/function design.
  4. Improved Automatically adding entities to the context

    • We don't need to do the "lower_snake_case convention" for entity names anymore to automatically add them to the context.
Entity Builder

The EntityBuilder struct centralizes entity creation logic and holds configuration on initialization.

Usage:

pub struct EntityBuilder {
    schema: CedarSchemaJson,
    entity_names: EntityNames,
    build_workload: bool,
    build_user: bool,
}

Function implementations on this struct will have the entity creation code. Other modules will only have to use these functions.

impl EntityBuilder {
    pub fn new(
        schema: CedarSchemaJson,
        entity_names: EntityNames,
        build_workload: bool,
        build_user: bool,
    ) -> Self {
        // ...
    }

    pub fn build_entities(
        &self,
        tokens: &DecodedTokens,
        resource: &ResourceData,
    ) -> Result<AuthorizeEntitiesData, BuildCedarlingEntityError> {
        // ...
    }
}

Test and Document the changes

  • Static code analysis has been run locally and issues have been fixed
  • Relevant unit and integration tests have been added/updated
  • Relevant documentation has been updated if any (i.e. user guides, installation and configuration guides, technical design docs etc)

Please check the below before submitting your PR. The PR will not be merged if there are no commits that start with docs: to indicate documentation changes or if the below checklist is not selected.

  • I confirm that there is no impact on the docs due to the code changes in this PR.

- implement an entity builder can can make workload entities

Signed-off-by: rmarinn <[email protected]>
- implement EntityBuilder::build_entities which builds all the
  cedarling-specific entities

Signed-off-by: rmarinn <[email protected]>
- start using the new CedarJsonSchema
- start using EntityBuilder to build entities

Signed-off-by: rmarinn <[email protected]>
- make the default type "EntityOrCommon" for unknown variants instead of
  failing desrialization.

Signed-off-by: rmarinn <[email protected]>
- fix the bug where the access_token is being used to create all token
  entities

Signed-off-by: rmarinn <[email protected]>
- fix CommonType contexts not being handled properly

Signed-off-by: rmarinn <[email protected]>
- fix entity references within entities not being qualified; i.e. the
  namespace is not included in the reference... which causes problems
  down the line

Signed-off-by: rmarinn <[email protected]>
- refactor role entities creation to not fail if no role entities were
  created but just return an empty Vec

Signed-off-by: rmarinn <[email protected]>
- silently fail non-required attr creation errors since it was making an
  existing test fail: "check_mapping_tokens_data"

Signed-off-by: rmarinn <[email protected]>
@rmarinn rmarinn requested a review from djellemah January 12, 2025 13:12
olehbozhok
olehbozhok previously approved these changes Jan 14, 2025
abaghinyan
abaghinyan previously approved these changes Jan 14, 2025
@rmarinn rmarinn dismissed stale reviews from abaghinyan and olehbozhok via 79e245c January 15, 2025 07:54
@rmarinn
Copy link
Contributor Author

rmarinn commented Jan 15, 2025

I had to resolve merge conflicts with the WASM PR so could you re-review @olehbozhok

@rmarinn rmarinn requested a review from olehbozhok January 15, 2025 08:03
olehbozhok
olehbozhok previously approved these changes Jan 15, 2025
@olehbozhok olehbozhok requested a review from abaghinyan January 15, 2025 18:20
Comment on lines 32 to 36
if regex_mapping.cedar_policy_type == type_name {
Some((claim_name, mapping))
} else {
None
}
Copy link
Contributor

Choose a reason for hiding this comment

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

(regex_mapping.cedar_policy_type == type_name).then_some((claim_name, mapping))

is quite a nice way to do it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed here bd58549

djellemah
djellemah previously approved these changes Jan 15, 2025
Copy link
Contributor

@djellemah djellemah left a comment

Choose a reason for hiding this comment

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

I'm not clear whether or not serde is inadequate to parse the json schema files. But it seems others are waiting for this PR.

@rmarinn rmarinn dismissed stale reviews from djellemah and olehbozhok via bd58549 January 16, 2025 11:00
@rmarinn
Copy link
Contributor Author

rmarinn commented Jan 16, 2025

I'm not clear whether or not serde is inadequate to parse the json schema files. But it seems others are waiting for this PR.

I'm not sure what you mean. I did used serde to parse the json schema files -- though we needed to add some since just because it's a valid json it doesn't mean that it's a valid schema as described in the cedar policy docs.

@olehbozhok olehbozhok merged commit 6c06ade into main Jan 16, 2025
27 of 30 checks passed
@olehbozhok olehbozhok deleted the jans-cedarling-10513 branch January 16, 2025 14:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp-jans-cedarling Touching folder /jans-cedarling
Projects
None yet
6 participants