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

Make Intermediate CA optional in TLS configuration #166

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 9 additions & 5 deletions tls.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,19 @@ func (c *TLSConfig) TLSConfig() (*tls.Config, error) {
return nil, fmt.Errorf("Error while reading Root CA file: " + c.RootCAPath + " error: " + err.Error())
}

caCertPool := x509.NewCertPool()
if ok := caCertPool.AppendCertsFromPEM(rootCA); !ok {
return nil, fmt.Errorf("failed to append Root CA certificates from file: %s", c.RootCAPath)
}

interCA, err := os.ReadFile(c.IntermediateCAPath)
if err != nil {
return nil, fmt.Errorf("Error while reading Intermediate CA file: " + c.IntermediateCAPath + " error: " + err.Error())
fmt.Printf("Warning: Unable to read Intermediate CA file: %s, error: %v", c.IntermediateCAPath, err)
keremdndr marked this conversation as resolved.
Show resolved Hide resolved
fmt.Println("Intermediate CA will be skipped.")
keremdndr marked this conversation as resolved.
Show resolved Hide resolved
} else if ok := caCertPool.AppendCertsFromPEM(interCA); !ok {
fmt.Printf("Warning: Failed to append Intermediate CA certificates from file: %s", c.IntermediateCAPath)
}

caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(rootCA)
caCertPool.AppendCertsFromPEM(interCA)

return &tls.Config{RootCAs: caCertPool}, nil //nolint:gosec
}

Expand Down
29 changes: 29 additions & 0 deletions tls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,31 @@ import (
"testing"
)

const testRootCA = `-----BEGIN CERTIFICATE-----
MIIECTCCA3KgAwIBAgIUDnU7Oa0fU9GFOwU7EWJP3HsRchEwDQYJKoZIhvcNAQEL
BQ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HREEFTATggtleGFtcGxlLmNvbYcEfwAAATAdBgNV
HSUEFjAUBggrBgEFBQcDAQYIKwYBBQUHAwIwDQYJKoZIhvcNAQELBQADgYEAuIC/
svWDlVGBan5BhynXw8nGm2DkZaEElx0bO+kn+kPWiWo8nr8o0XU3IfMNZBeyoy2D
Uv9X8EKpSKrYhOoNgAVxCqojtGzG1n8TSvSCueKBrkaMWfvDjG1b8zLshvBu2ip4
q/I2+0j6dAkOGcK/68z7qQXByeGri3n28a1Kn6o=
-----END CERTIFICATE-----`

func TestTLSConfig_TLSConfig(t *testing.T) {
// Given
rootca, err := os.CreateTemp("", "rootca*.pem")
Expand All @@ -19,6 +44,10 @@ func TestTLSConfig_TLSConfig(t *testing.T) {
}
defer os.Remove(intermediate.Name())

if err := os.WriteFile(rootca.Name(), []byte(testRootCA), 0644); err != nil {
t.Fatalf("Error writing root CA to file: %s", err.Error())
}

tlsCfg := TLSConfig{
RootCAPath: rootca.Name(),
IntermediateCAPath: intermediate.Name(),
Expand Down