4
0

simple.go 4.3 KB

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