-
Notifications
You must be signed in to change notification settings - Fork 275
Ensure custom user-agent flows to auth HTTP calls #6790
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
Merged
+172
−29
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
91056ef
Ensure custom user-agent flows to auth HTTP calls (#2723)
spboyer 1eb5b2e
Address review: cover Login* credential paths, guard nil headers
spboyer 67465dc
Address Copilot review: add tests, fix redundant header lookup
spboyer fbe8012
Fix lll lint violations in credential options
spboyer c9a7361
Simplify IoC registration with typed UserAgent alias
spboyer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package auth | ||
|
|
||
| import "net/http" | ||
|
|
||
| // userAgentClient wraps an HttpClient to inject a User-Agent header on all requests. | ||
| type userAgentClient struct { | ||
| inner HttpClient | ||
| userAgent string | ||
| } | ||
|
|
||
| func newUserAgentClient(inner HttpClient, userAgent string) HttpClient { | ||
| if userAgent == "" { | ||
| return inner | ||
| } | ||
| return &userAgentClient{inner: inner, userAgent: userAgent} | ||
| } | ||
|
|
||
| func (c *userAgentClient) Do(req *http.Request) (*http.Response, error) { | ||
| if req.Header == nil { | ||
| req.Header = make(http.Header) | ||
| } | ||
| existingUA := req.Header.Get("User-Agent") | ||
| if existingUA == "" { | ||
| req.Header.Set("User-Agent", c.userAgent) | ||
| } else { | ||
| req.Header.Set("User-Agent", existingUA+","+c.userAgent) | ||
| } | ||
| return c.inner.Do(req) | ||
| } | ||
|
|
||
| func (c *userAgentClient) CloseIdleConnections() { | ||
| c.inner.CloseIdleConnections() | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package auth | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| type mockHttpClient struct { | ||
| lastRequest *http.Request | ||
| } | ||
|
|
||
| func (m *mockHttpClient) Do(req *http.Request) (*http.Response, error) { | ||
| m.lastRequest = req | ||
| return &http.Response{StatusCode: 200}, nil | ||
| } | ||
|
|
||
| func (m *mockHttpClient) CloseIdleConnections() {} | ||
|
|
||
| func TestUserAgentClient(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| userAgent string | ||
| existingUserAgent string | ||
| nilHeader bool | ||
| expectedUserAgent string | ||
| expectWrapped bool | ||
| }{ | ||
| { | ||
| name: "SetsUserAgentWhenEmpty", | ||
| userAgent: "azdev/1.0.0", | ||
| existingUserAgent: "", | ||
| expectedUserAgent: "azdev/1.0.0", | ||
| expectWrapped: true, | ||
| }, | ||
| { | ||
| name: "AppendsToExistingUserAgent", | ||
| userAgent: "azdev/1.0.0", | ||
| existingUserAgent: "existing-agent/2.0", | ||
| expectedUserAgent: "existing-agent/2.0,azdev/1.0.0", | ||
| expectWrapped: true, | ||
| }, | ||
| { | ||
| name: "EmptyUserAgentReturnsInnerClient", | ||
| userAgent: "", | ||
| expectWrapped: false, | ||
| }, | ||
| { | ||
| name: "HandlesNilHeader", | ||
| userAgent: "azdev/1.0.0", | ||
| nilHeader: true, | ||
| expectedUserAgent: "azdev/1.0.0", | ||
| expectWrapped: true, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| inner := &mockHttpClient{} | ||
| client := newUserAgentClient(inner, tt.userAgent) | ||
|
|
||
| if !tt.expectWrapped { | ||
| // Should return the inner client unchanged | ||
| require.Equal(t, inner, client) | ||
| return | ||
| } | ||
|
|
||
| req, err := http.NewRequest("GET", "https://example.com", nil) | ||
| require.NoError(t, err) | ||
|
|
||
| if tt.nilHeader { | ||
| req.Header = nil | ||
| } else if tt.existingUserAgent != "" { | ||
| req.Header.Set("User-Agent", tt.existingUserAgent) | ||
| } | ||
|
|
||
| _, err = client.Do(req) | ||
| require.NoError(t, err) | ||
| require.Equal(t, tt.expectedUserAgent, inner.lastRequest.Header.Get("User-Agent")) | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.