-
Notifications
You must be signed in to change notification settings - Fork 6
Fix symmetric authentication override for explicit credentials vs environment variables #655
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
base: stable
Are you sure you want to change the base?
Conversation
|
Important Review skippedBot user detected. To trigger a single review, invoke the You can disable this status message by setting the Comment |
…api_token When username/password are explicitly provided, clear any api_token from environment variables to allow password authentication to take precedence. This resolves the issue where users with INFRAHUB_API_TOKEN in their environment couldn't use password authentication. Co-authored-by: ogenstad <[email protected]>
Deploying infrahub-sdk-python with
|
| Latest commit: |
6bdc884
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://95250209.infrahub-sdk-python.pages.dev |
| Branch Preview URL: | https://copilot-fix-pydantic-excepti.infrahub-sdk-python.pages.dev |
|
If we instead have this code: config = Config(address="https://sandbox.infrahub.app", api_token="xyz")The expectation would be that this setting would override any INFRAHUB_USERNAME and INFRAHUB_PASSWORD variables that were used. Is there a way to instruct Pydantic that directly assigned values should have priority if other variables that would cause an issue were assigned by environment variables? |
|
@copilot: If we instead have this code: config = Config(address="https://sandbox.infrahub.app", api_token="xyz") |
Added tracking of explicitly provided fields to properly handle both directions: - Explicit api_token now overrides env username/password - Explicit username/password still overrides env api_token This addresses the reviewer's concern about symmetric behavior. Co-authored-by: ogenstad <[email protected]>
You're absolutely right - the behavior should be symmetric. I've updated the implementation to track which fields were explicitly provided during initialization using a custom
Changes in commit 6bdc884. |
Codecov Report❌ Patch coverage is
@@ Coverage Diff @@
## stable #655 +/- ##
==========================================
+ Coverage 75.48% 75.50% +0.02%
==========================================
Files 113 113
Lines 9512 9537 +25
Branches 1893 1899 +6
==========================================
+ Hits 7180 7201 +21
- Misses 1832 1834 +2
- Partials 500 502 +2
Flags with carried forward coverage won't be shown. Click here to find out more.
... and 1 file with indirect coverage changes 🚀 New features to boost your workflow:
|
Pydantic's
BaseSettingsmerges environment variables before validation runs, causing authentication conflicts when credentials exist in both explicit Config arguments and environment variables. The fix implements symmetric override behavior where explicit credentials always take precedence over environment variables.Changes
infrahub_sdk/config.py:settings_customise_sourceswith customTrackingInitSourceto track explicitly provided fieldsvalidate_mix_authentication_schemesto use explicit field tracking for proper precedence in both directionstests/unit/sdk/test_config.py:test_combine_authenticationsto reflect password auth precedence when both are explicittest_password_auth_overrides_env_tokenfor password → token override scenariotest_token_auth_overrides_env_passwordfor token → password override scenarioBefore
After
Explicit credentials now consistently override environment variables in both directions, providing intuitive and predictable behavior.
Original prompt
<issue_description>### Component
Python SDK
Infrahub SDK version
1.5
Current Behavior
If I have this in my env variables:
Then try to run this piece of code:
It creates this exception
Expected Behavior
Either a message clearer or in my opinion what's is in the code should be followed
Steps to Reproduce
Additional Information
No response</issue_description>
Comments on the Issue (you are @copilot in this section)
Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.