Skip to content

Commit 5427cb5

Browse files
committed
[extensionauth] Split by protocol type
1 parent 92f5fb6 commit 5427cb5

File tree

6 files changed

+47
-23
lines changed

6 files changed

+47
-23
lines changed

config/configauth/configauth.go

+16-3
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,25 @@ func (a Authentication) GetServerAuthenticator(_ context.Context, extensions map
4040
return nil, fmt.Errorf("failed to resolve authenticator %q: %w", a.AuthenticatorID, errAuthenticatorNotFound)
4141
}
4242

43-
// GetClientAuthenticator attempts to select the appropriate extensionauth.Client from the list of extensions,
43+
// GetHTTPClientAuthenticator attempts to select the appropriate extensionauth.Client from the list of extensions,
4444
// based on the component id of the extension. If an authenticator is not found, an error is returned.
4545
// This should be only used by HTTP clients.
46-
func (a Authentication) GetClientAuthenticator(_ context.Context, extensions map[component.ID]component.Component) (extensionauth.Client, error) {
46+
func (a Authentication) GetHTTPClientAuthenticator(_ context.Context, extensions map[component.ID]component.Component) (extensionauth.HTTPClient, error) {
4747
if ext, found := extensions[a.AuthenticatorID]; found {
48-
if client, ok := ext.(extensionauth.Client); ok {
48+
if client, ok := ext.(extensionauth.HTTPClient); ok {
49+
return client, nil
50+
}
51+
return nil, errNotClient
52+
}
53+
return nil, fmt.Errorf("failed to resolve authenticator %q: %w", a.AuthenticatorID, errAuthenticatorNotFound)
54+
}
55+
56+
// GetGRPCClientAuthenticator attempts to select the appropriate extensionauth.Client from the list of extensions,
57+
// based on the component id of the extension. If an authenticator is not found, an error is returned.
58+
// This should be only used by HTTP clients.
59+
func (a Authentication) GetGRPCClientAuthenticator(_ context.Context, extensions map[component.ID]component.Component) (extensionauth.GRPCClient, error) {
60+
if ext, found := extensions[a.AuthenticatorID]; found {
61+
if client, ok := ext.(extensionauth.GRPCClient); ok {
4962
return client, nil
5063
}
5164
return nil, errNotClient

config/configauth/configauth_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func TestGetClient(t *testing.T) {
110110
mockID: tt.authenticator,
111111
}
112112

113-
authenticator, err := cfg.GetClientAuthenticator(context.Background(), ext)
113+
authenticator, err := cfg.GetHTTPClientAuthenticator(context.Background(), ext)
114114

115115
// verify
116116
if tt.expected != nil {
@@ -128,7 +128,7 @@ func TestGetClientFails(t *testing.T) {
128128
cfg := &Authentication{
129129
AuthenticatorID: component.MustNewID("does_not_exist"),
130130
}
131-
authenticator, err := cfg.GetClientAuthenticator(context.Background(), map[component.ID]component.Component{})
131+
authenticator, err := cfg.GetGRPCClientAuthenticator(context.Background(), map[component.ID]component.Component{})
132132
require.ErrorIs(t, err, errAuthenticatorNotFound)
133133
assert.Nil(t, authenticator)
134134
}

config/configgrpc/configgrpc.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ func (gcs *ClientConfig) getGrpcDialOptions(
322322
return nil, errors.New("no extensions configuration available")
323323
}
324324

325-
grpcAuthenticator, cerr := gcs.Auth.GetClientAuthenticator(ctx, host.GetExtensions())
325+
grpcAuthenticator, cerr := gcs.Auth.GetGRPCClientAuthenticator(ctx, host.GetExtensions())
326326
if cerr != nil {
327327
return nil, cerr
328328
}

config/configgrpc/configgrpc_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func mustNewServerAuth(t *testing.T, opts ...extensionauth.ServerOption) extensi
4040
return srv
4141
}
4242

43-
func mustNewClientAuth(t *testing.T, opts ...extensionauth.ClientOption) extensionauth.Client {
43+
func mustNewClientAuth(t *testing.T, opts ...extensionauth.ClientOption) *extensionauth.Client {
4444
t.Helper()
4545
client, err := extensionauth.NewClient(opts...)
4646
require.NoError(t, err)

config/confighttp/confighttp.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ func (hcs *ClientConfig) ToClient(ctx context.Context, host component.Host, sett
203203
return nil, errors.New("extensions configuration not found")
204204
}
205205

206-
httpCustomAuthRoundTripper, aerr := hcs.Auth.GetClientAuthenticator(ctx, ext)
206+
httpCustomAuthRoundTripper, aerr := hcs.Auth.GetHTTPClientAuthenticator(ctx, ext)
207207
if aerr != nil {
208208
return nil, aerr
209209
}

extension/extensionauth/client.go

+26-15
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,34 @@ import (
1212
"go.opentelemetry.io/collector/extension"
1313
)
1414

15-
// Client is an Extension that can be used as an authenticator for the configauth.Authentication option.
15+
// HTTPClient is an Extension that can be used as an HTTP authenticator for the configauth.Authentication option.
1616
// Authenticators are then included as part of OpenTelemetry Collector builds and can be referenced by their
1717
// names from the Authentication configuration.
18-
type Client interface {
18+
type HTTPClient interface {
1919
extension.Extension
2020

2121
// RoundTripper returns a RoundTripper that can be used to authenticate HTTP requests.
2222
RoundTripper(base http.RoundTripper) (http.RoundTripper, error)
23+
}
24+
25+
// GRPCClient is an Extension that can be used as an HTTP authenticator for the configauth.Authentication option.
26+
// Authenticators are then included as part of OpenTelemetry Collector builds and can be referenced by their
27+
// names from the Authentication configuration.
28+
type GRPCClient interface {
29+
extension.Extension
2330

2431
// PerRPCCredentials returns a PerRPCCredentials that can be used to authenticate gRPC requests.
2532
PerRPCCredentials() (credentials.PerRPCCredentials, error)
2633
}
2734

2835
// ClientOption represents the possible options for NewClient.
2936
type ClientOption interface {
30-
apply(*defaultClient)
37+
apply(*Client)
3138
}
3239

33-
type clientOptionFunc func(*defaultClient)
40+
type clientOptionFunc func(*Client)
3441

35-
func (of clientOptionFunc) apply(e *defaultClient) {
42+
func (of clientOptionFunc) apply(e *Client) {
3643
of(e)
3744
}
3845

@@ -42,25 +49,29 @@ type ClientRoundTripperFunc func(base http.RoundTripper) (http.RoundTripper, err
4249
// ClientPerRPCCredentialsFunc specifies the function that returns a PerRPCCredentials that can be used to authenticate gRPC requests.
4350
type ClientPerRPCCredentialsFunc func() (credentials.PerRPCCredentials, error)
4451

45-
var _ Client = (*defaultClient)(nil)
52+
var (
53+
_ HTTPClient = (*Client)(nil)
54+
_ GRPCClient = (*Client)(nil)
55+
)
4656

47-
type defaultClient struct {
57+
// Client is an Extension that can be used as an HTTP and gRPC authenticator for the configauth.Authentication option.
58+
type Client struct {
4859
component.StartFunc
4960
component.ShutdownFunc
5061
clientRoundTripperFunc ClientRoundTripperFunc
5162
clientPerRPCCredentialsFunc ClientPerRPCCredentialsFunc
5263
}
5364

5465
// PerRPCCredentials implements Client.
55-
func (d *defaultClient) PerRPCCredentials() (credentials.PerRPCCredentials, error) {
66+
func (d *Client) PerRPCCredentials() (credentials.PerRPCCredentials, error) {
5667
if d.clientPerRPCCredentialsFunc == nil {
5768
return nil, nil
5869
}
5970
return d.clientPerRPCCredentialsFunc()
6071
}
6172

6273
// RoundTripper implements Client.
63-
func (d *defaultClient) RoundTripper(base http.RoundTripper) (http.RoundTripper, error) {
74+
func (d *Client) RoundTripper(base http.RoundTripper) (http.RoundTripper, error) {
6475
if d.clientRoundTripperFunc == nil {
6576
return base, nil
6677
}
@@ -70,38 +81,38 @@ func (d *defaultClient) RoundTripper(base http.RoundTripper) (http.RoundTripper,
7081
// WithClientStart overrides the default `Start` function for a component.Component.
7182
// The default always returns nil.
7283
func WithClientStart(startFunc component.StartFunc) ClientOption {
73-
return clientOptionFunc(func(o *defaultClient) {
84+
return clientOptionFunc(func(o *Client) {
7485
o.StartFunc = startFunc
7586
})
7687
}
7788

7889
// WithClientShutdown overrides the default `Shutdown` function for a component.Component.
7990
// The default always returns nil.
8091
func WithClientShutdown(shutdownFunc component.ShutdownFunc) ClientOption {
81-
return clientOptionFunc(func(o *defaultClient) {
92+
return clientOptionFunc(func(o *Client) {
8293
o.ShutdownFunc = shutdownFunc
8394
})
8495
}
8596

8697
// WithClientRoundTripper provides a `RoundTripper` function for this client authenticator.
8798
// The default round tripper is no-op.
8899
func WithClientRoundTripper(roundTripperFunc ClientRoundTripperFunc) ClientOption {
89-
return clientOptionFunc(func(o *defaultClient) {
100+
return clientOptionFunc(func(o *Client) {
90101
o.clientRoundTripperFunc = roundTripperFunc
91102
})
92103
}
93104

94105
// WithClientPerRPCCredentials provides a `PerRPCCredentials` function for this client authenticator.
95106
// There's no default.
96107
func WithClientPerRPCCredentials(perRPCCredentialsFunc ClientPerRPCCredentialsFunc) ClientOption {
97-
return clientOptionFunc(func(o *defaultClient) {
108+
return clientOptionFunc(func(o *Client) {
98109
o.clientPerRPCCredentialsFunc = perRPCCredentialsFunc
99110
})
100111
}
101112

102113
// NewClient returns a Client configured with the provided options.
103-
func NewClient(options ...ClientOption) (Client, error) {
104-
bc := &defaultClient{}
114+
func NewClient(options ...ClientOption) (*Client, error) {
115+
bc := &Client{}
105116

106117
for _, op := range options {
107118
op.apply(bc)

0 commit comments

Comments
 (0)