Skip to content

Commit 1bca4b4

Browse files
committed
config: add support for certificate configuration
Fixes open-telemetry#6351 Signed-off-by: Alex Boten <[email protected]>
1 parent 04815fd commit 1bca4b4

File tree

8 files changed

+153
-0
lines changed

8 files changed

+153
-0
lines changed

config/log.go

+8
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"go.opentelemetry.io/otel/log/noop"
1818
sdklog "go.opentelemetry.io/otel/sdk/log"
1919
"go.opentelemetry.io/otel/sdk/resource"
20+
"google.golang.org/grpc/credentials"
2021
)
2122

2223
func loggerProvider(cfg configOptions, res *resource.Resource) (log.LoggerProvider, shutdownFunc, error) {
@@ -178,6 +179,13 @@ func otlpGRPCLogExporter(ctx context.Context, otlpConfig *OTLP) (sdklog.Exporter
178179
if u.Scheme == "http" {
179180
opts = append(opts, otlploggrpc.WithInsecure())
180181
}
182+
if otlpConfig.Certificate != nil {
183+
creds, err := credentials.NewClientTLSFromFile(*otlpConfig.Certificate, "")
184+
if err != nil {
185+
return nil, fmt.Errorf("could not create client tls credentials: %w", err)
186+
}
187+
opts = append(opts, otlploggrpc.WithTLSCredentials(creds))
188+
}
181189
}
182190
if otlpConfig.Compression != nil {
183191
switch *otlpConfig.Compression {

config/log_test.go

+36
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ package config // import "go.opentelemetry.io/contrib/config"
66
import (
77
"context"
88
"errors"
9+
"fmt"
910
"net/url"
11+
"path/filepath"
1012
"reflect"
1113
"testing"
1214

@@ -221,6 +223,40 @@ func TestLogProcessor(t *testing.T) {
221223
},
222224
wantProcessor: sdklog.NewBatchProcessor(otlpGRPCExporter),
223225
},
226+
{
227+
name: "batch/otlp-grpc-good-ca-certificate",
228+
processor: LogRecordProcessor{
229+
Batch: &BatchLogRecordProcessor{
230+
Exporter: LogRecordExporter{
231+
OTLP: &OTLP{
232+
Protocol: ptr("grpc"),
233+
Endpoint: ptr("localhost:4317"),
234+
Compression: ptr("gzip"),
235+
Timeout: ptr(1000),
236+
Certificate: ptr(filepath.Join("testdata", "ca.crt")),
237+
},
238+
},
239+
},
240+
},
241+
wantProcessor: sdklog.NewBatchProcessor(otlpGRPCExporter),
242+
},
243+
{
244+
name: "batch/otlp-grpc-bad-ca-certificate",
245+
processor: LogRecordProcessor{
246+
Batch: &BatchLogRecordProcessor{
247+
Exporter: LogRecordExporter{
248+
OTLP: &OTLP{
249+
Protocol: ptr("grpc"),
250+
Endpoint: ptr("localhost:4317"),
251+
Compression: ptr("gzip"),
252+
Timeout: ptr(1000),
253+
Certificate: ptr(filepath.Join("testdata", "bad_cert.crt")),
254+
},
255+
},
256+
},
257+
},
258+
wantErr: fmt.Errorf("could not create client tls credentials: %w", errors.New("credentials: failed to append certificates")),
259+
},
224260
{
225261
name: "batch/otlp-grpc-exporter-no-scheme",
226262
processor: LogRecordProcessor{

config/metric.go

+8
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717

1818
"github.com/prometheus/client_golang/prometheus"
1919
"github.com/prometheus/client_golang/prometheus/promhttp"
20+
"google.golang.org/grpc/credentials"
2021

2122
"go.opentelemetry.io/otel"
2223
"go.opentelemetry.io/otel/attribute"
@@ -205,6 +206,13 @@ func otlpGRPCMetricExporter(ctx context.Context, otlpConfig *OTLPMetric) (sdkmet
205206
if u.Scheme == "http" {
206207
opts = append(opts, otlpmetricgrpc.WithInsecure())
207208
}
209+
if otlpConfig.Certificate != nil {
210+
creds, err := credentials.NewClientTLSFromFile(*otlpConfig.Certificate, "")
211+
if err != nil {
212+
return nil, fmt.Errorf("could not create client tls credentials: %w", err)
213+
}
214+
opts = append(opts, otlpmetricgrpc.WithTLSCredentials(creds))
215+
}
208216
}
209217

210218
if otlpConfig.Compression != nil {

config/metric_test.go

+35
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"errors"
99
"fmt"
1010
"net/url"
11+
"path/filepath"
1112
"reflect"
1213
"testing"
1314
"time"
@@ -215,6 +216,40 @@ func TestReader(t *testing.T) {
215216
},
216217
wantReader: sdkmetric.NewPeriodicReader(otlpGRPCExporter),
217218
},
219+
{
220+
name: "periodic/otlp-grpc-good-ca-certificate",
221+
reader: MetricReader{
222+
Periodic: &PeriodicMetricReader{
223+
Exporter: PushMetricExporter{
224+
OTLP: &OTLPMetric{
225+
Protocol: ptr("grpc"),
226+
Endpoint: ptr("https://localhost:4317"),
227+
Compression: ptr("gzip"),
228+
Timeout: ptr(1000),
229+
Certificate: ptr(filepath.Join("testdata", "ca.crt")),
230+
},
231+
},
232+
},
233+
},
234+
wantReader: sdkmetric.NewPeriodicReader(otlpGRPCExporter),
235+
},
236+
{
237+
name: "periodic/otlp-grpc-bad-ca-certificate",
238+
reader: MetricReader{
239+
Periodic: &PeriodicMetricReader{
240+
Exporter: PushMetricExporter{
241+
OTLP: &OTLPMetric{
242+
Protocol: ptr("grpc"),
243+
Endpoint: ptr("https://localhost:4317"),
244+
Compression: ptr("gzip"),
245+
Timeout: ptr(1000),
246+
Certificate: ptr(filepath.Join("testdata", "bad_cert.crt")),
247+
},
248+
},
249+
},
250+
},
251+
wantErr: fmt.Errorf("could not create client tls credentials: %w", errors.New("credentials: failed to append certificates")),
252+
},
218253
{
219254
name: "periodic/otlp-grpc-exporter-no-endpoint",
220255
reader: MetricReader{

config/testdata/bad_cert.crt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is intentionally not a PEM formatted cert file.

config/testdata/ca.crt

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
-----BEGIN CERTIFICATE-----
2+
MIIDNjCCAh4CCQC0I5IQT7eziDANBgkqhkiG9w0BAQsFADBdMQswCQYDVQQGEwJB
3+
VTESMBAGA1UECAwJQXVzdHJhbGlhMQ8wDQYDVQQHDAZTeWRuZXkxEjAQBgNVBAoM
4+
CU15T3JnTmFtZTEVMBMGA1UEAwwMTXlDb21tb25OYW1lMB4XDTIyMDgwMzA0MTky
5+
MVoXDTMyMDczMTA0MTkyMVowXTELMAkGA1UEBhMCQVUxEjAQBgNVBAgMCUF1c3Ry
6+
YWxpYTEPMA0GA1UEBwwGU3lkbmV5MRIwEAYDVQQKDAlNeU9yZ05hbWUxFTATBgNV
7+
BAMMDE15Q29tbW9uTmFtZTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB
8+
AMhGP0dy3zvkdx9zI+/XVjPOWlER0OUp7Sgzidc3nLOk42+bH4ofIVNtOFVqlNKi
9+
O1bImu238VdBhd6R5IZZ1ZdIMcCeDgSJYu2X9wA3m4PKz8IdXo5ly2OHghhmCvqG
10+
WxgqDj5wPXiczQwuf1EcDMtRWbXJ6Z/XH1U68R/kRdNLkiZ2LwtjoQpis5XYckLL
11+
CrdF+AL6GeDIe0Mh9QGs26Vux+2kvaOGNUWRPE6Wt4GkqyKqmzYfR9HbflJ4xHT2
12+
I+jE1lg+jMBeom7z8Z90RE4GGcHjO+Vens/88r5EAjTnFj1Kb5gL2deSHY1m/++R
13+
Z/kRyg+zQJyw4fAzlAA4+VkCAwEAATANBgkqhkiG9w0BAQsFAAOCAQEAM3gRdTKX
14+
eGwGYVmmKqA2vTxeigQYLHml7OSopcWj2wJfxfp49HXPRuvgpQn9iubxO3Zmhd83
15+
2X1E+T0A8oy5CfxgpAhHb3lY0jm3TjKXm6m+dSODwL3uND8tX+SqR8sRTFxPvPuo
16+
pmvhdTZoRI3EzIiHLTgCuSU25JNP/vrVoKk0JvCkDYTU/WcVfj0v95DTMoWR4JGz
17+
mtBwrgD0EM2XRw5ZMc7sMPli1gqmCbCQUrDZ+rPB78WDCBILBd8Cz75qYTUp98BY
18+
akJyBckdJHAdyEQYDKa9HpmpexOO7IhSXCTEN1DEBgpZgEi/lBDRG/b0OzenUUgt
19+
LUABtWt3pNQ9HA==
20+
-----END CERTIFICATE-----

config/trace.go

+9
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
sdktrace "go.opentelemetry.io/otel/sdk/trace"
1818
"go.opentelemetry.io/otel/trace"
1919
"go.opentelemetry.io/otel/trace/noop"
20+
"google.golang.org/grpc/credentials"
2021
)
2122

2223
func tracerProvider(cfg configOptions, res *resource.Resource) (trace.TracerProvider, shutdownFunc, error) {
@@ -108,6 +109,14 @@ func otlpGRPCSpanExporter(ctx context.Context, otlpConfig *OTLP) (sdktrace.SpanE
108109
if u.Scheme == "http" {
109110
opts = append(opts, otlptracegrpc.WithInsecure())
110111
}
112+
113+
if otlpConfig.Certificate != nil {
114+
creds, err := credentials.NewClientTLSFromFile(*otlpConfig.Certificate, "")
115+
if err != nil {
116+
return nil, fmt.Errorf("could not create client tls credentials: %w", err)
117+
}
118+
opts = append(opts, otlptracegrpc.WithTLSCredentials(creds))
119+
}
111120
}
112121

113122
if otlpConfig.Compression != nil {

config/trace_test.go

+36
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ package config
66
import (
77
"context"
88
"errors"
9+
"fmt"
910
"net/url"
11+
"path/filepath"
1012
"reflect"
1113
"testing"
1214

@@ -261,6 +263,40 @@ func TestSpanProcessor(t *testing.T) {
261263
},
262264
wantProcessor: sdktrace.NewBatchSpanProcessor(otlpGRPCExporter),
263265
},
266+
{
267+
name: "batch/otlp-grpc-good-ca-certificate",
268+
processor: SpanProcessor{
269+
Batch: &BatchSpanProcessor{
270+
Exporter: SpanExporter{
271+
OTLP: &OTLP{
272+
Protocol: ptr("grpc"),
273+
Endpoint: ptr("localhost:4317"),
274+
Compression: ptr("gzip"),
275+
Timeout: ptr(1000),
276+
Certificate: ptr(filepath.Join("testdata", "ca.crt")),
277+
},
278+
},
279+
},
280+
},
281+
wantProcessor: sdktrace.NewBatchProcessor(otlpGRPCExporter),
282+
},
283+
{
284+
name: "batch/otlp-grpc-bad-ca-certificate",
285+
processor: SpanProcessor{
286+
Batch: &BatchSpanProcessor{
287+
Exporter: SpanExporter{
288+
OTLP: &OTLP{
289+
Protocol: ptr("grpc"),
290+
Endpoint: ptr("localhost:4317"),
291+
Compression: ptr("gzip"),
292+
Timeout: ptr(1000),
293+
Certificate: ptr(filepath.Join("testdata", "bad_cert.crt")),
294+
},
295+
},
296+
},
297+
},
298+
wantErr: fmt.Errorf("could not create client tls credentials: %w", errors.New("credentials: failed to append certificates")),
299+
},
264300
{
265301
name: "batch/otlp-grpc-exporter-no-scheme",
266302
processor: SpanProcessor{

0 commit comments

Comments
 (0)