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
|
package data
import (
"context"
"fmt"
"net/url"
"path"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/service/secretsmanager"
"github.com/pkg/errors"
gaws "github.com/hairyhenderson/gomplate/v3/aws"
)
// awsSecretsManagerGetter - A subset of Secrets Manager API for use in unit testing
type awsSecretsManagerGetter interface {
GetSecretValueWithContext(ctx context.Context, input *secretsmanager.GetSecretValueInput, opts ...request.Option) (*secretsmanager.GetSecretValueOutput, error)
}
func parseDatasourceURLArgs(sourceURL *url.URL, args ...string) (params map[string]interface{}, p string, err error) {
if len(args) >= 2 {
err = fmt.Errorf("maximum two arguments to %s datasource: alias, extraPath (found %d)",
sourceURL.Scheme, len(args))
return nil, "", err
}
p = sourceURL.Path
params = make(map[string]interface{})
for key, val := range sourceURL.Query() {
params[key] = strings.Join(val, " ")
}
if p == "" && sourceURL.Opaque != "" {
p = sourceURL.Opaque
}
if len(args) == 1 {
parsed, err := url.Parse(args[0])
if err != nil {
return nil, "", err
}
if parsed.Path != "" {
p = path.Join(p, parsed.Path)
if strings.HasSuffix(parsed.Path, "/") {
p += "/"
}
}
for key, val := range parsed.Query() {
params[key] = strings.Join(val, " ")
}
}
return params, p, nil
}
func readAWSSecretsManager(ctx context.Context, source *Source, args ...string) (output []byte, err error) {
if source.awsSecretsManager == nil {
source.awsSecretsManager = secretsmanager.New(gaws.SDKSession())
}
_, paramPath, err := parseDatasourceURLArgs(source.URL, args...)
if err != nil {
return nil, err
}
return readAWSSecretsManagerParam(ctx, source, paramPath)
}
func readAWSSecretsManagerParam(ctx context.Context, source *Source, paramPath string) ([]byte, error) {
input := &secretsmanager.GetSecretValueInput{
SecretId: aws.String(paramPath),
}
response, err := source.awsSecretsManager.GetSecretValueWithContext(ctx, input)
if err != nil {
return nil, errors.Wrapf(err, "Error reading aws+sm from AWS using GetSecretValue with input %v", input)
}
return []byte(*response.SecretString), nil
}
|