| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
// Package configauth implements the configuration settings to |
| 5 |
// ensure authentication on incoming requests, and allows |
| 6 |
// exporters to add authentication on outgoing requests. |
| 7 |
package configauth // import "go.opentelemetry.io/collector/config/configauth" |
| 8 |
|
| 9 |
import ( |
| 10 |
"context" |
| 11 |
"errors" |
| 12 |
"fmt" |
| 13 |
|
| 14 |
"go.opentelemetry.io/collector/component" |
| 15 |
"go.opentelemetry.io/collector/extension/extensionauth" |
| 16 |
) |
| 17 |
|
| 18 |
var ( |
| 19 |
errAuthenticatorNotFound = errors.New("authenticator not found") |
| 20 |
errNotHTTPClient = errors.New("requested authenticator is not a HTTP client authenticator") |
| 21 |
errNotGRPCClient = errors.New("requested authenticator is not a gRPC client authenticator") |
| 22 |
errNotServer = errors.New("requested authenticator is not a server authenticator") |
| 23 |
) |
| 24 |
|
| 25 |
// Config defines the auth settings for the receiver. |
| 26 |
type Config struct { |
| 27 |
// AuthenticatorID specifies the name of the extension to use in order to authenticate the incoming data point. |
| 28 |
AuthenticatorID component.ID `mapstructure:"authenticator,omitempty"` |
| 29 |
// prevent unkeyed literal initialization |
| 30 |
_ struct{} |
| 31 |
} |
| 32 |
|
| 33 |
// GetServerAuthenticator attempts to select the appropriate extensionauth.Server from the list of extensions, |
| 34 |
// based on the requested extension name. If an authenticator is not found, an error is returned. |
| 35 |
func (a Config) GetServerAuthenticator(_ context.Context, extensions map[component.ID]component.Component) (extensionauth.Server, error) { |
| 36 |
if ext, found := extensions[a.AuthenticatorID]; found { |
| 37 |
if server, ok := ext.(extensionauth.Server); ok { |
| 38 |
return server, nil |
| 39 |
} |
| 40 |
return nil, errNotServer |
| 41 |
} |
| 42 |
|
| 43 |
return nil, fmt.Errorf("failed to resolve authenticator %q: %w", a.AuthenticatorID, errAuthenticatorNotFound) |
| 44 |
} |
| 45 |
|
| 46 |
// GetHTTPClientAuthenticator attempts to select the appropriate extensionauth.Client from the list of extensions, |
| 47 |
// based on the component id of the extension. If an authenticator is not found, an error is returned. |
| 48 |
// This should be only used by HTTP clients. |
| 49 |
func (a Config) GetHTTPClientAuthenticator(_ context.Context, extensions map[component.ID]component.Component) (extensionauth.HTTPClient, error) { |
| 50 |
if ext, found := extensions[a.AuthenticatorID]; found { |
| 51 |
if client, ok := ext.(extensionauth.HTTPClient); ok { |
| 52 |
return client, nil |
| 53 |
} |
| 54 |
return nil, errNotHTTPClient |
| 55 |
} |
| 56 |
return nil, fmt.Errorf("failed to resolve authenticator %q: %w", a.AuthenticatorID, errAuthenticatorNotFound) |
| 57 |
} |
| 58 |
|
| 59 |
// GetGRPCClientAuthenticator attempts to select the appropriate extensionauth.Client from the list of extensions, |
| 60 |
// based on the component id of the extension. If an authenticator is not found, an error is returned. |
| 61 |
// This should be only used by gRPC clients. |
| 62 |
func (a Config) GetGRPCClientAuthenticator(_ context.Context, extensions map[component.ID]component.Component) (extensionauth.GRPCClient, error) { |
| 63 |
if ext, found := extensions[a.AuthenticatorID]; found { |
| 64 |
if client, ok := ext.(extensionauth.GRPCClient); ok { |
| 65 |
return client, nil |
| 66 |
} |
| 67 |
return nil, errNotGRPCClient |
| 68 |
} |
| 69 |
return nil, fmt.Errorf("failed to resolve authenticator %q: %w", a.AuthenticatorID, errAuthenticatorNotFound) |
| 70 |
} |