simple.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. // IsSSHRevoked noop
  28. func (s *SimpleDB) IsSSHRevoked(sn string) (bool, error) {
  29. return false, nil
  30. }
  31. // Revoke returns a "NotImplemented" error.
  32. func (s *SimpleDB) Revoke(rci *RevokedCertificateInfo) error {
  33. return ErrNotImplemented
  34. }
  35. // RevokeSSH returns a "NotImplemented" error.
  36. func (s *SimpleDB) RevokeSSH(rci *RevokedCertificateInfo) error {
  37. return ErrNotImplemented
  38. }
  39. // StoreCertificate returns a "NotImplemented" error.
  40. func (s *SimpleDB) StoreCertificate(crt *x509.Certificate) error {
  41. return ErrNotImplemented
  42. }
  43. type usedToken struct {
  44. UsedAt int64 `json:"ua,omitempty"`
  45. Token string `json:"tok,omitempty"`
  46. }
  47. // UseToken returns a "NotImplemented" error.
  48. func (s *SimpleDB) UseToken(id, tok string) (bool, error) {
  49. if _, ok := s.usedTokens.LoadOrStore(id, &usedToken{
  50. UsedAt: time.Now().Unix(),
  51. Token: tok,
  52. }); ok {
  53. // Token already exists in DB.
  54. return false, nil
  55. }
  56. // Successfully stored token.
  57. return true, nil
  58. }
  59. // IsSSHHost returns a "NotImplemented" error.
  60. func (s *SimpleDB) IsSSHHost(principal string) (bool, error) {
  61. return false, ErrNotImplemented
  62. }
  63. // StoreSSHCertificate returns a "NotImplemented" error.
  64. func (s *SimpleDB) StoreSSHCertificate(crt *ssh.Certificate) error {
  65. return ErrNotImplemented
  66. }
  67. // GetSSHHostPrincipals returns a "NotImplemented" error.
  68. func (s *SimpleDB) GetSSHHostPrincipals() ([]string, error) {
  69. return nil, ErrNotImplemented
  70. }
  71. // Shutdown returns nil
  72. func (s *SimpleDB) Shutdown() error {
  73. return nil
  74. }
  75. // nosql.DB interface implementation //
  76. // Open opens the database available with the given options.
  77. func (s *SimpleDB) Open(dataSourceName string, opt ...database.Option) error {
  78. return ErrNotImplemented
  79. }
  80. // Close closes the current database.
  81. func (s *SimpleDB) Close() error {
  82. return ErrNotImplemented
  83. }
  84. // Get returns the value stored in the given table/bucket and key.
  85. func (s *SimpleDB) Get(bucket, key []byte) ([]byte, error) {
  86. return nil, ErrNotImplemented
  87. }
  88. // Set sets the given value in the given table/bucket and key.
  89. func (s *SimpleDB) Set(bucket, key, value []byte) error {
  90. return ErrNotImplemented
  91. }
  92. // CmpAndSwap swaps the value at the given bucket and key if the current
  93. // value is equivalent to the oldValue input. Returns 'true' if the
  94. // swap was successful and 'false' otherwise.
  95. func (s *SimpleDB) CmpAndSwap(bucket, key, oldValue, newValue []byte) ([]byte, bool, error) {
  96. return nil, false, ErrNotImplemented
  97. }
  98. // Del deletes the data in the given table/bucket and key.
  99. func (s *SimpleDB) Del(bucket, key []byte) error {
  100. return ErrNotImplemented
  101. }
  102. // List returns a list of all the entries in a given table/bucket.
  103. func (s *SimpleDB) List(bucket []byte) ([]*database.Entry, error) {
  104. return nil, ErrNotImplemented
  105. }
  106. // Update performs a transaction with multiple read-write commands.
  107. func (s *SimpleDB) Update(tx *database.Tx) error {
  108. return ErrNotImplemented
  109. }
  110. // CreateTable creates a table or a bucket in the database.
  111. func (s *SimpleDB) CreateTable(bucket []byte) error {
  112. return ErrNotImplemented
  113. }
  114. // DeleteTable deletes a table or a bucket in the database.
  115. func (s *SimpleDB) DeleteTable(bucket []byte) error {
  116. return ErrNotImplemented
  117. }