feat: configure MariaDB TLS for setup and runtime connections (#5)
This commit is contained in:
1 parent
15f81e053c
commit
370407d3ba
9 files changed
+325
-9
No files matched your search
@@ -0,0 +1,71 @@
|
||||
package mariadb
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
mysql "github.com/go-sql-driver/mysql"
|
||||
)
|
||||
|
||||
// ConfigureTLS configures the DSN so setup and runtime use the same TLS policy.
|
||||
func ConfigureTLS(cfg *mysql.Config, mode, caPEM string) error {
|
||||
mode = strings.TrimSpace(mode)
|
||||
if mode == "" {
|
||||
mode = "preferred"
|
||||
}
|
||||
caPEM = strings.TrimSpace(caPEM)
|
||||
if caPEM != "" && mode != "verify-ca" && mode != "verify-full" {
|
||||
return fmt.Errorf("CA certificate requires verify-ca or verify-full")
|
||||
}
|
||||
switch mode {
|
||||
case "preferred":
|
||||
cfg.TLSConfig = "preferred"
|
||||
return nil
|
||||
case "required":
|
||||
cfg.TLSConfig = "skip-verify"
|
||||
return nil
|
||||
case "verify-full":
|
||||
if caPEM == "" {
|
||||
cfg.TLSConfig = "true"
|
||||
return nil
|
||||
}
|
||||
case "verify-ca":
|
||||
if caPEM == "" {
|
||||
return fmt.Errorf("verify-ca requires a CA certificate")
|
||||
}
|
||||
default:
|
||||
return fmt.Errorf("unsupported MariaDB TLS mode")
|
||||
}
|
||||
roots := x509.NewCertPool()
|
||||
if !roots.AppendCertsFromPEM([]byte(caPEM)) {
|
||||
return fmt.Errorf("invalid CA certificate PEM")
|
||||
}
|
||||
tlsConfig := &tls.Config{MinVersion: tls.VersionTLS12, RootCAs: roots}
|
||||
if mode == "verify-ca" {
|
||||
// Verify the certificate chain explicitly; this mode omits hostname checks.
|
||||
tlsConfig.InsecureSkipVerify = true
|
||||
tlsConfig.VerifyConnection = func(state tls.ConnectionState) error {
|
||||
if len(state.PeerCertificates) == 0 {
|
||||
return fmt.Errorf("MariaDB did not provide a certificate")
|
||||
}
|
||||
intermediates := x509.NewCertPool()
|
||||
for _, cert := range state.PeerCertificates[1:] {
|
||||
intermediates.AddCert(cert)
|
||||
}
|
||||
_, err := state.PeerCertificates[0].Verify(x509.VerifyOptions{Roots: roots, Intermediates: intermediates, KeyUsages: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}})
|
||||
if err != nil {
|
||||
return fmt.Errorf("verify MariaDB certificate: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
name := fmt.Sprintf("qfs-%x", sha256.Sum256([]byte(mode+"\n"+caPEM)))
|
||||
if err := mysql.RegisterTLSConfig(name, tlsConfig); err != nil {
|
||||
return fmt.Errorf("register MariaDB TLS: %w", err)
|
||||
}
|
||||
cfg.TLSConfig = name
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
package mariadb
|
||||
|
||||
import (
|
||||
"crypto/ecdsa"
|
||||
"crypto/elliptic"
|
||||
"crypto/rand"
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"crypto/x509/pkix"
|
||||
"encoding/pem"
|
||||
"math/big"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
mysql "github.com/go-sql-driver/mysql"
|
||||
)
|
||||
|
||||
func TestTLSPolicy(t *testing.T) {
|
||||
ca, cert := testCertificate(t)
|
||||
_, other := testCertificate(t)
|
||||
for _, tt := range []struct {
|
||||
mode, ca string
|
||||
wantError, fallback, insecure bool
|
||||
}{
|
||||
{"", "", false, true, true},
|
||||
{"preferred", "", false, true, true},
|
||||
{"required", "", false, false, true},
|
||||
{"verify-full", "", false, false, false},
|
||||
{"verify-full", ca, false, false, false},
|
||||
{"verify-ca", ca, false, false, true},
|
||||
{"verify-ca", "", true, false, false},
|
||||
{"verify-full", "invalid", true, false, false},
|
||||
{"preferred", ca, true, false, false},
|
||||
{"false", "", true, false, false},
|
||||
} {
|
||||
t.Run(tt.mode+tt.ca[:min(len(tt.ca), 5)], func(t *testing.T) {
|
||||
cfg := mysql.NewConfig()
|
||||
cfg.Net, cfg.Addr = "tcp", "db.example:3306"
|
||||
err := ConfigureTLS(cfg, tt.mode, tt.ca)
|
||||
if (err != nil) != tt.wantError {
|
||||
t.Fatalf("configure: %v", err)
|
||||
}
|
||||
if tt.wantError {
|
||||
return
|
||||
}
|
||||
parsed, err := mysql.ParseDSN(cfg.FormatDSN())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if parsed.TLS == nil || parsed.AllowFallbackToPlaintext != tt.fallback || parsed.TLS.InsecureSkipVerify != tt.insecure {
|
||||
t.Fatalf("unexpected TLS policy for %s", tt.mode)
|
||||
}
|
||||
if tt.mode == "verify-ca" {
|
||||
if err := parsed.TLS.VerifyConnection(tls.ConnectionState{PeerCertificates: []*x509.Certificate{cert}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, chain := range [][]*x509.Certificate{nil, {other}} {
|
||||
if err := parsed.TLS.VerifyConnection(tls.ConnectionState{PeerCertificates: chain}); err == nil {
|
||||
t.Fatal("untrusted certificate accepted")
|
||||
}
|
||||
}
|
||||
}
|
||||
if tt.mode == "verify-full" && tt.ca != "" {
|
||||
if parsed.TLS.ServerName != "db.example" {
|
||||
t.Fatalf("missing hostname verification: %s", parsed.TLS.ServerName)
|
||||
}
|
||||
if _, err := cert.Verify(x509.VerifyOptions{Roots: parsed.TLS.RootCAs, DNSName: parsed.TLS.ServerName}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := other.Verify(x509.VerifyOptions{Roots: parsed.TLS.RootCAs, DNSName: parsed.TLS.ServerName}); err == nil {
|
||||
t.Fatal("untrusted certificate accepted")
|
||||
}
|
||||
if _, err := cert.Verify(x509.VerifyOptions{Roots: parsed.TLS.RootCAs, DNSName: "other.example"}); err == nil {
|
||||
t.Fatal("wrong hostname accepted")
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func testCertificate(t *testing.T) (string, *x509.Certificate) {
|
||||
t.Helper()
|
||||
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
template := &x509.Certificate{SerialNumber: big.NewInt(1), Subject: pkix.Name{CommonName: "example"}, DNSNames: []string{"db.example"}, NotBefore: time.Now().Add(-time.Hour), NotAfter: time.Now().Add(time.Hour), IsCA: true, BasicConstraintsValid: true, KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageDigitalSignature, ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}}
|
||||
der, err := x509.CreateCertificate(rand.Reader, template, template, &key.PublicKey, key)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cert, err := x509.ParseCertificate(der)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return string(pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: der})), cert
|
||||
}
|
||||
Reference in new issue
Block a user