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

Enable per namespace jwt key #861

Merged
merged 13 commits into from
Jan 11, 2024
Merged

Enable per namespace jwt key #861

merged 13 commits into from
Jan 11, 2024

Conversation

haaawk
Copy link
Contributor

@haaawk haaawk commented Jan 5, 2024

No description provided.

Copy link
Contributor

@LucioFranco LucioFranco left a comment

Choose a reason for hiding this comment

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

I left some feedback on two big ticket items but for me the most important thing is that we at least remove all the context calls and wrap those in explicit error variants.

Some of the feedback is nits that to me remove a few of the leaky abstractions that we have here, I am okay with merging without those fixes but we should track those.

}

async fn handle_post_config<M: MakeNamespace, C>(
State(app_state): State<Arc<AppState<M, C>>>,
Path(namespace): Path<String>,
Json(req): Json<HttpDatabaseConfig>,
) -> crate::Result<()> {
if let Some(jwt_key) = req.jwt_key.as_deref() {
// Check that the jwt key is correct
parse_jwt_key(jwt_key).context("Could not parse JWT decoding key")?;
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we want to avoid using context here since its going to hide the inner message if we use display. This has bitten us in the past. We should rather, create a error variant for this error case that can add this contextual message and map the error to that either within parse_jwt_key or here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Would you have an example of something similar that's already in the codebase, please? @LucioFranco

Copy link
Contributor

Choose a reason for hiding this comment

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

In this case, it looks like we have a lot of detailed error messages here

pub fn parse_jwt_key(data: &str) -> Result<jsonwebtoken::DecodingKey> {
so I would just remove the call to context here.

In general, the pattern inside parse_jwt_key we should completely move away from because it tends to hide a lot of the actual errors that are useful for debugging due to the way anyhow uses debug over display. Happy to explain this more on slack but essentially we should avoid using context and instead when we want more detailed contextual errors use a new error enum that has those encoded into the variant via thiserror macro annotations.

@haaawk haaawk force-pushed the jwts2 branch 2 times, most recently from e0e9def to e6b390c Compare January 6, 2024 09:10
@haaawk haaawk requested a review from LucioFranco January 8, 2024 09:54
let config = self.db_config_store.get();
if let Some(jwt_key) = config.jwt_key.as_deref() {
Ok(Some(
parse_jwt_key(jwt_key).context("Could not parse JWT decoding key")?,
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be best if you decoded the key only once, when the namespace is loaded. As per jsonwebtoken Documentation:

All the different kind of keys we can use to decode a JWT. This key can be re-used so make sure you only initialize it once if you can for better performance.

Also, consider wrapping it in an Arc: cloning the key is not cheap.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was thinking about it but didn't have a good place to store the key. Let's do this in a follow up as this PR is hanging for some time now.

Signed-off-by: Piotr Jastrzebski <[email protected]>
Signed-off-by: Piotr Jastrzebski <[email protected]>
@haaawk haaawk merged commit f6577e1 into main Jan 11, 2024
12 checks passed
@haaawk haaawk deleted the jwts2 branch January 11, 2024 10:37
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.

3 participants