signhandler_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package signhandler
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "io"
  6. "net/http"
  7. "net/http/httptest"
  8. "os"
  9. "testing"
  10. "github.com/cloudflare/cfssl/api"
  11. "github.com/cloudflare/cfssl/certdb"
  12. "github.com/cloudflare/cfssl/certdb/sql"
  13. "github.com/cloudflare/cfssl/certdb/testdb"
  14. "github.com/cloudflare/cfssl/config"
  15. "github.com/cloudflare/cfssl/signer"
  16. "github.com/cloudflare/cfssl/signer/local"
  17. )
  18. const (
  19. testCaFile = "../testdata/ca.pem"
  20. testCaKeyFile = "../testdata/ca_key.pem"
  21. testCSRFile = "../testdata/csr.pem"
  22. )
  23. // GetUnexpiredCertificates sometimes doesn't return a certificate with an
  24. // expiry of 1m as above
  25. var validLocalConfigLongerExpiry = `
  26. {
  27. "signing": {
  28. "default": {
  29. "usages": ["digital signature", "email protection"],
  30. "expiry": "10m"
  31. }
  32. }
  33. }`
  34. var dbAccessor certdb.Accessor
  35. func TestSignerDBPersistence(t *testing.T) {
  36. conf, err := config.LoadConfig([]byte(validLocalConfigLongerExpiry))
  37. if err != nil {
  38. t.Fatal(err)
  39. }
  40. var s *local.Signer
  41. s, err = local.NewSignerFromFile(testCaFile, testCaKeyFile, conf.Signing)
  42. if err != nil {
  43. t.Fatal(err)
  44. }
  45. db := testdb.SQLiteDB("../../certdb/testdb/certstore_development.db")
  46. if err != nil {
  47. t.Fatal(err)
  48. }
  49. dbAccessor = sql.NewAccessor(db)
  50. s.SetDBAccessor(dbAccessor)
  51. var handler *api.HTTPHandler
  52. handler, err = NewHandlerFromSigner(signer.Signer(s))
  53. if err != nil {
  54. t.Fatal(err)
  55. }
  56. ts := httptest.NewServer(handler)
  57. defer ts.Close()
  58. var csrPEM, body []byte
  59. csrPEM, err = os.ReadFile(testCSRFile)
  60. if err != nil {
  61. t.Fatal(err)
  62. }
  63. blob, err := json.Marshal(&map[string]string{"certificate_request": string(csrPEM)})
  64. if err != nil {
  65. t.Fatal(err)
  66. }
  67. var resp *http.Response
  68. resp, err = http.Post(ts.URL, "application/json", bytes.NewReader(blob))
  69. if err != nil {
  70. t.Fatal(err)
  71. }
  72. body, err = io.ReadAll(resp.Body)
  73. if err != nil {
  74. t.Fatal(err)
  75. }
  76. if resp.StatusCode != http.StatusOK {
  77. t.Fatal(resp.Status, string(body))
  78. }
  79. message := new(api.Response)
  80. err = json.Unmarshal(body, message)
  81. if err != nil {
  82. t.Fatalf("failed to read response body: %v", err)
  83. }
  84. if !message.Success {
  85. t.Fatal("API operation failed")
  86. }
  87. crs, err := dbAccessor.GetUnexpiredCertificates()
  88. if err != nil {
  89. t.Fatal("Failed to get unexpired certificates")
  90. }
  91. if len(crs) != 1 {
  92. t.Fatal("Expected 1 unexpired certificate in the database after signing 1: len(crs)=", len(crs))
  93. }
  94. }