forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
telemetrygen: wire up InsecureSkipVerify (open-telemetry#35735)
#### Description Thread the InsecureSkipVerify config through properly, so the relevant `tls.Config` struct field is set. Previously, The `--otlp-insecure-skip-verify` flag wasn't effective. I feel like we'd be better off using https://github.com/open-telemetry/opentelemetry-go-contrib/tree/main/exporters/autoexport and configuring these things with environment variables, instead of defining flags. I will open an issue for that. #### Link to tracking issue None #### Testing I have added a test that shows the HTTP exporter honours the Insecure, InsecureSkipVerify, and CaFile config fields. I have not done the same for gRPC due to lack of time - this can be done as a followup. OTOH the logic for HTTP & gRPC exporter options shares more now, so the HTTP path tests a fair bit of what gRPC will do anyway.
- Loading branch information
1 parent
344422a
commit b2f34dc
Showing
7 changed files
with
204 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Use this changelog template to create an entry for release notes. | ||
|
||
# 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. filelogreceiver) | ||
component: telemetrygen | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: Enable the `--otlp-insecure-skip-verify` flag | ||
|
||
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
issues: [35735] | ||
|
||
# (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: | ||
|
||
# If your change doesn't affect end users or the exported elements of any package, | ||
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. | ||
# 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: [user] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package traces | ||
|
||
import ( | ||
"context" | ||
"encoding/pem" | ||
"net/http" | ||
"net/http/httptest" | ||
"net/url" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp" | ||
tracepb "go.opentelemetry.io/proto/otlp/trace/v1" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/cmd/telemetrygen/internal/common" | ||
) | ||
|
||
func TestHTTPExporterOptions_TLS(t *testing.T) { | ||
// TODO add test cases for mTLS | ||
for name, tc := range map[string]struct { | ||
tls bool | ||
tlsServerCA bool // use the httptest.Server's TLS cert as the CA | ||
cfg Config | ||
|
||
expectTransportError bool | ||
}{ | ||
"Insecure": { | ||
tls: false, | ||
cfg: Config{Config: common.Config{Insecure: true}}, | ||
}, | ||
"InsecureSkipVerify": { | ||
tls: true, | ||
cfg: Config{Config: common.Config{InsecureSkipVerify: true}}, | ||
}, | ||
"InsecureSkipVerifyDisabled": { | ||
tls: true, | ||
expectTransportError: true, | ||
}, | ||
"CaFile": { | ||
tls: true, | ||
tlsServerCA: true, | ||
}, | ||
} { | ||
t.Run(name, func(t *testing.T) { | ||
var called bool | ||
var h http.HandlerFunc = func(http.ResponseWriter, *http.Request) { | ||
called = true | ||
} | ||
var srv *httptest.Server | ||
if tc.tls { | ||
srv = httptest.NewTLSServer(h) | ||
} else { | ||
srv = httptest.NewServer(h) | ||
} | ||
defer srv.Close() | ||
srvURL, _ := url.Parse(srv.URL) | ||
|
||
cfg := tc.cfg | ||
cfg.CustomEndpoint = srvURL.Host | ||
if tc.tlsServerCA { | ||
caFile := filepath.Join(t.TempDir(), "cert.pem") | ||
err := os.WriteFile(caFile, pem.EncodeToMemory(&pem.Block{ | ||
Type: "CERTIFICATE", | ||
Bytes: srv.TLS.Certificates[0].Certificate[0], | ||
}), 0600) | ||
require.NoError(t, err) | ||
cfg.CaFile = caFile | ||
} | ||
|
||
opts, err := httpExporterOptions(&cfg) | ||
require.NoError(t, err) | ||
client := otlptracehttp.NewClient(opts...) | ||
|
||
err = client.UploadTraces(context.Background(), []*tracepb.ResourceSpans{}) | ||
if tc.expectTransportError { | ||
require.Error(t, err) | ||
assert.False(t, called) | ||
} else { | ||
require.NoError(t, err) | ||
assert.True(t, called) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestHTTPExporterOptions_HTTP(t *testing.T) { | ||
for name, tc := range map[string]struct { | ||
cfg Config | ||
|
||
expectedHTTPPath string | ||
expectedHeader http.Header | ||
}{ | ||
"HTTPPath": { | ||
cfg: Config{Config: common.Config{HTTPPath: "/foo"}}, | ||
expectedHTTPPath: "/foo", | ||
}, | ||
"Headers": { | ||
cfg: Config{ | ||
Config: common.Config{Headers: map[string]any{"a": "b"}}, | ||
}, | ||
expectedHTTPPath: "/v1/traces", | ||
expectedHeader: http.Header{"a": []string{"b"}}, | ||
}, | ||
} { | ||
t.Run(name, func(t *testing.T) { | ||
var httpPath string | ||
var header http.Header | ||
var h http.HandlerFunc = func(_ http.ResponseWriter, r *http.Request) { | ||
httpPath = r.URL.Path | ||
header = r.Header | ||
} | ||
srv := httptest.NewServer(h) | ||
defer srv.Close() | ||
srvURL, _ := url.Parse(srv.URL) | ||
|
||
cfg := tc.cfg | ||
cfg.Insecure = true | ||
cfg.CustomEndpoint = srvURL.Host | ||
opts, err := httpExporterOptions(&cfg) | ||
require.NoError(t, err) | ||
client := otlptracehttp.NewClient(opts...) | ||
|
||
err = client.UploadTraces(context.Background(), []*tracepb.ResourceSpans{}) | ||
require.NoError(t, err) | ||
assert.Equal(t, tc.expectedHTTPPath, httpPath) | ||
for k, expected := range tc.expectedHeader { | ||
assert.Equal(t, expected, []string{header.Get(k)}) | ||
} | ||
}) | ||
} | ||
} |