account.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. CreatedAt time.Time `json:"createdAt"`
  18. DeactivatedAt time.Time `json:"deactivatedAt"`
  19. }
  20. func (dba *dbAccount) clone() *dbAccount {
  21. nu := *dba
  22. return &nu
  23. }
  24. func (db *DB) getAccountIDByKeyID(ctx context.Context, kid string) (string, error) {
  25. id, err := db.db.Get(accountByKeyIDTable, []byte(kid))
  26. if err != nil {
  27. if nosqlDB.IsErrNotFound(err) {
  28. return "", acme.ErrNotFound
  29. }
  30. return "", errors.Wrapf(err, "error loading key-account index for key %s", kid)
  31. }
  32. return string(id), nil
  33. }
  34. // getDBAccount retrieves and unmarshals dbAccount.
  35. func (db *DB) getDBAccount(ctx context.Context, id string) (*dbAccount, error) {
  36. data, err := db.db.Get(accountTable, []byte(id))
  37. if err != nil {
  38. if nosqlDB.IsErrNotFound(err) {
  39. return nil, acme.ErrNotFound
  40. }
  41. return nil, errors.Wrapf(err, "error loading account %s", id)
  42. }
  43. dbacc := new(dbAccount)
  44. if err = json.Unmarshal(data, dbacc); err != nil {
  45. return nil, errors.Wrapf(err, "error unmarshaling account %s into dbAccount", id)
  46. }
  47. return dbacc, nil
  48. }
  49. // GetAccount retrieves an ACME account by ID.
  50. func (db *DB) GetAccount(ctx context.Context, id string) (*acme.Account, error) {
  51. dbacc, err := db.getDBAccount(ctx, id)
  52. if err != nil {
  53. return nil, err
  54. }
  55. return &acme.Account{
  56. Status: dbacc.Status,
  57. Contact: dbacc.Contact,
  58. Key: dbacc.Key,
  59. ID: dbacc.ID,
  60. }, nil
  61. }
  62. // GetAccountByKeyID retrieves an ACME account by KeyID (thumbprint of the Account Key -- JWK).
  63. func (db *DB) GetAccountByKeyID(ctx context.Context, kid string) (*acme.Account, error) {
  64. id, err := db.getAccountIDByKeyID(ctx, kid)
  65. if err != nil {
  66. return nil, err
  67. }
  68. return db.GetAccount(ctx, id)
  69. }
  70. // CreateAccount imlements the AcmeDB.CreateAccount interface.
  71. func (db *DB) CreateAccount(ctx context.Context, acc *acme.Account) error {
  72. var err error
  73. acc.ID, err = randID()
  74. if err != nil {
  75. return err
  76. }
  77. dba := &dbAccount{
  78. ID: acc.ID,
  79. Key: acc.Key,
  80. Contact: acc.Contact,
  81. Status: acc.Status,
  82. CreatedAt: clock.Now(),
  83. }
  84. kid, err := acme.KeyToID(dba.Key)
  85. if err != nil {
  86. return err
  87. }
  88. kidB := []byte(kid)
  89. // Set the jwkID -> acme account ID index
  90. _, swapped, err := db.db.CmpAndSwap(accountByKeyIDTable, kidB, nil, []byte(acc.ID))
  91. switch {
  92. case err != nil:
  93. return errors.Wrap(err, "error storing keyID to accountID index")
  94. case !swapped:
  95. return errors.Errorf("key-id to account-id index already exists")
  96. default:
  97. if err = db.save(ctx, acc.ID, dba, nil, "account", accountTable); err != nil {
  98. db.db.Del(accountByKeyIDTable, kidB)
  99. return err
  100. }
  101. return nil
  102. }
  103. }
  104. // UpdateAccount imlements the AcmeDB.UpdateAccount interface.
  105. func (db *DB) UpdateAccount(ctx context.Context, acc *acme.Account) error {
  106. old, err := db.getDBAccount(ctx, acc.ID)
  107. if err != nil {
  108. return err
  109. }
  110. nu := old.clone()
  111. nu.Contact = acc.Contact
  112. nu.Status = acc.Status
  113. // If the status has changed to 'deactivated', then set deactivatedAt timestamp.
  114. if acc.Status == acme.StatusDeactivated && old.Status != acme.StatusDeactivated {
  115. nu.DeactivatedAt = clock.Now()
  116. }
  117. return db.save(ctx, old.ID, nu, old, "account", accountTable)
  118. }