-
Notifications
You must be signed in to change notification settings - Fork 231
[registry] Fix auth token parsing for access_token #1689
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
[registry] Fix auth token parsing for access_token #1689
Conversation
Extend auth token parsing to support token in different json fields. There is no real consensus on Oauth2 token response format, which means that each registry can implement their own. In particular, Azure ACR uses `access_token` as described here https://github.com/Azure/acr/blob/main/docs/Token-BasicAuth.md#get-a-pull-access-token-for-the-user. As such, when attempting to parse the JSON response containing the authorization token, we should attempt to deserialize using either `token` or `access_token` (and potentially more fields in the future if needed). To not break the integration with existing registry, the behavior is to fallback to `access_token` only if `token` does not exist in the response. Signed-off-by: Baptiste Girard-Carrabin <baptiste.girardcarrabin@datadoghq.com>
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #1689 +/- ##
==========================================
+ Coverage 55.45% 55.50% +0.04%
==========================================
Files 197 197
Lines 55754 55824 +70
Branches 47176 47246 +70
==========================================
+ Hits 30920 30986 +66
Misses 23235 23235
- Partials 1599 1603 +4
🚀 New features to boost your workflow:
|
storage/src/backend/registry.rs
Outdated
@@ -119,6 +119,17 @@ impl<T> HashCache<T> { | |||
} | |||
} | |||
|
|||
#[derive(Clone, serde::Deserialize)] | |||
struct TokenRegistryResponse { |
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.
Thanks, very nice PR!
How about reusing the TokenResponse
struct but:
struct TokenResponse {
#[serde(default)]
token: String,
#[serde(default)]
access_token: String,
#[serde(default = "default_expires_in")]
expires_in: u64,
}
Then we can implement:
impl TokenResponse {
fn from_resp(resp: Response) -> Result<Self> {
// TODO: the codes in previous `extract_token`.
}
}
And use it:
let ret = TokenResponse::from_resp(resp)?;
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.
Thanks a lot for your comment. I'm very new to Rust so I do appreciate a lot your feedback to improve my code!
I didn't know about serde(default)
so that's indeed a good way to not have to add an additional struct.
I've updated my code in a2ca336 with this change. If that works for you, I'll squash the commit properly afterwards.
Apply github comment. Use `serde:default` in TokenResponse to have the same behavior as Option<String> without changing the struct signature. Signed-off-by: Baptiste Girard-Carrabin <baptiste.girardcarrabin@datadoghq.com>
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.
very nice updates, LGTM!
Relevant Issue (if applicable)
If there are Issues related to this PullRequest, please list it.
Details
Please describe the details of PullRequest.
Extend auth token parsing to support token in different json fields.
There is no real consensus on Oauth2 token response format, which means that each registry can implement their own. In particular, Azure ACR uses
access_token
as described here https://github.com/Azure/acr/blob/main/docs/Token-BasicAuth.md#get-a-pull-access-token-for-the-user. As such, when attempting to parse the JSON response containing the authorization token, we should attempt to deserialize using eithertoken
oraccess_token
(and potentially more fields in the future if needed).To not break the integration with existing registry, the behavior is to fallback to
access_token
only iftoken
does not exist in the response. The requested behavior is implemented by an intermediary structTokenRegistryResponse
which allows bothtoken
andaccess_token
as Options (to not fail the JSON parsing if one is missing). Afterwards, the struct is cast toTokenRegistry
, selecting at the same time the right token field by order of priority.Types of changes
What types of changes does your PullRequest introduce? Put an
x
in all the boxes that apply:Checklist
Go over all the following points, and put an
x
in all the boxes that apply.