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: flexible Authorization header type of HTTP request #3190

Conversation

rina23q
Copy link
Member

@rina23q rina23q commented Oct 16, 2024

Proposed changes

Pure refactoring PR to make a progress of #3036 easier.

  • Now C8YJwtRetriever returns HeaderMap, previously it was just JWT token (String).
  • Deprecate Auth enum in downloader so that downloader can accept any headers easily
  • Remove unused functions in 897c102

Why this refactoring is needed?
#3036 needs to introduce Basic authentication instead of JWT token authentication. However, the current implementation, especially HTTP APIs, are designed to use Authorization header together with JWT token. Since the JWT actor is actually a server trait, for basic authentication, we can just implement a server trait.

Question:
The JWT token inplementation in c8y_remote_access_plugin seems duplicated to C8YJwtRetriever implementation...

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Improvement (general improvements like code refactoring that doesn't explicitly fix a bug or add any new functionality)
  • Documentation Update (if none of the other choices apply)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Paste Link to the issue

#3036

Checklist

  • I have read the CONTRIBUTING doc
  • I have signed the CLA (in all commits with git commit -s)
  • I ran cargo fmt as mentioned in CODING_GUIDELINES
  • I used cargo clippy as mentioned in CODING_GUIDELINES
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)

Further comments

Copy link
Contributor

github-actions bot commented Oct 16, 2024

Robot Results

✅ Passed ❌ Failed ⏭️ Skipped Total Pass % ⏱️ Duration
508 0 2 508 100 1h27m40.911290999s

@rina23q rina23q force-pushed the refactor/3036/jwt_retriever_returns_header_value branch from 95d782a to 02a3a44 Compare October 16, 2024 18:32
@rina23q rina23q temporarily deployed to Test Pull Request October 16, 2024 18:32 — with GitHub Actions Inactive
@reubenmiller
Copy link
Contributor

reubenmiller commented Oct 17, 2024

@rina23q @didier-wenzek @albinsuresh Wouldn't it make more sense to have a generic "Add N Headers"...that way we could write providers to inject any required headers and not assume it was just be the "Authorization" header?

Copy link
Contributor

@didier-wenzek didier-wenzek left a comment

Choose a reason for hiding this comment

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

Question:
The JWT token implementation in c8y_remote_access_plugin seems duplicated to C8YJwtRetriever implementation...

This is not a duplicate, just a difference on the API level which is used. The c8y_remote_access_plugin directly invokes C8yMqttJwtTokenRetriever::get_jwt_token() instead as wrapping this call behind an actor as done by C8YJwtRetriever.

=> The implication for that refactoring PR is that the difference between the misc auth method has to be addressed in the c8y_api with a facade over the ``C8yMqttJwtTokenRetrieverand aBasicAuthRetriver`. Both returning an authentication header ready to be used.

crates/common/tedge_config_macros/src/multi.rs Outdated Show resolved Hide resolved
@@ -32,16 +32,17 @@ impl C8YJwtRetriever {

#[async_trait]
impl Server for C8YJwtRetriever {
Copy link
Contributor

Choose a reason for hiding this comment

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

Adding the new C8YBasicAuthProvider in this PR would really help to validate the refactoring, even if not use yet.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry, I should have remove this comment which is conflicting with my other comment #3190 (review), telling that:

the difference between the misc auth method has to be addressed in the c8y_api with a facade over the C8yMqttJwtTokenRetriever and a BasicAuthRetriver. Both returning an authentication header ready to be used.

Anyway. This can be done in the next step.

crates/common/download/src/download.rs Show resolved Hide resolved
@rina23q
Copy link
Member Author

rina23q commented Oct 17, 2024

@rina23q @didier-wenzek @albinsuresh Wouldn't it make more sense to have a generic "Add N Headers"...that way we could write providers to inject any required headers and not assume it was just be the "Authorization" header?

Ah like returning ({header_key}, {header_value}), e.g. ("Authorization", "Bearer XXX")?

So it means C8YJwtRetriever returns Result<(String, String), HeaderError> instead of Result<String, AuthError>.

Or even getting a help from the http crate's HeaderName and HeaderValue, it can be Result<(HeaderName, HeaderValue), Error).

@reubenmiller
Copy link
Contributor

@rina23q @didier-wenzek @albinsuresh Wouldn't it make more sense to have a generic "Add N Headers"...that way we could write providers to inject any required headers and not assume it was just be the "Authorization" header?

Ah like returning ({header_key}, {header_value}), e.g. ("Authorization", "Bearer XXX")?

So it means C8YJwtRetriever returns Result<(String, String), HeaderError> instead of Result<String, AuthError>.

Or even getting a help from the http crate's HeaderName and HeaderValue, it can be Result<(HeaderName, HeaderValue), Error).

Or even better, it results a vector of header name/value 's)...As some more complicated authenticated mechanisms need to set multiple header properties, for example AWS S3 API allows users to also add x-* headers to add some additional options.

@rina23q
Copy link
Member Author

rina23q commented Oct 17, 2024

Or even better, it results a vector of header name/value 's)...As some more complicated authenticated mechanisms need to set multiple header properties, for example AWS S3 API allows users to also add x-* headers to add some additional options.

Then this HeaderMap is the way to go 👍

@rina23q
Copy link
Member Author

rina23q commented Oct 20, 2024

Then this HeaderMap is the way to go 👍

This change is done in e7b8724.

Also, I removed unused code in 897c102. If they should stay in our code, please inform me. Then I can revert in that case.

Copy link
Contributor

@didier-wenzek didier-wenzek left a comment

Choose a reason for hiding this comment

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

Approved. This refactoring PR is a nice progress forward.

Also, I removed unused code in 897c102. If they should stay in our code, please inform me. Then I can revert in that case.

Removing dead code is always a good thing.

crates/common/download/src/download.rs Outdated Show resolved Hide resolved
@@ -32,16 +32,17 @@ impl C8YJwtRetriever {

#[async_trait]
impl Server for C8YJwtRetriever {
Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry, I should have remove this comment which is conflicting with my other comment #3190 (review), telling that:

the difference between the misc auth method has to be addressed in the c8y_api with a facade over the C8yMqttJwtTokenRetriever and a BasicAuthRetriver. Both returning an authentication header ready to be used.

Anyway. This can be done in the next step.

- Change JWT token retriever actor to return "Bearer {token}",
previously it was just "{token}"
- Deprecate Auth enum in downloader so that downloader can
accept any authorization header value easily

Signed-off-by: Rina Fujino <rina.fujino.23@gmail.com>
Signed-off-by: Rina Fujino <rina.fujino.23@gmail.com>
HttpHeaderRetriever returns HeaderMap. So, C8YJwtRetriever now returns
HeaderMap. The retrieved JWT token is included in the map.
In the future, there will be C8YBasicAuthRetriever, which will also
returns HeaderMap.

Signed-off-by: Rina Fujino <rina.fujino.23@gmail.com>
@rina23q rina23q force-pushed the refactor/3036/jwt_retriever_returns_header_value branch from e7b8724 to 9711f81 Compare October 21, 2024 10:16
@rina23q rina23q temporarily deployed to Test Pull Request October 21, 2024 10:16 — with GitHub Actions Inactive
@rina23q rina23q added this pull request to the merge queue Oct 21, 2024
Merged via the queue into thin-edge:main with commit 30962e6 Oct 21, 2024
33 checks passed
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