Skip to content

[configtls] add new config to specify TLS curve preferences #12174

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

Merged
merged 10 commits into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
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
25 changes: 25 additions & 0 deletions .chloggen/add-curve-preferences-tls.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
component: configtls

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Allow users to mention their preferred curve types for ECDHE handshake

# One or more tracking issues or pull requests related to the change
issues: [12174]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: []
6 changes: 6 additions & 0 deletions config/configtls/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ By default, TLS is enabled:
the exporter's HTTPs or gRPC connection. See
[grpc.WithInsecure()](https://godoc.org/google.golang.org/grpc#WithInsecure)
for gRPC.
- `curve_preferences` (default = []): specify your curve preferences that will
be used in an ECDHE handshake, in preference order. Accepted values are:
- X25519
- P521
- P256
- P384

As a result, the following parameters are also required:

Expand Down
21 changes: 21 additions & 0 deletions config/configtls/configtls.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ type Config struct {
// ReloadInterval specifies the duration after which the certificate will be reloaded
// If not set, it will never be reloaded (optional)
ReloadInterval time.Duration `mapstructure:"reload_interval"`

// contains the elliptic curves that will be used in
// an ECDHE handshake, in preference order
// Defaults to empty list and "crypto/tls" defaults are used, internally.
CurvePreferences []string `mapstructure:"curve_preferences"`
}

// NewDefaultConfig creates a new TLSSetting with any default values set.
Expand Down Expand Up @@ -231,6 +236,14 @@ func (c Config) loadTLSConfig() (*tls.Config, error) {
if err != nil {
return nil, err
}
curvePreferences := make([]tls.CurveID, 0, len(c.CurvePreferences))
for _, curve := range c.CurvePreferences {
curveID, ok := tlsCurveTypes[curve]
if !ok {
return nil, fmt.Errorf("invalid curve type: %s. Expected values are [P-256, P-384, P-521, X25519]", curveID)
}
curvePreferences = append(curvePreferences, curveID)
}

return &tls.Config{
RootCAs: certPool,
Expand All @@ -239,6 +252,7 @@ func (c Config) loadTLSConfig() (*tls.Config, error) {
MinVersion: minTLS,
MaxVersion: maxTLS,
CipherSuites: cipherSuites,
CurvePreferences: curvePreferences,
}, nil
}

Expand Down Expand Up @@ -448,3 +462,10 @@ var tlsVersions = map[string]uint16{
"1.2": tls.VersionTLS12,
"1.3": tls.VersionTLS13,
}

var tlsCurveTypes = map[string]tls.CurveID{
"P256": tls.CurveP256,
"P384": tls.CurveP384,
"P521": tls.CurveP521,
"X25519": tls.X25519,
}
50 changes: 50 additions & 0 deletions config/configtls/configtls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -871,3 +871,53 @@ func TestSystemCertPool_loadCert(t *testing.T) {
})
}
}

func TestCurvePreferences(t *testing.T) {
tests := []struct {
name string
preferences []string
expectedCurveIDs []tls.CurveID
expectedErr string
}{
{
name: "X25519",
preferences: []string{"X25519"},
expectedCurveIDs: []tls.CurveID{tls.X25519},
},
{
name: "P521",
preferences: []string{"P521"},
expectedCurveIDs: []tls.CurveID{tls.CurveP521},
},
{
name: "P-256",
preferences: []string{"P256"},
expectedCurveIDs: []tls.CurveID{tls.CurveP256},
},
{
name: "multiple",
preferences: []string{"P256", "P521", "X25519"},
expectedCurveIDs: []tls.CurveID{tls.CurveP256, tls.CurveP521, tls.X25519},
},
{
name: "invalid-curve",
preferences: []string{"P25223236"},
expectedCurveIDs: []tls.CurveID{},
expectedErr: "invalid curve type",
},
}
for _, test := range tests {
tlsSetting := ClientConfig{
Config: Config{
CurvePreferences: test.preferences,
},
}
config, err := tlsSetting.LoadTLSConfig(context.Background())
if test.expectedErr == "" {
require.NoError(t, err)
require.ElementsMatchf(t, test.expectedCurveIDs, config.CurvePreferences, "expected %v, got %v", test.expectedCurveIDs, config.CurvePreferences)
} else {
require.ErrorContains(t, err, test.expectedErr)
}
}
}
Loading