authz.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. // dbAuthz is the base authz type that others build from.
  11. type dbAuthz struct {
  12. ID string `json:"id"`
  13. AccountID string `json:"accountID"`
  14. Identifier acme.Identifier `json:"identifier"`
  15. Status acme.Status `json:"status"`
  16. Token string `json:"token"`
  17. Fingerprint string `json:"fingerprint,omitempty"`
  18. ChallengeIDs []string `json:"challengeIDs"`
  19. Wildcard bool `json:"wildcard"`
  20. CreatedAt time.Time `json:"createdAt"`
  21. ExpiresAt time.Time `json:"expiresAt"`
  22. Error *acme.Error `json:"error"`
  23. }
  24. func (ba *dbAuthz) clone() *dbAuthz {
  25. u := *ba
  26. return &u
  27. }
  28. // getDBAuthz retrieves and unmarshals a database representation of the
  29. // ACME Authorization type.
  30. func (db *DB) getDBAuthz(_ context.Context, id string) (*dbAuthz, error) {
  31. data, err := db.db.Get(authzTable, []byte(id))
  32. if nosql.IsErrNotFound(err) {
  33. return nil, acme.NewError(acme.ErrorMalformedType, "authz %s not found", id)
  34. } else if err != nil {
  35. return nil, errors.Wrapf(err, "error loading authz %s", id)
  36. }
  37. var dbaz dbAuthz
  38. if err = json.Unmarshal(data, &dbaz); err != nil {
  39. return nil, errors.Wrapf(err, "error unmarshaling authz %s into dbAuthz", id)
  40. }
  41. return &dbaz, nil
  42. }
  43. // GetAuthorization retrieves and unmarshals an ACME authz type from the database.
  44. // Implements acme.DB GetAuthorization interface.
  45. func (db *DB) GetAuthorization(ctx context.Context, id string) (*acme.Authorization, error) {
  46. dbaz, err := db.getDBAuthz(ctx, id)
  47. if err != nil {
  48. return nil, err
  49. }
  50. var chs = make([]*acme.Challenge, len(dbaz.ChallengeIDs))
  51. for i, chID := range dbaz.ChallengeIDs {
  52. chs[i], err = db.GetChallenge(ctx, chID, id)
  53. if err != nil {
  54. return nil, err
  55. }
  56. }
  57. return &acme.Authorization{
  58. ID: dbaz.ID,
  59. AccountID: dbaz.AccountID,
  60. Identifier: dbaz.Identifier,
  61. Status: dbaz.Status,
  62. Challenges: chs,
  63. Wildcard: dbaz.Wildcard,
  64. ExpiresAt: dbaz.ExpiresAt,
  65. Token: dbaz.Token,
  66. Fingerprint: dbaz.Fingerprint,
  67. Error: dbaz.Error,
  68. }, nil
  69. }
  70. // CreateAuthorization creates an entry in the database for the Authorization.
  71. // Implements the acme.DB.CreateAuthorization interface.
  72. func (db *DB) CreateAuthorization(ctx context.Context, az *acme.Authorization) error {
  73. az.ID = randID()
  74. chIDs := make([]string, len(az.Challenges))
  75. for i, ch := range az.Challenges {
  76. chIDs[i] = ch.ID
  77. }
  78. now := clock.Now()
  79. dbaz := &dbAuthz{
  80. ID: az.ID,
  81. AccountID: az.AccountID,
  82. Status: az.Status,
  83. CreatedAt: now,
  84. ExpiresAt: az.ExpiresAt,
  85. Identifier: az.Identifier,
  86. ChallengeIDs: chIDs,
  87. Token: az.Token,
  88. Fingerprint: az.Fingerprint,
  89. Wildcard: az.Wildcard,
  90. }
  91. return db.save(ctx, az.ID, dbaz, nil, "authz", authzTable)
  92. }
  93. // UpdateAuthorization saves an updated ACME Authorization to the database.
  94. func (db *DB) UpdateAuthorization(ctx context.Context, az *acme.Authorization) error {
  95. old, err := db.getDBAuthz(ctx, az.ID)
  96. if err != nil {
  97. return err
  98. }
  99. nu := old.clone()
  100. nu.Status = az.Status
  101. nu.Fingerprint = az.Fingerprint
  102. nu.Error = az.Error
  103. return db.save(ctx, old.ID, nu, old, "authz", authzTable)
  104. }
  105. // GetAuthorizationsByAccountID retrieves and unmarshals ACME authz types from the database.
  106. func (db *DB) GetAuthorizationsByAccountID(_ context.Context, accountID string) ([]*acme.Authorization, error) {
  107. entries, err := db.db.List(authzTable)
  108. if err != nil {
  109. return nil, errors.Wrapf(err, "error listing authz")
  110. }
  111. authzs := []*acme.Authorization{}
  112. for _, entry := range entries {
  113. dbaz := new(dbAuthz)
  114. if err = json.Unmarshal(entry.Value, dbaz); err != nil {
  115. return nil, errors.Wrapf(err, "error unmarshaling dbAuthz key '%s' into dbAuthz struct", string(entry.Key))
  116. }
  117. // Filter out all dbAuthzs that don't belong to the accountID. This
  118. // could be made more efficient with additional data structures mapping the
  119. // Account ID to authorizations. Not trivial to do, though.
  120. if dbaz.AccountID != accountID {
  121. continue
  122. }
  123. authzs = append(authzs, &acme.Authorization{
  124. ID: dbaz.ID,
  125. AccountID: dbaz.AccountID,
  126. Identifier: dbaz.Identifier,
  127. Status: dbaz.Status,
  128. Challenges: nil, // challenges not required for current use case
  129. Wildcard: dbaz.Wildcard,
  130. ExpiresAt: dbaz.ExpiresAt,
  131. Token: dbaz.Token,
  132. Fingerprint: dbaz.Fingerprint,
  133. Error: dbaz.Error,
  134. })
  135. }
  136. return authzs, nil
  137. }