/
provider.go
73 lines (58 loc) · 1.98 KB
/
provider.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
package awssecrets
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/secretsmanager"
plugin_v1 "github.com/cyberark/secretless-broker/internal/plugin/v1"
)
// Provider provides data values from AWS Secrets Manager.
type Provider struct {
Name string
Client *secretsmanager.SecretsManager
}
// ProviderFactory constructs a Provider. The API client is configured from
// in-cluster environment variables and files.
func ProviderFactory(options plugin_v1.ProviderOptions) (plugin_v1.Provider, error) {
// All clients require a Session. The Session provides the client with
// shared configuration such as region, endpoint, and credentials. A
// Session should be shared where possible to take advantage of
// configuration and credential caching.
sess, err := session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
})
if err != nil {
return nil, fmt.Errorf("ERROR: Could not create AWS Secrets provider: %s", err)
}
// Create a new instance of the service's client with a Session.
client := secretsmanager.New(sess)
provider := &Provider{
Name: options.Name,
Client: client,
}
return provider, nil
}
// GetName returns the name of the provider
func (p *Provider) GetName() string {
return p.Name
}
// GetValues takes in variable ids and returns their resolved values. This method is
// needed to the Provider interface
func (p *Provider) GetValues(ids ...string) (map[string]plugin_v1.ProviderResponse, error) {
return plugin_v1.GetValues(p, ids...)
}
// GetValue obtains a secret value by id.
func (p *Provider) GetValue(id string) ([]byte, error) {
client := p.Client
req, resp := client.GetSecretValueRequest(&secretsmanager.GetSecretValueInput{
SecretId: aws.String(id),
})
err := req.Send()
if err != nil { // resp is now filled
return nil, err
}
if resp.SecretString != nil {
return []byte(*resp.SecretString), nil
}
return resp.SecretBinary, nil
}