Skip to content

Commit

Permalink
[Minor] Refactor the client Authenticators for the new "ClientAuthent…
Browse files Browse the repository at this point in the history
…icator" interfaces (#5905)

* Refactor the client Authenticators to account for the new "ClientAuthenticator" interfaces

* fix linitng issue

* fixed weird formatting
  • Loading branch information
pavankrish123 authored Oct 26, 2021
1 parent 200a764 commit fc0caf5
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
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

0 comments on commit fc0caf5

Please sign in to comment.