Skip to content

Commit ebb0fbd

Browse files
authored
Make TLSClient config to pointer type (open-telemetry#4104)
Change TLSClient config to pointer type in `confighttp` and `configgrpc` config **Related Issue** open-telemetry#4028 open-telemetry#4063 (comment)
1 parent 71eaf3a commit ebb0fbd

File tree

9 files changed

+57
-46
lines changed

9 files changed

+57
-46
lines changed

config/configgrpc/configgrpc.go

+11-7
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ type GRPCClientSettings struct {
7373
Compression string `mapstructure:"compression"`
7474

7575
// TLSSetting struct exposes TLS client configuration.
76-
TLSSetting configtls.TLSClientSetting `mapstructure:"tls,omitempty"`
76+
TLSSetting *configtls.TLSClientSetting `mapstructure:"tls,omitempty"`
7777

7878
// The keepalive parameters for gRPC client. See grpc.WithKeepaliveParams.
7979
// (https://godoc.org/google.golang.org/grpc#WithKeepaliveParams).
@@ -181,24 +181,28 @@ func (gcs *GRPCClientSettings) isSchemeHTTPS() bool {
181181
// ToDialOptions maps configgrpc.GRPCClientSettings to a slice of dial options for gRPC.
182182
func (gcs *GRPCClientSettings) ToDialOptions(host component.Host) ([]grpc.DialOption, error) {
183183
var opts []grpc.DialOption
184+
var tlsCfg *tls.Config
185+
var err error
184186
if gcs.Compression != "" {
185187
if compressionKey := GetGRPCCompressionKey(gcs.Compression); compressionKey != CompressionUnsupported {
186188
opts = append(opts, grpc.WithDefaultCallOptions(grpc.UseCompressor(compressionKey)))
187189
} else {
188190
return nil, fmt.Errorf("unsupported compression type %q", gcs.Compression)
189191
}
190192
}
191-
192-
tlsCfg, err := gcs.TLSSetting.LoadTLSConfig()
193-
if err != nil {
194-
return nil, err
195-
}
196193
tlsDialOption := grpc.WithInsecure()
194+
if gcs.TLSSetting != nil {
195+
tlsCfg, err = gcs.TLSSetting.LoadTLSConfig()
196+
if err != nil {
197+
return nil, err
198+
}
199+
}
197200
if tlsCfg != nil {
198201
tlsDialOption = grpc.WithTransportCredentials(credentials.NewTLS(tlsCfg))
199202
} else if gcs.isSchemeHTTPS() {
200203
tlsDialOption = grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{}))
201204
}
205+
202206
opts = append(opts, tlsDialOption)
203207

204208
if gcs.ReadBufferSize > 0 {
@@ -235,7 +239,7 @@ func (gcs *GRPCClientSettings) ToDialOptions(host component.Host) ([]grpc.DialOp
235239

236240
perRPCCredentials, perr := grpcAuthenticator.PerRPCCredentials()
237241
if perr != nil {
238-
return nil, err
242+
return nil, perr
239243
}
240244
opts = append(opts, grpc.WithPerRPCCredentials(perRPCCredentials))
241245
}

config/configgrpc/configgrpc_test.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import (
3838

3939
func TestDefaultGrpcClientSettings(t *testing.T) {
4040
gcs := &GRPCClientSettings{
41-
TLSSetting: configtls.TLSClientSetting{
41+
TLSSetting: &configtls.TLSClientSetting{
4242
Insecure: true,
4343
},
4444
}
@@ -54,7 +54,7 @@ func TestAllGrpcClientSettings(t *testing.T) {
5454
},
5555
Endpoint: "localhost:1234",
5656
Compression: "gzip",
57-
TLSSetting: configtls.TLSClientSetting{
57+
TLSSetting: &configtls.TLSClientSetting{
5858
Insecure: false,
5959
},
6060
Keepalive: &KeepaliveClientConfig{
@@ -160,7 +160,7 @@ func TestGRPCClientSettingsError(t *testing.T) {
160160
Headers: nil,
161161
Endpoint: "",
162162
Compression: "",
163-
TLSSetting: configtls.TLSClientSetting{
163+
TLSSetting: &configtls.TLSClientSetting{
164164
TLSSetting: configtls.TLSSetting{
165165
CAFile: "/doesnt/exist",
166166
},
@@ -176,7 +176,7 @@ func TestGRPCClientSettingsError(t *testing.T) {
176176
Headers: nil,
177177
Endpoint: "",
178178
Compression: "",
179-
TLSSetting: configtls.TLSClientSetting{
179+
TLSSetting: &configtls.TLSClientSetting{
180180
TLSSetting: configtls.TLSSetting{
181181
CertFile: "/doesnt/exist",
182182
},
@@ -194,7 +194,7 @@ func TestGRPCClientSettingsError(t *testing.T) {
194194
},
195195
Endpoint: "localhost:1234",
196196
Compression: "gzip",
197-
TLSSetting: configtls.TLSClientSetting{
197+
TLSSetting: &configtls.TLSClientSetting{
198198
Insecure: false,
199199
},
200200
Keepalive: &KeepaliveClientConfig{
@@ -250,7 +250,7 @@ func TestUseSecure(t *testing.T) {
250250
Headers: nil,
251251
Endpoint: "",
252252
Compression: "",
253-
TLSSetting: configtls.TLSClientSetting{},
253+
TLSSetting: &configtls.TLSClientSetting{},
254254
Keepalive: nil,
255255
}
256256
dialOpts, err := gcs.ToDialOptions(componenttest.NewNopHost())
@@ -472,7 +472,7 @@ func TestHttpReception(t *testing.T) {
472472

473473
gcs := &GRPCClientSettings{
474474
Endpoint: ln.Addr().String(),
475-
TLSSetting: *tt.tlsClientCreds,
475+
TLSSetting: tt.tlsClientCreds,
476476
}
477477
clientOpts, errClient := gcs.ToDialOptions(componenttest.NewNopHost())
478478
assert.NoError(t, errClient)
@@ -517,7 +517,7 @@ func TestReceiveOnUnixDomainSocket(t *testing.T) {
517517

518518
gcs := &GRPCClientSettings{
519519
Endpoint: "unix://" + ln.Addr().String(),
520-
TLSSetting: configtls.TLSClientSetting{
520+
TLSSetting: &configtls.TLSClientSetting{
521521
Insecure: true,
522522
},
523523
}

config/confighttp/confighttp.go

+12-7
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ type HTTPClientSettings struct {
3838
Endpoint string `mapstructure:"endpoint"`
3939

4040
// TLSSetting struct exposes TLS client configuration.
41-
TLSSetting configtls.TLSClientSetting `mapstructure:"tls,omitempty"`
41+
TLSSetting *configtls.TLSClientSetting `mapstructure:"tls,omitempty"`
4242

4343
// ReadBufferSize for HTTP client. See http.Transport.ReadBufferSize.
4444
ReadBufferSize int `mapstructure:"read_buffer_size"`
@@ -62,14 +62,19 @@ type HTTPClientSettings struct {
6262

6363
// ToClient creates an HTTP client.
6464
func (hcs *HTTPClientSettings) ToClient(ext map[config.ComponentID]component.Extension) (*http.Client, error) {
65-
tlsCfg, err := hcs.TLSSetting.LoadTLSConfig()
66-
if err != nil {
67-
return nil, err
68-
}
65+
var err error
6966
transport := http.DefaultTransport.(*http.Transport).Clone()
70-
if tlsCfg != nil {
71-
transport.TLSClientConfig = tlsCfg
67+
68+
if hcs.TLSSetting != nil {
69+
tlsCfg, terr := hcs.TLSSetting.LoadTLSConfig()
70+
if terr != nil {
71+
return nil, terr
72+
}
73+
if tlsCfg != nil {
74+
transport.TLSClientConfig = tlsCfg
75+
}
7276
}
77+
7378
if hcs.ReadBufferSize > 0 {
7479
transport.ReadBufferSize = hcs.ReadBufferSize
7580
}

config/confighttp/confighttp_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func TestAllHTTPClientSettings(t *testing.T) {
5757
name: "all_valid_settings",
5858
settings: HTTPClientSettings{
5959
Endpoint: "localhost:1234",
60-
TLSSetting: configtls.TLSClientSetting{
60+
TLSSetting: &configtls.TLSClientSetting{
6161
Insecure: false,
6262
},
6363
ReadBufferSize: 1024,
@@ -70,7 +70,7 @@ func TestAllHTTPClientSettings(t *testing.T) {
7070
name: "error_round_tripper_returned",
7171
settings: HTTPClientSettings{
7272
Endpoint: "localhost:1234",
73-
TLSSetting: configtls.TLSClientSetting{
73+
TLSSetting: &configtls.TLSClientSetting{
7474
Insecure: false,
7575
},
7676
ReadBufferSize: 1024,
@@ -105,7 +105,7 @@ func TestHTTPClientSettingsError(t *testing.T) {
105105
err: "^failed to load TLS config: failed to load CA CertPool: failed to load CA /doesnt/exist:",
106106
settings: HTTPClientSettings{
107107
Endpoint: "",
108-
TLSSetting: configtls.TLSClientSetting{
108+
TLSSetting: &configtls.TLSClientSetting{
109109
TLSSetting: configtls.TLSSetting{
110110
CAFile: "/doesnt/exist",
111111
},
@@ -118,7 +118,7 @@ func TestHTTPClientSettingsError(t *testing.T) {
118118
err: "^failed to load TLS config: for auth via TLS, either both certificate and key must be supplied, or neither",
119119
settings: HTTPClientSettings{
120120
Endpoint: "",
121-
TLSSetting: configtls.TLSClientSetting{
121+
TLSSetting: &configtls.TLSClientSetting{
122122
TLSSetting: configtls.TLSSetting{
123123
CertFile: "/doesnt/exist",
124124
},
@@ -407,7 +407,7 @@ func TestHttpReception(t *testing.T) {
407407

408408
hcs := &HTTPClientSettings{
409409
Endpoint: prefix + ln.Addr().String(),
410-
TLSSetting: *tt.tlsClientCreds,
410+
TLSSetting: tt.tlsClientCreds,
411411
}
412412
client, errClient := hcs.ToClient(map[config.ComponentID]component.Extension{})
413413
assert.NoError(t, errClient)
@@ -582,7 +582,7 @@ func TestHttpHeaders(t *testing.T) {
582582
serverURL, _ := url.Parse(server.URL)
583583
setting := HTTPClientSettings{
584584
Endpoint: serverURL.String(),
585-
TLSSetting: configtls.TLSClientSetting{},
585+
TLSSetting: &configtls.TLSClientSetting{},
586586
ReadBufferSize: 0,
587587
WriteBufferSize: 0,
588588
Timeout: 0,

exporter/otlpexporter/config_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func TestLoadConfig(t *testing.T) {
7272
},
7373
Endpoint: "1.2.3.4:1234",
7474
Compression: "on",
75-
TLSSetting: configtls.TLSClientSetting{
75+
TLSSetting: &configtls.TLSClientSetting{
7676
TLSSetting: configtls.TLSSetting{
7777
CAFile: "/var/lib/mycert.pem",
7878
},

exporter/otlpexporter/factory_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func TestCreateTracesExporter(t *testing.T) {
7878
ExporterSettings: config.NewExporterSettings(config.NewID(typeStr)),
7979
GRPCClientSettings: configgrpc.GRPCClientSettings{
8080
Endpoint: endpoint,
81-
TLSSetting: configtls.TLSClientSetting{
81+
TLSSetting: &configtls.TLSClientSetting{
8282
Insecure: false,
8383
},
8484
},
@@ -147,7 +147,7 @@ func TestCreateTracesExporter(t *testing.T) {
147147
ExporterSettings: config.NewExporterSettings(config.NewID(typeStr)),
148148
GRPCClientSettings: configgrpc.GRPCClientSettings{
149149
Endpoint: endpoint,
150-
TLSSetting: configtls.TLSClientSetting{
150+
TLSSetting: &configtls.TLSClientSetting{
151151
TLSSetting: configtls.TLSSetting{
152152
CAFile: "testdata/test_cert.pem",
153153
},
@@ -161,7 +161,7 @@ func TestCreateTracesExporter(t *testing.T) {
161161
ExporterSettings: config.NewExporterSettings(config.NewID(typeStr)),
162162
GRPCClientSettings: configgrpc.GRPCClientSettings{
163163
Endpoint: endpoint,
164-
TLSSetting: configtls.TLSClientSetting{
164+
TLSSetting: &configtls.TLSClientSetting{
165165
TLSSetting: configtls.TLSSetting{
166166
CAFile: "nosuchfile",
167167
},

exporter/otlpexporter/otlp_test.go

+12-10
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ func TestSendTraces(t *testing.T) {
195195
cfg := factory.CreateDefaultConfig().(*Config)
196196
cfg.GRPCClientSettings = configgrpc.GRPCClientSettings{
197197
Endpoint: ln.Addr().String(),
198-
TLSSetting: configtls.TLSClientSetting{
198+
TLSSetting: &configtls.TLSClientSetting{
199199
Insecure: true,
200200
},
201201
Headers: map[string]string{
@@ -258,17 +258,19 @@ func TestSendTracesWhenEndpointHasHttpScheme(t *testing.T) {
258258
gRPCClientSettings configgrpc.GRPCClientSettings
259259
}{
260260
{
261-
name: "Use https scheme",
262-
useTLS: true,
263-
scheme: "https://",
264-
gRPCClientSettings: configgrpc.GRPCClientSettings{},
261+
name: "Use https scheme",
262+
useTLS: true,
263+
scheme: "https://",
264+
gRPCClientSettings: configgrpc.GRPCClientSettings{
265+
TLSSetting: &configtls.TLSClientSetting{},
266+
},
265267
},
266268
{
267269
name: "Use http scheme",
268270
useTLS: false,
269271
scheme: "http://",
270272
gRPCClientSettings: configgrpc.GRPCClientSettings{
271-
TLSSetting: configtls.TLSClientSetting{
273+
TLSSetting: &configtls.TLSClientSetting{
272274
Insecure: true,
273275
},
274276
},
@@ -336,7 +338,7 @@ func TestSendMetrics(t *testing.T) {
336338
cfg := factory.CreateDefaultConfig().(*Config)
337339
cfg.GRPCClientSettings = configgrpc.GRPCClientSettings{
338340
Endpoint: ln.Addr().String(),
339-
TLSSetting: configtls.TLSClientSetting{
341+
TLSSetting: &configtls.TLSClientSetting{
340342
Insecure: true,
341343
},
342344
Headers: map[string]string{
@@ -404,7 +406,7 @@ func TestSendTraceDataServerDownAndUp(t *testing.T) {
404406
cfg.QueueSettings.Enabled = false
405407
cfg.GRPCClientSettings = configgrpc.GRPCClientSettings{
406408
Endpoint: ln.Addr().String(),
407-
TLSSetting: configtls.TLSClientSetting{
409+
TLSSetting: &configtls.TLSClientSetting{
408410
Insecure: true,
409411
},
410412
// Need to wait for every request blocking until either request timeouts or succeed.
@@ -464,7 +466,7 @@ func TestSendTraceDataServerStartWhileRequest(t *testing.T) {
464466
cfg := factory.CreateDefaultConfig().(*Config)
465467
cfg.GRPCClientSettings = configgrpc.GRPCClientSettings{
466468
Endpoint: ln.Addr().String(),
467-
TLSSetting: configtls.TLSClientSetting{
469+
TLSSetting: &configtls.TLSClientSetting{
468470
Insecure: true,
469471
},
470472
}
@@ -540,7 +542,7 @@ func TestSendLogData(t *testing.T) {
540542
cfg := factory.CreateDefaultConfig().(*Config)
541543
cfg.GRPCClientSettings = configgrpc.GRPCClientSettings{
542544
Endpoint: ln.Addr().String(),
543-
TLSSetting: configtls.TLSClientSetting{
545+
TLSSetting: &configtls.TLSClientSetting{
544546
Insecure: true,
545547
},
546548
}

exporter/otlphttpexporter/config_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func TestLoadConfig(t *testing.T) {
6666
"another": "somevalue",
6767
},
6868
Endpoint: "https://1.2.3.4:1234",
69-
TLSSetting: configtls.TLSClientSetting{
69+
TLSSetting: &configtls.TLSClientSetting{
7070
TLSSetting: configtls.TLSSetting{
7171
CAFile: "/var/lib/mycert.pem",
7272
CertFile: "certfile",

exporter/otlphttpexporter/factory_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func TestCreateTracesExporter(t *testing.T) {
8282
ExporterSettings: config.NewExporterSettings(config.NewID(typeStr)),
8383
HTTPClientSettings: confighttp.HTTPClientSettings{
8484
Endpoint: endpoint,
85-
TLSSetting: configtls.TLSClientSetting{
85+
TLSSetting: &configtls.TLSClientSetting{
8686
Insecure: false,
8787
},
8888
},
@@ -107,7 +107,7 @@ func TestCreateTracesExporter(t *testing.T) {
107107
ExporterSettings: config.NewExporterSettings(config.NewID(typeStr)),
108108
HTTPClientSettings: confighttp.HTTPClientSettings{
109109
Endpoint: endpoint,
110-
TLSSetting: configtls.TLSClientSetting{
110+
TLSSetting: &configtls.TLSClientSetting{
111111
TLSSetting: configtls.TLSSetting{
112112
CAFile: "testdata/test_cert.pem",
113113
},
@@ -121,7 +121,7 @@ func TestCreateTracesExporter(t *testing.T) {
121121
ExporterSettings: config.NewExporterSettings(config.NewID(typeStr)),
122122
HTTPClientSettings: confighttp.HTTPClientSettings{
123123
Endpoint: endpoint,
124-
TLSSetting: configtls.TLSClientSetting{
124+
TLSSetting: &configtls.TLSClientSetting{
125125
TLSSetting: configtls.TLSSetting{
126126
CAFile: "nosuchfile",
127127
},

0 commit comments

Comments
 (0)