-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Validate TLS config: return error if cert or key is missing #13134
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
base: main
Are you sure you want to change the base?
Changes from 5 commits
66157a2
d813bb3
155d4e5
ba4f4e6
dffec0f
4df2df8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
change_type: changed | ||
component: configtls | ||
note: Validate TLS config earlier: return error if certificate or key is missing. | ||
issues: [13134] | ||
gupta-nu marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package configtls | ||
gupta-nu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestConfig_Validate_CertKeyPresence(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
config Config | ||
expectErr bool | ||
}{ | ||
{ | ||
name: "no cert or key", | ||
config: Config{}, | ||
expectErr: false, | ||
}, | ||
{ | ||
name: "only CertFile", | ||
config: Config{CertFile: "cert.pem"}, | ||
expectErr: true, | ||
}, | ||
{ | ||
name: "only KeyFile", | ||
config: Config{KeyFile: "key.pem"}, | ||
expectErr: true, | ||
}, | ||
{ | ||
name: "CertFile and KeyFile", | ||
config: Config{CertFile: "cert.pem", KeyFile: "Key.pem"}, | ||
expectErr: false, | ||
}, | ||
{ | ||
name: "CertPem and KeyPem", | ||
config: Config{CertPem: "cert", KeyPem: "key"}, | ||
expectErr: false, | ||
}, | ||
{ | ||
name: "CertFile and KeyPem(mixed)", | ||
config: Config{CertFile: "cert.pem", KeyPem: "key"}, | ||
expectErr: false, | ||
}, | ||
{ | ||
name: "CertPem only", | ||
config: Config{CertPem: "cert"}, | ||
expectErr: true, | ||
}, | ||
{ | ||
name: "KeyPem only", | ||
config: Config{KeyPem: "key"}, | ||
expectErr: true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
err := tt.config.Validate() | ||
if tt.expectErr { | ||
assert.Error(t, err) | ||
} else { | ||
assert.NoError(t, err) | ||
} | ||
}) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -208,6 +208,14 @@ func (c Config) Validate() error { | |
return errors.New("invalid TLS configuration: min_version cannot be greater than max_version") | ||
} | ||
|
||
certProvided := c.CertFile != "" || c.CertPem != "" | ||
keyProvided := c.KeyFile != "" || c.KeyPem != "" | ||
|
||
// If cert or key is provided, require both to be present | ||
if certProvided != keyProvided { | ||
return errors.New("TLS configuration must include certificate and key (CertFile/CertPem and KeyFile/KeyPem)") | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this check fixes issue #13130. The issue was originally filed because we noticed the case where the TLS config is empty led to errors, but these errors only surface at connection time. (Admittedly, the issue description may not have been clear about this). But this check doesn't fail if all fields are empty. Moreover, I believe this is identical to the first check in If we want to move as many checks as possible to config validation time, looking at the code for If we just want to fix the immediate issue, I think checking And if we don't mind an error on startup rather than an error at config validation time, considering the |
||
|
||
return nil | ||
} | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.