Skip to content

Commit c5614be

Browse files
committed
OCPBUGS-66064: use max TLS version only when defined
This commit ensures that the TLS max version is set only when explicitly configured from the command-line (or environment variable). In the previous version, the binary always defaulted to TLS Version 1.2 and it created an issue with the "modern" TLS profile which defines 1.3 as the minimum TLS (e.g. min version > max version). Signed-off-by: Simon Pasquier <[email protected]>
1 parent c89fe68 commit c5614be

File tree

3 files changed

+55
-6
lines changed

3 files changed

+55
-6
lines changed

cmd/plugin-backend.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ var (
2323
logLevelArg = flag.String("log-level", logrus.InfoLevel.String(), "verbosity of logs\noptions: ['panic', 'fatal', 'error', 'warn', 'info', 'debug', 'trace']\n'trace' level will log all incoming requests\n(default 'error')")
2424
alertmanagerUrlArg = flag.String("alertmanager", "", "alertmanager url to proxy to for acm mode")
2525
thanosQuerierUrlArg = flag.String("thanos-querier", "", "thanos querier url to proxy to for acm mode")
26-
tlsMinVersionArg = flag.String("tls-min-version", "", "minimum TLS version\noptions: ['VersionTLS10', 'VersionTLS11', 'VersionTLS12', 'VersionTLS13']\n(default 'VersionTLS12')")
26+
tlsMinVersionArg = flag.String("tls-min-version", "VersionTLS12", "minimum TLS version\noptions: ['VersionTLS10', 'VersionTLS11', 'VersionTLS12', 'VersionTLS13']")
2727
tlsMaxVersionArg = flag.String("tls-max-version", "", "maximum TLS version\noptions: ['VersionTLS10', 'VersionTLS11', 'VersionTLS12', 'VersionTLS13']\n(default is the highest supported by Go)")
2828
tlsCipherSuitesArg = flag.String("tls-cipher-suites", "", "comma-separated list of cipher suites for the server\nvalues are from tls package constants (https://golang.org/pkg/crypto/tls/#pkg-constants)")
2929
log = logrus.WithField("module", "main")
@@ -62,10 +62,17 @@ func main() {
6262

6363
log.Infof("enabled features: %+q\n", featuresList)
6464

65-
// Parse TLS configuration
65+
// Parse the TLS configuration.
6666
tlsMinVer := parseTLSVersion(tlsMinVersion)
67+
log.Infof("Min TLS version: %q", tls.VersionName(tlsMinVer))
6768
tlsMaxVer := parseTLSVersion(tlsMaxVersion)
69+
if tlsMaxVer != 0 {
70+
log.Infof("Max TLS version: %q", tls.VersionName(tlsMaxVer))
71+
}
6872
tlsCiphers := parseCipherSuites(tlsCipherSuites)
73+
if tlsCipherSuites != "" {
74+
log.Infof("TLS ciphers: %q", tlsCipherSuites)
75+
}
6976

7077
srv, err := server.CreateServer(context.Background(), &server.Config{
7178
Port: port,
@@ -141,11 +148,10 @@ func getTLSVersionsMap() map[string]uint16 {
141148

142149
func parseTLSVersion(version string) uint16 {
143150
if version == "" {
144-
return tls.VersionTLS12
151+
return 0
145152
}
146153

147154
tlsVersions := getTLSVersionsMap()
148-
149155
if v, ok := tlsVersions[version]; ok {
150156
return v
151157
}

pkg/server.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,14 +145,20 @@ func createHTTPServer(ctx context.Context, cfg *Config) (*http.Server, error) {
145145
tlsEnabled := cfg.IsTLSEnabled()
146146
if tlsEnabled {
147147
// Set MinVersion - default to TLS 1.2 if not specified
148+
tlsConfig.MinVersion = tls.VersionTLS12
148149
if cfg.TLSMinVersion != 0 {
149150
tlsConfig.MinVersion = cfg.TLSMinVersion
150-
} else {
151-
tlsConfig.MinVersion = tls.VersionTLS12
152151
}
153152

154153
if cfg.TLSMaxVersion != 0 {
155154
tlsConfig.MaxVersion = cfg.TLSMaxVersion
155+
if tlsConfig.MaxVersion < tlsConfig.MinVersion {
156+
return nil, fmt.Errorf(
157+
"min TLS version %q greater than max TLS version %q",
158+
tls.VersionName(tlsConfig.MinVersion),
159+
tls.VersionName(tlsConfig.MaxVersion),
160+
)
161+
}
156162
}
157163

158164
if len(cfg.TLSCipherSuites) > 0 {

pkg/server_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,43 @@ const (
3434
testHostname = "127.0.0.1"
3535
)
3636

37+
func TestCreateHTTPServer(t *testing.T) {
38+
for _, tc := range []struct {
39+
cfg *Config
40+
err bool
41+
}{
42+
{
43+
// The minimum TLS version is 1.2 by default.
44+
cfg: &Config{
45+
TLSMaxVersion: tls.VersionTLS11,
46+
CertFile: "/etc/tls/server.crt",
47+
PrivateKeyFile: "/etc/tls/server.key",
48+
},
49+
err: true,
50+
},
51+
{
52+
cfg: &Config{
53+
TLSMinVersion: tls.VersionTLS13,
54+
TLSMaxVersion: tls.VersionTLS12,
55+
CertFile: "/etc/tls/server.crt",
56+
PrivateKeyFile: "/etc/tls/server.key",
57+
},
58+
err: true,
59+
},
60+
} {
61+
t.Run("", func(t *testing.T) {
62+
_, err := createHTTPServer(context.Background(), tc.cfg)
63+
if tc.err {
64+
require.Error(t, err)
65+
return
66+
}
67+
68+
require.NoError(t, err)
69+
})
70+
}
71+
72+
}
73+
3774
// startTestServer is a helper that starts a server for testing and returns
3875
// a cleanup function that should be deferred by the caller.
3976
func startTestServer(t *testing.T, conf *Config) (*PluginServer, func()) {

0 commit comments

Comments
 (0)