Fix authentication and error handling for cloud registries #225
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Thank you again for oras-py! I have found a few more issues and have included a
PR below. I will also try to submit a PR soon for #217 (I was able to address
that downstream so less pressing, but I will submit PR soon).
Problems Fixed
1. Authentication headers not being applied consistently
The
do_request()method only adds authentication headers when very specific conditions are met:This restrictive check causes authentication to fail in several scenarios:
TokenAuthinstances2. JSONDecodeError on non-JSON registry responses
When registries return non-JSON responses (e.g., HTML error pages from web portals), the code attempts to parse them with
r.json(), resulting in confusing errors:This commonly occurs with any cloud registry when:
Solutions
Commit 1: Use duck typing for auth header application
Relaxes the authentication check to use duck typing, which is more Pythonic and flexible:
Note: In the future, defining an auth interface with a Python Protocol could make the contract more explicit, but the current duck typing approach works well and is less opinionated of a design choice.
Commit 2: Improve error handling for non-JSON responses
r.json()calls in error handling with descriptive HTTP status messagesLocationheader (some registries use "Location" instead of "location")Benefits
get_auth_header(), we can use itTokenAuthclassTesting
All unit tests pass. I have tested with OAuth2 bearer token authentication (Azure Container Registry):
Verified with cloud registry implementations including Azure Container Registry.
Compatibility
Both changes are fully backward compatible:
BasicAuth,TokenAuth) already haveget_auth_header()method