simple.go 3.4 KB

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