This repository has been archived by the owner on Mar 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
backend.go
87 lines (77 loc) · 2.08 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
package splunk
import (
"context"
"strings"
"sync"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
"github.com/splunk/vault-plugin-splunk/clients/splunk"
)
type backend struct {
*framework.Backend
conn *sync.Map
}
// Factory is the factory function to create a Splunk backend.
func Factory(ctx context.Context, conf *logical.BackendConfig) (logical.Backend, error) {
b := newBackend()
if err := b.Setup(ctx, conf); err != nil {
return nil, err
}
return b, nil
}
func newBackend() logical.Backend {
b := backend{}
b.Backend = &framework.Backend{
Help: strings.TrimSpace(backendHelp),
PathsSpecial: &logical.Paths{
SealWrapStorage: []string{
"config/",
},
},
Paths: []*framework.Path{
b.pathConfigConnection(),
b.pathConnectionsList(),
b.pathResetConnection(),
b.pathRotateRoot(),
b.pathRolesList(),
b.pathRoles(),
b.pathCredsCreate(),
b.pathCredsCreateMulti(),
},
Secrets: []*framework.Secret{
b.pathSecretCreds(),
},
WALRollback: b.walRollback,
WALRollbackMinAge: walRollbackMinAge,
BackendType: logical.TypeLogical,
}
b.conn = new(sync.Map)
return &b
}
func (b *backend) ensureConnection(ctx context.Context, config *splunkConfig) (*splunk.API, error) {
if conn, ok := b.conn.Load(config.ID); ok {
return conn.(*splunk.API), nil
}
// create and cache connection
conn, err := config.newConnection(ctx)
if err != nil {
return nil, err
}
if conn, loaded := b.conn.LoadOrStore(config.ID, conn); loaded {
// somebody else won the race
return conn.(*splunk.API), nil
}
return conn, nil
}
// clearConnection closes the connection and removes it from the cache.
func (b *backend) clearConnection(id string) error {
b.conn.Delete(id)
return nil
}
const backendHelp = `
The Splunk backend rotates admin credentials and dynamically generates new
users with limited life-time.
After mounting this backend, credentials for a Splunk admin role must
be configured and connections and roles must be written using
the "config/" and "roles/" endpoints before any logins can be generated.
`