Skip to content

Commit

Permalink
[configtls] [config/configgrpc] Use configtls.NewDefaultClientConfig …
Browse files Browse the repository at this point in the history
…instead of manually creating struct. See: open-telemetry#11383

Signed-off-by: wasim-nihal <[email protected]>
  • Loading branch information
wasim-nihal committed Nov 11, 2024
1 parent 1703ce6 commit 9c69a8a
Showing 1 changed file with 67 additions and 100 deletions.
167 changes: 67 additions & 100 deletions config/configgrpc/configgrpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,10 @@ func TestDefaultGrpcClientSettings(t *testing.T) {
tt, err := componenttest.SetupTelemetry(componentID)
require.NoError(t, err)
t.Cleanup(func() { require.NoError(t, tt.Shutdown(context.Background())) })

clientConfig := configtls.NewDefaultClientConfig()
clientConfig.Insecure = true
gcs := &ClientConfig{
TLSSetting: configtls.ClientConfig{
Insecure: true,
},
TLSSetting: clientConfig,
}
opts, err := gcs.getGrpcDialOptions(context.Background(), componenttest.NewNopHost(), tt.TelemetrySettings(), []ToClientConnOption{})
require.NoError(t, err)
Expand All @@ -117,11 +116,10 @@ func TestGrpcClientExtraOption(t *testing.T) {
tt, err := componenttest.SetupTelemetry(componentID)
require.NoError(t, err)
t.Cleanup(func() { require.NoError(t, tt.Shutdown(context.Background())) })

clientConfig := configtls.NewDefaultClientConfig()
clientConfig.Insecure = true
gcs := &ClientConfig{
TLSSetting: configtls.ClientConfig{
Insecure: true,
},
TLSSetting: clientConfig,
}
extraOpt := grpc.WithUserAgent("test-agent")
opts, err := gcs.getGrpcDialOptions(
Expand All @@ -139,7 +137,8 @@ func TestAllGrpcClientSettings(t *testing.T) {
tt, err := componenttest.SetupTelemetry(componentID)
require.NoError(t, err)
t.Cleanup(func() { require.NoError(t, tt.Shutdown(context.Background())) })

clientConfig := configtls.NewDefaultClientConfig()
clientConfig.Insecure = false
tests := []struct {
settings ClientConfig
name string
Expand All @@ -153,9 +152,7 @@ func TestAllGrpcClientSettings(t *testing.T) {
},
Endpoint: "localhost:1234",
Compression: configcompression.TypeGzip,
TLSSetting: configtls.ClientConfig{
Insecure: false,
},
TLSSetting: clientConfig,
Keepalive: &KeepaliveClientConfig{
Time: time.Second,
Timeout: time.Second,
Expand All @@ -182,9 +179,7 @@ func TestAllGrpcClientSettings(t *testing.T) {
},
Endpoint: "localhost:1234",
Compression: configcompression.TypeSnappy,
TLSSetting: configtls.ClientConfig{
Insecure: false,
},
TLSSetting: clientConfig,
Keepalive: &KeepaliveClientConfig{
Time: time.Second,
Timeout: time.Second,
Expand All @@ -211,9 +206,7 @@ func TestAllGrpcClientSettings(t *testing.T) {
},
Endpoint: "localhost:1234",
Compression: configcompression.TypeZstd,
TLSSetting: configtls.ClientConfig{
Insecure: false,
},
TLSSetting: clientConfig,
Keepalive: &KeepaliveClientConfig{
Time: time.Second,
Timeout: time.Second,
Expand Down Expand Up @@ -375,15 +368,15 @@ func TestGrpcServerAuthSettings(t *testing.T) {
}

func TestGrpcClientConfigInvalidBalancer(t *testing.T) {
clientConfig := configtls.NewDefaultClientConfig()
clientConfig.Insecure = false
settings := ClientConfig{
Headers: map[string]configopaque.String{
"test": "test",
},
Endpoint: "localhost:1234",
Compression: "gzip",
TLSSetting: configtls.ClientConfig{
Insecure: false,
},
TLSSetting: clientConfig,
Keepalive: &KeepaliveClientConfig{
Time: time.Second,
Timeout: time.Second,
Expand All @@ -398,6 +391,15 @@ func TestGrpcClientConfigInvalidBalancer(t *testing.T) {
}

func TestGRPCClientSettingsError(t *testing.T) {
clientConfigCADoesntExist := configtls.NewDefaultClientConfig()
clientConfigCADoesntExist.Config = configtls.Config{
CAFile: "/doesnt/exist",
}
clientConfigCADoesntExist.Insecure = false
clientConfigCADoesntExist.ServerName = ""

clientConfigWithInsecure := configtls.NewDefaultClientConfig()
clientConfigWithInsecure.Insecure = true
tests := []struct {
settings ClientConfig
err string
Expand All @@ -409,14 +411,8 @@ func TestGRPCClientSettingsError(t *testing.T) {
Headers: nil,
Endpoint: "",
Compression: "",
TLSSetting: configtls.ClientConfig{
Config: configtls.Config{
CAFile: "/doesnt/exist",
},
Insecure: false,
ServerName: "",
},
Keepalive: nil,
TLSSetting: clientConfigCADoesntExist,
Keepalive: nil,
},
},
{
Expand All @@ -425,14 +421,8 @@ func TestGRPCClientSettingsError(t *testing.T) {
Headers: nil,
Endpoint: "",
Compression: "",
TLSSetting: configtls.ClientConfig{
Config: configtls.Config{
CertFile: "/doesnt/exist",
},
Insecure: false,
ServerName: "",
},
Keepalive: nil,
TLSSetting: clientConfigCADoesntExist,
Keepalive: nil,
},
},
{
Expand All @@ -454,32 +444,26 @@ func TestGRPCClientSettingsError(t *testing.T) {
{
err: "unsupported compression type \"zlib\"",
settings: ClientConfig{
Endpoint: "localhost:1234",
TLSSetting: configtls.ClientConfig{
Insecure: true,
},
Endpoint: "localhost:1234",
TLSSetting: clientConfigWithInsecure,
Compression: "zlib",
},
host: &mockHost{},
},
{
err: "unsupported compression type \"deflate\"",
settings: ClientConfig{
Endpoint: "localhost:1234",
TLSSetting: configtls.ClientConfig{
Insecure: true,
},
Endpoint: "localhost:1234",
TLSSetting: clientConfigWithInsecure,
Compression: "deflate",
},
host: &mockHost{},
},
{
err: "unsupported compression type \"bad\"",
settings: ClientConfig{
Endpoint: "localhost:1234",
TLSSetting: configtls.ClientConfig{
Insecure: true,
},
Endpoint: "localhost:1234",
TLSSetting: clientConfigWithInsecure,
Compression: "bad",
},
host: &mockHost{},
Expand All @@ -504,7 +488,7 @@ func TestUseSecure(t *testing.T) {
Headers: nil,
Endpoint: "",
Compression: "",
TLSSetting: configtls.ClientConfig{},
TLSSetting: configtls.NewDefaultClientConfig(),
Keepalive: nil,
}
dialOpts, err := gcs.getGrpcDialOptions(context.Background(), componenttest.NewNopHost(), tt.TelemetrySettings(), []ToClientConnOption{})
Expand Down Expand Up @@ -632,6 +616,22 @@ func TestHttpReception(t *testing.T) {
require.NoError(t, err)
t.Cleanup(func() { require.NoError(t, tt.Shutdown(context.Background())) })

clientConfigWithInsecure := configtls.NewDefaultClientConfig()
clientConfigWithInsecure.Insecure = true

clientConfigWithCAFile := configtls.NewDefaultClientConfig()
clientConfigWithCAFile.Config = configtls.Config{
CAFile: filepath.Join("testdata", "ca.crt"),
}
clientConfigWithCAFile.ServerName = "localhost"

clientConfigWithCertAndKeyFile := configtls.NewDefaultClientConfig()
clientConfigWithCertAndKeyFile.Config = configtls.Config{
CAFile: filepath.Join("testdata", "ca.crt"),
CertFile: filepath.Join("testdata", "client.crt"),
KeyFile: filepath.Join("testdata", "client.key"),
}
clientConfigWithCertAndKeyFile.ServerName = "localhost"
tests := []struct {
name string
tlsServerCreds *configtls.ServerConfig
Expand All @@ -641,9 +641,7 @@ func TestHttpReception(t *testing.T) {
{
name: "noTLS",
tlsServerCreds: nil,
tlsClientCreds: &configtls.ClientConfig{
Insecure: true,
},
tlsClientCreds: &clientConfigWithInsecure,
},
{
name: "TLS",
Expand All @@ -654,12 +652,7 @@ func TestHttpReception(t *testing.T) {
KeyFile: filepath.Join("testdata", "server.key"),
},
},
tlsClientCreds: &configtls.ClientConfig{
Config: configtls.Config{
CAFile: filepath.Join("testdata", "ca.crt"),
},
ServerName: "localhost",
},
tlsClientCreds: &clientConfigWithCAFile,
},
{
name: "NoServerCertificates",
Expand All @@ -668,13 +661,8 @@ func TestHttpReception(t *testing.T) {
CAFile: filepath.Join("testdata", "ca.crt"),
},
},
tlsClientCreds: &configtls.ClientConfig{
Config: configtls.Config{
CAFile: filepath.Join("testdata", "ca.crt"),
},
ServerName: "localhost",
},
hasError: true,
tlsClientCreds: &clientConfigWithCAFile,
hasError: true,
},
{
name: "mTLS",
Expand All @@ -686,14 +674,7 @@ func TestHttpReception(t *testing.T) {
},
ClientCAFile: filepath.Join("testdata", "ca.crt"),
},
tlsClientCreds: &configtls.ClientConfig{
Config: configtls.Config{
CAFile: filepath.Join("testdata", "ca.crt"),
CertFile: filepath.Join("testdata", "client.crt"),
KeyFile: filepath.Join("testdata", "client.key"),
},
ServerName: "localhost",
},
tlsClientCreds: &clientConfigWithCertAndKeyFile,
},
{
name: "NoClientCertificate",
Expand All @@ -705,13 +686,8 @@ func TestHttpReception(t *testing.T) {
},
ClientCAFile: filepath.Join("testdata", "ca.crt"),
},
tlsClientCreds: &configtls.ClientConfig{
Config: configtls.Config{
CAFile: filepath.Join("testdata", "ca.crt"),
},
ServerName: "localhost",
},
hasError: true,
tlsClientCreds: &clientConfigWithCAFile,
hasError: true,
},
{
name: "WrongClientCA",
Expand All @@ -723,15 +699,8 @@ func TestHttpReception(t *testing.T) {
},
ClientCAFile: filepath.Join("testdata", "server.crt"),
},
tlsClientCreds: &configtls.ClientConfig{
Config: configtls.Config{
CAFile: filepath.Join("testdata", "ca.crt"),
CertFile: filepath.Join("testdata", "client.crt"),
KeyFile: filepath.Join("testdata", "client.key"),
},
ServerName: "localhost",
},
hasError: true,
tlsClientCreds: &clientConfigWithCertAndKeyFile,
hasError: true,
},
}
// prepare
Expand Down Expand Up @@ -801,12 +770,11 @@ func TestReceiveOnUnixDomainSocket(t *testing.T) {
go func() {
_ = srv.Serve(ln)
}()

clientConfig := configtls.NewDefaultClientConfig()
clientConfig.Insecure = true
gcs := &ClientConfig{
Endpoint: "unix://" + ln.Addr().String(),
TLSSetting: configtls.ClientConfig{
Insecure: true,
},
Endpoint: "unix://" + ln.Addr().String(),
TLSSetting: clientConfig,
}
grpcClientConn, errClient := gcs.ToClientConn(context.Background(), componenttest.NewNopHost(), tt.TelemetrySettings())
require.NoError(t, errClient)
Expand Down Expand Up @@ -979,7 +947,8 @@ func TestClientInfoInterceptors(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
mock := &grpcTraceServer{}
var l net.Listener

clientConfig := configtls.NewDefaultClientConfig()
clientConfig.Insecure = true
// prepare the server
{
gss := &ServerConfig{
Expand All @@ -1005,10 +974,8 @@ func TestClientInfoInterceptors(t *testing.T) {
// prepare the client and execute a RPC
{
gcs := &ClientConfig{
Endpoint: l.Addr().String(),
TLSSetting: configtls.ClientConfig{
Insecure: true,
},
Endpoint: l.Addr().String(),
TLSSetting: clientConfig,
}

tel, err := componenttest.SetupTelemetry(componentID)
Expand Down

0 comments on commit 9c69a8a

Please sign in to comment.