Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[configtls] [config/configgrpc] Use configtls.NewDefaultClientConfig(), configtls.NewDefaultConfig() and configtls.NewDefaultServerConfig() #11638

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: The term "clientConfig" is highly overloaded in this context, I wonder if it would be better to rename this local "tlsSettings" or something similar.

clientConfig.Insecure = true
gcs := &ClientConfig{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In most/all of these first cases, wouldn't you want to use the NewDefaultClientConfig at the outer level, rather than only on the inner TLSSetting?
i.e. using this function:
https://github.com/nokia/open-telemetry-opentelemetry-collector/blob/1703ce6df6979d84720b5826c97d579cf11a59bb/config/configgrpc/configgrpc.go#L111

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In most/all of these first cases, wouldn't you want to use the NewDefaultClientConfig at the outer level, rather than only on the inner TLSSetting?

Thanks for taking a look! This suggested change should be done for all the test cases or only the initial ones? PR includes changes only to migrate to new configtls.NewDefaultClientConfig(). If the community is okay, I am willing to create an issue to track this and create separate PR addressing this.

Please share your thoughts.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please ignore my comment if it doesn't match the requirements. I just thought it odd that we'd use one and not the other. I'm new here. :)

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

false by default

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Insecure is false by default

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
Loading