authz.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. var err error
  74. az.ID, err = randID()
  75. if err != nil {
  76. return err
  77. }
  78. chIDs := make([]string, len(az.Challenges))
  79. for i, ch := range az.Challenges {
  80. chIDs[i] = ch.ID
  81. }
  82. now := clock.Now()
  83. dbaz := &dbAuthz{
  84. ID: az.ID,
  85. AccountID: az.AccountID,
  86. Status: az.Status,
  87. CreatedAt: now,
  88. ExpiresAt: az.ExpiresAt,
  89. Identifier: az.Identifier,
  90. ChallengeIDs: chIDs,
  91. Token: az.Token,
  92. Fingerprint: az.Fingerprint,
  93. Wildcard: az.Wildcard,
  94. }
  95. return db.save(ctx, az.ID, dbaz, nil, "authz", authzTable)
  96. }
  97. // UpdateAuthorization saves an updated ACME Authorization to the database.
  98. func (db *DB) UpdateAuthorization(ctx context.Context, az *acme.Authorization) error {
  99. old, err := db.getDBAuthz(ctx, az.ID)
  100. if err != nil {
  101. return err
  102. }
  103. nu := old.clone()
  104. nu.Status = az.Status
  105. nu.Fingerprint = az.Fingerprint
  106. nu.Error = az.Error
  107. return db.save(ctx, old.ID, nu, old, "authz", authzTable)
  108. }
  109. // GetAuthorizationsByAccountID retrieves and unmarshals ACME authz types from the database.
  110. func (db *DB) GetAuthorizationsByAccountID(_ context.Context, accountID string) ([]*acme.Authorization, error) {
  111. entries, err := db.db.List(authzTable)
  112. if err != nil {
  113. return nil, errors.Wrapf(err, "error listing authz")
  114. }
  115. authzs := []*acme.Authorization{}
  116. for _, entry := range entries {
  117. dbaz := new(dbAuthz)
  118. if err = json.Unmarshal(entry.Value, dbaz); err != nil {
  119. return nil, errors.Wrapf(err, "error unmarshaling dbAuthz key '%s' into dbAuthz struct", string(entry.Key))
  120. }
  121. // Filter out all dbAuthzs that don't belong to the accountID. This
  122. // could be made more efficient with additional data structures mapping the
  123. // Account ID to authorizations. Not trivial to do, though.
  124. if dbaz.AccountID != accountID {
  125. continue
  126. }
  127. authzs = append(authzs, &acme.Authorization{
  128. ID: dbaz.ID,
  129. AccountID: dbaz.AccountID,
  130. Identifier: dbaz.Identifier,
  131. Status: dbaz.Status,
  132. Challenges: nil, // challenges not required for current use case
  133. Wildcard: dbaz.Wildcard,
  134. ExpiresAt: dbaz.ExpiresAt,
  135. Token: dbaz.Token,
  136. Fingerprint: dbaz.Fingerprint,
  137. Error: dbaz.Error,
  138. })
  139. }
  140. return authzs, nil
  141. }