https_customcert_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package httpapi_test
  2. // FORK: #1756 - setting a certificate from disk through Settings.
  3. import (
  4. "crypto/ecdsa"
  5. "crypto/elliptic"
  6. "crypto/rand"
  7. "crypto/x509"
  8. "crypto/x509/pkix"
  9. "encoding/pem"
  10. "fmt"
  11. "math/big"
  12. "net"
  13. "net/http"
  14. "os"
  15. "path/filepath"
  16. "testing"
  17. "time"
  18. )
  19. func writeTestPair(t *testing.T, dir, name string) (string, string) {
  20. t.Helper()
  21. key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
  22. if err != nil {
  23. t.Fatal(err)
  24. }
  25. tmpl := &x509.Certificate{
  26. SerialNumber: big.NewInt(9),
  27. Subject: pkix.Name{CommonName: name},
  28. DNSNames: []string{name},
  29. IPAddresses: []net.IP{net.ParseIP("192.168.50.2")},
  30. NotBefore: time.Now().Add(-time.Hour),
  31. NotAfter: time.Now().Add(24 * time.Hour),
  32. KeyUsage: x509.KeyUsageDigitalSignature,
  33. ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
  34. }
  35. der, err := x509.CreateCertificate(rand.Reader, tmpl, tmpl, &key.PublicKey, key)
  36. if err != nil {
  37. t.Fatal(err)
  38. }
  39. kb, err := x509.MarshalECPrivateKey(key)
  40. if err != nil {
  41. t.Fatal(err)
  42. }
  43. certPath := filepath.Join(dir, name+".crt")
  44. keyPath := filepath.Join(dir, name+".key")
  45. if err := os.WriteFile(certPath, pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: der}), 0o600); err != nil {
  46. t.Fatal(err)
  47. }
  48. if err := os.WriteFile(keyPath, pem.EncodeToMemory(&pem.Block{Type: "EC PRIVATE KEY", Bytes: kb}), 0o600); err != nil {
  49. t.Fatal(err)
  50. }
  51. return certPath, keyPath
  52. }
  53. func TestACertificateCanBeSetFromSettings(t *testing.T) {
  54. ts, mgr := makeHTTPSTestServer(t)
  55. certPath, keyPath := writeTestPair(t, t.TempDir(), "monitor-box")
  56. st, body := httpsCall(t, ts, "POST", "/api/settings/https",
  57. fmt.Sprintf(`{"cert":%q,"key":%q,"password":"test123456"}`, certPath, keyPath), true)
  58. if st != http.StatusOK {
  59. t.Fatalf("status %d: %v", st, body)
  60. }
  61. status, _ := body["status"].(map[string]any)
  62. if status["certSource"] != "custom" {
  63. t.Fatalf("status after setting the pair: %v", body)
  64. }
  65. if got := mgr.Status(); got.CertPath != certPath || got.KeyPath != keyPath {
  66. t.Fatalf("the manager kept %+v", got)
  67. }
  68. // A pair that does not load is rejected and the current one stays.
  69. st, body = httpsCall(t, ts, "POST", "/api/settings/https",
  70. fmt.Sprintf(`{"cert":%q,"key":%q,"password":"test123456"}`,
  71. filepath.Join(t.TempDir(), "nope.crt"), filepath.Join(t.TempDir(), "nope.key")), true)
  72. if st != http.StatusBadRequest {
  73. t.Fatalf("a bad pair: status %d: %v", st, body)
  74. }
  75. if got := mgr.Status(); got.CertPath != certPath {
  76. t.Fatalf("a rejected pair changed the setting: %+v", got)
  77. }
  78. // Empty paths go back to the private CA.
  79. st, body = httpsCall(t, ts, "POST", "/api/settings/https", `{"cert":"","key":"","password":"test123456"}`, true)
  80. if st != http.StatusOK {
  81. t.Fatalf("back to the CA: status %d: %v", st, body)
  82. }
  83. if got := mgr.Status(); got.CertSource != "ca" {
  84. t.Fatalf("back to the CA: %+v", got)
  85. }
  86. }