-
Notifications
You must be signed in to change notification settings - Fork 16
/
secrets_manager.go
56 lines (49 loc) · 1.25 KB
/
secrets_manager.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
package secrets
import (
"fmt"
"sync"
)
var (
instance Secrets
secretBackends = make(map[string]BackendInit)
lock sync.RWMutex
)
// Instance returns the instance set via SetInstance. nil if not set.
func Instance() Secrets {
return instance
}
// SetInstance sets the singleton instance of the secrets backend.
func SetInstance(secretsInstance Secrets) error {
if secretsInstance != nil {
lock.Lock()
defer lock.Unlock()
instance = secretsInstance
return nil
}
return fmt.Errorf("Secrets instance cannot be nil")
}
// New returns a new instance of Secrets backend KMS identified by
// the supplied name. SecretConfig is a map of key value pairs which could
// be used for authenticating with the backend
func New(
name string,
secretConfig map[string]interface{},
) (Secrets, error) {
lock.RLock()
defer lock.RUnlock()
if bInit, exists := secretBackends[name]; exists {
return bInit(secretConfig)
}
return nil, ErrNotSupported
}
// Register adds a new backend KMS
func Register(name string, bInit BackendInit) error {
lock.Lock()
defer lock.Unlock()
if _, exists := secretBackends[name]; exists {
return fmt.Errorf("Secrets Backend provider %v is already"+
" registered", name)
}
secretBackends[name] = bInit
return nil
}