simple.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package db
  2. import (
  3. "crypto/x509"
  4. "sync"
  5. "time"
  6. "github.com/pkg/errors"
  7. "github.com/smallstep/nosql/database"
  8. )
  9. // ErrNotImplemented is an error returned when an operation is Not Implemented.
  10. var ErrNotImplemented = errors.Errorf("not implemented")
  11. // SimpleDB is a barebones implementation of the DB interface. It is NOT an
  12. // in memory implementation of the DB, but rather the bare minimum of
  13. // functionality that the CA requires to operate securely.
  14. type SimpleDB struct {
  15. usedTokens *sync.Map
  16. }
  17. func newSimpleDB(c *Config) (AuthDB, error) {
  18. db := &SimpleDB{}
  19. db.usedTokens = new(sync.Map)
  20. return db, nil
  21. }
  22. // IsRevoked noop
  23. func (s *SimpleDB) IsRevoked(sn string) (bool, error) {
  24. return false, nil
  25. }
  26. // Revoke returns a "NotImplemented" error.
  27. func (s *SimpleDB) Revoke(rci *RevokedCertificateInfo) error {
  28. return ErrNotImplemented
  29. }
  30. // StoreCertificate returns a "NotImplemented" error.
  31. func (s *SimpleDB) StoreCertificate(crt *x509.Certificate) error {
  32. return ErrNotImplemented
  33. }
  34. type usedToken struct {
  35. UsedAt int64 `json:"ua,omitempty"`
  36. Token string `json:"tok,omitempty"`
  37. }
  38. // UseToken returns a "NotImplemented" error.
  39. func (s *SimpleDB) UseToken(id, tok string) (bool, error) {
  40. if _, ok := s.usedTokens.LoadOrStore(id, &usedToken{
  41. UsedAt: time.Now().Unix(),
  42. Token: tok,
  43. }); ok {
  44. // Token already exists in DB.
  45. return false, nil
  46. }
  47. // Successfully stored token.
  48. return true, nil
  49. }
  50. // Shutdown returns nil
  51. func (s *SimpleDB) Shutdown() error {
  52. return nil
  53. }
  54. // nosql.DB interface implementation //
  55. // Open opens the database available with the given options.
  56. func (s *SimpleDB) Open(dataSourceName string, opt ...database.Option) error {
  57. return ErrNotImplemented
  58. }
  59. // Close closes the current database.
  60. func (s *SimpleDB) Close() error {
  61. return ErrNotImplemented
  62. }
  63. // Get returns the value stored in the given table/bucket and key.
  64. func (s *SimpleDB) Get(bucket, key []byte) ([]byte, error) {
  65. return nil, ErrNotImplemented
  66. }
  67. // Set sets the given value in the given table/bucket and key.
  68. func (s *SimpleDB) Set(bucket, key, value []byte) error {
  69. return ErrNotImplemented
  70. }
  71. // CmpAndSwap swaps the value at the given bucket and key if the current
  72. // value is equivalent to the oldValue input. Returns 'true' if the
  73. // swap was successful and 'false' otherwise.
  74. func (s *SimpleDB) CmpAndSwap(bucket, key, oldValue, newValue []byte) ([]byte, bool, error) {
  75. return nil, false, ErrNotImplemented
  76. }
  77. // Del deletes the data in the given table/bucket and key.
  78. func (s *SimpleDB) Del(bucket, key []byte) error {
  79. return ErrNotImplemented
  80. }
  81. // List returns a list of all the entries in a given table/bucket.
  82. func (s *SimpleDB) List(bucket []byte) ([]*database.Entry, error) {
  83. return nil, ErrNotImplemented
  84. }
  85. // Update performs a transaction with multiple read-write commands.
  86. func (s *SimpleDB) Update(tx *database.Tx) error {
  87. return ErrNotImplemented
  88. }
  89. // CreateTable creates a table or a bucket in the database.
  90. func (s *SimpleDB) CreateTable(bucket []byte) error {
  91. return ErrNotImplemented
  92. }
  93. // DeleteTable deletes a table or a bucket in the database.
  94. func (s *SimpleDB) DeleteTable(bucket []byte) error {
  95. return ErrNotImplemented
  96. }