nosql.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package nosql
  2. import (
  3. "context"
  4. "encoding/json"
  5. "time"
  6. "github.com/pkg/errors"
  7. nosqlDB "github.com/smallstep/nosql"
  8. "go.step.sm/crypto/randutil"
  9. )
  10. var (
  11. accountTable = []byte("acme_accounts")
  12. accountByKeyIDTable = []byte("acme_keyID_accountID_index")
  13. authzTable = []byte("acme_authzs")
  14. challengeTable = []byte("acme_challenges")
  15. nonceTable = []byte("nonces")
  16. orderTable = []byte("acme_orders")
  17. ordersByAccountIDTable = []byte("acme_account_orders_index")
  18. certTable = []byte("acme_certs")
  19. certBySerialTable = []byte("acme_serial_certs_index")
  20. externalAccountKeyTable = []byte("acme_external_account_keys")
  21. externalAccountKeyIDsByReferenceTable = []byte("acme_external_account_keyID_reference_index")
  22. externalAccountKeyIDsByProvisionerIDTable = []byte("acme_external_account_keyID_provisionerID_index")
  23. wireDpopTokenTable = []byte("wire_acme_dpop_token")
  24. wireOidcTokenTable = []byte("wire_acme_oidc_token")
  25. )
  26. // DB is a struct that implements the AcmeDB interface.
  27. type DB struct {
  28. db nosqlDB.DB
  29. }
  30. // New configures and returns a new ACME DB backend implemented using a nosql DB.
  31. func New(db nosqlDB.DB) (*DB, error) {
  32. tables := [][]byte{accountTable, accountByKeyIDTable, authzTable,
  33. challengeTable, nonceTable, orderTable, ordersByAccountIDTable,
  34. certTable, certBySerialTable, externalAccountKeyTable,
  35. externalAccountKeyIDsByReferenceTable, externalAccountKeyIDsByProvisionerIDTable,
  36. wireDpopTokenTable, wireOidcTokenTable,
  37. }
  38. for _, b := range tables {
  39. if err := db.CreateTable(b); err != nil {
  40. return nil, errors.Wrapf(err, "error creating table %s", string(b))
  41. }
  42. }
  43. return &DB{db}, nil
  44. }
  45. // save writes the new data to the database, overwriting the old data if it
  46. // existed.
  47. func (db *DB) save(_ context.Context, id string, nu, old interface{}, typ string, table []byte) error {
  48. var (
  49. err error
  50. newB []byte
  51. )
  52. if nu == nil {
  53. newB = nil
  54. } else {
  55. newB, err = json.Marshal(nu)
  56. if err != nil {
  57. return errors.Wrapf(err, "error marshaling acme type: %s, value: %v", typ, nu)
  58. }
  59. }
  60. var oldB []byte
  61. if old == nil {
  62. oldB = nil
  63. } else {
  64. oldB, err = json.Marshal(old)
  65. if err != nil {
  66. return errors.Wrapf(err, "error marshaling acme type: %s, value: %v", typ, old)
  67. }
  68. }
  69. _, swapped, err := db.db.CmpAndSwap(table, []byte(id), oldB, newB)
  70. switch {
  71. case err != nil:
  72. return errors.Wrapf(err, "error saving acme %s", typ)
  73. case !swapped:
  74. return errors.Errorf("error saving acme %s; changed since last read", typ)
  75. default:
  76. return nil
  77. }
  78. }
  79. var idLen = 32
  80. func randID() string {
  81. return randutil.Alphanumeric(idLen)
  82. }
  83. // Clock that returns time in UTC rounded to seconds.
  84. type Clock struct{}
  85. // Now returns the UTC time rounded to seconds.
  86. func (c *Clock) Now() time.Time {
  87. return time.Now().UTC().Truncate(time.Second)
  88. }
  89. var clock = new(Clock)