-
Notifications
You must be signed in to change notification settings - Fork 0
/
secrets.go
88 lines (69 loc) · 1.75 KB
/
secrets.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
77
78
79
80
81
82
83
84
85
86
87
88
package secretlamb
import (
"encoding/json"
"fmt"
"net/url"
"github.com/hashicorp/go-retryablehttp"
)
type Secrets struct {
*client
}
type SecretOutput struct {
Arn string `json:"ARN"`
Name string `json:"Name"`
VersionID string `json:"VersionId"`
SecretString string `json:"SecretString"`
VersionStages []string `json:"VersionStages"`
CreatedDate string `json:"CreatedDate"`
}
type SecretOption struct {
Key string
Value string
}
func SecretVersionId(versionId string) *SecretOption {
return &SecretOption{
Key: "versionId",
Value: versionId,
}
}
func SecretVersionStage(versionStage string) *SecretOption {
return &SecretOption{
Key: "versionStage",
Value: versionStage,
}
}
func NewSecrets() (*Secrets, error) {
client, err := newClient("/secretsmanager/get")
return &Secrets{client: client}, err
}
func MustNewSecrets() *Secrets {
client, err := NewSecrets()
if err != nil {
panic("NewSecrets(): " + err.Error())
}
return client
}
func (s *Secrets) WithRetry(retryMax int) *Secrets {
retryClient := retryablehttp.NewClient()
retryClient.RetryMax = retryMax
retryClient.CheckRetry = retryPolicy
s.client.HTTPClient = retryClient.StandardClient()
return s
}
func (s *Secrets) Get(secretId string, options ...*SecretOption) (*SecretOutput, error) {
query := &url.Values{}
query.Add("secretId", secretId)
for _, opt := range options {
query.Add(opt.Key, opt.Value)
}
body, err := s.client.get(query)
if err != nil {
return nil, fmt.Errorf("failed to get secret - http request error: %w", err)
}
output := &SecretOutput{}
err = json.Unmarshal(body, output)
if err != nil {
return nil, fmt.Errorf("failed to get secret - json unmarshal error: %w", err)
}
return output, nil
}