challenge.go 3.0 KB

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