db.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package db
  2. import (
  3. "crypto/x509"
  4. "encoding/json"
  5. "time"
  6. "github.com/pkg/errors"
  7. "github.com/smallstep/nosql"
  8. )
  9. var (
  10. certsTable = []byte("x509_certs")
  11. revokedCertsTable = []byte("revoked_x509_certs")
  12. usedOTTTable = []byte("used_ott")
  13. )
  14. // ErrAlreadyExists can be returned if the DB attempts to set a key that has
  15. // been previously set.
  16. var ErrAlreadyExists = errors.New("already exists")
  17. // Config represents the JSON attributes used for configuring a step-ca DB.
  18. type Config struct {
  19. Type string `json:"type"`
  20. DataSource string `json:"dataSource"`
  21. ValueDir string `json:"valueDir,omitempty"`
  22. Database string `json:"database,omitempty"`
  23. }
  24. // AuthDB is an interface over an Authority DB client that implements a nosql.DB interface.
  25. type AuthDB interface {
  26. IsRevoked(sn string) (bool, error)
  27. Revoke(rci *RevokedCertificateInfo) error
  28. StoreCertificate(crt *x509.Certificate) error
  29. UseToken(id, tok string) (bool, error)
  30. Shutdown() error
  31. }
  32. // DB is a wrapper over the nosql.DB interface.
  33. type DB struct {
  34. nosql.DB
  35. isUp bool
  36. }
  37. // New returns a new database client that implements the AuthDB interface.
  38. func New(c *Config) (AuthDB, error) {
  39. if c == nil {
  40. return newSimpleDB(c)
  41. }
  42. db, err := nosql.New(c.Type, c.DataSource, nosql.WithDatabase(c.Database),
  43. nosql.WithValueDir(c.ValueDir))
  44. if err != nil {
  45. return nil, errors.Wrapf(err, "Error opening database of Type %s with source %s", c.Type, c.DataSource)
  46. }
  47. tables := [][]byte{revokedCertsTable, certsTable}
  48. for _, b := range tables {
  49. if err := db.CreateTable(b); err != nil {
  50. return nil, errors.Wrapf(err, "error creating table %s",
  51. string(b))
  52. }
  53. }
  54. return &DB{db, true}, nil
  55. }
  56. // RevokedCertificateInfo contains information regarding the certificate
  57. // revocation action.
  58. type RevokedCertificateInfo struct {
  59. Serial string
  60. ProvisionerID string
  61. ReasonCode int
  62. Reason string
  63. RevokedAt time.Time
  64. TokenID string
  65. MTLS bool
  66. }
  67. // IsRevoked returns whether or not a certificate with the given identifier
  68. // has been revoked.
  69. // In the case of an X509 Certificate the `id` should be the Serial Number of
  70. // the Certificate.
  71. func (db *DB) IsRevoked(sn string) (bool, error) {
  72. // If the DB is nil then act as pass through.
  73. if db == nil {
  74. return false, nil
  75. }
  76. // If the error is `Not Found` then the certificate has not been revoked.
  77. // Any other error should be propagated to the caller.
  78. if _, err := db.Get(revokedCertsTable, []byte(sn)); err != nil {
  79. if nosql.IsErrNotFound(err) {
  80. return false, nil
  81. }
  82. return false, errors.Wrap(err, "error checking revocation bucket")
  83. }
  84. // This certificate has been revoked.
  85. return true, nil
  86. }
  87. // Revoke adds a certificate to the revocation table.
  88. func (db *DB) Revoke(rci *RevokedCertificateInfo) error {
  89. isRvkd, err := db.IsRevoked(rci.Serial)
  90. if err != nil {
  91. return err
  92. }
  93. if isRvkd {
  94. return ErrAlreadyExists
  95. }
  96. rcib, err := json.Marshal(rci)
  97. if err != nil {
  98. return errors.Wrap(err, "error marshaling revoked certificate info")
  99. }
  100. if err = db.Set(revokedCertsTable, []byte(rci.Serial), rcib); err != nil {
  101. return errors.Wrap(err, "database Set error")
  102. }
  103. return nil
  104. }
  105. // StoreCertificate stores a certificate PEM.
  106. func (db *DB) StoreCertificate(crt *x509.Certificate) error {
  107. if err := db.Set(certsTable, []byte(crt.SerialNumber.String()), crt.Raw); err != nil {
  108. return errors.Wrap(err, "database Set error")
  109. }
  110. return nil
  111. }
  112. // UseToken returns true if we were able to successfully store the token for
  113. // for the first time, false otherwise.
  114. func (db *DB) UseToken(id, tok string) (bool, error) {
  115. _, swapped, err := db.CmpAndSwap(usedOTTTable, []byte(id), nil, []byte(tok))
  116. switch {
  117. case err != nil:
  118. return false, errors.Wrapf(err, "error storing used token %s/%s",
  119. string(usedOTTTable), id)
  120. case !swapped:
  121. return false, nil
  122. default:
  123. return true, nil
  124. }
  125. }
  126. // Shutdown sends a shutdown message to the database.
  127. func (db *DB) Shutdown() error {
  128. if db.isUp {
  129. if err := db.Close(); err != nil {
  130. return errors.Wrap(err, "database shutdown error")
  131. }
  132. db.isUp = false
  133. }
  134. return nil
  135. }