-
Notifications
You must be signed in to change notification settings - Fork 205
/
store.go
76 lines (64 loc) · 1.72 KB
/
store.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
package pluginstore
import (
"get.porter.sh/porter/pkg/config"
"get.porter.sh/porter/pkg/plugins/pluggable"
"get.porter.sh/porter/pkg/secrets"
cnabsecrets "github.com/cnabio/cnab-go/secrets"
"github.com/pkg/errors"
)
var _ cnabsecrets.Store = &Store{}
// Store is a plugin-backed source of secrets. It resolves the appropriate
// plugin based on Porter's config and implements the secrets.Store interface
// using the backing plugin.
type Store struct {
*config.Config
*secrets.SecretStore
cleanup func()
}
func NewStore(c *config.Config) *Store {
return &Store{
Config: c,
}
}
// NewSecretsPluginConfig for secret sources.
func NewSecretsPluginConfig() pluggable.PluginTypeConfig {
return pluggable.PluginTypeConfig{
Interface: secrets.PluginInterface,
Plugin: &secrets.Plugin{},
GetDefaultPluggable: func(datastore *config.Data) string {
return datastore.GetDefaultSecretSource()
},
GetPluggable: func(datastore *config.Data, name string) (pluggable.Entry, error) {
return datastore.GetSecretSource(name)
},
GetDefaultPlugin: func(datastore *config.Data) string {
return datastore.GetDefaultSecretsPlugin()
},
}
}
func (s *Store) Connect() error {
if s.SecretStore != nil {
return nil
}
pluginType := NewSecretsPluginConfig()
l := pluggable.NewPluginLoader(s.Config)
raw, cleanup, err := l.Load(pluginType)
if err != nil {
return err
}
s.cleanup = cleanup
store, ok := raw.(cnabsecrets.Store)
if !ok {
cleanup()
return errors.Errorf("the interface exposed by the %s plugin was not secrets.Store", l.SelectedPluginKey)
}
s.SecretStore = secrets.NewSecretStore(store)
return nil
}
func (s *Store) Close() error {
if s.cleanup != nil {
s.cleanup()
}
s.SecretStore = nil
return nil
}