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/confighttp] Use configtls.NewDefaultClientConfig(), configtls.NewDefaultConfig() and configtls.NewDefaultServerConfig() #11689

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
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
254 changes: 107 additions & 147 deletions config/confighttp/confighttp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,8 @@ func TestAllHTTPClientSettings(t *testing.T) {
{
name: "all_valid_settings",
settings: ClientConfig{
Endpoint: "localhost:1234",
TLSSetting: configtls.ClientConfig{
Insecure: false,
},
Endpoint: "localhost:1234",
TLSSetting: configtls.NewDefaultClientConfig(),
ReadBufferSize: 1024,
WriteBufferSize: 512,
MaxIdleConns: &maxIdleConns,
Expand All @@ -96,10 +94,8 @@ func TestAllHTTPClientSettings(t *testing.T) {
{
name: "all_valid_settings_with_none_compression",
settings: ClientConfig{
Endpoint: "localhost:1234",
TLSSetting: configtls.ClientConfig{
Insecure: false,
},
Endpoint: "localhost:1234",
TLSSetting: configtls.NewDefaultClientConfig(),
ReadBufferSize: 1024,
WriteBufferSize: 512,
MaxIdleConns: &maxIdleConns,
Expand All @@ -116,10 +112,8 @@ func TestAllHTTPClientSettings(t *testing.T) {
{
name: "all_valid_settings_with_gzip_compression",
settings: ClientConfig{
Endpoint: "localhost:1234",
TLSSetting: configtls.ClientConfig{
Insecure: false,
},
Endpoint: "localhost:1234",
TLSSetting: configtls.NewDefaultClientConfig(),
ReadBufferSize: 1024,
WriteBufferSize: 512,
MaxIdleConns: &maxIdleConns,
Expand All @@ -136,10 +130,8 @@ func TestAllHTTPClientSettings(t *testing.T) {
{
name: "all_valid_settings_http2_health_check",
settings: ClientConfig{
Endpoint: "localhost:1234",
TLSSetting: configtls.ClientConfig{
Insecure: false,
},
Endpoint: "localhost:1234",
TLSSetting: configtls.NewDefaultClientConfig(),
ReadBufferSize: 1024,
WriteBufferSize: 512,
MaxIdleConns: &maxIdleConns,
Expand Down Expand Up @@ -196,10 +188,8 @@ func TestPartialHTTPClientSettings(t *testing.T) {
{
name: "valid_partial_settings",
settings: ClientConfig{
Endpoint: "localhost:1234",
TLSSetting: configtls.ClientConfig{
Insecure: false,
},
Endpoint: "localhost:1234",
TLSSetting: configtls.NewDefaultClientConfig(),
ReadBufferSize: 1024,
WriteBufferSize: 512,
},
Expand Down Expand Up @@ -287,6 +277,15 @@ func TestProxyURL(t *testing.T) {
}

func TestHTTPClientSettingsError(t *testing.T) {
tlsConfigCADoesntExist := configtls.NewDefaultConfig()
tlsConfigCADoesntExist.CAFile = "/doesnt/exist"
tlsClientConfigCADoesntExist := configtls.NewDefaultClientConfig()
tlsClientConfigCADoesntExist.Config = tlsConfigCADoesntExist

tlsConfigCertDoesntExist := configtls.NewDefaultConfig()
tlsConfigCertDoesntExist.CertFile = "/doesnt/exist"
tlsClientConfigCertDoesntExist := configtls.NewDefaultClientConfig()
tlsClientConfigCertDoesntExist.Config = tlsConfigCertDoesntExist
host := &mockHost{
ext: map[component.ID]component.Component{},
}
Expand All @@ -297,27 +296,15 @@ func TestHTTPClientSettingsError(t *testing.T) {
{
err: "^failed to load TLS config: failed to load CA CertPool File: failed to load cert /doesnt/exist:",
settings: ClientConfig{
Endpoint: "",
TLSSetting: configtls.ClientConfig{
Config: configtls.Config{
CAFile: "/doesnt/exist",
},
Insecure: false,
ServerName: "",
},
Endpoint: "",
TLSSetting: tlsClientConfigCADoesntExist,
},
},
{
err: "^failed to load TLS config: failed to load TLS cert and key: for auth via TLS, provide both certificate and key, or neither",
settings: ClientConfig{
Endpoint: "",
TLSSetting: configtls.ClientConfig{
Config: configtls.Config{
CertFile: "/doesnt/exist",
},
Insecure: false,
ServerName: "",
},
Endpoint: "",
TLSSetting: tlsClientConfigCertDoesntExist,
},
},
{
Expand Down Expand Up @@ -557,6 +544,50 @@ func TestHTTPServerWarning(t *testing.T) {
}

func TestHttpReception(t *testing.T) {
tlsClientConfigWithInsecure := configtls.NewDefaultClientConfig()
tlsClientConfigWithInsecure.Insecure = true

tlsConfigWithCerts1 := configtls.NewDefaultConfig()
tlsConfigWithCerts1.CAFile = filepath.Join("testdata", "ca.crt")
tlsConfigWithCerts1.CertFile = filepath.Join("testdata", "server.crt")
tlsConfigWithCerts1.KeyFile = filepath.Join("testdata", "server.key")
tlsServerConfigWithCerts := configtls.NewDefaultServerConfig()
tlsServerConfigWithCerts.Config = tlsConfigWithCerts1

tlsConfigWithCAFile1 := configtls.NewDefaultConfig()
tlsConfigWithCAFile1.CAFile = filepath.Join("testdata", "ca.crt")
tlsClientConfigWithCAFile := configtls.NewDefaultClientConfig()
tlsClientConfigWithCAFile.Config = tlsConfigWithCAFile1
tlsClientConfigWithCAFile.ServerName = "localhost"

tlsConfigWithCertsAndClientCA := configtls.NewDefaultConfig()
tlsConfigWithCertsAndClientCA.CAFile = filepath.Join("testdata", "ca.crt")
tlsConfigWithCertsAndClientCA.CertFile = filepath.Join("testdata", "server.crt")
tlsConfigWithCertsAndClientCA.KeyFile = filepath.Join("testdata", "server.key")
tlsServerConfigWithCertsAndClientCA := configtls.NewDefaultServerConfig()
tlsServerConfigWithCertsAndClientCA.Config = tlsConfigWithCertsAndClientCA
tlsServerConfigWithCertsAndClientCA.ClientCAFile = filepath.Join("testdata", "ca.crt")

tlsConfigWithCAFile2 := configtls.NewDefaultConfig()
tlsConfigWithCAFile2.CAFile = filepath.Join("testdata", "ca.crt")
tlsServerConfigWithCAFile := configtls.NewDefaultServerConfig()
tlsServerConfigWithCAFile.Config = tlsConfigWithCAFile2

tlsConfigWithCertsAndWrongClientCA := configtls.NewDefaultConfig()
tlsConfigWithCertsAndWrongClientCA.CAFile = filepath.Join("testdata", "ca.crt")
tlsConfigWithCertsAndWrongClientCA.CertFile = filepath.Join("testdata", "server.crt")
tlsConfigWithCertsAndWrongClientCA.KeyFile = filepath.Join("testdata", "server.key")
tlsServerConfigWithCertsAndWrongClientCA := configtls.NewDefaultServerConfig()
tlsServerConfigWithCertsAndWrongClientCA.Config = tlsConfigWithCertsAndWrongClientCA
tlsServerConfigWithCertsAndWrongClientCA.ClientCAFile = filepath.Join("testdata", "server.crt")

tlsConfigWithCerts3 := configtls.NewDefaultConfig()
tlsConfigWithCerts3.CAFile = filepath.Join("testdata", "ca.crt")
tlsConfigWithCerts3.CertFile = filepath.Join("testdata", "client.crt")
tlsConfigWithCerts3.KeyFile = filepath.Join("testdata", "client.key")
tlsClientConfigWithCerts2 := configtls.NewDefaultClientConfig()
tlsClientConfigWithCerts2.Config = tlsConfigWithCerts3
tlsClientConfigWithCerts2.ServerName = "localhost"
tests := []struct {
name string
tlsServerCreds *configtls.ServerConfig
Expand All @@ -567,114 +598,41 @@ func TestHttpReception(t *testing.T) {
{
name: "noTLS",
tlsServerCreds: nil,
tlsClientCreds: &configtls.ClientConfig{
Insecure: true,
},
tlsClientCreds: &tlsClientConfigWithInsecure,
},
{
name: "TLS",
tlsServerCreds: &configtls.ServerConfig{
Config: configtls.Config{
CAFile: filepath.Join("testdata", "ca.crt"),
CertFile: filepath.Join("testdata", "server.crt"),
KeyFile: filepath.Join("testdata", "server.key"),
},
},
tlsClientCreds: &configtls.ClientConfig{
Config: configtls.Config{
CAFile: filepath.Join("testdata", "ca.crt"),
},
ServerName: "localhost",
},
name: "TLS",
tlsServerCreds: &tlsServerConfigWithCerts,
tlsClientCreds: &tlsClientConfigWithCAFile,
},
{
name: "TLS (HTTP/1.1)",
tlsServerCreds: &configtls.ServerConfig{
Config: configtls.Config{
CAFile: filepath.Join("testdata", "ca.crt"),
CertFile: filepath.Join("testdata", "server.crt"),
KeyFile: filepath.Join("testdata", "server.key"),
},
},
tlsClientCreds: &configtls.ClientConfig{
Config: configtls.Config{
CAFile: filepath.Join("testdata", "ca.crt"),
},
ServerName: "localhost",
},
forceHTTP1: true,
name: "TLS (HTTP/1.1)",
tlsServerCreds: &tlsServerConfigWithCerts,
tlsClientCreds: &tlsClientConfigWithCAFile,
forceHTTP1: true,
},
{
name: "NoServerCertificates",
tlsServerCreds: &configtls.ServerConfig{
Config: configtls.Config{
CAFile: filepath.Join("testdata", "ca.crt"),
},
},
tlsClientCreds: &configtls.ClientConfig{
Config: configtls.Config{
CAFile: filepath.Join("testdata", "ca.crt"),
},
ServerName: "localhost",
},
hasError: true,
name: "NoServerCertificates",
tlsServerCreds: &tlsServerConfigWithCAFile,
tlsClientCreds: &tlsClientConfigWithCAFile,
hasError: true,
},
{
name: "mTLS",
tlsServerCreds: &configtls.ServerConfig{
Config: configtls.Config{
CAFile: filepath.Join("testdata", "ca.crt"),
CertFile: filepath.Join("testdata", "server.crt"),
KeyFile: filepath.Join("testdata", "server.key"),
},
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",
},
name: "mTLS",
tlsServerCreds: &tlsServerConfigWithCertsAndClientCA,
tlsClientCreds: &tlsClientConfigWithCerts2,
},
{
name: "NoClientCertificate",
tlsServerCreds: &configtls.ServerConfig{
Config: configtls.Config{
CAFile: filepath.Join("testdata", "ca.crt"),
CertFile: filepath.Join("testdata", "server.crt"),
KeyFile: filepath.Join("testdata", "server.key"),
},
ClientCAFile: filepath.Join("testdata", "ca.crt"),
},
tlsClientCreds: &configtls.ClientConfig{
Config: configtls.Config{
CAFile: filepath.Join("testdata", "ca.crt"),
},
ServerName: "localhost",
},
hasError: true,
name: "NoClientCertificate",
tlsServerCreds: &tlsServerConfigWithCertsAndClientCA,
tlsClientCreds: &tlsClientConfigWithCAFile,
hasError: true,
},
{
name: "WrongClientCA",
tlsServerCreds: &configtls.ServerConfig{
Config: configtls.Config{
CAFile: filepath.Join("testdata", "ca.crt"),
CertFile: filepath.Join("testdata", "server.crt"),
KeyFile: filepath.Join("testdata", "server.key"),
},
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,
name: "WrongClientCA",
tlsServerCreds: &tlsServerConfigWithCertsAndWrongClientCA,
tlsClientCreds: &tlsClientConfigWithCerts2,
hasError: true,
},
}
// prepare
Expand Down Expand Up @@ -1025,7 +983,7 @@ func TestHttpClientHeaders(t *testing.T) {
serverURL, _ := url.Parse(server.URL)
setting := ClientConfig{
Endpoint: serverURL.String(),
TLSSetting: configtls.ClientConfig{},
TLSSetting: configtls.NewDefaultClientConfig(),
ReadBufferSize: 0,
WriteBufferSize: 0,
Timeout: 0,
Expand Down Expand Up @@ -1061,7 +1019,7 @@ func TestHttpClientHostHeader(t *testing.T) {
serverURL, _ := url.Parse(server.URL)
setting := ClientConfig{
Endpoint: serverURL.String(),
TLSSetting: configtls.ClientConfig{},
TLSSetting: configtls.NewDefaultClientConfig(),
ReadBufferSize: 0,
WriteBufferSize: 0,
Timeout: 0,
Expand Down Expand Up @@ -1419,6 +1377,19 @@ func (nh *mockHost) GetExtensions() map[component.ID]component.Component {
}

func BenchmarkHttpRequest(b *testing.B) {
tlsConfigWithCerts := configtls.NewDefaultConfig()
tlsConfigWithCerts.CAFile = filepath.Join("testdata", "ca.crt")
tlsConfigWithCerts.CertFile = filepath.Join("testdata", "server.crt")
tlsConfigWithCerts.KeyFile = filepath.Join("testdata", "server.key")
tlsServerConfigWithCerts := configtls.NewDefaultServerConfig()
tlsServerConfigWithCerts.Config = tlsConfigWithCerts

tlsConfigWithCAFile := configtls.NewDefaultConfig()
tlsConfigWithCAFile.CAFile = filepath.Join("testdata", "ca.crt")
tlsClientConfigWithCAFile := configtls.NewDefaultClientConfig()
tlsClientConfigWithCAFile.Config = tlsConfigWithCAFile
tlsClientConfigWithCAFile.ServerName = "localhost"

tests := []struct {
name string
forceHTTP1 bool
Expand Down Expand Up @@ -1446,19 +1417,8 @@ func BenchmarkHttpRequest(b *testing.B) {
},
}

tlsServerCreds := &configtls.ServerConfig{
Config: configtls.Config{
CAFile: filepath.Join("testdata", "ca.crt"),
CertFile: filepath.Join("testdata", "server.crt"),
KeyFile: filepath.Join("testdata", "server.key"),
},
}
tlsClientCreds := &configtls.ClientConfig{
Config: configtls.Config{
CAFile: filepath.Join("testdata", "ca.crt"),
},
ServerName: "localhost",
}
tlsServerCreds := &tlsServerConfigWithCerts
tlsClientCreds := &tlsClientConfigWithCAFile

hss := &ServerConfig{
Endpoint: "localhost:0",
Expand Down
Loading