Skip to content
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

[Minor] Refactor the client Authenticators for the new "ClientAuthenticator" interfaces #5905

Merged
merged 3 commits into from
Oct 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion extension/bearertokenauthextension/bearertokenauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ package bearertokenauthextension

import (
"context"
"errors"
"fmt"
"net/http"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/configauth"
Expand Down Expand Up @@ -47,7 +49,8 @@ type BearerTokenAuth struct {
logger *zap.Logger
}

var _ configauth.GRPCClientAuthenticator = (*BearerTokenAuth)(nil)
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{
Expand All @@ -72,3 +75,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
}
4 changes: 4 additions & 0 deletions extension/bearertokenauthextension/bearertokenauth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
7 changes: 2 additions & 5 deletions extension/oauth2clientauthextension/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,8 @@ 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 == "" {
Expand Down