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

Fix inconsistency between function/type names #58

Closed
wants to merge 6 commits into from
Closed
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
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 @@ -42,38 +42,38 @@ type ServerAuthenticator interface {
// The deadline and cancellation given to this function must be respected, but note that authentication data has to be part of the map, not context.
Authenticate(ctx context.Context, headers map[string][]string) error

// GrpcUnaryServerInterceptor is a helper method to provide a gRPC-compatible UnaryServerInterceptor, typically calling the authenticator's Authenticate method.
// GRPCUnaryServerInterceptor is a helper method to provide a gRPC-compatible UnaryServerInterceptor, typically calling the authenticator's Authenticate method.
// While the context is the typical source of authentication data, the interceptor is free to determine where the auth data should come from. For instance, some
// receivers might implement an interceptor that looks into the payload instead.
// Once the authentication succeeds, the interceptor is expected to call the handler.
// See https://pkg.go.dev/google.golang.org/grpc#UnaryServerInterceptor.
GrpcUnaryServerInterceptor(ctx context.Context, req interface{}, srvInfo *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error)
GRPCUnaryServerInterceptor(ctx context.Context, req interface{}, srvInfo *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error)

// GrpcStreamServerInterceptor is a helper method to provide a gRPC-compatible StreamServerInterceptor, typically calling the authenticator's Authenticate method.
// GRPCStreamServerInterceptor is a helper method to provide a gRPC-compatible StreamServerInterceptor, typically calling the authenticator's Authenticate method.
// While the context is the typical source of authentication data, the interceptor is free to determine where the auth data should come from. For instance, some
// receivers might implement an interceptor that looks into the payload instead.
// Once the authentication succeeds, the interceptor is expected to call the handler.
// See https://pkg.go.dev/google.golang.org/grpc#StreamServerInterceptor.
GrpcStreamServerInterceptor(srv interface{}, stream grpc.ServerStream, srvInfo *grpc.StreamServerInfo, handler grpc.StreamHandler) error
GRPCStreamServerInterceptor(srv interface{}, stream grpc.ServerStream, srvInfo *grpc.StreamServerInfo, handler grpc.StreamHandler) error
}

// 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
// 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.
type GrpcUnaryInterceptorFunc func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler, authenticate AuthenticateFunc) (interface{}, error)
// 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
// 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.
type GrpcStreamInterceptorFunc func(srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler, authenticate AuthenticateFunc) error
// 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.
// DefaultGRPCUnaryServerInterceptor provides a default implementation of GRPCUnaryInterceptorFunc, useful for most authenticators.
// It extracts the headers from the incoming request, under the assumption that the credentials will be part of the resulting map.
func DefaultGrpcUnaryServerInterceptor(ctx context.Context, req interface{}, _ *grpc.UnaryServerInfo, handler grpc.UnaryHandler, authenticate AuthenticateFunc) (interface{}, error) {
func DefaultGRPCUnaryServerInterceptor(ctx context.Context, req interface{}, _ *grpc.UnaryServerInfo, handler grpc.UnaryHandler, authenticate AuthenticateFunc) (interface{}, error) {
headers, ok := metadata.FromIncomingContext(ctx)
if !ok {
return nil, errMetadataNotFound
Expand All @@ -86,9 +86,9 @@ func DefaultGrpcUnaryServerInterceptor(ctx context.Context, req interface{}, _ *
return handler(ctx, req)
}

// DefaultGrpcStreamServerInterceptor provides a default implementation of GrpcStreamInterceptorFunc, useful for most authenticators.
// DefaultGRPCStreamServerInterceptor provides a default implementation of GRPCStreamInterceptorFunc, useful for most authenticators.
// It extracts the headers from the incoming request, under the assumption that the credentials will be part of the resulting map.
func DefaultGrpcStreamServerInterceptor(srv interface{}, stream grpc.ServerStream, _ *grpc.StreamServerInfo, handler grpc.StreamHandler, authenticate AuthenticateFunc) error {
func DefaultGRPCStreamServerInterceptor(srv interface{}, stream grpc.ServerStream, _ *grpc.StreamServerInfo, handler grpc.StreamHandler, authenticate AuthenticateFunc) error {
ctx := stream.Context()
headers, ok := metadata.FromIncomingContext(ctx)
if !ok {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestDefaultUnaryInterceptorAuthSucceeded(t *testing.T) {
ctx := metadata.NewIncomingContext(context.Background(), metadata.Pairs("authorization", "some-auth-data"))

// test
res, err := DefaultGrpcUnaryServerInterceptor(ctx, nil, &grpc.UnaryServerInfo{}, handler, authFunc)
res, err := DefaultGRPCUnaryServerInterceptor(ctx, nil, &grpc.UnaryServerInfo{}, handler, authFunc)

// verify
assert.Nil(t, res)
Expand All @@ -63,7 +63,7 @@ func TestDefaultUnaryInterceptorAuthFailure(t *testing.T) {
ctx := metadata.NewIncomingContext(context.Background(), metadata.Pairs("authorization", "some-auth-data"))

// test
res, err := DefaultGrpcUnaryServerInterceptor(ctx, nil, &grpc.UnaryServerInfo{}, handler, authFunc)
res, err := DefaultGRPCUnaryServerInterceptor(ctx, nil, &grpc.UnaryServerInfo{}, handler, authFunc)

// verify
assert.Nil(t, res)
Expand All @@ -83,7 +83,7 @@ func TestDefaultUnaryInterceptorMissingMetadata(t *testing.T) {
}

// test
res, err := DefaultGrpcUnaryServerInterceptor(context.Background(), nil, &grpc.UnaryServerInfo{}, handler, authFunc)
res, err := DefaultGRPCUnaryServerInterceptor(context.Background(), nil, &grpc.UnaryServerInfo{}, handler, authFunc)

// verify
assert.Nil(t, res)
Expand All @@ -108,7 +108,7 @@ func TestDefaultStreamInterceptorAuthSucceeded(t *testing.T) {
}

// test
err := DefaultGrpcStreamServerInterceptor(nil, streamServer, &grpc.StreamServerInfo{}, handler, authFunc)
err := DefaultGRPCStreamServerInterceptor(nil, streamServer, &grpc.StreamServerInfo{}, handler, authFunc)

// verify
assert.NoError(t, err)
Expand All @@ -134,7 +134,7 @@ func TestDefaultStreamInterceptorAuthFailure(t *testing.T) {
}

// test
err := DefaultGrpcStreamServerInterceptor(nil, streamServer, &grpc.StreamServerInfo{}, handler, authFunc)
err := DefaultGRPCStreamServerInterceptor(nil, streamServer, &grpc.StreamServerInfo{}, handler, authFunc)

// verify
assert.Equal(t, expectedErr, err)
Expand All @@ -156,7 +156,7 @@ func TestDefaultStreamInterceptorMissingMetadata(t *testing.T) {
}

// test
err := DefaultGrpcStreamServerInterceptor(nil, streamServer, &grpc.StreamServerInfo{}, handler, authFunc)
err := DefaultGRPCStreamServerInterceptor(nil, streamServer, &grpc.StreamServerInfo{}, handler, authFunc)

// verify
assert.Equal(t, errMetadataNotFound, err)
Expand Down
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