/
options.go
256 lines (229 loc) · 6.16 KB
/
options.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package awskms
import (
"strconv"
"github.com/hashicorp/go-hclog"
wrapping "github.com/hashicorp/go-kms-wrapping/v2"
)
// getOpts iterates the inbound Options and returns a struct
func getOpts(opt ...wrapping.Option) (*options, error) {
// First, separate out options into local and global
opts := getDefaultOptions()
var wrappingOptions []wrapping.Option
var localOptions []OptionFunc
for _, o := range opt {
if o == nil {
continue
}
iface := o()
switch to := iface.(type) {
case wrapping.OptionFunc:
wrappingOptions = append(wrappingOptions, o)
case OptionFunc:
localOptions = append(localOptions, to)
}
}
// Parse the global options
var err error
opts.Options, err = wrapping.GetOpts(wrappingOptions...)
if err != nil {
return nil, err
}
// Don't ever return blank options
if opts.Options == nil {
opts.Options = new(wrapping.Options)
}
// Local options can be provided either via the WithConfigMap field
// (for over the plugin barrier or embedding) or via local option functions
// (for embedding). First pull from the option.
if opts.WithConfigMap != nil {
for k, v := range opts.WithConfigMap {
switch k {
case "disallow_env_vars":
disallowEnvVars, err := strconv.ParseBool(v)
if err != nil {
return nil, err
}
opts.withDisallowEnvVars = disallowEnvVars
case "key_not_required":
keyNotRequired, err := strconv.ParseBool(v)
if err != nil {
return nil, err
}
opts.withKeyNotRequired = keyNotRequired
case "kms_key_id": // deprecated backend-specific value, set global
opts.WithKeyId = v
case "region":
opts.withRegion = v
case "endpoint":
opts.withEndpoint = v
case "access_key":
opts.withAccessKey = v
case "secret_key":
opts.withSecretKey = v
case "session_token":
opts.withSessionToken = v
case "shared_creds_filename":
opts.withSharedCredsFilename = v
case "shared_creds_profile":
opts.withSharedCredsProfile = v
case "web_identity_token_file":
opts.withWebIdentityTokenFile = v
case "role_session_name":
opts.withRoleSessionName = v
case "role_arn":
opts.withRoleArn = v
}
}
}
// Now run the local options functions. This may overwrite options set by
// the options above.
for _, o := range localOptions {
if o != nil {
if err := o(&opts); err != nil {
return nil, err
}
}
}
return &opts, nil
}
// OptionFunc holds a function with local options
type OptionFunc func(*options) error
// options = how options are represented
type options struct {
*wrapping.Options
withDisallowEnvVars bool
withKeyNotRequired bool
withRegion string
withEndpoint string
withAccessKey string
withSecretKey string
withSessionToken string
withSharedCredsFilename string
withSharedCredsProfile string
withWebIdentityTokenFile string
withRoleSessionName string
withRoleArn string
withLogger hclog.Logger
}
func getDefaultOptions() options {
return options{}
}
// WithDisallowEnvVars provides a way to disable using env vars
func WithDisallowEnvVars(with bool) wrapping.Option {
return func() interface{} {
return OptionFunc(func(o *options) error {
o.withDisallowEnvVars = with
return nil
})
}
}
// WithKeyNotRequired provides a way to not require a key at config time
func WithKeyNotRequired(with bool) wrapping.Option {
return func() interface{} {
return OptionFunc(func(o *options) error {
o.withKeyNotRequired = with
return nil
})
}
}
// WithRegion provides a way to chose the region
func WithRegion(with string) wrapping.Option {
return func() interface{} {
return OptionFunc(func(o *options) error {
o.withRegion = with
return nil
})
}
}
// WithEndpoint provides a way to chose the endpoint
func WithEndpoint(with string) wrapping.Option {
return func() interface{} {
return OptionFunc(func(o *options) error {
o.withEndpoint = with
return nil
})
}
}
// WithAccessKey provides a way to chose the access key
func WithAccessKey(with string) wrapping.Option {
return func() interface{} {
return OptionFunc(func(o *options) error {
o.withAccessKey = with
return nil
})
}
}
// WithSecretKey provides a way to chose the secret key
func WithSecretKey(with string) wrapping.Option {
return func() interface{} {
return OptionFunc(func(o *options) error {
o.withSecretKey = with
return nil
})
}
}
// WithSessionToken provides a way to chose the session token
func WithSessionToken(with string) wrapping.Option {
return func() interface{} {
return OptionFunc(func(o *options) error {
o.withSessionToken = with
return nil
})
}
}
// WithSharedCredsFilename provides a way to chose the shared creds filename
func WithSharedCredsFilename(with string) wrapping.Option {
return func() interface{} {
return OptionFunc(func(o *options) error {
o.withSharedCredsFilename = with
return nil
})
}
}
// WithSharedCredsProfile provides a way to chose the shared creds profile
func WithSharedCredsProfile(with string) wrapping.Option {
return func() interface{} {
return OptionFunc(func(o *options) error {
o.withSharedCredsProfile = with
return nil
})
}
}
// WithWebIdentityTokenFile provides a way to chose the web identity token file
func WithWebIdentityTokenFile(with string) wrapping.Option {
return func() interface{} {
return OptionFunc(func(o *options) error {
o.withWebIdentityTokenFile = with
return nil
})
}
}
// WithRoleSessionName provides a way to chose the role session name
func WithRoleSessionName(with string) wrapping.Option {
return func() interface{} {
return OptionFunc(func(o *options) error {
o.withRoleSessionName = with
return nil
})
}
}
// WithRoleArn provides a way to chose the role ARN
func WithRoleArn(with string) wrapping.Option {
return func() interface{} {
return OptionFunc(func(o *options) error {
o.withRoleArn = with
return nil
})
}
}
// WithLogger provides a way to pass in a logger
func WithLogger(with hclog.Logger) wrapping.Option {
return func() interface{} {
return OptionFunc(func(o *options) error {
o.withLogger = with
return nil
})
}
}