-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssm.go
65 lines (56 loc) · 1.42 KB
/
ssm.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
package ssm
import (
"errors"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ssm"
"log"
)
var (
ErrInvalidConfig = errors.New("config cannot be nil")
ErrConfigInvalidRegion = errors.New("region not found: ssm region is a required config")
ErrSSMNotEnabled = errors.New("ssm is disabled")
)
type Config struct {
Enabled bool
SecretsPath string
Region string
}
type Client struct {
client *ssm.SSM
config *Config
}
func New(cfg *Config) (*Client, error) {
if cfg == nil {
return nil, ErrInvalidConfig
}
if !cfg.Enabled {
return nil, ErrSSMNotEnabled
}
if cfg.Region == "" {
return nil, ErrConfigInvalidRegion
}
opts := session.Options{
Config: aws.Config{
CredentialsChainVerboseErrors: aws.Bool(true),
Region: aws.String(cfg.Region),
},
}
sess := session.Must(session.NewSessionWithOptions(opts))
clt := Client{client: ssm.New(sess), config: cfg}
return &clt, nil
}
func (c *Client) GetValueByName(name string, decrypt bool) (string, error) {
path := fmt.Sprintf("%s/%s", c.config.SecretsPath, name)
input := ssm.GetParameterInput{
Name: aws.String(path),
WithDecryption: aws.Bool(decrypt),
}
out, err := c.client.GetParameter(&input)
if err != nil {
log.Fatalf("unable to get parameter from SSM at path: %s", path)
return "", err
}
return *out.Parameter.Value, nil
}