generated from nginxinc/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Improve enum readability - Improve TLS Mode configuration validation and application - Better error message
- Loading branch information
Showing
6 changed files
with
163 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright 2023 F5 Inc. All rights reserved. | ||
* Use of this source code is governed by the Apache License that can be found in the LICENSE file. | ||
*/ | ||
|
||
package configuration | ||
|
||
const ( | ||
NoTLS TLSMode = iota | ||
CertificateAuthorityTLS | ||
CertificateAuthorityMutualTLS | ||
SelfSignedTLS | ||
SelfSignedMutualTLS | ||
) | ||
|
||
const ( | ||
NoTLSString = "no-tls" | ||
CertificateAuthorityTLSString = "ca-tls" | ||
CertificateAuthorityMutualTLSString = "ca-mtls" | ||
SelfSignedTLSString = "ss-tls" | ||
SelfSignedMutualTLSString = "ss-mtls" | ||
) | ||
|
||
type TLSMode int | ||
|
||
var TLSModeMap = map[string]TLSMode{ | ||
NoTLSString: NoTLS, | ||
CertificateAuthorityTLSString: CertificateAuthorityTLS, | ||
CertificateAuthorityMutualTLSString: CertificateAuthorityMutualTLS, | ||
SelfSignedTLSString: SelfSignedTLS, | ||
SelfSignedMutualTLSString: SelfSignedMutualTLS, | ||
} | ||
|
||
func (t TLSMode) String() string { | ||
modes := []string{ | ||
NoTLSString, | ||
CertificateAuthorityTLSString, | ||
CertificateAuthorityMutualTLSString, | ||
SelfSignedTLSString, | ||
SelfSignedMutualTLSString, | ||
} | ||
if t < NoTLS || t > SelfSignedMutualTLS { | ||
return "" | ||
} | ||
return modes[t] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright 2023 F5 Inc. All rights reserved. | ||
* Use of this source code is governed by the Apache License that can be found in the LICENSE file. | ||
*/ | ||
|
||
package configuration | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func Test_String(t *testing.T) { | ||
mode := NoTLS.String() | ||
if mode != "no-tls" { | ||
t.Errorf("Expected TLSModeNoTLS to be 'no-tls', got '%s'", mode) | ||
} | ||
|
||
mode = CertificateAuthorityTLS.String() | ||
if mode != "ca-tls" { | ||
t.Errorf("Expected TLSModeCaTLS to be 'ca-tls', got '%s'", mode) | ||
} | ||
|
||
mode = CertificateAuthorityMutualTLS.String() | ||
if mode != "ca-mtls" { | ||
t.Errorf("Expected TLSModeCaMTLS to be 'ca-mtls', got '%s'", mode) | ||
} | ||
|
||
mode = SelfSignedTLS.String() | ||
if mode != "ss-tls" { | ||
t.Errorf("Expected TLSModeSsTLS to be 'ss-tls', got '%s'", mode) | ||
} | ||
|
||
mode = SelfSignedMutualTLS.String() | ||
if mode != "ss-mtls" { | ||
t.Errorf("Expected TLSModeSsMTLS to be 'ss-mtls', got '%s',", mode) | ||
} | ||
|
||
mode = TLSMode(5).String() | ||
if mode != "" { | ||
t.Errorf("Expected TLSMode(5) to be '', got '%s'", mode) | ||
} | ||
} | ||
|
||
func Test_TLSModeMap(t *testing.T) { | ||
mode := TLSModeMap["no-tls"] | ||
if mode != NoTLS { | ||
t.Errorf("Expected TLSModeMap['no-tls'] to be TLSModeNoTLS, got '%d'", mode) | ||
} | ||
|
||
mode = TLSModeMap["ca-tls"] | ||
if mode != CertificateAuthorityTLS { | ||
t.Errorf("Expected TLSModeMap['ca-tls'] to be TLSModeCaTLS, got '%d'", mode) | ||
} | ||
|
||
mode = TLSModeMap["ca-mtls"] | ||
if mode != CertificateAuthorityMutualTLS { | ||
t.Errorf("Expected TLSModeMap['ca-mtls'] to be TLSModeCaMTLS, got '%d'", mode) | ||
} | ||
|
||
mode = TLSModeMap["ss-tls"] | ||
if mode != SelfSignedTLS { | ||
t.Errorf("Expected TLSModeMap['ss-tls'] to be TLSModeSsTLS, got '%d'", mode) | ||
} | ||
|
||
mode = TLSModeMap["ss-mtls"] | ||
if mode != SelfSignedMutualTLS { | ||
t.Errorf("Expected TLSModeMap['ss-mtls'] to be TLSModeSsMTLS, got '%d'", mode) | ||
} | ||
|
||
mode = TLSModeMap["invalid"] | ||
if mode != TLSMode(0) { | ||
t.Errorf("Expected TLSModeMap['invalid'] to be TLSMode(0), got '%d'", mode) | ||
} | ||
} |