From 6e3903b02a675448ceb77f4698f730bedfda3a6d Mon Sep 17 00:00:00 2001 From: Pavan Krishna Nimmagadda Date: Mon, 25 Oct 2021 12:53:18 -0700 Subject: [PATCH 1/3] Refactor the client Authenticators to account for the new "ClientAuthenticator" interfaces --- .../bearertokenauthextension/bearertokenauth.go | 12 +++++++++++- .../bearertokenauthextension/bearertokenauth_test.go | 4 ++++ extension/oauth2clientauthextension/extension.go | 9 ++++----- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/extension/bearertokenauthextension/bearertokenauth.go b/extension/bearertokenauthextension/bearertokenauth.go index a6053ad4ba63..2e779d5e8ed8 100644 --- a/extension/bearertokenauthextension/bearertokenauth.go +++ b/extension/bearertokenauthextension/bearertokenauth.go @@ -16,7 +16,9 @@ package bearertokenauthextension import ( "context" + "errors" "fmt" + "net/http" "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/config/configauth" @@ -47,7 +49,10 @@ type BearerTokenAuth struct { logger *zap.Logger } -var _ configauth.GRPCClientAuthenticator = (*BearerTokenAuth)(nil) +var ( + _ configauth.ClientAuthenticator = (*BearerTokenAuth)(nil) + errNotHTTPClientAuthenticator = errors.New("requested authenticator is not a HTTP client authenticator") +) func newBearerTokenAuth(cfg *Config, logger *zap.Logger) *BearerTokenAuth { return &BearerTokenAuth{ @@ -72,3 +77,8 @@ func (b *BearerTokenAuth) PerRPCCredentials() (credentials.PerRPCCredentials, er metadata: map[string]string{"authorization": fmt.Sprintf("Bearer %s", b.tokenString)}, }, nil } + +// RoundTripper is not implemented by BearerTokenAuth +func (b *BearerTokenAuth) RoundTripper(base http.RoundTripper) (http.RoundTripper, error) { + return nil, errNotHTTPClientAuthenticator +} diff --git a/extension/bearertokenauthextension/bearertokenauth_test.go b/extension/bearertokenauthextension/bearertokenauth_test.go index 528c580bde67..494c26e74c7e 100644 --- a/extension/bearertokenauthextension/bearertokenauth_test.go +++ b/extension/bearertokenauthextension/bearertokenauth_test.go @@ -52,6 +52,10 @@ func TestBearerAuthenticator(t *testing.T) { assert.NoError(t, err) assert.NotNil(t, credential) + c, err := bauth.RoundTripper(nil) + assert.ErrorIs(t, err, errNotHTTPClientAuthenticator) + assert.Nil(t, c) + md, err := credential.GetRequestMetadata(context.Background()) expectedMd := map[string]string{ "authorization": fmt.Sprintf("Bearer %s", cfg.BearerToken), diff --git a/extension/oauth2clientauthextension/extension.go b/extension/oauth2clientauthextension/extension.go index 8eac72f30367..7830f467559a 100644 --- a/extension/oauth2clientauthextension/extension.go +++ b/extension/oauth2clientauthextension/extension.go @@ -35,11 +35,10 @@ type ClientCredentialsAuthenticator struct { client *http.Client } -// ClientCredentialsAuthenticator implements both HTTPClientAuth and GRPCClientAuth -var ( - _ configauth.HTTPClientAuthenticator = (*ClientCredentialsAuthenticator)(nil) - _ configauth.GRPCClientAuthenticator = (*ClientCredentialsAuthenticator)(nil) -) +// ClientCredentialsAuthenticator implements ClientAuthenticator + var ( + _ configauth.ClientAuthenticator = (*ClientCredentialsAuthenticator)(nil) + ) func newClientCredentialsExtension(cfg *Config, logger *zap.Logger) (*ClientCredentialsAuthenticator, error) { if cfg.ClientID == "" { From a88d0d926a75926e23e6115ef7716709889de671 Mon Sep 17 00:00:00 2001 From: Pavan Krishna Nimmagadda Date: Mon, 25 Oct 2021 13:41:41 -0700 Subject: [PATCH 2/3] fix linitng issue --- extension/oauth2clientauthextension/extension.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/extension/oauth2clientauthextension/extension.go b/extension/oauth2clientauthextension/extension.go index 7830f467559a..579966782d2f 100644 --- a/extension/oauth2clientauthextension/extension.go +++ b/extension/oauth2clientauthextension/extension.go @@ -36,9 +36,7 @@ type ClientCredentialsAuthenticator struct { } // ClientCredentialsAuthenticator implements ClientAuthenticator - var ( - _ configauth.ClientAuthenticator = (*ClientCredentialsAuthenticator)(nil) - ) +var _ configauth.ClientAuthenticator = (*ClientCredentialsAuthenticator)(nil) func newClientCredentialsExtension(cfg *Config, logger *zap.Logger) (*ClientCredentialsAuthenticator, error) { if cfg.ClientID == "" { From cb4a2542813145105375171f35389405caee7a4a Mon Sep 17 00:00:00 2001 From: Pavan Krishna Nimmagadda Date: Tue, 26 Oct 2021 12:41:41 -0700 Subject: [PATCH 3/3] fixed weird formatting --- extension/bearertokenauthextension/bearertokenauth.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/extension/bearertokenauthextension/bearertokenauth.go b/extension/bearertokenauthextension/bearertokenauth.go index 2e779d5e8ed8..86bea5251d43 100644 --- a/extension/bearertokenauthextension/bearertokenauth.go +++ b/extension/bearertokenauthextension/bearertokenauth.go @@ -49,10 +49,8 @@ type BearerTokenAuth struct { logger *zap.Logger } -var ( - _ configauth.ClientAuthenticator = (*BearerTokenAuth)(nil) - errNotHTTPClientAuthenticator = errors.New("requested authenticator is not a HTTP client authenticator") -) +var _ configauth.ClientAuthenticator = (*BearerTokenAuth)(nil) +var errNotHTTPClientAuthenticator = errors.New("requested authenticator is not a HTTP client authenticator") func newBearerTokenAuth(cfg *Config, logger *zap.Logger) *BearerTokenAuth { return &BearerTokenAuth{