account.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package nosql
  2. import (
  3. "context"
  4. "encoding/json"
  5. "time"
  6. "github.com/pkg/errors"
  7. "github.com/smallstep/certificates/acme"
  8. nosqlDB "github.com/smallstep/nosql"
  9. "go.step.sm/crypto/jose"
  10. )
  11. // dbAccount represents an ACME account.
  12. type dbAccount struct {
  13. ID string `json:"id"`
  14. Key *jose.JSONWebKey `json:"key"`
  15. Contact []string `json:"contact,omitempty"`
  16. Status acme.Status `json:"status"`
  17. LocationPrefix string `json:"locationPrefix"`
  18. ProvisionerID string `json:"provisionerID,omitempty"`
  19. ProvisionerName string `json:"provisionerName"`
  20. CreatedAt time.Time `json:"createdAt"`
  21. DeactivatedAt time.Time `json:"deactivatedAt"`
  22. }
  23. func (dba *dbAccount) clone() *dbAccount {
  24. nu := *dba
  25. return &nu
  26. }
  27. func (db *DB) getAccountIDByKeyID(_ context.Context, kid string) (string, error) {
  28. id, err := db.db.Get(accountByKeyIDTable, []byte(kid))
  29. if err != nil {
  30. if nosqlDB.IsErrNotFound(err) {
  31. return "", acme.ErrNotFound
  32. }
  33. return "", errors.Wrapf(err, "error loading key-account index for key %s", kid)
  34. }
  35. return string(id), nil
  36. }
  37. // getDBAccount retrieves and unmarshals dbAccount.
  38. func (db *DB) getDBAccount(_ context.Context, id string) (*dbAccount, error) {
  39. data, err := db.db.Get(accountTable, []byte(id))
  40. if err != nil {
  41. if nosqlDB.IsErrNotFound(err) {
  42. return nil, acme.ErrNotFound
  43. }
  44. return nil, errors.Wrapf(err, "error loading account %s", id)
  45. }
  46. dbacc := new(dbAccount)
  47. if err = json.Unmarshal(data, dbacc); err != nil {
  48. return nil, errors.Wrapf(err, "error unmarshaling account %s into dbAccount", id)
  49. }
  50. return dbacc, nil
  51. }
  52. // GetAccount retrieves an ACME account by ID.
  53. func (db *DB) GetAccount(ctx context.Context, id string) (*acme.Account, error) {
  54. dbacc, err := db.getDBAccount(ctx, id)
  55. if err != nil {
  56. return nil, err
  57. }
  58. return &acme.Account{
  59. Status: dbacc.Status,
  60. Contact: dbacc.Contact,
  61. Key: dbacc.Key,
  62. ID: dbacc.ID,
  63. LocationPrefix: dbacc.LocationPrefix,
  64. ProvisionerID: dbacc.ProvisionerID,
  65. ProvisionerName: dbacc.ProvisionerName,
  66. }, nil
  67. }
  68. // GetAccountByKeyID retrieves an ACME account by KeyID (thumbprint of the Account Key -- JWK).
  69. func (db *DB) GetAccountByKeyID(ctx context.Context, kid string) (*acme.Account, error) {
  70. id, err := db.getAccountIDByKeyID(ctx, kid)
  71. if err != nil {
  72. return nil, err
  73. }
  74. return db.GetAccount(ctx, id)
  75. }
  76. // CreateAccount implements the AcmeDB.CreateAccount interface.
  77. func (db *DB) CreateAccount(ctx context.Context, acc *acme.Account) error {
  78. var err error
  79. acc.ID, err = randID()
  80. if err != nil {
  81. return err
  82. }
  83. dba := &dbAccount{
  84. ID: acc.ID,
  85. Key: acc.Key,
  86. Contact: acc.Contact,
  87. Status: acc.Status,
  88. CreatedAt: clock.Now(),
  89. LocationPrefix: acc.LocationPrefix,
  90. ProvisionerID: acc.ProvisionerID,
  91. ProvisionerName: acc.ProvisionerName,
  92. }
  93. kid, err := acme.KeyToID(dba.Key)
  94. if err != nil {
  95. return err
  96. }
  97. kidB := []byte(kid)
  98. // Set the jwkID -> acme account ID index
  99. _, swapped, err := db.db.CmpAndSwap(accountByKeyIDTable, kidB, nil, []byte(acc.ID))
  100. switch {
  101. case err != nil:
  102. return errors.Wrap(err, "error storing keyID to accountID index")
  103. case !swapped:
  104. return errors.Errorf("key-id to account-id index already exists")
  105. default:
  106. if err = db.save(ctx, acc.ID, dba, nil, "account", accountTable); err != nil {
  107. db.db.Del(accountByKeyIDTable, kidB)
  108. return err
  109. }
  110. return nil
  111. }
  112. }
  113. // UpdateAccount implements the AcmeDB.UpdateAccount interface.
  114. func (db *DB) UpdateAccount(ctx context.Context, acc *acme.Account) error {
  115. old, err := db.getDBAccount(ctx, acc.ID)
  116. if err != nil {
  117. return err
  118. }
  119. nu := old.clone()
  120. nu.Contact = acc.Contact
  121. nu.Status = acc.Status
  122. // If the status has changed to 'deactivated', then set deactivatedAt timestamp.
  123. if acc.Status == acme.StatusDeactivated && old.Status != acme.StatusDeactivated {
  124. nu.DeactivatedAt = clock.Now()
  125. }
  126. return db.save(ctx, old.ID, nu, old, "account", accountTable)
  127. }