-
Notifications
You must be signed in to change notification settings - Fork 9
/
appconfig.go
339 lines (292 loc) · 9.37 KB
/
appconfig.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// Copyright (c) 2024 The konf authors
// Use of this source code is governed by a MIT license found in the LICENSE file.
// Package appconfig loads configuration from AWS [AppConfig].
//
// It requires following permissions to access AWS AppConfig:
// - appconfig:StartConfigurationSession
// - appconfig:GetLatestConfiguration
//
// If change notification is enabled, it also requires following permissions:
// - appconfig:GetApplication
// - appconfig:GetEnvironment
//
// # Change notification
//
// By default, it periodically polls the configuration only.
// It also listens to change events by register it to SNS notifier with one of following extensions:
// - [EventBridge extension] With SNS target
// - [SNS extension]
//
// Only ON_DEPLOYMENT_ROLLED_BACK events trigger polling the configuration and other type of events are ignored.
//
// [AppConfig]: https://aws.amazon.com/systems-manager/features/appconfig
// [EventBridge extension]: https://docs.aws.amazon.com/appconfig/latest/userguide/working-with-appconfig-extensions-about-predefined-notification-eventbridge.html
// [SNS extension]: https://docs.aws.amazon.com/appconfig/latest/userguide/working-with-appconfig-extensions-about-predefined-notification-sns.html
package appconfig
import (
"context"
"encoding/json"
"errors"
"fmt"
"reflect"
"sync/atomic"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/appconfig"
"github.com/aws/aws-sdk-go-v2/service/appconfigdata"
)
// AppConfig is a Provider that loads configuration from AWS AppConfig.
//
// To create a new AppConfig, call [New].
type AppConfig struct {
unmarshal func([]byte, any) error
pollInterval time.Duration
onStatus func(bool, error)
changedCh chan struct{}
client clientProxy
}
// New creates an AppConfig with the given application (ID or Name),
// environment (ID or Name), profile (ID or Name) and Option(s).
func New(application, environment, profile string, opts ...Option) *AppConfig {
option := &options{
client: clientProxy{
application: application,
environment: environment,
profile: profile,
},
changedCh: make(chan struct{}, 1),
}
for _, opt := range opts {
opt(option)
}
option.client.timeout = option.pollInterval / 2 //nolint:mnd
return (*AppConfig)(option)
}
var errNil = errors.New("nil AppConfig")
func (a *AppConfig) Load() (map[string]any, error) {
if a == nil {
return nil, errNil
}
values, _, err := a.load(context.Background())
return values, err
}
func (a *AppConfig) Watch(ctx context.Context, onChange func(map[string]any)) error {
if a == nil {
return errNil
}
if a.changedCh == nil {
a.changedCh = make(chan struct{}, 1)
}
pollInterval := a.pollInterval
if pollInterval == 0 {
pollInterval = time.Minute
}
ticker := time.NewTicker(pollInterval)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return nil
case <-ticker.C:
a.changed()
case <-a.changedCh:
values, changed, err := a.load(ctx)
if a.onStatus != nil {
a.onStatus(changed, err)
}
if changed {
onChange(values)
}
}
}
}
func (a *AppConfig) changed() {
select {
case a.changedCh <- struct{}{}:
default:
// Ignore if the channel is full since it's already triggered.
}
}
func (a *AppConfig) load(ctx context.Context) (map[string]any, bool, error) {
resp, changed, err := a.client.load(ctx)
if !changed || err != nil {
return nil, false, err
}
unmarshal := a.unmarshal
if unmarshal == nil {
unmarshal = json.Unmarshal
}
var values map[string]any
if e := unmarshal(resp, &values); e != nil {
return nil, false, fmt.Errorf("unmarshal: %w", e)
}
return values, true, nil
}
func (a *AppConfig) OnEvent(msg []byte) error { //nolint:cyclop,funlen
if a == nil {
return errNil
}
//nolint:tagliatelle
type (
appConfig struct {
Type string `json:"Type"`
Application struct {
ID string `json:"Id"`
} `json:"Application"`
Environment struct {
ID string `json:"Id"`
} `json:"Environment"`
ConfigurationProfile struct {
ID string `json:"Id"`
Name string `json:"Name"`
} `json:"ConfigurationProfile"`
}
appConfigEvent struct {
// From EventBridge: https://docs.aws.amazon.com/appconfig/latest/userguide/working-with-appconfig-extensions-about-predefined-notification-eventbridge.html
Source string `json:"source"`
Detail appConfig `json:"detail"`
// From SNS: https://docs.aws.amazon.com/appconfig/latest/userguide/working-with-appconfig-extensions-about-predefined-notification-sns.html
appConfig
}
)
var event appConfigEvent
if err := json.Unmarshal(msg, &event); err != nil {
return fmt.Errorf("unmarshal appconfig event: %w", err)
}
applicationID := event.Detail.Application.ID
if applicationID == "" {
applicationID = event.Application.ID
}
if err := a.client.ensureApplicationID(applicationID); err != nil {
return err
}
environmentID := event.Detail.Environment.ID
if environmentID == "" {
environmentID = event.Environment.ID
}
if err := a.client.ensureEnvironmentID(environmentID); err != nil {
return err
}
if event.Source == "aws.appconfig" &&
event.Detail.Application.ID == a.client.applicationID &&
event.Detail.Environment.ID == a.client.environmentID &&
(event.Detail.ConfigurationProfile.ID == a.client.profile ||
event.Detail.ConfigurationProfile.Name == a.client.profile) {
if event.Detail.Type == "OnDeploymentRolledBack" {
// Trigger to reload the configuration.
a.changed()
}
return nil
}
if event.Application.ID == a.client.applicationID &&
event.Environment.ID == a.client.environmentID &&
(event.ConfigurationProfile.ID == a.client.profile ||
event.ConfigurationProfile.Name == a.client.profile) {
if event.Type == "OnDeploymentRolledBack" {
// Trigger to reload the configuration.
a.changed()
}
return nil
}
return fmt.Errorf("unsupported appconfig event: %w", errors.ErrUnsupported)
}
func (a *AppConfig) Status(onStatus func(bool, error)) {
a.onStatus = onStatus
}
func (a *AppConfig) String() string {
return "appconfig://" + a.client.application + "/" + a.client.profile
}
type clientProxy struct {
config aws.Config
application string
applicationID string
environment string
environmentID string
profile string
client *appconfigdata.Client
timeout time.Duration
nextPollToken atomic.Pointer[string]
nextPollTime atomic.Pointer[time.Time]
}
func (p *clientProxy) load(ctx context.Context) ([]byte, bool, error) {
if p.client == nil {
if reflect.ValueOf(p.config).IsZero() {
var err error
if p.config, err = config.LoadDefaultConfig(ctx); err != nil {
return nil, false, fmt.Errorf("load default AWS config: %w", err)
}
}
p.client = appconfigdata.NewFromConfig(p.config)
}
if nextPollTime := p.nextPollTime.Load(); nextPollTime != nil && time.Now().Before(*nextPollTime) {
// Have to wait until the next poll time.
time.Sleep(time.Until(*nextPollTime))
}
ctx, cancel := context.WithTimeout(ctx, max(p.timeout, 10*time.Second)) //nolint:mnd
defer cancel()
if p.nextPollToken.Load() == nil {
session, err := p.client.StartConfigurationSession(ctx, &appconfigdata.StartConfigurationSessionInput{
ApplicationIdentifier: aws.String(p.application),
ConfigurationProfileIdentifier: aws.String(p.profile),
EnvironmentIdentifier: aws.String(p.environment),
RequiredMinimumPollIntervalInSeconds: aws.Int32(15), //nolint:mnd // The minimum interval supported.
})
if err != nil {
return nil, false, fmt.Errorf("start configuration session: %w", err)
}
p.nextPollToken.Store(session.InitialConfigurationToken)
}
resp, err := p.client.GetLatestConfiguration(ctx,
&appconfigdata.GetLatestConfigurationInput{ConfigurationToken: p.nextPollToken.Load()},
)
if err != nil {
return nil, false, fmt.Errorf("get latest configuration: %w", err)
}
p.nextPollToken.Store(resp.NextPollConfigurationToken)
nextPollTime := time.Now().Add(time.Duration(resp.NextPollIntervalInSeconds) * time.Second)
p.nextPollTime.Store(&nextPollTime)
// It may return empty configuration data if the client already has the latest version.
return resp.Configuration, len(resp.Configuration) > 0, nil
}
func (p *clientProxy) ensureApplicationID(applicationID string) error {
if p.applicationID != "" || applicationID == "" {
return nil
}
if applicationID == p.application {
p.applicationID = applicationID
return nil
}
client := appconfig.NewFromConfig(p.config)
application, err := client.GetApplication(context.Background(), &appconfig.GetApplicationInput{
ApplicationId: aws.String(applicationID),
})
if err != nil {
return fmt.Errorf("get application: %w", err)
}
if aws.ToString(application.Name) == p.application {
p.applicationID = applicationID
}
return nil
}
func (p *clientProxy) ensureEnvironmentID(environmentID string) error {
if p.environmentID != "" || p.applicationID == "" || environmentID == "" {
return nil
}
if environmentID == p.environment {
p.environmentID = environmentID
return nil
}
client := appconfig.NewFromConfig(p.config)
environment, err := client.GetEnvironment(context.Background(), &appconfig.GetEnvironmentInput{
ApplicationId: aws.String(p.applicationID),
EnvironmentId: aws.String(environmentID),
})
if err != nil {
return fmt.Errorf("get environment: %w", err)
}
if aws.ToString(environment.Name) == p.environment {
p.environmentID = environmentID
}
return nil
}