-
Notifications
You must be signed in to change notification settings - Fork 74
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): consolidate JwtService initialization into the module #9980
base: main
Are you sure you want to change the base?
Conversation
…nhance error handling - consolidate `JwtService` initialization logic into the module to enhance maintainability and clarity. - improve error handling during initialization, ensuring that errors are more descriptive and easier to trace. - define separate error types for each sub-module within the `jwt` module, allowing for better error categorization and easier debugging. - enhance documentation for error variants to provide clearer guidance on potential issues and their causes. Signed-off-by: rmarinn <[email protected]>
DryRun Security SummaryThe pull request focuses on improving the security and reliability of the JWT (JSON Web Token) handling functionality within the Expand for full summarySummary: The code changes in this pull request focus on improving the security and reliability of the JWT (JSON Web Token) handling functionality within the Key security-related changes include:
Overall, these changes demonstrate a strong focus on improving the security and robustness of the JWT handling functionality within the Files Changed:
Code AnalysisWe ran Riskiness🟢 Risk threshold not exceeded. |
crate::JwtConfig::Enabled { .. } => JwtServiceConfig::WithValidation { | ||
supported_algs: self.service_config.jwt_algorithms.clone(), | ||
crate::JwtConfig::Enabled { signature_algorithms } => JwtServiceConfig::WithValidation { | ||
supported_algs: signature_algorithms.to_vec(), | ||
trusted_idps: self.policy_store().trusted_issuers.expect("Expected trusted issuers to be present for JWT validation, but found None. Ensure that the policy store is properly initialized with trusted issuers before using JWT validation.").clone(), |
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 should not panic!. We need to get value from service_config
|
||
// TODO: handle intialization errors | ||
let jwt_service = Arc::new( | ||
JwtService::new_with_config(config).expect("should initialize jwt service"), |
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 should not panic!
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 know we shouldn't panic but look at the code calling it...
// get jwt service
pub fn jwt_service(&mut self) -> Arc<JwtService> {
if let Some(jwt_service) = &self.container.jwt_service {
jwt_service.clone()
} else {
let config = match &self.bootstrap_config.jwt_config {
crate::JwtConfig::Disabled => JwtServiceConfig::WithoutValidation,
crate::JwtConfig::Enabled { signature_algorithms } => JwtServiceConfig::WithValidation {
supported_algs: signature_algorithms.to_vec(),
trusted_idps: self.policy_store().trusted_issuers.expect("Expected trusted issuers to be present for JWT validation, but found None. Ensure that the policy store is properly initialized with trusted issuers before using JWT validation.").clone(),
},
};
// TODO: handle intialization errors
let jwt_service = Arc::new(
JwtService::new_with_config(config).expect("should initialize jwt service"),
);
self.container.jwt_service = Some(jwt_service.clone());
jwt_service
}
}
ServiceFactory::jwt_service
doesn't return a Result
when initializing...
@@ -169,7 +199,7 @@ fn decode_and_validate_jwt<T: DeserializeOwned>( | |||
/// | |||
/// # Errors | |||
/// Returns an error if the algorithm is not implemented. | |||
pub fn string_to_alg(algorithm: &str) -> Result<jwt::Algorithm, Error> { | |||
pub fn string_to_alg(algorithm: &str) -> Result<jwt::Algorithm, Box<str>> { |
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 return error as string?
trusted_idps: Vec<TrustedIssuer>, | ||
) -> Result<Self, Error> { | ||
) -> Result<Self, InitError> { |
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.
It should get already validated value and do not return any errors
Prepare
Description
This PR refactors the initialization process for the
jwt
module to enhance modularity and reduce the likelihood of merge conflicts when modifications occur in theinit
orauthz
modules.Target issue
The current initialization logic for
JwtService
is cumbersome and not encapsulated within its module, leading to a tangled initialization process. This lack of modularity complicates maintenance and fosters tight coupling between multiple modules. Any changes to theJwtService
constructor can cause cascading modifications across theinit
andcommon
modules, resulting in unintended dependencies and increased complexity.closes #9945
Implementation Details
jwt
module, facilitating better error categorization and debugging.Test 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.