challenge.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. ch.ID = randID()
  43. dbch := &dbChallenge{
  44. ID: ch.ID,
  45. AccountID: ch.AccountID,
  46. Value: ch.Value,
  47. Status: acme.StatusPending,
  48. Token: ch.Token,
  49. CreatedAt: clock.Now(),
  50. Type: ch.Type,
  51. Target: ch.Target,
  52. }
  53. return db.save(ctx, ch.ID, dbch, nil, "challenge", challengeTable)
  54. }
  55. // GetChallenge retrieves and unmarshals an ACME challenge type from the database.
  56. // Implements the acme.DB GetChallenge interface.
  57. func (db *DB) GetChallenge(ctx context.Context, id, authzID string) (*acme.Challenge, error) {
  58. _ = authzID // unused input
  59. dbch, err := db.getDBChallenge(ctx, id)
  60. if err != nil {
  61. return nil, err
  62. }
  63. ch := &acme.Challenge{
  64. ID: dbch.ID,
  65. AccountID: dbch.AccountID,
  66. Type: dbch.Type,
  67. Value: dbch.Value,
  68. Status: dbch.Status,
  69. Token: dbch.Token,
  70. Error: dbch.Error,
  71. ValidatedAt: dbch.ValidatedAt,
  72. Target: dbch.Target,
  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. }