-
Notifications
You must be signed in to change notification settings - Fork 76
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): make all tokens optional when calling Cedarling::authorize #10436
Conversation
- make the `access_token` param for the JwtService::process_tokens optional - make the `id_token` param for the JwtService::process_tokens optional - return an `Unimplemented` error when any `None` is passed in for the tokens since the `authz` module doesn't support optional params yet. Signed-off-by: rmarinn <[email protected]>
- implement JwtService::process_token which decodes and optionally validates a single JWT. Signed-off-by: rmarinn <[email protected]>
- replace the old jwt validation implementation that needs multiple tokens at once with the new implementation which validates the tokens one at a time. Signed-off-by: rmarinn <[email protected]>
- implement allowing passing `None` for some tokens when calling authorize. Signed-off-by: rmarinn <[email protected]>
Signed-off-by: rmarinn <[email protected]>
Signed-off-by: rmarinn <[email protected]>
Signed-off-by: rmarinn <[email protected]>
Signed-off-by: rmarinn <[email protected]>
implement convenience function for getting the following data from a decoded JWT: - metadata - user_mapping - claim_mapping - role_mapping - logging_info Signed-off-by: rmarinn <[email protected]>
Signed-off-by: rmarinn <[email protected]>
…nation - change order for creating workload entities to: id_token then access_token - improve `create_workload` error handling - renamed `CedarPolicyCreateTypeError` to `CreateCedarEntityError` Signed-off-by: rmarinn <[email protected]>
- change order for creating workload entities to: id_token, access_token, then userinfo_token - improve `create_user_entitiy` error handling Signed-off-by: rmarinn <[email protected]>
Signed-off-by: rmarinn <[email protected]>
- Error when both `CEDARLING_USER_AUTHZ` and `CEDARLING_WORKLOAD_AUTHZ` are disabled. Signed-off-by: rmarinn <[email protected]>
Signed-off-by: rmarinn <[email protected]>
- add `trusted` and `principal_identifier` to the TokenEntityMetadata struct so all tokens have access to them. Signed-off-by: rmarinn <[email protected]>
DryRun Security SummaryThe pull request focuses on enhancing the security and robustness of the Cedarling application by improving JWT validation, error handling, entity creation, policy schema parsing, and documentation across multiple modules and files. Expand for full summarySummary: The code changes in this pull request cover a wide range of updates and improvements to the Cedarling application, with a strong emphasis on application security. The changes span multiple modules and files, addressing various aspects of the authorization and policy enforcement mechanisms, token validation, error handling, and documentation. Some of the key security-focused changes include:
Overall, the changes in this pull request demonstrate a strong commitment to maintaining the security and integrity of the Cedarling application. The attention to detail in the error handling, input validation, and token management mechanisms is particularly noteworthy and should help to mitigate potential security risks. Files Changed:
Code AnalysisWe ran |
Signed-off-by: rmarinn <[email protected]>
- add tests for creating user entity - add tests for creating workload entity Signed-off-by: rmarinn <[email protected]>
- errors that occur because no tokens were available should now be tracable when creating user and workload entities Signed-off-by: rmarinn <[email protected]>
Signed-off-by: rmarinn <[email protected]>
Signed-off-by: rmarinn <[email protected]>
Signed-off-by: rmarinn <[email protected]>
- combine `entities::create_<token_kind>_token` functions into a single, unified function. - prevent incorrect token types from creating entities of the wrong kind (e.g., using access_tokens to create id_token entities). - simplify logic and enforce stricter type safety for token-to-entity mapping. Signed-off-by: rmarinn <[email protected]>
…ation - remove the use of access_token in the create_user_entity function - simplify the user entity creation process. Signed-off-by: rmarinn <[email protected]>
- fix field name conflict by using serde_rename for UserAuthorizeInfo and WorkloadAuthorizeInfo Signed-off-by: rmarinn <[email protected]>
Signed-off-by: rmarinn <[email protected]>
- change the token priority when creating the userinfo entity to be 1. userinfo_token then 2. id_token Signed-off-by: rmarinn <[email protected]>
Signed-off-by: rmarinn <[email protected]>
Signed-off-by: rmarinn <[email protected]>
Signed-off-by: rmarinn <[email protected]>
/// cedar-policy user/person principal | ||
pub person_principal: String, | ||
pub principal: String, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how about diagnostics
?
} | ||
} | ||
|
||
#[allow(dead_code)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why we need this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we have a TokenKind::Transaction
so i added it for completeness' sake.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i just removed it here since it seems easy enough to add back later: 87b399f.
- rename UserAuthorizeInfo.principal to user_principal - rename UserAuthorizeInfo.diagnostics to user_diagnostics - rename UserAuthorizeInfo.decision to user_decision - rename WorkloadAuthorizeInfo.principal to workload_principal - rename WorkloadAuthorizeInfo.diagnostics to workload_diagnostics - rename WorkloadAuthorizeInfo.decision to workload_decision Signed-off-by: rmarinn <[email protected]>
Signed-off-by: rmarinn <[email protected]>
Signed-off-by: rmarinn <[email protected]>
Signed-off-by: rmarinn <[email protected]>
- change `user` to `person` Signed-off-by: rmarinn <[email protected]>
Signed-off-by: rmarinn <[email protected]>
// Convert [`AuthorizeEntitiesData`] to [`cedar_policy::Entities`] structure, | ||
// hold all entities that will be used on authorize check. | ||
let entities = entities_data.entities(Some(&schema.schema))?; | ||
|
||
// Check authorize where principal is `"Jans::Workload"` from cedar-policy schema. | ||
let workload_result: Option<Response> = if self.config.authorization.use_workload_principal | ||
{ | ||
match self.execute_authorize(ExecuteAuthorizeParameters { | ||
entities: &entities, | ||
principal: principal_workload_uid.clone(), | ||
action: action.clone(), | ||
resource: resource_uid.clone(), | ||
context: context.clone(), | ||
}) { | ||
Ok(resp) => Some(resp), | ||
Err(err) => return Err(AuthorizeError::CreateRequestWorkloadEntity(err)), | ||
} | ||
} else { | ||
None | ||
}; | ||
let entities = entities_data.clone().entities(Some(&schema.schema))?; | ||
|
||
let (workload_authz_result, workload_authz_info, workload_entity_claims) = | ||
if let Some(workload) = entities_data.workload { | ||
let principal = workload.uid(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
adding 2 lines we can avoid clone whole structure of entities_data
// Convert [`AuthorizeEntitiesData`] to [`cedar_policy::Entities`] structure, | |
// hold all entities that will be used on authorize check. | |
let entities = entities_data.entities(Some(&schema.schema))?; | |
// Check authorize where principal is `"Jans::Workload"` from cedar-policy schema. | |
let workload_result: Option<Response> = if self.config.authorization.use_workload_principal | |
{ | |
match self.execute_authorize(ExecuteAuthorizeParameters { | |
entities: &entities, | |
principal: principal_workload_uid.clone(), | |
action: action.clone(), | |
resource: resource_uid.clone(), | |
context: context.clone(), | |
}) { | |
Ok(resp) => Some(resp), | |
Err(err) => return Err(AuthorizeError::CreateRequestWorkloadEntity(err)), | |
} | |
} else { | |
None | |
}; | |
let entities = entities_data.clone().entities(Some(&schema.schema))?; | |
let (workload_authz_result, workload_authz_info, workload_entity_claims) = | |
if let Some(workload) = entities_data.workload { | |
let principal = workload.uid(); | |
let workload_principal = entities_data.workload.as_ref().map(|e| e.uid()).to_owned(); | |
let user_principal = entities_data.user.as_ref().map(|e| e.uid()).to_owned(); | |
// Convert [`AuthorizeEntitiesData`] to [`cedar_policy::Entities`] structure, | |
// hold all entities that will be used on authorize check. | |
let entities = entities_data.entities(Some(&schema.schema))?; | |
let (workload_authz_result, workload_authz_info, workload_entity_claims) = | |
if let Some(workload) = workload_principal { | |
let principal = workload; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
resolved in a4cbbda
added comment |
Signed-off-by: Oleh Bohzok <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks OK to me, but need to notice about #10436 (comment)
…ling::authorize (#10436) * refactor(jans-cedarling): make process_tokens params optional - make the `access_token` param for the JwtService::process_tokens optional - make the `id_token` param for the JwtService::process_tokens optional - return an `Unimplemented` error when any `None` is passed in for the tokens since the `authz` module doesn't support optional params yet. Signed-off-by: rmarinn <[email protected]> * feat(jans-cedarling): implement processing single tokens - implement JwtService::process_token which decodes and optionally validates a single JWT. Signed-off-by: rmarinn <[email protected]> * refactor(jans-cedarling): replace jwt validation implementation - replace the old jwt validation implementation that needs multiple tokens at once with the new implementation which validates the tokens one at a time. Signed-off-by: rmarinn <[email protected]> * feat(jans-cedarling): make tokens optional when calling authorize - implement allowing passing `None` for some tokens when calling authorize. Signed-off-by: rmarinn <[email protected]> * refactor(jans-cedarling): make creation of workload entity optional Signed-off-by: rmarinn <[email protected]> * fix(jans-cedarling): user and role entity creation Signed-off-by: rmarinn <[email protected]> * refactor(jans-cedarling): make creation of user entity optional Signed-off-by: rmarinn <[email protected]> * refactor(jans-cedarling): use if-else instead of match for bool Signed-off-by: rmarinn <[email protected]> * refactor(jans-cedarling): improve functionality for decoded JWTs implement convenience function for getting the following data from a decoded JWT: - metadata - user_mapping - claim_mapping - role_mapping - logging_info Signed-off-by: rmarinn <[email protected]> * chore(jans-cedarling): removed unused functions for TrustedIssuer Signed-off-by: rmarinn <[email protected]> * refactor(jans-cedarling): improve create_workload_entity func implemenation - change order for creating workload entities to: id_token then access_token - improve `create_workload` error handling - renamed `CedarPolicyCreateTypeError` to `CreateCedarEntityError` Signed-off-by: rmarinn <[email protected]> * refactor(jans-cedarling): improve create_user_entity func implemenation - change order for creating workload entities to: id_token, access_token, then userinfo_token - improve `create_user_entitiy` error handling Signed-off-by: rmarinn <[email protected]> * chore(jans-cedarling): resolve clippy issues Signed-off-by: rmarinn <[email protected]> * refactor(jans-cedarling): error check when loading bootstrap config - Error when both `CEDARLING_USER_AUTHZ` and `CEDARLING_WORKLOAD_AUTHZ` are disabled. Signed-off-by: rmarinn <[email protected]> * refactor(jans-cedarling): add new fields to TokenEntityMetadata - add `trusted` and `principal_identifier` to the TokenEntityMetadata struct so all tokens have access to them. Signed-off-by: rmarinn <[email protected]> * refactor(jans-cedarling): remove some cloning when logging authz Signed-off-by: rmarinn <[email protected]> * test(jans-cedarling): add unit tests for entity creation - add tests for creating user entity - add tests for creating workload entity Signed-off-by: rmarinn <[email protected]> * refactor(jans-cedarling): improve error handling for entity creation - errors that occur because no tokens were available should now be tracable when creating user and workload entities Signed-off-by: rmarinn <[email protected]> * test(jans-cedarling): update python tests expected error Signed-off-by: rmarinn <[email protected]> * feat(jans-cedarling): make tokens optional in the python binding Signed-off-by: rmarinn <[email protected]> * chore(jans-cedarling): change error message to start with a lowercase letter Signed-off-by: rmarinn <[email protected]> * chore(jans-cedarling): flattened `use` statements for readability Signed-off-by: rmarinn <[email protected]> * chore(jans-cedarling): remove unused JwtProcessingError variant Signed-off-by: rmarinn <[email protected]> * refactor(jans-cedarling): simplify iterator creation Signed-off-by: rmarinn <[email protected]> * refactor(jans-cedarling): make workload entity creation optional Signed-off-by: rmarinn <[email protected]> * refactor(jans-cedarling): make user entity creation optional Signed-off-by: rmarinn <[email protected]> * chore(jans-cedarling): remove outdated comment Signed-off-by: rmarinn <[email protected]> * refactor(jans-cedarling): convert Token into a struct from an enum Signed-off-by: rmarinn <[email protected]> * refactor(jans-cedarling): unify token entity creation logic - combine `entities::create_<token_kind>_token` functions into a single, unified function. - prevent incorrect token types from creating entities of the wrong kind (e.g., using access_tokens to create id_token entities). - simplify logic and enforce stricter type safety for token-to-entity mapping. Signed-off-by: rmarinn <[email protected]> * refactor(jans-cedarling): eliminate access_token from user entity creation - remove the use of access_token in the create_user_entity function - simplify the user entity creation process. Signed-off-by: rmarinn <[email protected]> * fix(jans-cedarling): field name conflict for serializing/deserializing - fix field name conflict by using serde_rename for UserAuthorizeInfo and WorkloadAuthorizeInfo Signed-off-by: rmarinn <[email protected]> * chore(jans-cedarling): remove unused commented code Signed-off-by: rmarinn <[email protected]> * refactor(jans-cedarling): token priority when creating userinfo entity - change the token priority when creating the userinfo entity to be 1. userinfo_token then 2. id_token Signed-off-by: rmarinn <[email protected]> * chore(jans-cedarling): resolve clippy issues Signed-off-by: rmarinn <[email protected]> * test(jans-cedarling): update expected error in python bindings test Signed-off-by: rmarinn <[email protected]> * refactor(jans-cedarling): implement rename for logging info fields - rename UserAuthorizeInfo.principal to user_principal - rename UserAuthorizeInfo.diagnostics to user_diagnostics - rename UserAuthorizeInfo.decision to user_decision - rename WorkloadAuthorizeInfo.principal to workload_principal - rename WorkloadAuthorizeInfo.diagnostics to workload_diagnostics - rename WorkloadAuthorizeInfo.decision to workload_decision Signed-off-by: rmarinn <[email protected]> * chore(jans-cedarling): run cargo fmt and add missing license headers Signed-off-by: rmarinn <[email protected]> * chore(jans-cedarling): rename the Vec of role from `role` to `roles` Signed-off-by: rmarinn <[email protected]> * chore(jans-cedarling): remove unused function Signed-off-by: rmarinn <[email protected]> * refactor(jans-cedarling): rename UserAuthorizeInfo fields - change `user` to `person` Signed-off-by: rmarinn <[email protected]> * chore(jans-cedarling): run cargo fmt Signed-off-by: rmarinn <[email protected]> * chore(jans-cedarling): refactor to avoid unnecessary cloning Signed-off-by: Oleh Bohzok <[email protected]> --------- Signed-off-by: rmarinn <[email protected]> Signed-off-by: Oleh Bohzok <[email protected]> Co-authored-by: Oleh Bohzok <[email protected]>
Prepare
Description
This PR implements making tokens optional, making it more flexible for users who doesn't have all the tokens.
Target issue
target issue: #10408
closes #10408
Implementation Details
CEDARLING_USER_AUTHZ
is enabled; if it is, then the user entity is requiredCEDARLING_WORKLOAD_AUTHZ
enabled; if it is, then the workload entity is requiredCEDARLING_USER_AUTHZ
andCEDARLING_WORKLOAD_AUTHZ
is disabled, error on startupTest and Document the changes
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.