-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Implement default server authenticators #4558
Merged
bogdandrutu
merged 3 commits into
open-telemetry:main
from
jpkrohling:jpkrohling/issue4556
Jan 3, 2022
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,86 @@ | ||
// 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" | ||
|
||
"go.opentelemetry.io/collector/component" | ||
"go.opentelemetry.io/collector/component/componenthelper" | ||
) | ||
|
||
var _ ServerAuthenticator = (*defaultServerAuthenticator)(nil) | ||
|
||
// Option represents the possible options for NewServerAuthenticator. | ||
type Option func(*defaultServerAuthenticator) | ||
|
||
type defaultServerAuthenticator struct { | ||
AuthenticateFunc | ||
componenthelper.StartFunc | ||
componenthelper.ShutdownFunc | ||
} | ||
|
||
// WithAuthenticate specifies which function to use to perform the authentication. | ||
func WithAuthenticate(authenticateFunc AuthenticateFunc) Option { | ||
return func(o *defaultServerAuthenticator) { | ||
o.AuthenticateFunc = authenticateFunc | ||
} | ||
} | ||
|
||
// 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 }, | ||
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 | ||
} | ||
|
||
// Authenticate performs the authentication. | ||
func (a *defaultServerAuthenticator) Authenticate(ctx context.Context, headers map[string][]string) (context.Context, error) { | ||
return a.AuthenticateFunc(ctx, headers) | ||
} | ||
|
||
// Start the component. | ||
func (a *defaultServerAuthenticator) Start(ctx context.Context, host component.Host) error { | ||
return a.StartFunc(ctx, host) | ||
} | ||
|
||
// Shutdown stops the component. | ||
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,95 @@ | ||
// 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" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"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 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.
This file was deleted.
Oops, something went wrong.
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a good example of how consumers would be able to use this. I like the new approach, especially as it allows consumers to omit specifying the default implementations to use, like demonstrated by the removal of line 785.