-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongodb_depot.go
184 lines (158 loc) · 5.15 KB
/
mongodb_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
174
175
176
177
178
179
180
181
182
183
184
package certdepot
import (
"context"
"github.com/cdr/grip"
"github.com/cdr/grip/message"
"github.com/pkg/errors"
"github.com/square/certstrap/depot"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type mongoDepot struct {
ctx context.Context
client *mongo.Client
databaseName string
collectionName string
opts DepotOptions
}
// NewMongoDBCertDepot returns a new cert depot backed by MongoDB using the
// mongo driver.
func NewMongoDBCertDepot(ctx context.Context, opts *MongoDBOptions) (Depot, error) {
if err := opts.validate(); err != nil {
return nil, errors.Wrap(err, "invalid options")
}
client, err := mongo.Connect(ctx, options.Client().ApplyURI(opts.MongoDBURI).SetConnectTimeout(opts.MongoDBDialTimeout))
if err != nil {
return nil, errors.Wrap(err, "problem connecting to database")
}
return &mongoDepot{
ctx: ctx,
client: client,
databaseName: opts.DatabaseName,
collectionName: opts.CollectionName,
opts: opts.DepotOptions,
}, nil
}
// NewMongoDBCertDepotWithClient returns a new cert depot backed by MongoDB
// using the provided mongo driver client.
func NewMongoDBCertDepotWithClient(ctx context.Context, client *mongo.Client, opts *MongoDBOptions) (Depot, error) {
if client == nil {
return nil, errors.New("must specify a non-nil client")
}
if err := opts.validate(); err != nil {
return nil, errors.Wrap(err, "invalid options")
}
return &mongoDepot{
ctx: ctx,
client: client,
databaseName: opts.DatabaseName,
collectionName: opts.CollectionName,
opts: opts.DepotOptions,
}, nil
}
// Put inserts the data into the document specified by the tag.
func (m *mongoDepot) 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)
}
update := bson.M{"$set": bson.M{key: string(data)}}
res, err := m.client.Database(m.databaseName).Collection(m.collectionName).UpdateOne(m.ctx,
bson.D{{Key: userIDKey, Value: name}},
update,
options.Update().SetUpsert(true))
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,
"matched": res.MatchedCount,
"modified": res.ModifiedCount,
"op": "put",
})
return nil
}
// Check returns whether the user and data specified by the tag exists.
func (m *mongoDepot) Check(tag *depot.Tag) bool {
name, key, err := getNameAndKey(tag)
if err != nil {
return false
}
u := &User{}
err = m.client.Database(m.databaseName).Collection(m.collectionName).FindOne(m.ctx, bson.D{{Key: userIDKey, Value: name}}).Decode(u)
grip.WarningWhen(errNotNoDocuments(err), message.WrapError(err, message.Fields{
"db": m.databaseName,
"coll": m.collectionName,
"id": name,
"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 *mongoDepot) 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)
}
u := &User{}
if err = m.client.Database(m.databaseName).Collection(m.collectionName).FindOne(m.ctx, bson.D{{Key: userIDKey, Value: name}}).Decode(u); err != nil {
if err == mongo.ErrNoDocuments {
return nil, errors.Wrapf(err, "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 *mongoDepot) Delete(tag *depot.Tag) error {
name, key, err := getNameAndKey(tag)
if err != nil {
return errors.Wrapf(err, "could not format name %s", name)
}
if _, err = m.client.Database(m.databaseName).Collection(m.collectionName).UpdateOne(m.ctx,
bson.D{{Key: userIDKey, Value: name}},
bson.M{"$unset": bson.M{key: ""}}); errNotNoDocuments(err) {
return errors.Wrapf(err, "problem deleting %s.%s from the database", name, key)
}
return nil
}
func (m *mongoDepot) Save(name string, creds *Credentials) error { return depotSave(m, name, creds) }
func (m *mongoDepot) Find(name string) (*Credentials, error) { return depotFind(m, name, m.opts) }
func (m *mongoDepot) Generate(name string) (*Credentials, error) {
return depotGenerate(m, name, m.opts)
}
func errNotNoDocuments(err error) bool {
return err != nil && err != mongo.ErrNoDocuments
}