challenge.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package nosql
  2. import (
  3. "context"
  4. "encoding/json"
  5. "time"
  6. "github.com/pkg/errors"
  7. "github.com/smallstep/certificates/acme"
  8. "github.com/smallstep/nosql"
  9. )
  10. type dbChallenge struct {
  11. ID string `json:"id"`
  12. AccountID string `json:"accountID"`
  13. Type acme.ChallengeType `json:"type"`
  14. Status acme.Status `json:"status"`
  15. Token string `json:"token"`
  16. Value string `json:"value"`
  17. ValidatedAt string `json:"validatedAt"`
  18. CreatedAt time.Time `json:"createdAt"`
  19. Error *acme.Error `json:"error"`
  20. }
  21. func (dbc *dbChallenge) clone() *dbChallenge {
  22. u := *dbc
  23. return &u
  24. }
  25. func (db *DB) getDBChallenge(ctx context.Context, id string) (*dbChallenge, error) {
  26. data, err := db.db.Get(challengeTable, []byte(id))
  27. if nosql.IsErrNotFound(err) {
  28. return nil, acme.NewError(acme.ErrorMalformedType, "challenge %s not found", id)
  29. } else if err != nil {
  30. return nil, errors.Wrapf(err, "error loading acme challenge %s", id)
  31. }
  32. dbch := new(dbChallenge)
  33. if err := json.Unmarshal(data, dbch); err != nil {
  34. return nil, errors.Wrap(err, "error unmarshaling dbChallenge")
  35. }
  36. return dbch, nil
  37. }
  38. // CreateChallenge creates a new ACME challenge data structure in the database.
  39. // Implements acme.DB.CreateChallenge interface.
  40. func (db *DB) CreateChallenge(ctx context.Context, ch *acme.Challenge) error {
  41. var err error
  42. ch.ID, err = randID()
  43. if err != nil {
  44. return errors.Wrap(err, "error generating random id for ACME challenge")
  45. }
  46. dbch := &dbChallenge{
  47. ID: ch.ID,
  48. AccountID: ch.AccountID,
  49. Value: ch.Value,
  50. Status: acme.StatusPending,
  51. Token: ch.Token,
  52. CreatedAt: clock.Now(),
  53. Type: ch.Type,
  54. }
  55. return db.save(ctx, ch.ID, dbch, nil, "challenge", challengeTable)
  56. }
  57. // GetChallenge retrieves and unmarshals an ACME challenge type from the database.
  58. // Implements the acme.DB GetChallenge interface.
  59. func (db *DB) GetChallenge(ctx context.Context, id, authzID string) (*acme.Challenge, error) {
  60. dbch, err := db.getDBChallenge(ctx, id)
  61. if err != nil {
  62. return nil, err
  63. }
  64. ch := &acme.Challenge{
  65. ID: dbch.ID,
  66. AccountID: dbch.AccountID,
  67. Type: dbch.Type,
  68. Value: dbch.Value,
  69. Status: dbch.Status,
  70. Token: dbch.Token,
  71. Error: dbch.Error,
  72. ValidatedAt: dbch.ValidatedAt,
  73. }
  74. return ch, nil
  75. }
  76. // UpdateChallenge updates an ACME challenge type in the database.
  77. func (db *DB) UpdateChallenge(ctx context.Context, ch *acme.Challenge) error {
  78. old, err := db.getDBChallenge(ctx, ch.ID)
  79. if err != nil {
  80. return err
  81. }
  82. nu := old.clone()
  83. // These should be the only values changing in an Update request.
  84. nu.Status = ch.Status
  85. nu.Error = ch.Error
  86. nu.ValidatedAt = ch.ValidatedAt
  87. return db.save(ctx, old.ID, nu, old, "challenge", challengeTable)
  88. }