129 lines
4.3 KiB
Go
129 lines
4.3 KiB
Go
//go:build integration
|
|
|
|
package mariadb
|
|
|
|
import (
|
|
"context"
|
|
"crypto/rand"
|
|
"crypto/rsa"
|
|
"crypto/x509"
|
|
"crypto/x509/pkix"
|
|
"database/sql"
|
|
"encoding/pem"
|
|
"math/big"
|
|
"net"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
mysql "github.com/go-sql-driver/mysql"
|
|
)
|
|
|
|
func TestMariaDBTLSIntegration(t *testing.T) {
|
|
dir := t.TempDir()
|
|
if err := os.Chmod(dir, 0755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
key, err := rsa.GenerateKey(rand.Reader, 2048)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
cert := &x509.Certificate{SerialNumber: big.NewInt(1), Subject: pkix.Name{CommonName: "example"},
|
|
NotBefore: time.Now().Add(-time.Hour), NotAfter: time.Now().Add(time.Hour),
|
|
IsCA: true, BasicConstraintsValid: true, KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment,
|
|
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}, IPAddresses: []net.IP{net.ParseIP("127.0.0.1")}}
|
|
der, err := x509.CreateCertificate(rand.Reader, cert, cert, &key.PublicKey, key)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ca := string(pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: der}))
|
|
for name, data := range map[string][]byte{"server.crt": []byte(ca), "server.key": pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(key)})} {
|
|
// Docker's mysql user must read these disposable test files.
|
|
if err := os.WriteFile(filepath.Join(dir, name), data, 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
otherCA, _ := testCertificate(t)
|
|
for _, tlsEnabled := range []bool{true, false} {
|
|
t.Run(map[bool]string{true: "TLS", false: "plaintext"}[tlsEnabled], func(t *testing.T) {
|
|
args := []string{"run", "--detach", "--rm", "--pull=never", "-p", "127.0.0.1::3306",
|
|
"-e", "MARIADB_ALLOW_EMPTY_ROOT_PASSWORD=1", "-e", "MARIADB_ROOT_HOST=%", "-v", dir + ":/qfs-tls:ro", "mariadb:11.8"}
|
|
if tlsEnabled {
|
|
args = append(args, "--ssl-cert=/qfs-tls/server.crt", "--ssl-key=/qfs-tls/server.key")
|
|
} else {
|
|
args = append(args, "--skip-ssl")
|
|
}
|
|
out, err := exec.Command("docker", args...).CombinedOutput()
|
|
if err != nil {
|
|
t.Fatalf("start disposable MariaDB: %v: %s", err, out)
|
|
}
|
|
id := strings.TrimSpace(string(out))
|
|
t.Cleanup(func() {
|
|
if out, err := exec.Command("docker", "stop", "-t", "2", id).CombinedOutput(); err != nil {
|
|
t.Errorf("stop test MariaDB: %v: %s", err, out)
|
|
}
|
|
})
|
|
out, err = exec.Command("docker", "port", id, "3306/tcp").CombinedOutput()
|
|
if err != nil {
|
|
t.Fatalf("test MariaDB port: %v: %s", err, out)
|
|
}
|
|
addr := strings.TrimSpace(string(out))
|
|
connect := func(mode, roots, serverName string) (string, error) {
|
|
cfg := mysql.NewConfig()
|
|
cfg.User, cfg.Net, cfg.Addr, cfg.Timeout = "root", "tcp", addr, time.Second
|
|
if err := ConfigureTLS(cfg, mode, roots); err != nil {
|
|
return "", err
|
|
}
|
|
cfg, err = mysql.ParseDSN(cfg.FormatDSN())
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if serverName != "" {
|
|
cfg.TLS.ServerName = serverName
|
|
}
|
|
connector, err := mysql.NewConnector(cfg)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
db := sql.OpenDB(connector)
|
|
defer db.Close()
|
|
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
|
defer cancel()
|
|
var name, cipher string
|
|
err = db.QueryRowContext(ctx, "SHOW SESSION STATUS LIKE 'Ssl_cipher'").Scan(&name, &cipher)
|
|
return cipher, err
|
|
}
|
|
deadline := time.Now().Add(45 * time.Second)
|
|
for {
|
|
if _, err := connect("preferred", "", ""); err == nil {
|
|
break
|
|
}
|
|
if time.Now().After(deadline) {
|
|
t.Fatal("test MariaDB did not become ready")
|
|
}
|
|
time.Sleep(100 * time.Millisecond)
|
|
}
|
|
for _, tt := range []struct {
|
|
mode, roots, name string
|
|
success bool
|
|
}{
|
|
{"preferred", "", "", true}, {"required", "", "", tlsEnabled},
|
|
{"verify-ca", ca, "", tlsEnabled}, {"verify-full", ca, "", tlsEnabled},
|
|
{"verify-ca", otherCA, "", false}, {"verify-full", otherCA, "", false},
|
|
{"verify-full", ca, "wrong.example", false},
|
|
} {
|
|
cipher, err := connect(tt.mode, tt.roots, tt.name)
|
|
if (err == nil) != tt.success {
|
|
t.Errorf("mode=%s name=%s success=%v error=%v", tt.mode, tt.name, tt.success, err)
|
|
}
|
|
if err == nil && (cipher != "") != tlsEnabled {
|
|
t.Errorf("mode=%s cipher=%q TLS=%v", tt.mode, cipher, tlsEnabled)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|