simple.go 4.0 KB

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