Debian Codegraph @

providers/parameterstore/parameterstore.go

raw
1 // Package parameterstore implements a koanf.Provider for AWS Systems Manager Parameter Store.
2 package parameterstore
3
4 import (
5 "context"
6 "errors"
7
8 awsconfig "github.com/aws/aws-sdk-go-v2/config"
9 "github.com/aws/aws-sdk-go-v2/service/ssm"
10 "github.com/knadh/koanf/maps"
11 )
12
13 // Input is a constraint that permits any input type
14 // to get parameters from AWS Systems Manager Parameter Store.
15 type Input interface {
16 ssm.GetParameterInput | ssm.GetParametersInput | ssm.GetParametersByPathInput
17 }
18
19 // Config represents a ParameterStore provider configuration.
20 type Config[T Input] struct {
21 // Delim is the delimiter to use
22 // when specifying config key paths, for instance a . for `parent.child.key`
23 // or a / for `parent/child/key`.
24 Delim string
25
26 // Input is the input to get parameters.
27 Input T
28
29 // OptFns is the additional functional options to get parameters.
30 OptFns []func(*ssm.Options)
31
32 // Callback is an optional callback that takes a (key, value)
33 // with the variable name and value and allows you to modify both.
34 // If the callback returns an empty key, the variable will be ignored.
35 Callback func(key, value string) (string, any)
36 }
37
38 // ParameterStore implements an AWS Systems Manager Parameter Store provider.
39 type ParameterStore[T Input] struct {
40 client *ssm.Client
41 config Config[T]
42 }
43
44 // Provider returns a ParameterStore provider.
45 // The AWS Systems Manager Client is configured via environment variables.
46 // The configuration values are read from the environment variables.
47 // - AWS_REGION
48 // - AWS_ACCESS_KEY_ID
49 // - AWS_SECRET_ACCESS_KEY
50 // - AWS_SESSION_TOKEN
51 func Provider[T Input](config Config[T]) (*ParameterStore[T], error) {
52 c, err := awsconfig.LoadDefaultConfig(context.TODO())
53 if err != nil {
54 return nil, err
55 }
56 return ProviderWithClient[T](config, ssm.NewFromConfig(c)), nil
57 }
58
59 // ProviderWithClient returns a ParameterStore provider
60 // using an existing AWS Systems Manager client.
61 func ProviderWithClient[T Input](config Config[T], client *ssm.Client) *ParameterStore[T] {
62 return &ParameterStore[T]{
63 client: client,
64 config: config,
65 }
66 }
67
68 // ReadBytes is not supported by the ParameterStore provider.
69 func (ps *ParameterStore[T]) ReadBytes() ([]byte, error) {
70 return nil, errors.New("parameterstore provider does not support this method")
71 }
72
73 // Read returns a nested config map.
74 func (ps *ParameterStore[T]) Read() (map[string]any, error) {
75 var (
76 mp = make(map[string]any)
77 )
78 switch input := any(ps.config.Input).(type) {
79 case ssm.GetParameterInput:
80 output, err := ps.client.GetParameter(context.TODO(), &input, ps.config.OptFns...)
81 if err != nil {
82 return nil, err
83 }
84 // If there's a transformation callback, run it.
85 if ps.config.Callback != nil {
86 name, value := ps.config.Callback(*output.Parameter.Name, *output.Parameter.Value)
87 // If the callback blanked the key, it should be omitted.
88 if name == "" {
89 break
90 }
91 mp[name] = value
92 } else {
93 mp[*output.Parameter.Name] = *output.Parameter.Value
94 }
95 case ssm.GetParametersInput:
96 output, err := ps.client.GetParameters(context.TODO(), &input, ps.config.OptFns...)
97 if err != nil {
98 return nil, err
99 }
100 for _, p := range output.Parameters {
101 // If there's a transformation callback, run it.
102 if ps.config.Callback != nil {
103 name, value := ps.config.Callback(*p.Name, *p.Value)
104 // If the callback blanked the key, it should be omitted.
105 if name == "" {
106 break
107 }
108 mp[name] = value
109 } else {
110 mp[*p.Name] = *p.Value
111 }
112 }
113 case ssm.GetParametersByPathInput:
114 var nextToken *string
115 for {
116 input.NextToken = nextToken
117 output, err := ps.client.GetParametersByPath(context.TODO(), &input, ps.config.OptFns...)
118 if err != nil {
119 return nil, err
120 }
121 for _, p := range output.Parameters {
122 // If there's a transformation callback, run it.
123 if ps.config.Callback != nil {
124 name, value := ps.config.Callback(*p.Name, *p.Value)
125 // If the callback blanked the key, it should be omitted.
126 if name == "" {
127 break
128 }
129 mp[name] = value
130 } else {
131 mp[*p.Name] = *p.Value
132 }
133 }
134 if output.NextToken == nil {
135 break
136 }
137 nextToken = output.NextToken
138 }
139 }
140 // Unflatten only when a delimiter is specified.
141 if ps.config.Delim != "" {
142 mp = maps.Unflatten(mp, ps.config.Delim)
143 }
144 return mp, nil
145 }