-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsecrets.go
More file actions
61 lines (50 loc) · 1.41 KB
/
Copy pathsecrets.go
File metadata and controls
61 lines (50 loc) · 1.41 KB
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
package config
import (
"context"
"encoding/json"
"errors"
"net/url"
"strings"
)
// SecretResolver looks up secrets from a source.
type SecretResolver struct {
Fetcher Fetcher
}
// NewSecretResolver returns new SecretResolver with an AWSSecretManager as the fetcher.
func NewSecretResolver() *SecretResolver {
return &SecretResolver{
Fetcher: NewAWSSecretManager(),
}
}
// Resolve resolves the secret if needed, and returns the value.
func (r *SecretResolver) Resolve(ctx context.Context, val string) (*string, error) {
u, err := url.Parse(val)
if err != nil {
return nil, err
}
if len(u.Scheme) == 0 || strings.HasPrefix(u.Scheme, "postgres") {
// we can just use the datasource as is.
return &val, nil
}
if u.Scheme != "secretsmanager" {
// we do not know how to resolve this url
return nil, errors.New("Unresolvable")
}
// We need to retreive the connection string details from the secret manager.
//
// The secret name is in the host part of the url.
name := u.Host
// get the value from the secret mananager
b, err := r.Fetcher.Get(ctx, name)
if err != nil {
return nil, err
}
// the DB secrets are stored in JSON format for RDS, so we need to parse the value.
secret := RDSSecret{}
if err := json.Unmarshal(b, &secret); err != nil {
return nil, err
}
// build the connection string from the connection string elements.
connStr := secret.ConnectionString()
return &connStr, nil
}