forked from hashicorp/vault
/
backend.go
144 lines (116 loc) · 2.79 KB
/
backend.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
package mongodb
import (
"context"
"fmt"
"strings"
"sync"
"time"
"github.com/hashicorp/vault/logical"
"github.com/hashicorp/vault/logical/framework"
"gopkg.in/mgo.v2"
)
func Factory(ctx context.Context, conf *logical.BackendConfig) (logical.Backend, error) {
b := Backend()
if err := b.Setup(ctx, conf); err != nil {
return nil, err
}
return b, nil
}
func Backend() *framework.Backend {
var b backend
b.Backend = &framework.Backend{
Help: strings.TrimSpace(backendHelp),
PathsSpecial: &logical.Paths{
SealWrapStorage: []string{
"config/connection",
},
},
Paths: []*framework.Path{
pathConfigConnection(&b),
pathConfigLease(&b),
pathListRoles(&b),
pathRoles(&b),
pathCredsCreate(&b),
},
Secrets: []*framework.Secret{
secretCreds(&b),
},
Clean: b.ResetSession,
Invalidate: b.invalidate,
BackendType: logical.TypeLogical,
}
return b.Backend
}
type backend struct {
*framework.Backend
session *mgo.Session
lock sync.Mutex
}
// Session returns the database connection.
func (b *backend) Session(ctx context.Context, s logical.Storage) (*mgo.Session, error) {
b.lock.Lock()
defer b.lock.Unlock()
if b.session != nil {
if err := b.session.Ping(); err == nil {
return b.session, nil
}
b.session.Close()
}
connConfigJSON, err := s.Get(ctx, "config/connection")
if err != nil {
return nil, err
}
if connConfigJSON == nil {
return nil, fmt.Errorf("configure the MongoDB connection with config/connection first")
}
var connConfig connectionConfig
if err := connConfigJSON.DecodeJSON(&connConfig); err != nil {
return nil, err
}
dialInfo, err := parseMongoURI(connConfig.URI)
if err != nil {
return nil, err
}
b.session, err = mgo.DialWithInfo(dialInfo)
if err != nil {
return nil, err
}
b.session.SetSyncTimeout(1 * time.Minute)
b.session.SetSocketTimeout(1 * time.Minute)
return b.session, nil
}
// ResetSession forces creation of a new connection next time Session() is called.
func (b *backend) ResetSession(_ context.Context) {
b.lock.Lock()
defer b.lock.Unlock()
if b.session != nil {
b.session.Close()
}
b.session = nil
}
func (b *backend) invalidate(ctx context.Context, key string) {
switch key {
case "config/connection":
b.ResetSession(ctx)
}
}
// LeaseConfig returns the lease configuration
func (b *backend) LeaseConfig(ctx context.Context, s logical.Storage) (*configLease, error) {
entry, err := s.Get(ctx, "config/lease")
if err != nil {
return nil, err
}
if entry == nil {
return nil, nil
}
var result configLease
if err := entry.DecodeJSON(&result); err != nil {
return nil, err
}
return &result, nil
}
const backendHelp = `
The mongodb backend dynamically generates MongoDB credentials.
After mounting this backend, configure it using the endpoints within
the "config/" path.
`