-
Notifications
You must be signed in to change notification settings - Fork 2
/
ssmcache.go
91 lines (71 loc) · 1.93 KB
/
ssmcache.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
89
90
91
package ssmcache
import (
"log"
"sync"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ssm"
"github.com/aws/aws-sdk-go/service/ssm/ssmiface"
"github.com/pkg/errors"
)
var defaultExpiry = 30 * time.Second
// SetDefaultExpiry update the default expiry for all cached parameters
//
// Note this will update expires value on the next refresh of entries.
func SetDefaultExpiry(expires time.Duration) {
defaultExpiry = expires
}
// Entry an SSM entry in the cache
type Entry struct {
value string
expires time.Time
}
// Cache SSM cache which provides read access to parameters
type Cache interface {
GetKey(key string) (string, error)
}
type cache struct {
ssm sync.Mutex
ssmValues map[string]*Entry
ssmSvc ssmiface.SSMAPI
}
// New new SSM cache
func New(sess *session.Session) Cache {
return &cache{
ssmSvc: ssm.New(sess),
ssmValues: make(map[string]*Entry),
}
}
// GetKey retrieve a parameter from SSM and cache it.
func (ssc *cache) GetKey(key string) (string, error) {
ssc.ssm.Lock()
defer ssc.ssm.Unlock()
ent, ok := ssc.ssmValues[key]
if !ok {
// record is missing
return ssc.updateParam(key)
}
if time.Now().After(ent.expires) {
// we have expired and need to refresh
log.Println("expired cache refreshing value")
return ssc.updateParam(key)
}
// return the value
return ent.value, nil
}
func (ssc *cache) updateParam(key string) (string, error) {
log.Println("updating key from ssm:", key)
resp, err := ssc.ssmSvc.GetParameter(&ssm.GetParameterInput{
Name: aws.String(key),
})
if err != nil {
return "", errors.Wrapf(err, "failed to retrieve key %s from ssm", key)
}
ssc.ssmValues[key] = &Entry{
value: aws.StringValue(resp.Parameter.Value),
expires: time.Now().Add(defaultExpiry), // reset the expiry
}
log.Println("key value refreshed from ssm at:", time.Now())
return aws.StringValue(resp.Parameter.Value), nil
}