Skip to content

Commit

Permalink
Use directly the ComponentID in configauth (#4238)
Browse files Browse the repository at this point in the history
Depends on #4237

Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com>
  • Loading branch information
bogdandrutu committed Oct 22, 2021
1 parent bd13c61 commit 108a49e
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 52 deletions.
31 changes: 8 additions & 23 deletions config/configauth/configauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,60 +28,45 @@ var (

// 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.
AuthenticatorName string `mapstructure:"authenticator"`
// AuthenticatorID specifies the name of the extension to use in order to authenticate the incoming data point.
AuthenticatorID config.ComponentID `mapstructure:"authenticator"`
}

// GetServerAuthenticator attempts to select the appropriate ServerAuthenticator from the list of extensions,
// based on the requested extension name. If an authenticator is not found, an error is returned.
func (a Authentication) GetServerAuthenticator(extensions map[config.ComponentID]component.Extension) (ServerAuthenticator, error) {
componentID, err := config.NewComponentIDFromString(a.AuthenticatorName)
if err != nil {
return nil, err
}

if ext, found := extensions[componentID]; found {
if ext, found := extensions[a.AuthenticatorID]; found {
if auth, ok := ext.(ServerAuthenticator); ok {
return auth, nil
}
return nil, fmt.Errorf("requested authenticator is not a server authenticator")
}

return nil, fmt.Errorf("failed to resolve authenticator %q: %w", componentID.String(), errAuthenticatorNotFound)
return nil, fmt.Errorf("failed to resolve authenticator %q: %w", a.AuthenticatorID, errAuthenticatorNotFound)
}

// GetHTTPClientAuthenticator attempts to select the appropriate HTTPClientAuthenticator 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) GetHTTPClientAuthenticator(extensions map[config.ComponentID]component.Extension) (HTTPClientAuthenticator, error) {
componentID, err := config.NewComponentIDFromString(a.AuthenticatorName)
if err != nil {
return nil, err
}

if ext, found := extensions[componentID]; found {
if ext, found := extensions[a.AuthenticatorID]; found {
if auth, ok := ext.(HTTPClientAuthenticator); ok {
return auth, nil
}
return nil, fmt.Errorf("requested authenticator is not a HTTPClientAuthenticator")
}
return nil, fmt.Errorf("failed to resolve authenticator %q: %w", componentID.String(), errAuthenticatorNotFound)
return nil, fmt.Errorf("failed to resolve authenticator %q: %w", a.AuthenticatorID, errAuthenticatorNotFound)
}

// GetGRPCClientAuthenticator attempts to select the appropriate GRPCClientAuthenticator 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 only be used by gRPC clients.
func (a Authentication) GetGRPCClientAuthenticator(extensions map[config.ComponentID]component.Extension) (GRPCClientAuthenticator, error) {
componentID, err := config.NewComponentIDFromString(a.AuthenticatorName)
if err != nil {
return nil, err
}

if ext, found := extensions[componentID]; found {
if ext, found := extensions[a.AuthenticatorID]; found {
if auth, ok := ext.(GRPCClientAuthenticator); ok {
return auth, nil
}
return nil, fmt.Errorf("requested authenticator is not a GRPCClientAuthenticator")
}
return nil, fmt.Errorf("failed to resolve authenticator %q: %w", componentID.String(), errAuthenticatorNotFound)
return nil, fmt.Errorf("failed to resolve authenticator %q: %w", a.AuthenticatorID, errAuthenticatorNotFound)
}
4 changes: 2 additions & 2 deletions config/configauth/configauth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
func TestGetAuthenticator(t *testing.T) {
// prepare
cfg := &Authentication{
AuthenticatorName: "mock",
AuthenticatorID: config.NewComponentID("mock"),
}
ext := map[config.ComponentID]component.Extension{
config.NewComponentID("mock"): &MockAuthenticator{},
Expand All @@ -49,7 +49,7 @@ func TestGetAuthenticatorFails(t *testing.T) {
{
desc: "ServerAuthenticator not found",
cfg: &Authentication{
AuthenticatorName: "does-not-exist",
AuthenticatorID: config.NewComponentID("does-not-exist"),
},
ext: map[config.ComponentID]component.Extension{},
expected: errAuthenticatorNotFound,
Expand Down
18 changes: 4 additions & 14 deletions config/configgrpc/configgrpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func TestAllGrpcClientSettings(t *testing.T) {
WriteBufferSize: 1024,
WaitForReady: true,
BalancerName: "round_robin",
Auth: &configauth.Authentication{AuthenticatorName: "testauth"},
Auth: &configauth.Authentication{AuthenticatorID: config.NewComponentID("testauth")},
}

host := &mockHost{
Expand Down Expand Up @@ -133,7 +133,7 @@ func TestGrpcServerAuthSettings(t *testing.T) {

// test
gss.Auth = &configauth.Authentication{
AuthenticatorName: "mock",
AuthenticatorID: config.NewComponentID("mock"),
}
host := &mockHost{
ext: map[config.ComponentID]component.Extension{
Expand Down Expand Up @@ -208,29 +208,19 @@ func TestGRPCClientSettingsError(t *testing.T) {
BalancerName: "test",
},
},
{
err: "id must not be empty",
settings: GRPCClientSettings{
Endpoint: "localhost:1234",
Auth: &configauth.Authentication{},
},
host: &mockHost{ext: map[config.ComponentID]component.Extension{
config.NewComponentID("mock"): &configauth.MockClientAuthenticator{},
}},
},
{
err: "failed to resolve authenticator \"doesntexist\": authenticator not found",
settings: GRPCClientSettings{
Endpoint: "localhost:1234",
Auth: &configauth.Authentication{AuthenticatorName: "doesntexist"},
Auth: &configauth.Authentication{AuthenticatorID: config.NewComponentID("doesntexist")},
},
host: &mockHost{ext: map[config.ComponentID]component.Extension{}},
},
{
err: "no extensions configuration available",
settings: GRPCClientSettings{
Endpoint: "localhost:1234",
Auth: &configauth.Authentication{AuthenticatorName: "doesntexist"},
Auth: &configauth.Authentication{AuthenticatorID: config.NewComponentID("doesntexist")},
},
host: &mockHost{},
},
Expand Down
17 changes: 5 additions & 12 deletions config/confighttp/confighttp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,7 @@ func TestHTTPClientSettingsError(t *testing.T) {
err: "failed to resolve authenticator \"dummy\": authenticator not found",
settings: HTTPClientSettings{
Endpoint: "https://localhost:1234/v1/traces",
Auth: &configauth.Authentication{AuthenticatorName: "dummy"},
},
},
{
err: "id must not be empty",
settings: HTTPClientSettings{
Endpoint: "https://localhost:1234/v1/traces",
Auth: &configauth.Authentication{AuthenticatorName: ""},
Auth: &configauth.Authentication{AuthenticatorID: config.NewComponentID("dummy")},
},
},
}
Expand Down Expand Up @@ -174,7 +167,7 @@ func TestHTTPClientSettingWithAuthConfig(t *testing.T) {
name: "with_auth_configuration_and_no_extension",
settings: HTTPClientSettings{
Endpoint: "localhost:1234",
Auth: &configauth.Authentication{AuthenticatorName: "dummy"},
Auth: &configauth.Authentication{AuthenticatorID: config.NewComponentID("dummy")},
},
shouldErr: true,
extensionMap: map[config.ComponentID]component.Extension{
Expand All @@ -185,15 +178,15 @@ func TestHTTPClientSettingWithAuthConfig(t *testing.T) {
name: "with_auth_configuration_and_no_extension_map",
settings: HTTPClientSettings{
Endpoint: "localhost:1234",
Auth: &configauth.Authentication{AuthenticatorName: "dummy"},
Auth: &configauth.Authentication{AuthenticatorID: config.NewComponentID("dummy")},
},
shouldErr: true,
},
{
name: "with_auth_configuration_has_extension",
settings: HTTPClientSettings{
Endpoint: "localhost:1234",
Auth: &configauth.Authentication{AuthenticatorName: "mock"},
Auth: &configauth.Authentication{AuthenticatorID: config.NewComponentID("mock")},
},
shouldErr: false,
extensionMap: map[config.ComponentID]component.Extension{
Expand All @@ -204,7 +197,7 @@ func TestHTTPClientSettingWithAuthConfig(t *testing.T) {
name: "with_auth_configuration_has_err_extension",
settings: HTTPClientSettings{
Endpoint: "localhost:1234",
Auth: &configauth.Authentication{AuthenticatorName: "mock"},
Auth: &configauth.Authentication{AuthenticatorID: config.NewComponentID("mock")},
},
shouldErr: true,
extensionMap: map[config.ComponentID]component.Extension{
Expand Down
2 changes: 1 addition & 1 deletion exporter/otlpexporter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func TestLoadConfig(t *testing.T) {
},
WriteBufferSize: 512 * 1024,
BalancerName: "round_robin",
Auth: &configauth.Authentication{AuthenticatorName: "nop"},
Auth: &configauth.Authentication{AuthenticatorID: config.NewComponentID("nop")},
},
})
}

0 comments on commit 108a49e

Please sign in to comment.