Skip to content

Commit d4f068d

Browse files
authored
Allow users to set min and max TLS versions (#3591)
* Allow users to set min and max TLS versions Users want to be to choose the min and max versions they want to allow. Introduce two new settings to make the version range configurable. * Make linter happy
1 parent 9e8bded commit d4f068d

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

config/configtls/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ won't use TLS at all.
3434
- `insecure_skip_verify` (default = false): whether to skip verifying the
3535
certificate or not.
3636

37+
Minimum and maximum TLS version can be set:
38+
39+
- `min_version` (default = "1.0"): Minimum acceptable TLS version.
40+
41+
- `max_version` (default = "1.3"): Maximum acceptable TLS version.
42+
3743
How TLS/mTLS is configured depends on whether configuring the client or server.
3844
See below for examples.
3945

@@ -63,6 +69,8 @@ exporters:
6369
ca_file: server.crt
6470
cert_file: client.crt
6571
key_file: client.key
72+
min_version: "1.1"
73+
max_version: "1.2"
6674
otlp/insecure:
6775
endpoint: myserver.local:55690
6876
insecure: true

config/configtls/configtls.go

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,20 @@ type TLSSetting struct {
3030
// For a server this verifies client certificates. If empty uses system root CA.
3131
// (optional)
3232
CAFile string `mapstructure:"ca_file"`
33+
3334
// Path to the TLS cert to use for TLS required connections. (optional)
3435
CertFile string `mapstructure:"cert_file"`
36+
3537
// Path to the TLS key to use for TLS required connections. (optional)
3638
KeyFile string `mapstructure:"key_file"`
39+
40+
// MinVersion sets the minimum TLS version that is acceptable.
41+
// If not set, TLS 1.0 is used. (optional)
42+
MinVersion string `mapstructure:"min_version"`
43+
44+
// MaxVersion sets the maximum TLS version that is acceptable.
45+
// If not set, TLS 1.3 is used. (optional)
46+
MaxVersion string `mapstructure:"max_version"`
3747
}
3848

3949
// TLSClientSetting contains TLS configurations that are specific to client
@@ -96,16 +106,28 @@ func (c TLSSetting) loadTLSConfig() (*tls.Config, error) {
96106

97107
var certificates []tls.Certificate
98108
if c.CertFile != "" && c.KeyFile != "" {
99-
tlsCert, err := tls.LoadX509KeyPair(filepath.Clean(c.CertFile), filepath.Clean(c.KeyFile))
109+
var tlsCert tls.Certificate
110+
tlsCert, err = tls.LoadX509KeyPair(filepath.Clean(c.CertFile), filepath.Clean(c.KeyFile))
100111
if err != nil {
101112
return nil, fmt.Errorf("failed to load TLS cert and key: %w", err)
102113
}
103114
certificates = append(certificates, tlsCert)
104115
}
105116

117+
minTLS, err := convertVersion(c.MinVersion)
118+
if err != nil {
119+
return nil, fmt.Errorf("invalid TLS min_version: %w", err)
120+
}
121+
maxTLS, err := convertVersion(c.MaxVersion)
122+
if err != nil {
123+
return nil, fmt.Errorf("invalid TLS max_version: %w", err)
124+
}
125+
106126
return &tls.Config{
107127
RootCAs: certPool,
108128
Certificates: certificates,
129+
MinVersion: minTLS,
130+
MaxVersion: maxTLS,
109131
}, nil
110132
}
111133

@@ -153,3 +175,21 @@ func (c TLSServerSetting) LoadTLSConfig() (*tls.Config, error) {
153175
}
154176
return tlsCfg, nil
155177
}
178+
179+
func convertVersion(v string) (uint16, error) {
180+
if v == "" {
181+
return 0, nil // default
182+
}
183+
val, ok := tlsVersions[v]
184+
if !ok {
185+
return 0, fmt.Errorf("unsupported TLS version: %q", v)
186+
}
187+
return val, nil
188+
}
189+
190+
var tlsVersions = map[string]uint16{
191+
"1.0": tls.VersionTLS10,
192+
"1.1": tls.VersionTLS11,
193+
"1.2": tls.VersionTLS12,
194+
"1.3": tls.VersionTLS13,
195+
}

config/configtls/configtls_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,27 @@ func TestOptionsToConfig(t *testing.T) {
107107
CAFile: "testdata/testCA.pem",
108108
},
109109
},
110+
{
111+
name: "should pass with valid min and max version",
112+
options: TLSSetting{
113+
MinVersion: "1.1",
114+
MaxVersion: "1.2",
115+
},
116+
},
117+
{
118+
name: "should pass with invalid min",
119+
options: TLSSetting{
120+
MinVersion: "1.7",
121+
},
122+
expectError: "invalid TLS min_",
123+
},
124+
{
125+
name: "should pass with invalid max",
126+
options: TLSSetting{
127+
MaxVersion: "1.7",
128+
},
129+
expectError: "invalid TLS max_",
130+
},
110131
}
111132

112133
for _, test := range tests {

0 commit comments

Comments
 (0)