-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement default server authenticators
Allow the interfaces to be extended without affecting implementations by allowing authenticators to provide which functions to override. This is a non-breaking change, and current implementations might be changed in the future to use this. Fixes #4556 Signed-off-by: Juraci Paixão Kröhling <[email protected]>
- Loading branch information
1 parent
5514ecb
commit 684e4e6
Showing
8 changed files
with
277 additions
and
159 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
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" | ||
) | ||
|
||
var _ ServerAuthenticator = (*defaultServerAuthenticator)(nil) | ||
|
||
// Option represents the possible options for New. | ||
type Option func(*defaultServerAuthenticator) | ||
|
||
type defaultServerAuthenticator struct { | ||
AuthenticateFunc | ||
GRPCStreamInterceptorFunc | ||
GRPCUnaryInterceptorFunc | ||
HTTPInterceptorFunc | ||
|
||
componenthelper.StartFunc | ||
componenthelper.ShutdownFunc | ||
} | ||
|
||
// WithAuthenticate | ||
func WithAuthenticate(authenticateFunc AuthenticateFunc) Option { | ||
return func(o *defaultServerAuthenticator) { | ||
o.AuthenticateFunc = authenticateFunc | ||
} | ||
} | ||
|
||
// 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 { | ||
return func(o *defaultServerAuthenticator) { | ||
o.StartFunc = startFunc | ||
} | ||
} | ||
|
||
// WithShutdown overrides the default `Shutdown` function for a component.Component. | ||
// The default always returns nil. | ||
func WithShutdown(shutdownFunc componenthelper.ShutdownFunc) Option { | ||
return func(o *defaultServerAuthenticator) { | ||
o.ShutdownFunc = shutdownFunc | ||
} | ||
} | ||
|
||
// 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 }, | ||
} | ||
|
||
for _, op := range options { | ||
op(bc) | ||
} | ||
|
||
return bc | ||
} | ||
|
||
func (a *defaultServerAuthenticator) Authenticate(ctx context.Context, headers map[string][]string) (context.Context, error) { | ||
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) | ||
} | ||
|
||
func (a *defaultServerAuthenticator) Shutdown(ctx context.Context) error { | ||
return a.ShutdownFunc(ctx) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
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" | ||
) | ||
|
||
func TestDefaultValues(t *testing.T) { | ||
// prepare | ||
e := NewServerAuthenticator() | ||
|
||
// test | ||
t.Run("start", func(t *testing.T) { | ||
err := e.Start(context.Background(), componenttest.NewNopHost()) | ||
assert.NoError(t, err) | ||
}) | ||
|
||
t.Run("authenticate", func(t *testing.T) { | ||
ctx, err := e.Authenticate(context.Background(), make(map[string][]string)) | ||
assert.NotNil(t, ctx) | ||
assert.NoError(t, err) | ||
}) | ||
|
||
t.Run("shutdown", func(t *testing.T) { | ||
err := e.Shutdown(context.Background()) | ||
assert.NoError(t, err) | ||
}) | ||
} | ||
|
||
func TestWithAuthenticateFunc(t *testing.T) { | ||
// prepare | ||
authCalled := false | ||
e := NewServerAuthenticator( | ||
WithAuthenticate(func(ctx context.Context, headers map[string][]string) (context.Context, error) { | ||
authCalled = true | ||
return ctx, nil | ||
}), | ||
) | ||
|
||
// test | ||
_, err := e.Authenticate(context.Background(), make(map[string][]string)) | ||
|
||
// verify | ||
assert.True(t, authCalled) | ||
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 { | ||
called = true | ||
return nil | ||
})) | ||
|
||
// test | ||
err := e.Start(context.Background(), componenttest.NewNopHost()) | ||
|
||
// verify | ||
assert.True(t, called) | ||
assert.NoError(t, err) | ||
} | ||
|
||
func TestWithShutdown(t *testing.T) { | ||
called := false | ||
e := NewServerAuthenticator(WithShutdown(func(c context.Context) error { | ||
called = true | ||
return nil | ||
})) | ||
|
||
// test | ||
err := e.Shutdown(context.Background()) | ||
|
||
// verify | ||
assert.True(t, called) | ||
assert.NoError(t, err) | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.