-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmgo_depot.go
173 lines (150 loc) · 4.6 KB
/
mgo_depot.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package certdepot
import (
"github.com/cdr/grip"
"github.com/cdr/grip/message"
"github.com/pkg/errors"
"github.com/square/certstrap/depot"
mgo "gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
type mgoCertDepot struct {
session *mgo.Session
databaseName string
collectionName string
opts DepotOptions
}
// NewMgoCertDepot creates a new cert depot using the legacy mgo driver.
func NewMgoCertDepot(opts *MongoDBOptions) (Depot, error) {
s, err := mgo.DialWithTimeout(opts.MongoDBURI, opts.MongoDBDialTimeout)
if err != nil {
return nil, errors.Wrapf(err, "could not connect to db %s", opts.MongoDBURI)
}
s.SetSocketTimeout(opts.MongoDBSocketTimeout)
return NewMgoCertDepotWithSession(s, opts)
}
// NewMgoCertDepotWithSession creates a certificate depot using the provided
// legacy mgo drivers session.
func NewMgoCertDepotWithSession(s *mgo.Session, opts *MongoDBOptions) (Depot, error) {
if s == nil {
return nil, errors.New("must specify a non-nil session")
}
if err := opts.validate(); err != nil {
return nil, errors.Wrap(err, "invalid options!")
}
return &mgoCertDepot{
session: s,
databaseName: opts.DatabaseName,
collectionName: opts.CollectionName,
opts: opts.DepotOptions,
}, nil
}
// Put inserts the data into the document specified by the tag.
func (m *mgoCertDepot) Put(tag *depot.Tag, data []byte) error {
if data == nil {
return errors.New("data is nil")
}
name, key, err := getNameAndKey(tag)
if err != nil {
return errors.Wrapf(err, "could not format name %s", name)
}
session := m.session.Clone()
defer session.Close()
update := bson.M{"$set": bson.M{key: string(data)}}
changeInfo, err := session.DB(m.databaseName).C(m.collectionName).UpsertId(name, update)
if err != nil {
return errors.Wrap(err, "problem adding data to the database")
}
grip.Debug(message.Fields{
"db": m.databaseName,
"coll": m.collectionName,
"id": name,
"change": changeInfo,
"op": "put",
})
return nil
}
// Check returns whether the user and data specified by the tag exists.
func (m *mgoCertDepot) Check(tag *depot.Tag) bool {
name, key, err := getNameAndKey(tag)
if err != nil {
return false
}
session := m.session.Clone()
defer session.Close()
u := &User{}
if err = session.DB(m.databaseName).C(m.collectionName).FindId(name).One(u); errNotNotFound(err) {
grip.Warning(message.Fields{
"db": m.databaseName,
"coll": m.collectionName,
"id": name,
"err": err,
"op": "check",
})
}
switch key {
case userCertKey:
return u.Cert != ""
case userPrivateKeyKey:
return u.PrivateKey != ""
case userCertReqKey:
return u.CertReq != ""
case userCertRevocListKey:
return u.CertRevocList != ""
default:
return false
}
}
// Get reads the data for the user specified by tag. Returns an error if the
// user does not exist or if the data is empty.
func (m *mgoCertDepot) Get(tag *depot.Tag) ([]byte, error) {
name, key, err := getNameAndKey(tag)
if err != nil {
return nil, errors.Wrapf(err, "could not format name %s", name)
}
session := m.session.Clone()
defer session.Close()
u := &User{}
if err = session.DB(m.databaseName).C(m.collectionName).FindId(name).One(u); err != nil {
if err == mgo.ErrNotFound {
return nil, errors.Errorf("could not find %s in the database", name)
}
return nil, errors.Wrapf(err, "problem looking up %s in the database", name)
}
var data []byte
switch key {
case userCertKey:
data = []byte(u.Cert)
case userPrivateKeyKey:
data = []byte(u.PrivateKey)
case userCertReqKey:
data = []byte(u.CertReq)
case userCertRevocListKey:
data = []byte(u.CertRevocList)
}
if len(data) == 0 {
return nil, errors.New("no data available")
}
return data, nil
}
// Delete removes the data from a user specified by the tag.
func (m *mgoCertDepot) Delete(tag *depot.Tag) error {
name, key, err := getNameAndKey(tag)
if err != nil {
return errors.Wrapf(err, "could not format name %s", name)
}
session := m.session.Clone()
defer session.Close()
update := bson.M{"$unset": bson.M{key: ""}}
if err = m.session.DB(m.databaseName).C(m.collectionName).UpdateId(name, update); errNotNotFound(err) {
return errors.Wrapf(err, "problem deleting %s.%s from the database", name, key)
}
return nil
}
func (m *mgoCertDepot) Save(name string, creds *Credentials) error { return depotSave(m, name, creds) }
func (m *mgoCertDepot) Find(name string) (*Credentials, error) { return depotFind(m, name, m.opts) }
func (m *mgoCertDepot) Generate(name string) (*Credentials, error) {
return depotGenerate(m, name, m.opts)
}
func errNotNotFound(err error) bool {
return err != nil && err != mgo.ErrNotFound
}