aboutsummaryrefslogtreecommitdiff
path: root/db/mongodb/client.go
diff options
context:
space:
mode:
Diffstat (limited to 'db/mongodb/client.go')
-rw-r--r--db/mongodb/client.go13
1 files changed, 6 insertions, 7 deletions
diff --git a/db/mongodb/client.go b/db/mongodb/client.go
index 1f3815d..f06f5ec 100644
--- a/db/mongodb/client.go
+++ b/db/mongodb/client.go
@@ -7,12 +7,11 @@ import (
7 "net/url" 7 "net/url"
8 "strings" 8 "strings"
9 9
10 "code.crute.us/mcrute/golib/secrets"
10 "go.mongodb.org/mongo-driver/bson" 11 "go.mongodb.org/mongo-driver/bson"
11 "go.mongodb.org/mongo-driver/mongo" 12 "go.mongodb.org/mongo-driver/mongo"
12 "go.mongodb.org/mongo-driver/mongo/options" 13 "go.mongodb.org/mongo-driver/mongo/options"
13 "go.mongodb.org/mongo-driver/x/mongo/driver/connstring" 14 "go.mongodb.org/mongo-driver/x/mongo/driver/connstring"
14
15 "code.crute.us/mcrute/golib/vault"
16) 15)
17 16
18// AnyInTopLevelArray is just a convenience method so apps don't have to repeat 17// AnyInTopLevelArray is just a convenience method so apps don't have to repeat
@@ -26,12 +25,12 @@ type Mongo struct {
26 db *mongo.Database 25 db *mongo.Database
27} 26}
28 27
29func Connect(ctx context.Context, uri string, vc vault.VaultClient) (*Mongo, error) { 28func Connect(ctx context.Context, uri string, sc secrets.Client) (*Mongo, error) {
30 db := &Mongo{} 29 db := &Mongo{}
31 30
32 // Prefix uri with mongodb:// unless it already includes one of the 31 // Prefix uri with mongodb:// unless it already includes one of the
33 // standard prefixes (only these two are valid). Otherwise if scheme is 32 // standard prefixes (only these two are valid). Otherwise if scheme is
34 // omitted then url parsing will fail to capture the username for Vault 33 // omitted then url parsing will fail to capture the username for secret
35 // lookup. 34 // lookup.
36 if !strings.HasPrefix(uri, "mongodb://") && !strings.HasPrefix(uri, "mongodb+srv://") { 35 if !strings.HasPrefix(uri, "mongodb://") && !strings.HasPrefix(uri, "mongodb+srv://") {
37 uri = "mongodb://" + uri 36 uri = "mongodb://" + uri
@@ -43,11 +42,11 @@ func Connect(ctx context.Context, uri string, vc vault.VaultClient) (*Mongo, err
43 } 42 }
44 43
45 // The username provided by the user (there should be no 44 // The username provided by the user (there should be no
46 // password) will be a reference to a vault material with the 45 // password) will be a reference to a secret material with the
47 // prefix database/creds/. This needs to be replaced with the real 46 // prefix database/creds/. This needs to be replaced with the real
48 // username/password pair fetched from Vault before attempting to 47 // username/password pair fetched from secret before attempting to
49 // connect. 48 // connect.
50 cred, err := vc.DbCredential(ctx, u.User.Username()) 49 cred, _, err := sc.DatabaseCredential(ctx, u.User.Username())
51 if err != nil { 50 if err != nil {
52 return nil, err 51 return nil, err
53 } 52 }