Skip to content

Commit

Permalink
fixuop
Browse files Browse the repository at this point in the history
Signed-off-by: Juraci Paixão Kröhling <[email protected]>
  • Loading branch information
jpkrohling committed Dec 21, 2021
1 parent 5807be2 commit b6f0c01
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 93 deletions.
50 changes: 3 additions & 47 deletions config/configauth/default_serverauthenticator.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ package configauth // import "go.opentelemetry.io/collector/config/configauth"

import (
"context"
"net/http"

"google.golang.org/grpc"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componenthelper"
Expand All @@ -31,10 +28,6 @@ type Option func(*defaultServerAuthenticator)

type defaultServerAuthenticator struct {
AuthenticateFunc
GRPCStreamInterceptorFunc
GRPCUnaryInterceptorFunc
HTTPInterceptorFunc

componenthelper.StartFunc
componenthelper.ShutdownFunc
}
Expand All @@ -46,27 +39,6 @@ func WithAuthenticate(authenticateFunc AuthenticateFunc) Option {
}
}

// WithGRPCStreamInterceptor
func WithGRPCStreamInterceptor(grpcStreamInterceptorFunc GRPCStreamInterceptorFunc) Option {
return func(o *defaultServerAuthenticator) {
o.GRPCStreamInterceptorFunc = grpcStreamInterceptorFunc
}
}

// WithGRPCUnaryInterceptor
func WithGRPCUnaryInterceptor(grpcUnaryInterceptorFunc GRPCUnaryInterceptorFunc) Option {
return func(o *defaultServerAuthenticator) {
o.GRPCUnaryInterceptorFunc = grpcUnaryInterceptorFunc
}
}

// WithHTTPInterceptor
func WithHTTPInterceptor(httpInterceptorFunc HTTPInterceptorFunc) Option {
return func(o *defaultServerAuthenticator) {
o.HTTPInterceptorFunc = httpInterceptorFunc
}
}

// WithStart overrides the default `Start` function for a component.Component.
// The default always returns nil.
func WithStart(startFunc componenthelper.StartFunc) Option {
Expand All @@ -86,13 +58,9 @@ func WithShutdown(shutdownFunc componenthelper.ShutdownFunc) Option {
// NewServerAuthenticator returns a ServerAuthenticator configured with the provided options.
func NewServerAuthenticator(options ...Option) ServerAuthenticator {
bc := &defaultServerAuthenticator{
AuthenticateFunc: func(ctx context.Context, headers map[string][]string) (context.Context, error) { return ctx, nil },
GRPCStreamInterceptorFunc: DefaultGRPCStreamServerInterceptor,
GRPCUnaryInterceptorFunc: DefaultGRPCUnaryServerInterceptor,
HTTPInterceptorFunc: DefaultHTTPInterceptor,

StartFunc: func(ctx context.Context, host component.Host) error { return nil },
ShutdownFunc: func(ctx context.Context) error { return nil },
AuthenticateFunc: func(ctx context.Context, headers map[string][]string) (context.Context, error) { return ctx, nil },
StartFunc: func(ctx context.Context, host component.Host) error { return nil },
ShutdownFunc: func(ctx context.Context) error { return nil },
}

for _, op := range options {
Expand All @@ -106,18 +74,6 @@ func (a *defaultServerAuthenticator) Authenticate(ctx context.Context, headers m
return a.AuthenticateFunc(ctx, headers)
}

func (a *defaultServerAuthenticator) GRPCStreamServerInterceptor(srv interface{}, stream grpc.ServerStream, srvInfo *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
return a.GRPCStreamInterceptorFunc(srv, stream, srvInfo, handler, a.AuthenticateFunc)
}

func (a *defaultServerAuthenticator) GRPCUnaryServerInterceptor(ctx context.Context, req interface{}, srvInfo *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
return a.GRPCUnaryInterceptorFunc(ctx, req, srvInfo, handler, a.AuthenticateFunc)
}

func (a *defaultServerAuthenticator) HTTPInterceptor(next http.Handler) http.Handler {
return a.HTTPInterceptorFunc(next, a.AuthenticateFunc)
}

func (a *defaultServerAuthenticator) Start(ctx context.Context, host component.Host) error {
return a.StartFunc(ctx, host)
}
Expand Down
46 changes: 0 additions & 46 deletions config/configauth/default_serverauthenticator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@ package configauth

import (
"context"
"net/http"
"testing"

"github.com/stretchr/testify/assert"
"google.golang.org/grpc"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componenttest"
Expand Down Expand Up @@ -66,50 +64,6 @@ func TestWithAuthenticateFunc(t *testing.T) {
assert.NoError(t, err)
}

func TestWithGRPCStreamInterceptor(t *testing.T) {
called := false
e := NewServerAuthenticator(WithGRPCStreamInterceptor(func(srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler, authenticate AuthenticateFunc) error {
called = true
return nil
}))

// test
err := e.GRPCStreamServerInterceptor(nil, nil, nil, nil)

// verify
assert.True(t, called)
assert.NoError(t, err)
}

func TestWithGRPCUnaryInterceptor(t *testing.T) {
called := false
e := NewServerAuthenticator(WithGRPCUnaryInterceptor(func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler, authenticate AuthenticateFunc) (interface{}, error) {
called = true
return nil, nil
}))

// test
_, err := e.GRPCUnaryServerInterceptor(context.Background(), nil, nil, nil)

// verify
assert.True(t, called)
assert.NoError(t, err)
}

func TestWithHTTPInterceptor(t *testing.T) {
called := false
e := NewServerAuthenticator(WithHTTPInterceptor(func(handler http.Handler, authenticate AuthenticateFunc) http.Handler {
called = true
return handler
}))

// test
e.HTTPInterceptor(nil)

// verify
assert.True(t, called)
}

func TestWithStart(t *testing.T) {
called := false
e := NewServerAuthenticator(WithStart(func(c context.Context, h component.Host) error {
Expand Down

0 comments on commit b6f0c01

Please sign in to comment.