order.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. package nosql
  2. import (
  3. "context"
  4. "encoding/json"
  5. "sync"
  6. "time"
  7. "github.com/pkg/errors"
  8. "github.com/smallstep/certificates/acme"
  9. "github.com/smallstep/nosql"
  10. )
  11. // Mutex for locking ordersByAccount index operations.
  12. var ordersByAccountMux sync.Mutex
  13. type dbOrder struct {
  14. ID string `json:"id"`
  15. AccountID string `json:"accountID"`
  16. ProvisionerID string `json:"provisionerID"`
  17. Identifiers []acme.Identifier `json:"identifiers"`
  18. AuthorizationIDs []string `json:"authorizationIDs"`
  19. Status acme.Status `json:"status"`
  20. NotBefore time.Time `json:"notBefore,omitempty"`
  21. NotAfter time.Time `json:"notAfter,omitempty"`
  22. CreatedAt time.Time `json:"createdAt"`
  23. ExpiresAt time.Time `json:"expiresAt,omitempty"`
  24. CertificateID string `json:"certificate,omitempty"`
  25. Error *acme.Error `json:"error,omitempty"`
  26. }
  27. func (a *dbOrder) clone() *dbOrder {
  28. b := *a
  29. return &b
  30. }
  31. // getDBOrder retrieves and unmarshals an ACME Order type from the database.
  32. func (db *DB) getDBOrder(ctx context.Context, id string) (*dbOrder, error) {
  33. b, err := db.db.Get(orderTable, []byte(id))
  34. if nosql.IsErrNotFound(err) {
  35. return nil, acme.NewError(acme.ErrorMalformedType, "order %s not found", id)
  36. } else if err != nil {
  37. return nil, errors.Wrapf(err, "error loading order %s", id)
  38. }
  39. o := new(dbOrder)
  40. if err := json.Unmarshal(b, &o); err != nil {
  41. return nil, errors.Wrapf(err, "error unmarshaling order %s into dbOrder", id)
  42. }
  43. return o, nil
  44. }
  45. // GetOrder retrieves an ACME Order from the database.
  46. func (db *DB) GetOrder(ctx context.Context, id string) (*acme.Order, error) {
  47. dbo, err := db.getDBOrder(ctx, id)
  48. if err != nil {
  49. return nil, err
  50. }
  51. o := &acme.Order{
  52. ID: dbo.ID,
  53. AccountID: dbo.AccountID,
  54. ProvisionerID: dbo.ProvisionerID,
  55. CertificateID: dbo.CertificateID,
  56. Status: dbo.Status,
  57. ExpiresAt: dbo.ExpiresAt,
  58. Identifiers: dbo.Identifiers,
  59. NotBefore: dbo.NotBefore,
  60. NotAfter: dbo.NotAfter,
  61. AuthorizationIDs: dbo.AuthorizationIDs,
  62. Error: dbo.Error,
  63. }
  64. return o, nil
  65. }
  66. // CreateOrder creates ACME Order resources and saves them to the DB.
  67. func (db *DB) CreateOrder(ctx context.Context, o *acme.Order) error {
  68. var err error
  69. o.ID, err = randID()
  70. if err != nil {
  71. return err
  72. }
  73. now := clock.Now()
  74. dbo := &dbOrder{
  75. ID: o.ID,
  76. AccountID: o.AccountID,
  77. ProvisionerID: o.ProvisionerID,
  78. Status: o.Status,
  79. CreatedAt: now,
  80. ExpiresAt: o.ExpiresAt,
  81. Identifiers: o.Identifiers,
  82. NotBefore: o.NotBefore,
  83. NotAfter: o.NotAfter,
  84. AuthorizationIDs: o.AuthorizationIDs,
  85. }
  86. if err := db.save(ctx, o.ID, dbo, nil, "order", orderTable); err != nil {
  87. return err
  88. }
  89. _, err = db.updateAddOrderIDs(ctx, o.AccountID, o.ID)
  90. if err != nil {
  91. return err
  92. }
  93. return nil
  94. }
  95. // UpdateOrder saves an updated ACME Order to the database.
  96. func (db *DB) UpdateOrder(ctx context.Context, o *acme.Order) error {
  97. old, err := db.getDBOrder(ctx, o.ID)
  98. if err != nil {
  99. return err
  100. }
  101. nu := old.clone()
  102. nu.Status = o.Status
  103. nu.Error = o.Error
  104. nu.CertificateID = o.CertificateID
  105. return db.save(ctx, old.ID, nu, old, "order", orderTable)
  106. }
  107. func (db *DB) updateAddOrderIDs(ctx context.Context, accID string, addOids ...string) ([]string, error) {
  108. ordersByAccountMux.Lock()
  109. defer ordersByAccountMux.Unlock()
  110. b, err := db.db.Get(ordersByAccountIDTable, []byte(accID))
  111. var (
  112. oldOids []string
  113. )
  114. if err != nil {
  115. if !nosql.IsErrNotFound(err) {
  116. return nil, errors.Wrapf(err, "error loading orderIDs for account %s", accID)
  117. }
  118. } else {
  119. if err := json.Unmarshal(b, &oldOids); err != nil {
  120. return nil, errors.Wrapf(err, "error unmarshaling orderIDs for account %s", accID)
  121. }
  122. }
  123. // Remove any order that is not in PENDING state and update the stored list
  124. // before returning.
  125. //
  126. // According to RFC 8555:
  127. // The server SHOULD include pending orders and SHOULD NOT include orders
  128. // that are invalid in the array of URLs.
  129. pendOids := []string{}
  130. for _, oid := range oldOids {
  131. o, err := db.GetOrder(ctx, oid)
  132. if err != nil {
  133. return nil, acme.WrapErrorISE(err, "error loading order %s for account %s", oid, accID)
  134. }
  135. if err = o.UpdateStatus(ctx, db); err != nil {
  136. return nil, acme.WrapErrorISE(err, "error updating order %s for account %s", oid, accID)
  137. }
  138. if o.Status == acme.StatusPending {
  139. pendOids = append(pendOids, oid)
  140. }
  141. }
  142. pendOids = append(pendOids, addOids...)
  143. var (
  144. _old interface{} = oldOids
  145. _new interface{} = pendOids
  146. )
  147. switch {
  148. case len(oldOids) == 0 && len(pendOids) == 0:
  149. // If list has not changed from empty, then no need to write the DB.
  150. return []string{}, nil
  151. case len(oldOids) == 0:
  152. _old = nil
  153. case len(pendOids) == 0:
  154. _new = nil
  155. }
  156. if err = db.save(ctx, accID, _new, _old, "orderIDsByAccountID", ordersByAccountIDTable); err != nil {
  157. // Delete all orders that may have been previously stored if orderIDsByAccountID update fails.
  158. for _, oid := range addOids {
  159. // Ignore error from delete -- we tried our best.
  160. // TODO when we have logging w/ request ID tracking, logging this error.
  161. db.db.Del(orderTable, []byte(oid))
  162. }
  163. return nil, errors.Wrapf(err, "error saving orderIDs index for account %s", accID)
  164. }
  165. return pendOids, nil
  166. }
  167. // GetOrdersByAccountID returns a list of order IDs owned by the account.
  168. func (db *DB) GetOrdersByAccountID(ctx context.Context, accID string) ([]string, error) {
  169. return db.updateAddOrderIDs(ctx, accID)
  170. }