Skip to content

Commit 99b9e2a

Browse files
codebotenMrAlias
andauthored
otelconf: refactor code to internal package (#8050)
Signed-off-by: alex boten <[email protected]> Co-authored-by: Tyler Yahn <[email protected]>
1 parent e0f043f commit 99b9e2a

File tree

7 files changed

+124
-93
lines changed

7 files changed

+124
-93
lines changed

otelconf/internal/tls/config.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
// Package tls provides functionality to translate configuration options into tls.Config.
5+
package tls // import "go.opentelemetry.io/contrib/otelconf/internal/tls"
6+
7+
import (
8+
"crypto/tls"
9+
"crypto/x509"
10+
"errors"
11+
"fmt"
12+
"os"
13+
)
14+
15+
// CreateConfig creates a tls.Config from certificate files.
16+
func CreateConfig(caCertFile, clientCertFile, clientKeyFile *string) (*tls.Config, error) {
17+
tlsConfig := &tls.Config{}
18+
if caCertFile != nil {
19+
caText, err := os.ReadFile(*caCertFile)
20+
if err != nil {
21+
return nil, err
22+
}
23+
certPool := x509.NewCertPool()
24+
if !certPool.AppendCertsFromPEM(caText) {
25+
return nil, errors.New("could not create certificate authority chain from certificate")
26+
}
27+
tlsConfig.RootCAs = certPool
28+
}
29+
if clientCertFile != nil {
30+
if clientKeyFile == nil {
31+
return nil, errors.New("client certificate was provided but no client key was provided")
32+
}
33+
clientCert, err := tls.LoadX509KeyPair(*clientCertFile, *clientKeyFile)
34+
if err != nil {
35+
return nil, fmt.Errorf("could not use client certificate: %w", err)
36+
}
37+
tlsConfig.Certificates = []tls.Certificate{clientCert}
38+
}
39+
return tlsConfig, nil
40+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package tls
5+
6+
import (
7+
"crypto/tls"
8+
"path/filepath"
9+
"testing"
10+
11+
"github.com/stretchr/testify/require"
12+
)
13+
14+
func TestCreateConfig(t *testing.T) {
15+
tests := []struct {
16+
name string
17+
caCertFile *string
18+
clientCertFile *string
19+
clientKeyFile *string
20+
wantErrContains string
21+
want func(*tls.Config, *testing.T)
22+
}{
23+
{
24+
name: "no-input",
25+
want: func(result *tls.Config, t *testing.T) {
26+
require.Nil(t, result.Certificates)
27+
require.Nil(t, result.RootCAs)
28+
},
29+
},
30+
{
31+
name: "only-cacert-provided",
32+
caCertFile: ptr(filepath.Join("..", "..", "testdata", "ca.crt")),
33+
want: func(result *tls.Config, t *testing.T) {
34+
require.Nil(t, result.Certificates)
35+
require.NotNil(t, result.RootCAs)
36+
},
37+
},
38+
{
39+
name: "nonexistent-cacert-file",
40+
caCertFile: ptr("nowhere.crt"),
41+
wantErrContains: "open nowhere.crt:",
42+
},
43+
{
44+
name: "nonexistent-clientcert-file",
45+
clientCertFile: ptr("nowhere.crt"),
46+
clientKeyFile: ptr("nowhere.crt"),
47+
wantErrContains: "could not use client certificate: open nowhere.crt:",
48+
},
49+
{
50+
name: "bad-cacert-file",
51+
caCertFile: ptr(filepath.Join("..", "..", "testdata", "bad_cert.crt")),
52+
wantErrContains: "could not create certificate authority chain from certificate",
53+
},
54+
}
55+
56+
for _, tt := range tests {
57+
t.Run(tt.name, func(t *testing.T) {
58+
got, err := CreateConfig(tt.caCertFile, tt.clientCertFile, tt.clientKeyFile)
59+
60+
if tt.wantErrContains != "" {
61+
require.Contains(t, err.Error(), tt.wantErrContains)
62+
} else {
63+
require.NoError(t, err)
64+
tt.want(got, t)
65+
}
66+
})
67+
}
68+
}
69+
70+
func ptr[T any](v T) *T {
71+
return &v
72+
}

otelconf/v0.3.0/config.go

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,8 @@ package otelconf // import "go.opentelemetry.io/contrib/otelconf/v0.3.0"
66

77
import (
88
"context"
9-
"crypto/tls"
10-
"crypto/x509"
119
"errors"
1210
"fmt"
13-
"os"
1411

1512
"go.opentelemetry.io/otel/baggage"
1613
"go.opentelemetry.io/otel/log"
@@ -195,33 +192,6 @@ func ParseYAML(file []byte) (*OpenTelemetryConfiguration, error) {
195192
return &cfg, nil
196193
}
197194

198-
// createTLSConfig creates a tls.Config from certificate files.
199-
func createTLSConfig(caCertFile, clientCertFile, clientKeyFile *string) (*tls.Config, error) {
200-
tlsConfig := &tls.Config{}
201-
if caCertFile != nil {
202-
caText, err := os.ReadFile(*caCertFile)
203-
if err != nil {
204-
return nil, err
205-
}
206-
certPool := x509.NewCertPool()
207-
if !certPool.AppendCertsFromPEM(caText) {
208-
return nil, errors.New("could not create certificate authority chain from certificate")
209-
}
210-
tlsConfig.RootCAs = certPool
211-
}
212-
if clientCertFile != nil {
213-
if clientKeyFile == nil {
214-
return nil, errors.New("client certificate was provided but no client key was provided")
215-
}
216-
clientCert, err := tls.LoadX509KeyPair(*clientCertFile, *clientKeyFile)
217-
if err != nil {
218-
return nil, fmt.Errorf("could not use client certificate: %w", err)
219-
}
220-
tlsConfig.Certificates = []tls.Certificate{clientCert}
221-
}
222-
return tlsConfig, nil
223-
}
224-
225195
// createHeadersConfig combines the two header config fields. Headers take precedence over headersList.
226196
func createHeadersConfig(headers []NameStringValuePair, headersList *string) (map[string]string, error) {
227197
result := make(map[string]string)

otelconf/v0.3.0/config_test.go

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package otelconf
55

66
import (
7-
"crypto/tls"
87
"encoding/json"
98
"errors"
109
"os"
@@ -610,62 +609,6 @@ func TestSerializeJSON(t *testing.T) {
610609
}
611610
}
612611

613-
func TestCreateTLSConfig(t *testing.T) {
614-
tests := []struct {
615-
name string
616-
caCertFile *string
617-
clientCertFile *string
618-
clientKeyFile *string
619-
wantErrContains string
620-
want func(*tls.Config, *testing.T)
621-
}{
622-
{
623-
name: "no-input",
624-
want: func(result *tls.Config, t *testing.T) {
625-
require.Nil(t, result.Certificates)
626-
require.Nil(t, result.RootCAs)
627-
},
628-
},
629-
{
630-
name: "only-cacert-provided",
631-
caCertFile: ptr(filepath.Join("..", "testdata", "ca.crt")),
632-
want: func(result *tls.Config, t *testing.T) {
633-
require.Nil(t, result.Certificates)
634-
require.NotNil(t, result.RootCAs)
635-
},
636-
},
637-
{
638-
name: "nonexistent-cacert-file",
639-
caCertFile: ptr("nowhere.crt"),
640-
wantErrContains: "open nowhere.crt:",
641-
},
642-
{
643-
name: "nonexistent-clientcert-file",
644-
clientCertFile: ptr("nowhere.crt"),
645-
clientKeyFile: ptr("nowhere.crt"),
646-
wantErrContains: "could not use client certificate: open nowhere.crt:",
647-
},
648-
{
649-
name: "bad-cacert-file",
650-
caCertFile: ptr(filepath.Join("..", "testdata", "bad_cert.crt")),
651-
wantErrContains: "could not create certificate authority chain from certificate",
652-
},
653-
}
654-
655-
for _, tt := range tests {
656-
t.Run(tt.name, func(t *testing.T) {
657-
got, err := createTLSConfig(tt.caCertFile, tt.clientCertFile, tt.clientKeyFile)
658-
659-
if tt.wantErrContains != "" {
660-
require.Contains(t, err.Error(), tt.wantErrContains)
661-
} else {
662-
require.NoError(t, err)
663-
tt.want(got, t)
664-
}
665-
})
666-
}
667-
}
668-
669612
func TestCreateHeadersConfig(t *testing.T) {
670613
tests := []struct {
671614
name string

otelconf/v0.3.0/log.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import (
1818
sdklog "go.opentelemetry.io/otel/sdk/log"
1919
"go.opentelemetry.io/otel/sdk/resource"
2020
"google.golang.org/grpc/credentials"
21+
22+
"go.opentelemetry.io/contrib/otelconf/internal/tls"
2123
)
2224

2325
func loggerProvider(cfg configOptions, res *resource.Resource) (log.LoggerProvider, shutdownFunc, error) {
@@ -158,7 +160,7 @@ func otlpHTTPLogExporter(ctx context.Context, otlpConfig *OTLP) (sdklog.Exporter
158160
opts = append(opts, otlploghttp.WithHeaders(headersConfig))
159161
}
160162

161-
tlsConfig, err := createTLSConfig(otlpConfig.Certificate, otlpConfig.ClientCertificate, otlpConfig.ClientKey)
163+
tlsConfig, err := tls.CreateConfig(otlpConfig.Certificate, otlpConfig.ClientCertificate, otlpConfig.ClientKey)
162164
if err != nil {
163165
return nil, err
164166
}
@@ -211,7 +213,7 @@ func otlpGRPCLogExporter(ctx context.Context, otlpConfig *OTLP) (sdklog.Exporter
211213
}
212214

213215
if otlpConfig.Certificate != nil || otlpConfig.ClientCertificate != nil || otlpConfig.ClientKey != nil {
214-
tlsConfig, err := createTLSConfig(otlpConfig.Certificate, otlpConfig.ClientCertificate, otlpConfig.ClientKey)
216+
tlsConfig, err := tls.CreateConfig(otlpConfig.Certificate, otlpConfig.ClientCertificate, otlpConfig.ClientKey)
215217
if err != nil {
216218
return nil, err
217219
}

otelconf/v0.3.0/metric.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ import (
3131
"go.opentelemetry.io/otel/sdk/metric/metricdata"
3232
"go.opentelemetry.io/otel/sdk/resource"
3333
"google.golang.org/grpc/credentials"
34+
35+
"go.opentelemetry.io/contrib/otelconf/internal/tls"
3436
)
3537

3638
var zeroScope instrumentation.Scope
@@ -184,7 +186,7 @@ func otlpHTTPMetricExporter(ctx context.Context, otlpConfig *OTLPMetric) (sdkmet
184186
}
185187
}
186188

187-
tlsConfig, err := createTLSConfig(otlpConfig.Certificate, otlpConfig.ClientCertificate, otlpConfig.ClientKey)
189+
tlsConfig, err := tls.CreateConfig(otlpConfig.Certificate, otlpConfig.ClientCertificate, otlpConfig.ClientKey)
188190
if err != nil {
189191
return nil, err
190192
}
@@ -250,7 +252,7 @@ func otlpGRPCMetricExporter(ctx context.Context, otlpConfig *OTLPMetric) (sdkmet
250252
}
251253

252254
if otlpConfig.Certificate != nil || otlpConfig.ClientCertificate != nil || otlpConfig.ClientKey != nil {
253-
tlsConfig, err := createTLSConfig(otlpConfig.Certificate, otlpConfig.ClientCertificate, otlpConfig.ClientKey)
255+
tlsConfig, err := tls.CreateConfig(otlpConfig.Certificate, otlpConfig.ClientCertificate, otlpConfig.ClientKey)
254256
if err != nil {
255257
return nil, err
256258
}

otelconf/v0.3.0/trace.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import (
1818
"go.opentelemetry.io/otel/trace"
1919
"go.opentelemetry.io/otel/trace/noop"
2020
"google.golang.org/grpc/credentials"
21+
22+
"go.opentelemetry.io/contrib/otelconf/internal/tls"
2123
)
2224

2325
var errInvalidSamplerConfiguration = errors.New("invalid sampler configuration")
@@ -214,7 +216,7 @@ func otlpGRPCSpanExporter(ctx context.Context, otlpConfig *OTLP) (sdktrace.SpanE
214216
}
215217

216218
if otlpConfig.Certificate != nil || otlpConfig.ClientCertificate != nil || otlpConfig.ClientKey != nil {
217-
tlsConfig, err := createTLSConfig(otlpConfig.Certificate, otlpConfig.ClientCertificate, otlpConfig.ClientKey)
219+
tlsConfig, err := tls.CreateConfig(otlpConfig.Certificate, otlpConfig.ClientCertificate, otlpConfig.ClientKey)
218220
if err != nil {
219221
return nil, err
220222
}
@@ -262,7 +264,7 @@ func otlpHTTPSpanExporter(ctx context.Context, otlpConfig *OTLP) (sdktrace.SpanE
262264
opts = append(opts, otlptracehttp.WithHeaders(headersConfig))
263265
}
264266

265-
tlsConfig, err := createTLSConfig(otlpConfig.Certificate, otlpConfig.ClientCertificate, otlpConfig.ClientKey)
267+
tlsConfig, err := tls.CreateConfig(otlpConfig.Certificate, otlpConfig.ClientCertificate, otlpConfig.ClientKey)
266268
if err != nil {
267269
return nil, err
268270
}

0 commit comments

Comments
 (0)