package isolation import ( "context" "crypto/ed25519" "crypto/rand" "crypto/x509" "crypto/x509/pkix" "encoding/pem" "math/big" "os" "path/filepath" "strings" "testing" "time" ) func TestApplyFTPTLSPolicyRejectsCertificateForAnotherDomain(t *testing.T) { root := t.TempDir() certRoot := filepath.Join(root, "cert") domain := "ftp.example.com" certDir := filepath.Join(certRoot, domain) if err := os.MkdirAll(certDir, 0700); err != nil { t.Fatal(err) } certificate, privateKey := ftpTestCertificatePair(t, "other.example.com") if err := os.WriteFile(filepath.Join(certDir, "fullchain.pem"), certificate, 0600); err != nil { t.Fatal(err) } if err := os.WriteFile(filepath.Join(certDir, "privkey.pem"), privateKey, 0600); err != nil { t.Fatal(err) } configPath := filepath.Join(root, "pure-ftpd.conf") if err := os.WriteFile(configPath, []byte("TLS 0\n"), 0600); err != nil { t.Fatal(err) } pemPath := filepath.Join(root, "pure-ftpd.pem") agent := Agent{ CertificateRoot: certRoot, PureFTPConfig: configPath, PureFTPPem: pemPath, runCommand: func(context.Context, string, ...string) error { t.Fatal("service restart must not run for a mismatched certificate") return nil }, } _, err := agent.applyFTPTLSPolicy(context.Background(), domain) if err == nil || !strings.Contains(err.Error(), "does not match") { t.Fatalf("mismatched certificate error = %v", err) } if _, statErr := os.Stat(pemPath); !os.IsNotExist(statErr) { t.Fatalf("FTP PEM was written for a mismatched certificate: %v", statErr) } config, readErr := os.ReadFile(configPath) if readErr != nil || string(config) != "TLS 0\n" { t.Fatalf("FTP config changed after rejection: %q, %v", config, readErr) } } func TestApplyFTPTLSPolicyNormalizesTLSAndRestartsDaemon(t *testing.T) { root := t.TempDir() certRoot := filepath.Join(root, "cert") domain := "ftp.example.com" certDir := filepath.Join(certRoot, domain) if err := os.MkdirAll(certDir, 0700); err != nil { t.Fatal(err) } certificate, privateKey := ftpTestCertificatePair(t, domain) if err := os.WriteFile(filepath.Join(certDir, "fullchain.pem"), certificate, 0600); err != nil { t.Fatal(err) } if err := os.WriteFile(filepath.Join(certDir, "privkey.pem"), privateKey, 0600); err != nil { t.Fatal(err) } configPath := filepath.Join(root, "pure-ftpd.conf") if err := os.WriteFile(configPath, []byte("NoAnonymous yes\r\nTLS 1\r\nTLS 0\n"), 0600); err != nil { t.Fatal(err) } commands := make([]string, 0, 1) agent := Agent{ CertificateRoot: certRoot, PureFTPConfig: configPath, PureFTPPem: filepath.Join(root, "pure-ftpd.pem"), runCommand: func(_ context.Context, name string, args ...string) error { commands = append(commands, name+" "+strings.Join(args, " ")) return nil }, } policy, err := agent.applyFTPTLSPolicy(context.Background(), domain) if err != nil || policy != "explicit_tls_required" { t.Fatalf("policy = %q, error = %v", policy, err) } config, err := os.ReadFile(configPath) if err != nil { t.Fatal(err) } matches := pureFTPTLSLine.FindAll(config, -1) if strings.Count(string(config), "TLS 2") != 1 || len(matches) != 1 { t.Fatalf("TLS directives were not normalized: %q", config) } wantedCertFile := "CertFile " + filepath.Join(root, "pure-ftpd.pem") if strings.Count(string(config), wantedCertFile) != 1 || len(pureFTPCertFileLine.FindAll(config, -1)) != 1 { t.Fatalf("certificate file directive was not normalized: %q", config) } if len(commands) != 2 || commands[0] != "systemctl stop pure-ftpd.service" || commands[1] != "systemctl start pure-ftpd.service" { t.Fatalf("Pure-FTPd was not fully restarted: %#v", commands) } } func ftpTestCertificatePair(t *testing.T, domain string) ([]byte, []byte) { t.Helper() publicKey, privateKey, err := ed25519.GenerateKey(rand.Reader) if err != nil { t.Fatal(err) } now := time.Now().UTC() template := &x509.Certificate{ SerialNumber: big.NewInt(1), Subject: pkix.Name{CommonName: domain}, DNSNames: []string{domain}, NotBefore: now.Add(-time.Hour), NotAfter: now.Add(time.Hour), KeyUsage: x509.KeyUsageDigitalSignature, ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}, } der, err := x509.CreateCertificate(rand.Reader, template, template, publicKey, privateKey) if err != nil { t.Fatal(err) } keyDER, err := x509.MarshalPKCS8PrivateKey(privateKey) if err != nil { t.Fatal(err) } return pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: der}), pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: keyDER}) }