| 1 |
// Package appconfig implements a koanf.Provider for AWS AppConfig |
| 2 |
// and provides it to koanf to be parsed by a koanf.Parser. |
| 3 |
package appconfig |
| 4 |
|
| 5 |
import ( |
| 6 |
"context" |
| 7 |
"errors" |
| 8 |
"time" |
| 9 |
|
| 10 |
"github.com/aws/aws-sdk-go-v2/aws" |
| 11 |
"github.com/aws/aws-sdk-go-v2/config" |
| 12 |
"github.com/aws/aws-sdk-go-v2/credentials" |
| 13 |
"github.com/aws/aws-sdk-go-v2/credentials/stscreds" |
| 14 |
"github.com/aws/aws-sdk-go-v2/service/appconfig" |
| 15 |
"github.com/aws/aws-sdk-go-v2/service/sts" |
| 16 |
) |
| 17 |
|
| 18 |
// Config holds the AWS AppConfig Configuration. |
| 19 |
type Config struct { |
| 20 |
// The AWS AppConfig Application to get. Specify either the application |
| 21 |
// name or the application ID. |
| 22 |
Application string |
| 23 |
|
| 24 |
// The Client ID for the AppConfig. Enables AppConfig to deploy the |
| 25 |
// configuration in intervals, as defined in the deployment strategy. |
| 26 |
ClientID string |
| 27 |
|
| 28 |
// The AppConfig configuration to fetch. Specify either the configuration |
| 29 |
// name or the configuration ID. |
| 30 |
Configuration string |
| 31 |
|
| 32 |
// The AppConfig environment to get. Specify either the environment |
| 33 |
// name or the environment ID. |
| 34 |
Environment string |
| 35 |
|
| 36 |
// The AppConfig Configuration Version to fetch. Specifying a ClientConfigurationVersion |
| 37 |
// ensures that the configuration is only fetched if it is updated. If not specified, |
| 38 |
// the latest available configuration is fetched always. |
| 39 |
// Setting this to the latest configuration version will return an empty slice of bytes. |
| 40 |
ClientConfigurationVersion string |
| 41 |
|
| 42 |
// The AWS Access Key ID to use. This value is fetched from the environment |
| 43 |
// if not specified. |
| 44 |
AWSAccessKeyID string |
| 45 |
|
| 46 |
// The AWS Secret Access Key to use. This value is fetched from the environment |
| 47 |
// if not specified. |
| 48 |
AWSSecretAccessKey string |
| 49 |
|
| 50 |
// The AWS IAM Role ARN to use. Useful for access requiring IAM AssumeRole. |
| 51 |
AWSRoleARN string |
| 52 |
|
| 53 |
// The AWS Region to use. This value is fetched from the environment if not specified. |
| 54 |
AWSRegion string |
| 55 |
|
| 56 |
// Time interval at which the watcher will refresh the configuration. |
| 57 |
// Defaults to 60 seconds. |
| 58 |
WatchInterval time.Duration |
| 59 |
} |
| 60 |
|
| 61 |
// AppConfig implements an AWS AppConfig provider. |
| 62 |
type AppConfig struct { |
| 63 |
client *appconfig.Client |
| 64 |
config Config |
| 65 |
input appconfig.GetConfigurationInput |
| 66 |
} |
| 67 |
|
| 68 |
// Provider returns an AWS AppConfig provider. |
| 69 |
func Provider(cfg Config) (*AppConfig, error) { |
| 70 |
c, err := config.LoadDefaultConfig(context.TODO()) |
| 71 |
if err != nil { |
| 72 |
return nil, err |
| 73 |
} |
| 74 |
|
| 75 |
if cfg.AWSRegion != "" { |
| 76 |
c.Region = cfg.AWSRegion |
| 77 |
} |
| 78 |
|
| 79 |
// Check if AWS Access Key ID and Secret Key are specified. |
| 80 |
if cfg.AWSAccessKeyID != "" && cfg.AWSSecretAccessKey != "" { |
| 81 |
c.Credentials = credentials.NewStaticCredentialsProvider(cfg.AWSAccessKeyID, cfg.AWSSecretAccessKey, "") |
| 82 |
} |
| 83 |
|
| 84 |
// Check if AWS Role ARN is present. |
| 85 |
if cfg.AWSRoleARN != "" { |
| 86 |
stsSvc := sts.NewFromConfig(c) |
| 87 |
credentials := stscreds.NewAssumeRoleProvider(stsSvc, cfg.AWSRoleARN) |
| 88 |
c.Credentials = aws.NewCredentialsCache(credentials) |
| 89 |
} |
| 90 |
client := appconfig.NewFromConfig(c) |
| 91 |
|
| 92 |
return &AppConfig{client: client, config: cfg}, nil |
| 93 |
} |
| 94 |
|
| 95 |
// ProviderWithClient returns an AWS AppConfig provider |
| 96 |
// using an existing AWS appconfig client. |
| 97 |
func ProviderWithClient(cfg Config, client *appconfig.Client) *AppConfig { |
| 98 |
return &AppConfig{client: client, config: cfg} |
| 99 |
} |
| 100 |
|
| 101 |
// ReadBytes returns the raw bytes for parsing. |
| 102 |
func (ac *AppConfig) ReadBytes() ([]byte, error) { |
| 103 |
ac.input = appconfig.GetConfigurationInput{ |
| 104 |
Application: &ac.config.Application, |
| 105 |
ClientId: &ac.config.ClientID, |
| 106 |
Configuration: &ac.config.Configuration, |
| 107 |
Environment: &ac.config.Environment, |
| 108 |
} |
| 109 |
if ac.config.ClientConfigurationVersion != "" { |
| 110 |
ac.input.ClientConfigurationVersion = &ac.config.ClientConfigurationVersion |
| 111 |
} |
| 112 |
|
| 113 |
conf, err := ac.client.GetConfiguration(context.TODO(), &ac.input) |
| 114 |
if err != nil { |
| 115 |
return nil, err |
| 116 |
} |
| 117 |
|
| 118 |
// Set the response configuration version as the current configuration version. |
| 119 |
// Useful for Watch(). |
| 120 |
ac.input.ClientConfigurationVersion = conf.ConfigurationVersion |
| 121 |
|
| 122 |
return conf.Content, nil |
| 123 |
} |
| 124 |
|
| 125 |
// Read is not supported by the appconfig provider. |
| 126 |
func (ac *AppConfig) Read() (map[string]any, error) { |
| 127 |
return nil, errors.New("appconfig provider does not support this method") |
| 128 |
} |
| 129 |
|
| 130 |
// Watch polls AWS AppConfig for configuration updates. |
| 131 |
func (ac *AppConfig) Watch(cb func(event any, err error)) error { |
| 132 |
if ac.config.WatchInterval == 0 { |
| 133 |
// Set default watch interval to 60 seconds. |
| 134 |
ac.config.WatchInterval = 60 * time.Second |
| 135 |
} |
| 136 |
|
| 137 |
go func() { |
| 138 |
loop: |
| 139 |
for { |
| 140 |
conf, err := ac.client.GetConfiguration(context.TODO(), &ac.input) |
| 141 |
if err != nil { |
| 142 |
cb(nil, err) |
| 143 |
break loop |
| 144 |
} |
| 145 |
|
| 146 |
// Check if the configuration has been updated. |
| 147 |
if len(conf.Content) == 0 { |
| 148 |
// Configuration is not updated and we have the latest version. |
| 149 |
// Sleep for WatchInterval and retry watcher. |
| 150 |
time.Sleep(ac.config.WatchInterval) |
| 151 |
continue |
| 152 |
} |
| 153 |
|
| 154 |
// Trigger event. |
| 155 |
cb(nil, nil) |
| 156 |
} |
| 157 |
}() |
| 158 |
|
| 159 |
return nil |
| 160 |
} |