-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Internal Telemetry Authenticator Extension Support #13779
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
Closed
marcsnid
wants to merge
4
commits into
open-telemetry:main
from
marcsnid:authenticators_internal_telemetry
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
42a757d
Added internal authentication to telemetry for adding extension auth..
marcsnid dab4510
Added authenticator field to otel conf under telemetry.
marcsnid bdbb3db
Added authenticator field to service setup.
marcsnid 2a2bf93
Merge branch 'main' into authenticators_internal_telemetry
marcsnid 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 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,102 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package telemetry // import "go.opentelemetry.io/collector/internal/telemetry" | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "sync" | ||
|
|
||
| "google.golang.org/grpc/credentials" | ||
|
|
||
| "go.opentelemetry.io/collector/extension/extensionauth" | ||
| ) | ||
|
|
||
| // AuthenticatorProvider is an interface that provides authentication | ||
| // for internal telemetry exports. | ||
| type AuthenticatorProvider interface { | ||
| // GetHTTPRoundTripper returns an authenticated round tripper for HTTP requests. | ||
| GetHTTPRoundTripper(base http.RoundTripper) (http.RoundTripper, error) | ||
|
|
||
| // GetGRPCCredentials returns gRPC per-RPC credentials for authentication. | ||
| GetGRPCCredentials() (credentials.PerRPCCredentials, error) | ||
| } | ||
|
|
||
| // AuthenticatorManager manages authentication for internal telemetry. | ||
| // This allows for late binding of authenticators since telemetry providers | ||
| // are created before extensions are initialized. | ||
| type AuthenticatorManager struct { | ||
| mu sync.RWMutex | ||
| authenticator AuthenticatorProvider | ||
| } | ||
|
|
||
| // NewAuthenticatorManager creates a new AuthenticatorManager. | ||
| func NewAuthenticatorManager() *AuthenticatorManager { | ||
| return &AuthenticatorManager{} | ||
| } | ||
|
|
||
| // SetAuthenticator sets the authenticator provider. | ||
| func (am *AuthenticatorManager) SetAuthenticator(auth AuthenticatorProvider) { | ||
| am.mu.Lock() | ||
| defer am.mu.Unlock() | ||
| am.authenticator = auth | ||
| } | ||
|
|
||
| // GetHTTPRoundTripper returns an authenticated round tripper if an authenticator is set. | ||
| func (am *AuthenticatorManager) GetHTTPRoundTripper(base http.RoundTripper) (http.RoundTripper, error) { | ||
| am.mu.RLock() | ||
| defer am.mu.RUnlock() | ||
|
|
||
| if am.authenticator == nil { | ||
| return base, nil | ||
| } | ||
|
|
||
| return am.authenticator.GetHTTPRoundTripper(base) | ||
| } | ||
|
|
||
| // GetGRPCCredentials returns gRPC credentials if an authenticator is set. | ||
| func (am *AuthenticatorManager) GetGRPCCredentials() (credentials.PerRPCCredentials, error) { | ||
| am.mu.RLock() | ||
| defer am.mu.RUnlock() | ||
|
|
||
| if am.authenticator == nil { | ||
| return nil, nil | ||
| } | ||
|
|
||
| return am.authenticator.GetGRPCCredentials() | ||
| } | ||
|
|
||
| // GlobalAuthenticatorManager is the global instance used by internal telemetry. | ||
| // This allows telemetry providers to be authenticated even when they're created | ||
| // before extensions are initialized. | ||
| var GlobalAuthenticatorManager = NewAuthenticatorManager() | ||
|
|
||
| // DefaultAuthenticatorProvider creates an AuthenticatorProvider from extension components. | ||
| type DefaultAuthenticatorProvider struct { | ||
| httpClient extensionauth.HTTPClient | ||
| grpcClient extensionauth.GRPCClient | ||
| } | ||
|
|
||
| // NewDefaultAuthenticatorProvider creates a new DefaultAuthenticatorProvider. | ||
| func NewDefaultAuthenticatorProvider(httpClient extensionauth.HTTPClient, grpcClient extensionauth.GRPCClient) *DefaultAuthenticatorProvider { | ||
| return &DefaultAuthenticatorProvider{ | ||
| httpClient: httpClient, | ||
| grpcClient: grpcClient, | ||
| } | ||
| } | ||
|
|
||
| // GetHTTPRoundTripper implements AuthenticatorProvider. | ||
| func (p *DefaultAuthenticatorProvider) GetHTTPRoundTripper(base http.RoundTripper) (http.RoundTripper, error) { | ||
| if p.httpClient == nil { | ||
| return base, nil | ||
| } | ||
| return p.httpClient.RoundTripper(base) | ||
| } | ||
|
|
||
| // GetGRPCCredentials implements AuthenticatorProvider. | ||
| func (p *DefaultAuthenticatorProvider) GetGRPCCredentials() (credentials.PerRPCCredentials, error) { | ||
| if p.grpcClient == nil { | ||
| return nil, nil | ||
| } | ||
| return p.grpcClient.PerRPCCredentials() | ||
| } | ||
|
Comment on lines
+88
to
+102
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The suggestion above means we can remove all this. |
||
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,129 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package telemetry | ||
|
|
||
| import ( | ||
| "context" | ||
| "net/http" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| "google.golang.org/grpc/credentials" | ||
| ) | ||
|
|
||
| func TestAuthenticatorManager(t *testing.T) { | ||
| manager := NewAuthenticatorManager() | ||
|
|
||
| // Test no authenticator set | ||
| rt, err := manager.GetHTTPRoundTripper(http.DefaultTransport) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, http.DefaultTransport, rt) | ||
|
|
||
| creds, err := manager.GetGRPCCredentials() | ||
| require.NoError(t, err) | ||
| assert.Nil(t, creds) | ||
|
|
||
| // Test with authenticator set | ||
| mockAuth := &mockAuthenticatorProvider{} | ||
| manager.SetAuthenticator(mockAuth) | ||
|
|
||
| rt, err = manager.GetHTTPRoundTripper(http.DefaultTransport) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, mockAuth.roundTripper, rt) | ||
|
|
||
| creds, err = manager.GetGRPCCredentials() | ||
| require.NoError(t, err) | ||
| assert.Equal(t, mockAuth.creds, creds) | ||
| } | ||
|
|
||
| func TestDefaultAuthenticatorProvider(t *testing.T) { | ||
| mockHTTPClient := &mockHTTPClient{} | ||
| mockGRPCClient := &mockGRPCClient{} | ||
|
|
||
| provider := NewDefaultAuthenticatorProvider(mockHTTPClient, mockGRPCClient) | ||
|
|
||
| // Test HTTP round tripper | ||
| rt, err := provider.GetHTTPRoundTripper(http.DefaultTransport) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, mockHTTPClient.roundTripper, rt) | ||
|
|
||
| // Test gRPC credentials | ||
| creds, err := provider.GetGRPCCredentials() | ||
| require.NoError(t, err) | ||
| assert.Equal(t, mockGRPCClient.creds, creds) | ||
| } | ||
|
|
||
| func TestDefaultAuthenticatorProviderWithNilClients(t *testing.T) { | ||
| provider := NewDefaultAuthenticatorProvider(nil, nil) | ||
|
|
||
| // Test HTTP round tripper | ||
| rt, err := provider.GetHTTPRoundTripper(http.DefaultTransport) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, http.DefaultTransport, rt) | ||
|
|
||
| // Test gRPC credentials | ||
| creds, err := provider.GetGRPCCredentials() | ||
| require.NoError(t, err) | ||
| assert.Nil(t, creds) | ||
| } | ||
|
|
||
| // Mock implementations for testing | ||
|
|
||
| type mockAuthenticatorProvider struct { | ||
| roundTripper http.RoundTripper | ||
| creds credentials.PerRPCCredentials | ||
| } | ||
|
|
||
| func (m *mockAuthenticatorProvider) GetHTTPRoundTripper(base http.RoundTripper) (http.RoundTripper, error) { | ||
| if m.roundTripper == nil { | ||
| m.roundTripper = &mockRoundTripper{} | ||
| } | ||
| return m.roundTripper, nil | ||
| } | ||
|
|
||
| func (m *mockAuthenticatorProvider) GetGRPCCredentials() (credentials.PerRPCCredentials, error) { | ||
| if m.creds == nil { | ||
| m.creds = &mockCredentials{} | ||
| } | ||
| return m.creds, nil | ||
| } | ||
|
|
||
| type mockHTTPClient struct { | ||
| roundTripper http.RoundTripper | ||
| } | ||
|
|
||
| func (m *mockHTTPClient) RoundTripper(base http.RoundTripper) (http.RoundTripper, error) { | ||
| if m.roundTripper == nil { | ||
| m.roundTripper = &mockRoundTripper{} | ||
| } | ||
| return m.roundTripper, nil | ||
| } | ||
|
|
||
| type mockGRPCClient struct { | ||
| creds credentials.PerRPCCredentials | ||
| } | ||
|
|
||
| func (m *mockGRPCClient) PerRPCCredentials() (credentials.PerRPCCredentials, error) { | ||
| if m.creds == nil { | ||
| m.creds = &mockCredentials{} | ||
| } | ||
| return m.creds, nil | ||
| } | ||
|
|
||
| type mockRoundTripper struct{} | ||
|
|
||
| func (m *mockRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { | ||
| return nil, nil | ||
| } | ||
|
|
||
| type mockCredentials struct{} | ||
|
|
||
| func (m *mockCredentials) GetRequestMetadata(ctx context.Context, uri ...string) (map[string]string, error) { | ||
| return map[string]string{"authorization": "Bearer mock-token"}, nil | ||
| } | ||
|
|
||
| func (m *mockCredentials) RequireTransportSecurity() bool { | ||
| return false | ||
| } |
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
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.
Should be able to replace this type with an unexported form.
Reviewers, see this evidently-stalled RFC on the topic of how we construct these default implementations: #13263