Skip to content
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

[tlscommon] Make genTest and getFingerPrint methods public #268

Merged
merged 3 commits into from
Jan 28, 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
2 changes: 1 addition & 1 deletion transport/tlscommon/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
// Config defines the user configurable options in the yaml file.
type Config struct {
Enabled *bool `config:"enabled" yaml:"enabled,omitempty"`
VerificationMode TLSVerificationMode `config:"verification_mode" yaml:"verification_mode"` // one of 'none', 'full'
VerificationMode TLSVerificationMode `config:"verification_mode" yaml:"verification_mode"` // one of 'none', 'full', 'certificate' and 'strict'
Versions []TLSVersion `config:"supported_protocols" yaml:"supported_protocols,omitempty"`
CipherSuites []CipherSuite `config:"cipher_suites" yaml:"cipher_suites,omitempty"`
CAs []string `config:"certificate_authorities" yaml:"certificate_authorities,omitempty"`
Expand Down
2 changes: 1 addition & 1 deletion transport/tlscommon/tls_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func (c *TLSConfig) BuildModuleClientConfig(host string) *tls.Config {
// because all slice/pointer fields won't be modified.
cc := *c

// Keep a copy of the host (wheather an IP or hostname)
// Keep a copy of the host (whether an IP or hostname)
// for later validation. It is used by makeVerifyConnection
cc.ServerName = host
config := cc.ToConfig()
Expand Down
16 changes: 9 additions & 7 deletions transport/tlscommon/tls_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import (
)

func TestMakeVerifyServerConnection(t *testing.T) {
testCerts := genTestCerts(t)
testCerts := GenTestCerts(t)

certPool := x509.NewCertPool()
certPool.AddCert(testCerts["ca"])
Expand Down Expand Up @@ -192,13 +192,13 @@ func TestMakeVerifyServerConnection(t *testing.T) {
}

func TestTrustRootCA(t *testing.T) {
certs := genTestCerts(t)
certs := GenTestCerts(t)

nonEmptyCertPool := x509.NewCertPool()
nonEmptyCertPool.AddCert(certs["wildcard"])
nonEmptyCertPool.AddCert(certs["unknown_authority"])

fingerprint := getFingerprint(certs["ca"])
fingerprint := GetCertFingerprint(certs["ca"])

testCases := []struct {
name string
Expand Down Expand Up @@ -267,8 +267,8 @@ func TestTrustRootCA(t *testing.T) {
}

func TestMakeVerifyConnectionUsesCATrustedFingerprint(t *testing.T) {
testCerts := genTestCerts(t)
fingerprint := getFingerprint(testCerts["ca"])
testCerts := GenTestCerts(t)
fingerprint := GetCertFingerprint(testCerts["ca"])

testcases := map[string]struct {
verificationMode TLSVerificationMode
Expand Down Expand Up @@ -684,12 +684,14 @@ func startTestServer(t *testing.T, serverAddr string, serverCerts []tls.Certific
return *serverURL
}

func getFingerprint(cert *x509.Certificate) string {
// GetCertFingerPrint takes a certificate and returns its HEX encoded SHA-256
func GetCertFingerprint(cert *x509.Certificate) string {
caSHA256 := sha256.Sum256(cert.Raw)
return hex.EncodeToString(caSHA256[:])
}

func genTestCerts(t *testing.T) map[string]*x509.Certificate {
func GenTestCerts(t *testing.T) map[string]*x509.Certificate {
mauri870 marked this conversation as resolved.
Show resolved Hide resolved
t.Helper()
ca, err := genCA()
if err != nil {
t.Fatalf("cannot generate root CA: %s", err)
Expand Down
2 changes: 1 addition & 1 deletion transport/tlscommon/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ var tlsClientAuthTypes = map[string]TLSClientAuth{
}

// TLSVerificationMode represents the type of verification to do on the remote host:
// `none`, `certificate`, and `full` and we default to `full`.
// `none`, `certificate`, `full` and `strict` - we default to `full`.
// Internally this option is transformed into the `insecure` field in the `tls.Config` struct.
type TLSVerificationMode uint8

Expand Down
Loading