Skip to content
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

[config/confighttp] Ensure Auth happens after compression #7574

Merged
merged 2 commits into from
May 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .chloggen/fix_configHTTPAuth.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: "bug_fix"

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

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Ensure Auth RoundTripper follows compression/header changes

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

# (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:
47 changes: 26 additions & 21 deletions config/confighttp/confighttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,28 +135,10 @@ func (hcs *HTTPClientSettings) ToClient(host component.Host, settings component.
}

clientTransport := (http.RoundTripper)(transport)
if len(hcs.Headers) > 0 {
clientTransport = &headerRoundTripper{
transport: transport,
headers: hcs.Headers,
}
}
// wrapping http transport with otelhttp transport to enable otel instrumenetation
if settings.TracerProvider != nil && settings.MeterProvider != nil {
clientTransport = otelhttp.NewTransport(
clientTransport,
otelhttp.WithTracerProvider(settings.TracerProvider),
otelhttp.WithMeterProvider(settings.MeterProvider),
otelhttp.WithPropagators(otel.GetTextMapPropagator()),
)
}

// Compress the body using specified compression methods if non-empty string is provided.
// Supporting gzip, zlib, deflate, snappy, and zstd; none is treated as uncompressed.
if configcompression.IsCompressed(hcs.Compression) {
clientTransport = newCompressRoundTripper(clientTransport, hcs.Compression)
}

// The Auth RoundTripper should always be the innermost to ensure that
// request signing-based auth mechanisms operate after compression
// and header middleware modifies the request
if hcs.Auth != nil {
ext := host.GetExtensions()
if ext == nil {
Expand All @@ -174,6 +156,29 @@ func (hcs *HTTPClientSettings) ToClient(host component.Host, settings component.
}
}

if len(hcs.Headers) > 0 {
clientTransport = &headerRoundTripper{
transport: clientTransport,
headers: hcs.Headers,
}
}

// Compress the body using specified compression methods if non-empty string is provided.
// Supporting gzip, zlib, deflate, snappy, and zstd; none is treated as uncompressed.
if configcompression.IsCompressed(hcs.Compression) {
clientTransport = newCompressRoundTripper(clientTransport, hcs.Compression)
}

// wrapping http transport with otelhttp transport to enable otel instrumenetation
if settings.TracerProvider != nil && settings.MeterProvider != nil {
clientTransport = otelhttp.NewTransport(
clientTransport,
otelhttp.WithTracerProvider(settings.TracerProvider),
otelhttp.WithMeterProvider(settings.MeterProvider),
otelhttp.WithPropagators(otel.GetTextMapPropagator()),
)
}

if hcs.CustomRoundTripper != nil {
clientTransport, err = hcs.CustomRoundTripper(clientTransport)
if err != nil {
Expand Down
53 changes: 51 additions & 2 deletions config/confighttp/confighttp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ import (
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/config/configauth"
"go.opentelemetry.io/collector/config/configcompression"
"go.opentelemetry.io/collector/config/configopaque"
"go.opentelemetry.io/collector/config/configtelemetry"
"go.opentelemetry.io/collector/config/configtls"
"go.opentelemetry.io/collector/extension/auth"
"go.opentelemetry.io/collector/extension/auth/authtest"
Expand Down Expand Up @@ -319,6 +321,34 @@ func TestHTTPClientSettingWithAuthConfig(t *testing.T) {
},
},
},
{
name: "with_auth_configuration_has_extension_and_headers",
settings: HTTPClientSettings{
Endpoint: "localhost:1234",
Auth: &configauth.Authentication{AuthenticatorID: component.NewID("mock")},
Headers: map[string]configopaque.String{"foo": "bar"},
},
shouldErr: false,
host: &mockHost{
ext: map[component.ID]component.Component{
component.NewID("mock"): &authtest.MockClient{ResultRoundTripper: &customRoundTripper{}},
},
},
},
{
name: "with_auth_configuration_has_extension_and_compression",
settings: HTTPClientSettings{
Endpoint: "localhost:1234",
Auth: &configauth.Authentication{AuthenticatorID: component.NewID("mock")},
Compression: configcompression.Gzip,
},
shouldErr: false,
host: &mockHost{
ext: map[component.ID]component.Component{
component.NewID("mock"): &authtest.MockClient{ResultRoundTripper: &customRoundTripper{}},
},
},
},
{
name: "with_auth_configuration_has_err_extension",
settings: HTTPClientSettings{
Expand All @@ -336,15 +366,34 @@ func TestHTTPClientSettingWithAuthConfig(t *testing.T) {
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
client, err := test.settings.ToClient(test.host, componenttest.NewNopTelemetrySettings())
// Omit TracerProvider and MeterProvider in TelemetrySettings as otelhttp.Transport cannot be introspected
client, err := test.settings.ToClient(test.host, component.TelemetrySettings{Logger: zap.NewNop(), MetricsLevel: configtelemetry.LevelNone})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use componenttest to create the TelemetrySettings?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As noted in the comment the otelhttp.Transport doesn't export the RoundTripper that it wraps and so there would be no way to test the containment order beyond that transport if the componenttest.NewNopTelemetrySettings() were used.

if test.shouldErr {
assert.Error(t, err)
return
}
assert.NoError(t, err)
assert.NotNil(t, client)
transport := client.Transport

// Compression should wrap Auth, unwrap it
if configcompression.IsCompressed(test.settings.Compression) {
ct, ok := transport.(*compressRoundTripper)
assert.True(t, ok)
assert.Equal(t, test.settings.Compression, ct.compressionType)
transport = ct.RoundTripper
}

// Headers should wrap Auth, unwrap it
if test.settings.Headers != nil {
ht, ok := transport.(*headerRoundTripper)
assert.True(t, ok)
assert.Equal(t, test.settings.Headers, ht.headers)
transport = ht.transport
}

if test.settings.Auth != nil {
_, ok := client.Transport.(*customRoundTripper)
_, ok := transport.(*customRoundTripper)
assert.True(t, ok)
}
})
Expand Down