-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Add auth token propagation for metrics reader #3341
Merged
Merged
Changes from 14 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
a5133cc
Add auth token propagation for metrics reader
albertteoh fa128b3
Consolidate token propagation code
albertteoh d1c96ad
Rename to use PropagationHandler for consistency
albertteoh f0774b2
Split and move common transports
albertteoh 8e5f1b9
Give bearerToken value less meaning
albertteoh 73db361
Fix import grouping
albertteoh 787b07b
Give token value less meaning
albertteoh 9af8681
make fmt
albertteoh bac98cc
Merge branch 'master' into 3340-token-prop
albertteoh 56ac31b
Remove functional options
albertteoh f5e3801
Fix build
albertteoh 65208c2
Merge branch 'master' into 3340-token-prop
albertteoh 696eb45
Merge branch 'master' into 3340-token-prop
albertteoh 4d7e1c5
Merge branch 'master' into 3340-token-prop
albertteoh 696cb5f
Address review comments
albertteoh bf6a818
Merge branch 'master' into 3340-token-prop
albertteoh 2742a11
Fix staticcheck err
albertteoh 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
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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,52 @@ | ||
// Copyright (c) 2021 The Jaeger Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package bearertoken | ||
|
||
import ( | ||
"errors" | ||
"net/http" | ||
) | ||
|
||
// RoundTripper wraps another http.RoundTripper and injects | ||
// an authentication header with bearer token into requests. | ||
type RoundTripper struct { | ||
// Transport is the underlying http.RoundTripper being wrapped. Required. | ||
Transport http.RoundTripper | ||
|
||
// StaticToken is the pre-configured bearer token. Optional. | ||
StaticToken string | ||
|
||
// OverrideFromCtx enables reading bearer token from Context. | ||
OverrideFromCtx bool | ||
} | ||
|
||
// RoundTrip injects the outbound Authorization header with the | ||
// token provided in the inbound request. | ||
func (tr RoundTripper) RoundTrip(r *http.Request) (*http.Response, error) { | ||
if tr.Transport == nil { | ||
return nil, errors.New("no http.RoundTripper provided") | ||
} | ||
token := tr.StaticToken | ||
if tr.OverrideFromCtx { | ||
headerToken, _ := GetBearerToken(r.Context()) | ||
if headerToken != "" { | ||
token = headerToken | ||
} | ||
} | ||
if token != "" { | ||
r.Header.Set("Authorization", "Bearer "+token) | ||
} | ||
return tr.Transport.RoundTrip(r) | ||
} |
This file contains 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,111 @@ | ||
// Copyright (c) 2021 The Jaeger Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package bearertoken | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type roundTripFunc func(r *http.Request) (*http.Response, error) | ||
|
||
func (s roundTripFunc) RoundTrip(r *http.Request) (*http.Response, error) { | ||
return s(r) | ||
} | ||
|
||
func TestRoundTripper(t *testing.T) { | ||
for _, tc := range []struct { | ||
name string | ||
staticToken string | ||
overrideFromCtx bool | ||
wrappedTransport http.RoundTripper | ||
requestContext context.Context | ||
wantError bool | ||
}{ | ||
{ | ||
name: "Default RoundTripper and request context set should have empty Bearer token", | ||
wrappedTransport: roundTripFunc(func(r *http.Request) (*http.Response, error) { | ||
assert.Empty(t, r.Header.Get("Authorization")) | ||
return &http.Response{ | ||
StatusCode: http.StatusOK, | ||
}, nil | ||
}), | ||
requestContext: ContextWithBearerToken(context.Background(), "tokenFromContext"), | ||
}, | ||
{ | ||
name: "Override from context provided, and request context set should use request context token", | ||
overrideFromCtx: true, | ||
wrappedTransport: roundTripFunc(func(r *http.Request) (*http.Response, error) { | ||
assert.Equal(t, "Bearer tokenFromContext", r.Header.Get("Authorization")) | ||
return &http.Response{ | ||
StatusCode: http.StatusOK, | ||
}, nil | ||
}), | ||
requestContext: ContextWithBearerToken(context.Background(), "tokenFromContext"), | ||
}, | ||
{ | ||
name: "Allow override from context and token provided, and request context unset should use defaultToken", | ||
overrideFromCtx: true, | ||
staticToken: "initToken", | ||
wrappedTransport: roundTripFunc(func(r *http.Request) (*http.Response, error) { | ||
assert.Equal(t, "Bearer initToken", r.Header.Get("Authorization")) | ||
return &http.Response{}, nil | ||
}), | ||
requestContext: context.Background(), | ||
}, | ||
{ | ||
name: "Allow override from context and token provided, and request context set should use context token", | ||
overrideFromCtx: true, | ||
staticToken: "initToken", | ||
wrappedTransport: roundTripFunc(func(r *http.Request) (*http.Response, error) { | ||
assert.Equal(t, "Bearer tokenFromContext", r.Header.Get("Authorization")) | ||
return &http.Response{}, nil | ||
}), | ||
requestContext: ContextWithBearerToken(context.Background(), "tokenFromContext"), | ||
}, | ||
{ | ||
name: "Nil roundTripper provided should return an error", | ||
requestContext: context.Background(), | ||
wantError: true, | ||
}, | ||
} { | ||
t.Run(tc.name, func(t *testing.T) { | ||
server := httptest.NewServer(nil) | ||
defer server.Close() | ||
req, err := http.NewRequestWithContext(tc.requestContext, "GET", server.URL, nil) | ||
require.NoError(t, err) | ||
|
||
tr := RoundTripper{ | ||
Transport: tc.wrappedTransport, | ||
OverrideFromCtx: tc.overrideFromCtx, | ||
StaticToken: tc.staticToken, | ||
} | ||
resp, err := tr.RoundTrip(req) | ||
|
||
if tc.wantError { | ||
assert.Nil(t, resp) | ||
assert.Error(t, err) | ||
} else { | ||
assert.NotNil(t, resp) | ||
assert.NoError(t, err) | ||
} | ||
}) | ||
} | ||
} |
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.
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.
Key
is used outside of this package here.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.
Strictly speaking, I think it would've been better if grpc plugin just defined its own public constant instead of relying on this one, since they are completely independent.
If we remove that external dependency, then this key can be made private, and doesn't need to be a string, just this:
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.
Addressed in 696cb5f.