Skip to content

Commit

Permalink
Revert PR#3128 (#3276)
Browse files Browse the repository at this point in the history
Signed-off-by: Bogdan Drutu <[email protected]>
  • Loading branch information
bogdandrutu authored May 24, 2021
1 parent ffc56e0 commit 86ea0a1
Show file tree
Hide file tree
Showing 49 changed files with 299 additions and 1,294 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ var (
errMetadataNotFound = errors.New("no request metadata found")
)

// ServerAuthenticator is an Extension that can be used as an authenticator for the configauth.Authentication option.
// Authenticator is an Extension that can be used as an authenticator for the configauth.Authentication option.
// Authenticators are then included as part of OpenTelemetry Collector builds and can be referenced by their
// names from the Authentication configuration. Each ServerAuthenticator is free to define its own behavior and configuration options,
// names from the Authentication configuration. Each Authenticator is free to define its own behavior and configuration options,
// but note that the expectations that come as part of Extensions exist here as well. For instance, multiple instances of the same
// authenticator should be possible to exist under different names.
type ServerAuthenticator interface {
type Authenticator interface {
component.Extension

// Authenticate checks whether the given headers map contains valid auth data. Successfully authenticated calls will always return a nil error.
Expand All @@ -58,17 +58,17 @@ type ServerAuthenticator interface {
}

// AuthenticateFunc defines the signature for the function responsible for performing the authentication based on the given headers map.
// See ServerAuthenticator.Authenticate.
// See Authenticator.Authenticate.
type AuthenticateFunc func(ctx context.Context, headers map[string][]string) error

// GrpcUnaryInterceptorFunc defines the signature for the function intercepting unary gRPC calls, useful for authenticators to use as
// types for internal structs, making it easier to mock them in tests.
// See ServerAuthenticator.GrpcUnaryServerInterceptor.
// See Authenticator.GrpcUnaryServerInterceptor.
type GrpcUnaryInterceptorFunc func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler, authenticate AuthenticateFunc) (interface{}, error)

// GrpcStreamInterceptorFunc defines the signature for the function intercepting streaming gRPC calls, useful for authenticators to use as
// types for internal structs, making it easier to mock them in tests.
// See ServerAuthenticator.GrpcStreamServerInterceptor.
// See Authenticator.GrpcStreamServerInterceptor.
type GrpcStreamInterceptorFunc func(srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler, authenticate AuthenticateFunc) error

// DefaultGrpcUnaryServerInterceptor provides a default implementation of GrpcUnaryInterceptorFunc, useful for most authenticators.
Expand Down
File renamed without changes.
78 changes: 0 additions & 78 deletions config/configauth/clientauth.go

This file was deleted.

24 changes: 17 additions & 7 deletions config/configauth/configauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,35 @@ import (
)

var (
errAuthenticatorNotFound = errors.New("authenticator not found")
errAuthenticatorNotFound = errors.New("authenticator not found")
errAuthenticatorNotProvided = errors.New("authenticator not provided")
)

// Authentication defines the auth settings for the receiver
type Authentication struct {
// AuthenticatorName specifies the name of the extension to use in order to authenticate the incoming data point.
// Authenticator specifies the name of the extension to use in order to authenticate the incoming data point.
AuthenticatorName string `mapstructure:"authenticator"`
}

// GetServerAuthenticator attempts to select the appropriate from the list of extensions, based on the requested extension name.
// GetAuthenticator attempts to select the appropriate from the list of extensions, based on the requested extension name.
// If an authenticator is not found, an error is returned.
func GetServerAuthenticator(extensions map[config.ComponentID]component.Extension, componentID config.ComponentID) (ServerAuthenticator, error) {
func GetAuthenticator(extensions map[config.ComponentID]component.Extension, requested string) (Authenticator, error) {
if requested == "" {
return nil, errAuthenticatorNotProvided
}

reqID, err := config.NewIDFromString(requested)
if err != nil {
return nil, err
}

for name, ext := range extensions {
if auth, ok := ext.(ServerAuthenticator); ok {
if name == componentID {
if auth, ok := ext.(Authenticator); ok {
if name == reqID {
return auth, nil
}
}
}

return nil, fmt.Errorf("failed to resolve authenticator %q: %w", componentID.String(), errAuthenticatorNotFound)
return nil, fmt.Errorf("failed to resolve authenticator %q: %w", requested, errAuthenticatorNotFound)
}
17 changes: 9 additions & 8 deletions config/configauth/configauth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ func TestGetAuthenticator(t *testing.T) {
}

// test
componentID, err := config.NewIDFromString(cfg.AuthenticatorName)
assert.NoError(t, err)

authenticator, err := GetServerAuthenticator(ext, componentID)
authenticator, err := GetAuthenticator(ext, cfg.AuthenticatorName)

// verify
assert.NoError(t, err)
Expand All @@ -51,7 +48,13 @@ func TestGetAuthenticatorFails(t *testing.T) {
expected error
}{
{
desc: "ServerAuthenticator not found",
desc: "Authenticator not provided",
cfg: &Authentication{},
ext: map[config.ComponentID]component.Extension{},
expected: errAuthenticatorNotProvided,
},
{
desc: "Authenticator not found",
cfg: &Authentication{
AuthenticatorName: "does-not-exist",
},
Expand All @@ -61,9 +64,7 @@ func TestGetAuthenticatorFails(t *testing.T) {
}
for _, tC := range testCases {
t.Run(tC.desc, func(t *testing.T) {
componentID, err := config.NewIDFromString(tC.cfg.AuthenticatorName)
assert.NoError(t, err)
authenticator, err := GetServerAuthenticator(tC.ext, componentID)
authenticator, err := GetAuthenticator(tC.ext, tC.cfg.AuthenticatorName)
assert.ErrorIs(t, err, tC.expected)
assert.Nil(t, authenticator)
})
Expand Down
66 changes: 0 additions & 66 deletions config/configauth/mock_clientauth.go

This file was deleted.

Loading

0 comments on commit 86ea0a1

Please sign in to comment.