account.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. package acme
  2. import (
  3. "encoding/json"
  4. "time"
  5. "github.com/pkg/errors"
  6. "github.com/smallstep/certificates/authority/provisioner"
  7. "github.com/smallstep/cli/jose"
  8. "github.com/smallstep/nosql"
  9. )
  10. // Account is a subset of the internal account type containing only those
  11. // attributes required for responses in the ACME protocol.
  12. type Account struct {
  13. Contact []string `json:"contact,omitempty"`
  14. Status string `json:"status"`
  15. Orders string `json:"orders"`
  16. ID string `json:"-"`
  17. Key *jose.JSONWebKey `json:"-"`
  18. }
  19. // ToLog enables response logging.
  20. func (a *Account) ToLog() (interface{}, error) {
  21. b, err := json.Marshal(a)
  22. if err != nil {
  23. return nil, ServerInternalErr(errors.Wrap(err, "error marshaling account for logging"))
  24. }
  25. return string(b), nil
  26. }
  27. // GetID returns the account ID.
  28. func (a *Account) GetID() string {
  29. return a.ID
  30. }
  31. // GetKey returns the JWK associated with the account.
  32. func (a *Account) GetKey() *jose.JSONWebKey {
  33. return a.Key
  34. }
  35. // IsValid returns true if the Account is valid.
  36. func (a *Account) IsValid() bool {
  37. return a.Status == StatusValid
  38. }
  39. // AccountOptions are the options needed to create a new ACME account.
  40. type AccountOptions struct {
  41. Key *jose.JSONWebKey
  42. Contact []string
  43. }
  44. // account represents an ACME account.
  45. type account struct {
  46. ID string `json:"id"`
  47. Created time.Time `json:"created"`
  48. Deactivated time.Time `json:"deactivated"`
  49. Key *jose.JSONWebKey `json:"key"`
  50. Contact []string `json:"contact,omitempty"`
  51. Status string `json:"status"`
  52. }
  53. // newAccount returns a new acme account type.
  54. func newAccount(db nosql.DB, ops AccountOptions) (*account, error) {
  55. id, err := randID()
  56. if err != nil {
  57. return nil, err
  58. }
  59. a := &account{
  60. ID: id,
  61. Key: ops.Key,
  62. Contact: ops.Contact,
  63. Status: "valid",
  64. Created: clock.Now(),
  65. }
  66. return a, a.saveNew(db)
  67. }
  68. // toACME converts the internal Account type into the public acmeAccount
  69. // type for presentation in the ACME protocol.
  70. func (a *account) toACME(db nosql.DB, dir *directory, p provisioner.Interface) (*Account, error) {
  71. return &Account{
  72. Status: a.Status,
  73. Contact: a.Contact,
  74. Orders: dir.getLink(OrdersByAccountLink, URLSafeProvisionerName(p), true, a.ID),
  75. Key: a.Key,
  76. ID: a.ID,
  77. }, nil
  78. }
  79. // save writes the Account to the DB.
  80. // If the account is new then the necessary indices will be created.
  81. // Else, the account in the DB will be updated.
  82. func (a *account) saveNew(db nosql.DB) error {
  83. kid, err := keyToID(a.Key)
  84. if err != nil {
  85. return err
  86. }
  87. kidB := []byte(kid)
  88. // Set the jwkID -> acme account ID index
  89. _, swapped, err := db.CmpAndSwap(accountByKeyIDTable, kidB, nil, []byte(a.ID))
  90. switch {
  91. case err != nil:
  92. return ServerInternalErr(errors.Wrap(err, "error setting key-id to account-id index"))
  93. case !swapped:
  94. return ServerInternalErr(errors.Errorf("key-id to account-id index already exists"))
  95. default:
  96. if err = a.save(db, nil); err != nil {
  97. db.Del(accountByKeyIDTable, kidB)
  98. return err
  99. }
  100. return nil
  101. }
  102. }
  103. func (a *account) save(db nosql.DB, old *account) error {
  104. var (
  105. err error
  106. oldB []byte
  107. )
  108. if old == nil {
  109. oldB = nil
  110. } else {
  111. if oldB, err = json.Marshal(old); err != nil {
  112. return ServerInternalErr(errors.Wrap(err, "error marshaling old acme order"))
  113. }
  114. }
  115. b, err := json.Marshal(*a)
  116. if err != nil {
  117. return errors.Wrap(err, "error marshaling new account object")
  118. }
  119. // Set the Account
  120. _, swapped, err := db.CmpAndSwap(accountTable, []byte(a.ID), oldB, b)
  121. switch {
  122. case err != nil:
  123. return ServerInternalErr(errors.Wrap(err, "error storing account"))
  124. case !swapped:
  125. return ServerInternalErr(errors.New("error storing account; " +
  126. "value has changed since last read"))
  127. default:
  128. return nil
  129. }
  130. }
  131. // update updates the acme account object stored in the database if,
  132. // and only if, the account has not changed since the last read.
  133. func (a *account) update(db nosql.DB, contact []string) (*account, error) {
  134. b := *a
  135. b.Contact = contact
  136. if err := b.save(db, a); err != nil {
  137. return nil, err
  138. }
  139. return &b, nil
  140. }
  141. // deactivate deactivates the acme account.
  142. func (a *account) deactivate(db nosql.DB) (*account, error) {
  143. b := *a
  144. b.Status = StatusDeactivated
  145. b.Deactivated = clock.Now()
  146. if err := b.save(db, a); err != nil {
  147. return nil, err
  148. }
  149. return &b, nil
  150. }
  151. // getAccountByID retrieves the account with the given ID.
  152. func getAccountByID(db nosql.DB, id string) (*account, error) {
  153. ab, err := db.Get(accountTable, []byte(id))
  154. if err != nil {
  155. if nosql.IsErrNotFound(err) {
  156. return nil, MalformedErr(errors.Wrapf(err, "account %s not found", id))
  157. }
  158. return nil, ServerInternalErr(errors.Wrapf(err, "error loading account %s", id))
  159. }
  160. a := new(account)
  161. if err = json.Unmarshal(ab, a); err != nil {
  162. return nil, ServerInternalErr(errors.Wrap(err, "error unmarshaling account"))
  163. }
  164. return a, nil
  165. }
  166. // getAccountByKeyID retrieves Id associated with the given Kid.
  167. func getAccountByKeyID(db nosql.DB, kid string) (*account, error) {
  168. id, err := db.Get(accountByKeyIDTable, []byte(kid))
  169. if err != nil {
  170. if nosql.IsErrNotFound(err) {
  171. return nil, MalformedErr(errors.Wrapf(err, "account with key id %s not found", kid))
  172. }
  173. return nil, ServerInternalErr(errors.Wrapf(err, "error loading key-account index"))
  174. }
  175. return getAccountByID(db, string(id))
  176. }
  177. // getOrderIDsByAccount retrieves a list of Order IDs that were created by the
  178. // account.
  179. func getOrderIDsByAccount(db nosql.DB, id string) ([]string, error) {
  180. b, err := db.Get(ordersByAccountIDTable, []byte(id))
  181. if err != nil {
  182. if nosql.IsErrNotFound(err) {
  183. return []string{}, nil
  184. }
  185. return nil, ServerInternalErr(errors.Wrapf(err, "error loading orderIDs for account %s", id))
  186. }
  187. var orderIDs []string
  188. if err := json.Unmarshal(b, &orderIDs); err != nil {
  189. return nil, ServerInternalErr(errors.Wrapf(err, "error unmarshaling orderIDs for account %s", id))
  190. }
  191. return orderIDs, nil
  192. }