Skip to content

Commit e5790ee

Browse files
committed
TLS Mode is now an enum
- Improve enum readability - Improve TLS Mode configuration validation and application - Better error message
1 parent 360536b commit e5790ee

File tree

6 files changed

+163
-46
lines changed

6 files changed

+163
-46
lines changed

cmd/tls-config-factory-test-harness/main.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func ssTlsConfig() configuration.Settings {
8787
certificates[ClientCertificateSecretKey] = buildClientCertificateEntry(clientKeyPEM(), clientCertificatePEM())
8888

8989
return configuration.Settings{
90-
TlsMode: "ss-tls",
90+
TlsMode: configuration.SelfSignedTLS,
9191
Certificates: &certification.Certificates{
9292
Certificates: certificates,
9393
},
@@ -100,7 +100,7 @@ func ssMtlsConfig() configuration.Settings {
100100
certificates[ClientCertificateSecretKey] = buildClientCertificateEntry(clientKeyPEM(), clientCertificatePEM())
101101

102102
return configuration.Settings{
103-
TlsMode: "ss-mtls",
103+
TlsMode: configuration.SelfSignedMutualTLS,
104104
Certificates: &certification.Certificates{
105105
Certificates: certificates,
106106
},
@@ -109,7 +109,7 @@ func ssMtlsConfig() configuration.Settings {
109109

110110
func caTlsConfig() configuration.Settings {
111111
return configuration.Settings{
112-
TlsMode: "ca-tls",
112+
TlsMode: configuration.CertificateAuthorityTLS,
113113
}
114114
}
115115

@@ -118,7 +118,7 @@ func caMtlsConfig() configuration.Settings {
118118
certificates[ClientCertificateSecretKey] = buildClientCertificateEntry(clientKeyPEM(), clientCertificatePEM())
119119

120120
return configuration.Settings{
121-
TlsMode: "ca-mtls",
121+
TlsMode: configuration.CertificateAuthorityMutualTLS,
122122
Certificates: &certification.Certificates{
123123
Certificates: certificates,
124124
},

internal/authentication/factory.go

+10-6
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,24 @@ import (
2020
func NewTlsConfig(settings *configuration.Settings) (*tls.Config, error) {
2121
logrus.Debugf("authentication::NewTlsConfig Creating TLS config for mode: '%s'", settings.TlsMode)
2222
switch settings.TlsMode {
23-
case "ss-tls": // needs ca cert
23+
24+
case configuration.NoTLS:
25+
return buildBasicTlsConfig(true), nil
26+
27+
case configuration.SelfSignedTLS: // needs ca cert
2428
return buildSelfSignedTlsConfig(settings.Certificates)
2529

26-
case "ss-mtls": // needs ca cert and client cert
30+
case configuration.SelfSignedMutualTLS: // needs ca cert and client cert
2731
return buildSelfSignedMtlsConfig(settings.Certificates)
2832

29-
case "ca-tls": // needs nothing
33+
case configuration.CertificateAuthorityTLS: // needs nothing
3034
return buildBasicTlsConfig(false), nil
3135

32-
case "ca-mtls": // needs client cert
36+
case configuration.CertificateAuthorityMutualTLS: // needs client cert
3337
return buildCaTlsConfig(settings.Certificates)
3438

35-
default: // no-tls, needs nothing
36-
return buildBasicTlsConfig(true), nil
39+
default:
40+
return nil, fmt.Errorf("unknown TLS mode: %s", settings.TlsMode)
3741
}
3842
}
3943

internal/authentication/factory_test.go

+9-28
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,6 @@ const (
1616
ClientCertificateSecretKey = "nlk-tls-client-secret"
1717
)
1818

19-
func TestTlsFactory_EmptyStringModeDefaultsToNoTls(t *testing.T) {
20-
settings := configuration.Settings{
21-
TlsMode: "",
22-
}
23-
24-
tlsConfig, err := NewTlsConfig(&settings)
25-
if err != nil {
26-
t.Fatalf(`Unexpected error: %v`, err)
27-
}
28-
29-
if tlsConfig == nil {
30-
t.Fatalf(`tlsConfig should not be nil`)
31-
}
32-
33-
if tlsConfig.InsecureSkipVerify != true {
34-
t.Fatalf(`tlsConfig.InsecureSkipVerify should be true`)
35-
}
36-
}
37-
3819
func TestTlsFactory_UnspecifiedModeDefaultsToNoTls(t *testing.T) {
3920
settings := configuration.Settings{}
4021

@@ -57,7 +38,7 @@ func TestTlsFactory_SelfSignedTlsMode(t *testing.T) {
5738
certificates[CaCertificateSecretKey] = buildCaCertificateEntry(caCertificatePEM())
5839

5940
settings := configuration.Settings{
60-
TlsMode: "ss-tls",
41+
TlsMode: configuration.SelfSignedTLS,
6142
Certificates: &certification.Certificates{
6243
Certificates: certificates,
6344
CaCertificateSecretKey: CaCertificateSecretKey,
@@ -92,7 +73,7 @@ func TestTlsFactory_SelfSignedTlsModeCertPoolError(t *testing.T) {
9273
certificates[CaCertificateSecretKey] = buildCaCertificateEntry(invalidCertificatePEM())
9374

9475
settings := configuration.Settings{
95-
TlsMode: "ss-tls",
76+
TlsMode: configuration.SelfSignedTLS,
9677
Certificates: &certification.Certificates{
9778
Certificates: certificates,
9879
},
@@ -113,7 +94,7 @@ func TestTlsFactory_SelfSignedTlsModeCertPoolCertificateParseError(t *testing.T)
11394
certificates[CaCertificateSecretKey] = buildCaCertificateEntry(invalidCertificateDataPEM())
11495

11596
settings := configuration.Settings{
116-
TlsMode: "ss-tls",
97+
TlsMode: configuration.SelfSignedTLS,
11798
Certificates: &certification.Certificates{
11899
Certificates: certificates,
119100
CaCertificateSecretKey: CaCertificateSecretKey,
@@ -137,7 +118,7 @@ func TestTlsFactory_SelfSignedMtlsMode(t *testing.T) {
137118
certificates[ClientCertificateSecretKey] = buildClientCertificateEntry(clientKeyPEM(), clientCertificatePEM())
138119

139120
settings := configuration.Settings{
140-
TlsMode: "ss-mtls",
121+
TlsMode: configuration.SelfSignedMutualTLS,
141122
Certificates: &certification.Certificates{
142123
Certificates: certificates,
143124
CaCertificateSecretKey: CaCertificateSecretKey,
@@ -173,7 +154,7 @@ func TestTlsFactory_SelfSignedMtlsModeCertPoolError(t *testing.T) {
173154
certificates[ClientCertificateSecretKey] = buildClientCertificateEntry(clientKeyPEM(), clientCertificatePEM())
174155

175156
settings := configuration.Settings{
176-
TlsMode: "ss-mtls",
157+
TlsMode: configuration.SelfSignedMutualTLS,
177158
Certificates: &certification.Certificates{
178159
Certificates: certificates,
179160
},
@@ -195,7 +176,7 @@ func TestTlsFactory_SelfSignedMtlsModeClientCertificateError(t *testing.T) {
195176
certificates[ClientCertificateSecretKey] = buildClientCertificateEntry(clientKeyPEM(), invalidCertificatePEM())
196177

197178
settings := configuration.Settings{
198-
TlsMode: "ss-mtls",
179+
TlsMode: configuration.SelfSignedMutualTLS,
199180
Certificates: &certification.Certificates{
200181
Certificates: certificates,
201182
CaCertificateSecretKey: CaCertificateSecretKey,
@@ -215,7 +196,7 @@ func TestTlsFactory_SelfSignedMtlsModeClientCertificateError(t *testing.T) {
215196

216197
func TestTlsFactory_CaTlsMode(t *testing.T) {
217198
settings := configuration.Settings{
218-
TlsMode: "ca-tls",
199+
TlsMode: configuration.CertificateAuthorityTLS,
219200
}
220201

221202
tlsConfig, err := NewTlsConfig(&settings)
@@ -245,7 +226,7 @@ func TestTlsFactory_CaMtlsMode(t *testing.T) {
245226
certificates[ClientCertificateSecretKey] = buildClientCertificateEntry(clientKeyPEM(), clientCertificatePEM())
246227

247228
settings := configuration.Settings{
248-
TlsMode: "ca-mtls",
229+
TlsMode: configuration.CertificateAuthorityMutualTLS,
249230
Certificates: &certification.Certificates{
250231
Certificates: certificates,
251232
CaCertificateSecretKey: CaCertificateSecretKey,
@@ -281,7 +262,7 @@ func TestTlsFactory_CaMtlsModeClientCertificateError(t *testing.T) {
281262
certificates[ClientCertificateSecretKey] = buildClientCertificateEntry(clientKeyPEM(), invalidCertificatePEM())
282263

283264
settings := configuration.Settings{
284-
TlsMode: "ca-mtls",
265+
TlsMode: configuration.CertificateAuthorityMutualTLS,
285266
Certificates: &certification.Certificates{
286267
Certificates: certificates,
287268
},

internal/configuration/settings.go

+20-8
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ type Settings struct {
110110
NginxPlusHosts []string
111111

112112
// TlsMode is the value used to determine which of the five TLS modes will be used to communicate with the Border Servers (see: ../../docs/tls/README.md).
113-
TlsMode string
113+
TlsMode TLSMode
114114

115115
// Certificates is the object used to retrieve the certificates and keys used to communicate with the Border Servers.
116116
Certificates *certification.Certificates
@@ -139,7 +139,7 @@ func NewSettings(ctx context.Context, k8sClient kubernetes.Interface) (*Settings
139139
settings := &Settings{
140140
Context: ctx,
141141
K8sClient: k8sClient,
142-
TlsMode: "",
142+
TlsMode: NoTLS,
143143
Certificates: nil,
144144
Handler: HandlerSettings{
145145
RetryCount: 5,
@@ -282,13 +282,12 @@ func (s *Settings) handleUpdateEvent(_ interface{}, newValue interface{}) {
282282
logrus.Warnf("Settings::handleUpdateEvent: nginx-hosts key not found in ConfigMap")
283283
}
284284

285-
tlsMode, found := configMap.Data["tls-mode"]
286-
if found {
287-
s.TlsMode = tlsMode
288-
logrus.Debugf("Settings::handleUpdateEvent: tls-mode: %s", s.TlsMode)
285+
tlsMode, err := validateTlsMode(configMap)
286+
if err != nil {
287+
// NOTE: the TLSMode defaults to NoTLS on startup, or the last known good value if previously set.
288+
logrus.Errorf("There was an error with the configured TLS Mode. TLS Mode has NOT been changed. The current mode is: '%v'. Error: %v. ", s.TlsMode, err)
289289
} else {
290-
s.TlsMode = "no-tls"
291-
logrus.Warnf("Settings::handleUpdateEvent: tls-mode key not found in ConfigMap, defaulting to 'no-tls'")
290+
s.TlsMode = tlsMode
292291
}
293292

294293
caCertificateSecretKey, found := configMap.Data["ca-certificate"]
@@ -314,6 +313,19 @@ func (s *Settings) handleUpdateEvent(_ interface{}, newValue interface{}) {
314313
logrus.Debugf("Settings::handleUpdateEvent: \n\tHosts: %v,\n\tSettings: %v ", s.NginxPlusHosts, configMap)
315314
}
316315

316+
func validateTlsMode(configMap *corev1.ConfigMap) (TLSMode, error) {
317+
tlsConfigMode, tlsConfigModeFound := configMap.Data["tls-mode"]
318+
if !tlsConfigModeFound {
319+
return NoTLS, fmt.Errorf(`tls-mode key not found in ConfigMap`)
320+
}
321+
322+
if tlsMode, tlsModeFound := TLSModeMap[tlsConfigMode]; tlsModeFound {
323+
return tlsMode, nil
324+
}
325+
326+
return NoTLS, fmt.Errorf(`invalid tls-mode value: %s`, tlsConfigMode)
327+
}
328+
317329
func (s *Settings) parseHosts(hosts string) []string {
318330
return strings.Split(hosts, ",")
319331
}

internal/configuration/tlsmodes.go

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2023 F5 Inc. All rights reserved.
3+
* Use of this source code is governed by the Apache License that can be found in the LICENSE file.
4+
*/
5+
6+
package configuration
7+
8+
const (
9+
NoTLS TLSMode = iota
10+
CertificateAuthorityTLS
11+
CertificateAuthorityMutualTLS
12+
SelfSignedTLS
13+
SelfSignedMutualTLS
14+
)
15+
16+
const (
17+
NoTLSString = "no-tls"
18+
CertificateAuthorityTLSString = "ca-tls"
19+
CertificateAuthorityMutualTLSString = "ca-mtls"
20+
SelfSignedTLSString = "ss-tls"
21+
SelfSignedMutualTLSString = "ss-mtls"
22+
)
23+
24+
type TLSMode int
25+
26+
var TLSModeMap = map[string]TLSMode{
27+
NoTLSString: NoTLS,
28+
CertificateAuthorityTLSString: CertificateAuthorityTLS,
29+
CertificateAuthorityMutualTLSString: CertificateAuthorityMutualTLS,
30+
SelfSignedTLSString: SelfSignedTLS,
31+
SelfSignedMutualTLSString: SelfSignedMutualTLS,
32+
}
33+
34+
func (t TLSMode) String() string {
35+
modes := []string{
36+
NoTLSString,
37+
CertificateAuthorityTLSString,
38+
CertificateAuthorityMutualTLSString,
39+
SelfSignedTLSString,
40+
SelfSignedMutualTLSString,
41+
}
42+
if t < NoTLS || t > SelfSignedMutualTLS {
43+
return ""
44+
}
45+
return modes[t]
46+
}
+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright 2023 F5 Inc. All rights reserved.
3+
* Use of this source code is governed by the Apache License that can be found in the LICENSE file.
4+
*/
5+
6+
package configuration
7+
8+
import (
9+
"testing"
10+
)
11+
12+
func Test_String(t *testing.T) {
13+
mode := NoTLS.String()
14+
if mode != "no-tls" {
15+
t.Errorf("Expected TLSModeNoTLS to be 'no-tls', got '%s'", mode)
16+
}
17+
18+
mode = CertificateAuthorityTLS.String()
19+
if mode != "ca-tls" {
20+
t.Errorf("Expected TLSModeCaTLS to be 'ca-tls', got '%s'", mode)
21+
}
22+
23+
mode = CertificateAuthorityMutualTLS.String()
24+
if mode != "ca-mtls" {
25+
t.Errorf("Expected TLSModeCaMTLS to be 'ca-mtls', got '%s'", mode)
26+
}
27+
28+
mode = SelfSignedTLS.String()
29+
if mode != "ss-tls" {
30+
t.Errorf("Expected TLSModeSsTLS to be 'ss-tls', got '%s'", mode)
31+
}
32+
33+
mode = SelfSignedMutualTLS.String()
34+
if mode != "ss-mtls" {
35+
t.Errorf("Expected TLSModeSsMTLS to be 'ss-mtls', got '%s',", mode)
36+
}
37+
38+
mode = TLSMode(5).String()
39+
if mode != "" {
40+
t.Errorf("Expected TLSMode(5) to be '', got '%s'", mode)
41+
}
42+
}
43+
44+
func Test_TLSModeMap(t *testing.T) {
45+
mode := TLSModeMap["no-tls"]
46+
if mode != NoTLS {
47+
t.Errorf("Expected TLSModeMap['no-tls'] to be TLSModeNoTLS, got '%d'", mode)
48+
}
49+
50+
mode = TLSModeMap["ca-tls"]
51+
if mode != CertificateAuthorityTLS {
52+
t.Errorf("Expected TLSModeMap['ca-tls'] to be TLSModeCaTLS, got '%d'", mode)
53+
}
54+
55+
mode = TLSModeMap["ca-mtls"]
56+
if mode != CertificateAuthorityMutualTLS {
57+
t.Errorf("Expected TLSModeMap['ca-mtls'] to be TLSModeCaMTLS, got '%d'", mode)
58+
}
59+
60+
mode = TLSModeMap["ss-tls"]
61+
if mode != SelfSignedTLS {
62+
t.Errorf("Expected TLSModeMap['ss-tls'] to be TLSModeSsTLS, got '%d'", mode)
63+
}
64+
65+
mode = TLSModeMap["ss-mtls"]
66+
if mode != SelfSignedMutualTLS {
67+
t.Errorf("Expected TLSModeMap['ss-mtls'] to be TLSModeSsMTLS, got '%d'", mode)
68+
}
69+
70+
mode = TLSModeMap["invalid"]
71+
if mode != TLSMode(0) {
72+
t.Errorf("Expected TLSModeMap['invalid'] to be TLSMode(0), got '%d'", mode)
73+
}
74+
}

0 commit comments

Comments
 (0)