Skip to content

Commit

Permalink
Updated public methods in configauth (#9880)
Browse files Browse the repository at this point in the history
Added context.Context to the following functions:

- GetClientAuthenticator
- GetServerAuthenticator
Link to the issue:
#9808

---------

Co-authored-by: Tyler Helmuth <12352919+TylerHelmuth@users.noreply.github.com>
Co-authored-by: Alex Boten <223565+codeboten@users.noreply.github.com>
  • Loading branch information
3 people committed Jun 4, 2024
1 parent a6fdb11 commit c0e8a0b
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 17 deletions.
25 changes: 25 additions & 0 deletions .chloggen/configauth-add-context-to-public-funcs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: deprecation

# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
component: configauth

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Deprecate `GetClientAuthenticator` and `GetServerAuthenticator`, use `GetClientAuthenticatorContext` and `GetServerAuthenticatorContext` instead.

# One or more tracking issues or pull requests related to the change
issues: [9808]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [api]
18 changes: 16 additions & 2 deletions config/configauth/configauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package configauth // import "go.opentelemetry.io/collector/config/configauth"

import (
"context"
"errors"
"fmt"

Expand All @@ -33,7 +34,15 @@ func NewDefaultAuthentication() *Authentication {

// GetServerAuthenticator attempts to select the appropriate auth.Server from the list of extensions,
// based on the requested extension name. If an authenticator is not found, an error is returned.
//
// Deprecated: [v0.103.0] Use GetServerAuthenticatorContext instead.
func (a Authentication) GetServerAuthenticator(extensions map[component.ID]component.Component) (auth.Server, error) {
return a.GetServerAuthenticatorContext(context.Background(), extensions)
}

// GetServerAuthenticatorContext attempts to select the appropriate auth.Server from the list of extensions,
// based on the requested extension name. If an authenticator is not found, an error is returned.
func (a Authentication) GetServerAuthenticatorContext(_ context.Context, extensions map[component.ID]component.Component) (auth.Server, error) {
if ext, found := extensions[a.AuthenticatorID]; found {
if server, ok := ext.(auth.Server); ok {
return server, nil
Expand All @@ -44,10 +53,15 @@ func (a Authentication) GetServerAuthenticator(extensions map[component.ID]compo
return nil, fmt.Errorf("failed to resolve authenticator %q: %w", a.AuthenticatorID, errAuthenticatorNotFound)
}

// GetClientAuthenticator attempts to select the appropriate auth.Client from the list of extensions,
// Deprecated: [v0.103.0] Use GetClientAuthenticatorContext instead.
func (a Authentication) GetClientAuthenticator(extensions map[component.ID]component.Component) (auth.Client, error) {
return a.GetClientAuthenticatorContext(context.Background(), extensions)
}

// GetClientAuthenticatorContext attempts to select the appropriate auth.Client from the list of extensions,
// based on the component id of the extension. If an authenticator is not found, an error is returned.
// This should be only used by HTTP clients.
func (a Authentication) GetClientAuthenticator(extensions map[component.ID]component.Component) (auth.Client, error) {
func (a Authentication) GetClientAuthenticatorContext(_ context.Context, extensions map[component.ID]component.Component) (auth.Client, error) {
if ext, found := extensions[a.AuthenticatorID]; found {
if client, ok := ext.(auth.Client); ok {
return client, nil
Expand Down
9 changes: 5 additions & 4 deletions config/configauth/configauth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package configauth

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -48,7 +49,7 @@ func TestGetServer(t *testing.T) {
mockID: tC.authenticator,
}

authenticator, err := cfg.GetServerAuthenticator(ext)
authenticator, err := cfg.GetServerAuthenticatorContext(context.Background(), ext)

// verify
if tC.expected != nil {
Expand All @@ -67,7 +68,7 @@ func TestGetServerFails(t *testing.T) {
AuthenticatorID: component.MustNewID("does_not_exist"),
}

authenticator, err := cfg.GetServerAuthenticator(map[component.ID]component.Component{})
authenticator, err := cfg.GetServerAuthenticatorContext(context.Background(), map[component.ID]component.Component{})
assert.ErrorIs(t, err, errAuthenticatorNotFound)
assert.Nil(t, authenticator)
}
Expand Down Expand Up @@ -99,7 +100,7 @@ func TestGetClient(t *testing.T) {
mockID: tC.authenticator,
}

authenticator, err := cfg.GetClientAuthenticator(ext)
authenticator, err := cfg.GetClientAuthenticatorContext(context.Background(), ext)

// verify
if tC.expected != nil {
Expand All @@ -117,7 +118,7 @@ func TestGetClientFails(t *testing.T) {
cfg := &Authentication{
AuthenticatorID: component.MustNewID("does_not_exist"),
}
authenticator, err := cfg.GetClientAuthenticator(map[component.ID]component.Component{})
authenticator, err := cfg.GetClientAuthenticatorContext(context.Background(), map[component.ID]component.Component{})
assert.ErrorIs(t, err, errAuthenticatorNotFound)
assert.Nil(t, authenticator)
}
12 changes: 6 additions & 6 deletions config/configgrpc/configgrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,16 +218,16 @@ func (gcs *ClientConfig) isSchemeHTTPS() bool {
// a non-blocking dial (the function won't wait for connections to be
// established, and connecting happens in the background). To make it a blocking
// dial, use grpc.WithBlock() dial option.
func (gcs *ClientConfig) ToClientConn(_ context.Context, host component.Host, settings component.TelemetrySettings, extraOpts ...grpc.DialOption) (*grpc.ClientConn, error) {
opts, err := gcs.toDialOptions(host, settings)
func (gcs *ClientConfig) ToClientConn(ctx context.Context, host component.Host, settings component.TelemetrySettings, extraOpts ...grpc.DialOption) (*grpc.ClientConn, error) {
opts, err := gcs.toDialOptions(ctx, host, settings)
if err != nil {
return nil, err
}
opts = append(opts, extraOpts...)
return grpc.NewClient(gcs.sanitizedEndpoint(), opts...)
}

func (gcs *ClientConfig) toDialOptions(host component.Host, settings component.TelemetrySettings) ([]grpc.DialOption, error) {
func (gcs *ClientConfig) toDialOptions(ctx context.Context, host component.Host, settings component.TelemetrySettings) ([]grpc.DialOption, error) {
var opts []grpc.DialOption
if gcs.Compression.IsCompressed() {
cp, err := getGRPCCompressionName(gcs.Compression)
Expand All @@ -237,7 +237,7 @@ func (gcs *ClientConfig) toDialOptions(host component.Host, settings component.T
opts = append(opts, grpc.WithDefaultCallOptions(grpc.UseCompressor(cp)))
}

tlsCfg, err := gcs.TLSSetting.LoadTLSConfig(context.Background())
tlsCfg, err := gcs.TLSSetting.LoadTLSConfig(ctx)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -271,7 +271,7 @@ func (gcs *ClientConfig) toDialOptions(host component.Host, settings component.T
return nil, errors.New("no extensions configuration available")
}

grpcAuthenticator, cerr := gcs.Auth.GetClientAuthenticator(host.GetExtensions())
grpcAuthenticator, cerr := gcs.Auth.GetClientAuthenticatorContext(ctx, host.GetExtensions())
if cerr != nil {
return nil, cerr
}
Expand Down Expand Up @@ -387,7 +387,7 @@ func (gss *ServerConfig) toServerOption(host component.Host, settings component.
var sInterceptors []grpc.StreamServerInterceptor

if gss.Auth != nil {
authenticator, err := gss.Auth.GetServerAuthenticator(host.GetExtensions())
authenticator, err := gss.Auth.GetServerAuthenticatorContext(context.Background(), host.GetExtensions())
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions config/configgrpc/configgrpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func TestDefaultGrpcClientSettings(t *testing.T) {
Insecure: true,
},
}
opts, err := gcs.toDialOptions(componenttest.NewNopHost(), tt.TelemetrySettings())
opts, err := gcs.toDialOptions(context.Background(), componenttest.NewNopHost(), tt.TelemetrySettings())
assert.NoError(t, err)
assert.Len(t, opts, 2)
}
Expand Down Expand Up @@ -226,7 +226,7 @@ func TestAllGrpcClientSettings(t *testing.T) {
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
opts, err := test.settings.toDialOptions(test.host, tt.TelemetrySettings())
opts, err := test.settings.toDialOptions(context.Background(), test.host, tt.TelemetrySettings())
assert.NoError(t, err)
assert.Len(t, opts, 9)
})
Expand Down Expand Up @@ -431,7 +431,7 @@ func TestUseSecure(t *testing.T) {
TLSSetting: configtls.ClientConfig{},
Keepalive: nil,
}
dialOpts, err := gcs.toDialOptions(componenttest.NewNopHost(), tt.TelemetrySettings())
dialOpts, err := gcs.toDialOptions(context.Background(), componenttest.NewNopHost(), tt.TelemetrySettings())
assert.NoError(t, err)
assert.Len(t, dialOpts, 2)
}
Expand Down
4 changes: 2 additions & 2 deletions config/confighttp/confighttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func (hcs *ClientConfig) ToClient(ctx context.Context, host component.Host, sett
return nil, errors.New("extensions configuration not found")
}

httpCustomAuthRoundTripper, aerr := hcs.Auth.GetClientAuthenticator(ext)
httpCustomAuthRoundTripper, aerr := hcs.Auth.GetClientAuthenticatorContext(ctx, ext)
if aerr != nil {
return nil, aerr
}
Expand Down Expand Up @@ -352,7 +352,7 @@ func (hss *ServerConfig) ToServer(_ context.Context, host component.Host, settin
}

if hss.Auth != nil {
server, err := hss.Auth.GetServerAuthenticator(host.GetExtensions())
server, err := hss.Auth.GetServerAuthenticatorContext(context.Background(), host.GetExtensions())
if err != nil {
return nil, err
}
Expand Down

0 comments on commit c0e8a0b

Please sign in to comment.