signhandler_test.go 2.4 KB

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