diff options
Diffstat (limited to 'server')
-rw-r--r-- | server/config/config.go | 2 | ||||
-rw-r--r-- | server/config/config_test.go | 3 | ||||
-rw-r--r-- | server/store/mongo.go | 123 | ||||
-rw-r--r-- | server/store/store.go | 2 | ||||
-rw-r--r-- | server/store/store_test.go | 12 |
5 files changed, 1 insertions, 141 deletions
diff --git a/server/config/config.go b/server/config/config.go index 9ac4a7d..573ae85 100644 --- a/server/config/config.go +++ b/server/config/config.go @@ -95,7 +95,7 @@ func convertDatastoreConfig(c *Config) { conf := c.Server.Datastore engine := strings.Split(conf, ":")[0] switch engine { - case "mysql", "mongo": + case "mysql": s := strings.SplitN(conf, ":", 4) engine, user, passwd, addrs := s[0], s[1], s[2], s[3] c.Server.Database = map[string]string{ diff --git a/server/config/config_test.go b/server/config/config_test.go index 182436a..b3f356e 100644 --- a/server/config/config_test.go +++ b/server/config/config_test.go @@ -68,9 +68,6 @@ func TestDatastoreConversion(t *testing.T) { "mysql:user:passwd:localhost:3306", Database{"type": "mysql", "username": "user", "password": "passwd", "address": "localhost:3306"}, }, { - "mongo:::host1,host2", Database{"type": "mongo", "username": "", "password": "", "address": "host1,host2"}, - }, - { "mem", Database{"type": "mem"}, }, { diff --git a/server/store/mongo.go b/server/store/mongo.go deleted file mode 100644 index 6a23e89..0000000 --- a/server/store/mongo.go +++ /dev/null @@ -1,123 +0,0 @@ -package store - -import ( - "strings" - "time" - - "github.com/nsheridan/cashier/server/config" - - "golang.org/x/crypto/ssh" - - mgo "gopkg.in/mgo.v2" - "gopkg.in/mgo.v2/bson" -) - -var ( - certsDB = "certs" - issuedTable = "issued_certs" -) - -func collection(session *mgo.Session) *mgo.Collection { - return session.DB(certsDB).C(issuedTable) -} - -// NewMongoStore returns a MongoDB CertStorer. -func NewMongoStore(c config.Database) (*MongoStore, error) { - m := &mgo.DialInfo{ - Addrs: strings.Split(c["address"], ","), - Username: c["username"], - Password: c["password"], - Database: certsDB, - Timeout: time.Second * 5, - } - session, err := mgo.DialWithInfo(m) - if err != nil { - return nil, err - } - return &MongoStore{ - session: session, - }, nil -} - -var _ CertStorer = (*MongoStore)(nil) - -// MongoStore is a MongoDB-based CertStorer -type MongoStore struct { - session *mgo.Session -} - -// Get a single *CertRecord -func (m *MongoStore) Get(id string) (*CertRecord, error) { - s := m.session.Copy() - defer s.Close() - if err := s.Ping(); err != nil { - return nil, err - } - c := &CertRecord{} - err := collection(s).Find(bson.M{"keyid": id}).One(c) - return c, err -} - -// SetCert parses a *ssh.Certificate and records it -func (m *MongoStore) SetCert(cert *ssh.Certificate) error { - r := parseCertificate(cert) - return m.SetRecord(r) -} - -// SetRecord records a *CertRecord -func (m *MongoStore) SetRecord(record *CertRecord) error { - s := m.session.Copy() - defer s.Close() - if err := s.Ping(); err != nil { - return err - } - return collection(s).Insert(record) -} - -// List returns all recorded certs. -// By default only active certs are returned. -func (m *MongoStore) List(includeExpired bool) ([]*CertRecord, error) { - s := m.session.Copy() - defer s.Close() - if err := s.Ping(); err != nil { - return nil, err - } - var result []*CertRecord - var err error - c := collection(s) - if includeExpired { - err = c.Find(nil).All(&result) - } else { - err = c.Find(bson.M{"expires": bson.M{"$gte": time.Now().UTC()}}).All(&result) - } - return result, err -} - -// Revoke an issued cert by id. -func (m *MongoStore) Revoke(id string) error { - s := m.session.Copy() - defer s.Close() - if err := s.Ping(); err != nil { - return err - } - c := collection(s) - return c.Update(bson.M{"keyid": id}, bson.M{"$set": bson.M{"revoked": true}}) -} - -// GetRevoked returns all revoked certs -func (m *MongoStore) GetRevoked() ([]*CertRecord, error) { - s := m.session.Copy() - defer s.Close() - if err := s.Ping(); err != nil { - return nil, err - } - var result []*CertRecord - err := collection(s).Find(bson.M{"expires": bson.M{"$gte": time.Now().UTC()}, "revoked": true}).All(&result) - return result, err -} - -// Close the connection to the database -func (m *MongoStore) Close() error { - m.session.Close() - return nil -} diff --git a/server/store/store.go b/server/store/store.go index 249489a..d157fd1 100644 --- a/server/store/store.go +++ b/server/store/store.go @@ -13,8 +13,6 @@ import ( // New returns a new configured database. func New(c config.Database) (CertStorer, error) { switch c["type"] { - case "mongo": - return NewMongoStore(c) case "mysql", "sqlite": return NewSQLStore(c) case "mem": diff --git a/server/store/store_test.go b/server/store/store_test.go index 4196c37..47e0f74 100644 --- a/server/store/store_test.go +++ b/server/store/store_test.go @@ -111,18 +111,6 @@ func TestMySQLStore(t *testing.T) { testStore(t, db) } -func TestMongoStore(t *testing.T) { - t.Parallel() - if os.Getenv("MONGO_TEST") == "" { - t.Skip("No MONGO_TEST environment variable") - } - db, err := NewMongoStore(map[string]string{"type": "mongo"}) - if err != nil { - t.Error(err) - } - testStore(t, db) -} - func TestSQLiteStore(t *testing.T) { t.Parallel() f, err := ioutil.TempFile("", "sqlite_test_db") |