Skip to content

Commit 326ebb5

Browse files
committed
feat: Add configuration for AWS services
1 parent bc49b52 commit 326ebb5

File tree

3 files changed

+244
-0
lines changed

3 files changed

+244
-0
lines changed

pkg/aws/config.go

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package aws
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"strings"
7+
8+
cbuilder "github.com/scribd/go-sdk/internal/pkg/configuration/builder"
9+
)
10+
11+
type (
12+
HTTPClient struct {
13+
// MaxIdleConns, if non-zero, controls the maximum idle
14+
// (keep-alive) connections to keep per-host.
15+
MaxIdleConns int `mapstructure:"max_idle_conns"`
16+
}
17+
18+
StaticConfig struct {
19+
// AccessKeyID is the AWS access key ID.
20+
AccessKeyID string `mapstructure:"access_key_id"`
21+
// SecretAccessKey is the AWS secret access key.
22+
SecretAccessKey string `mapstructure:"secret_access_key"`
23+
// SessionToken is the AWS session token.
24+
SessionToken string `mapstructure:"session_token"`
25+
}
26+
27+
AssumeRoleConfig struct {
28+
// ARN is the ARN of the role to assume.
29+
ARN string `mapstructure:"arn"`
30+
}
31+
32+
CredentialsConfig struct {
33+
// Static is the configuration for static credentials.
34+
Static StaticConfig `mapstructure:"static"`
35+
// AssumeRole is the configuration for assuming a role.
36+
AssumeRole AssumeRoleConfig `mapstructure:"assume_role"`
37+
}
38+
39+
AWSConfig struct {
40+
// Region is the region to send requests to.
41+
Region string `mapstructure:"region"`
42+
// HTTPClient is the configuration for the HttpClient AWS the SDK's API clients will use to invoke HTTP requests.
43+
HTTPClient HTTPClient `mapstructure:"http_client"`
44+
}
45+
46+
S3Config struct {
47+
// Region is the region to send requests to.
48+
Region string `mapstructure:"region"`
49+
// HTTPClient is the configuration for the HttpClient AWS the SDK's API clients will use to invoke HTTP requests.
50+
HTTPClient HTTPClient `mapstructure:"http_client"`
51+
// Credentials is the configuration for the AWS credentials.
52+
Credentials CredentialsConfig `mapstructure:"credentials"`
53+
}
54+
55+
SagemakerRuntimeConfig struct {
56+
// Region is the region to send requests to.
57+
Region string `mapstructure:"region"`
58+
// HTTPClient is the configuration for the HttpClient AWS the SDK's API clients will use to invoke HTTP requests.
59+
HTTPClient HTTPClient `mapstructure:"http_client"`
60+
// Credentials is the configuration for the AWS credentials.
61+
Credentials CredentialsConfig `mapstructure:"credentials"`
62+
}
63+
64+
SFNConfig struct {
65+
// Region is the region to send requests to.
66+
Region string `mapstructure:"region"`
67+
// HTTPClient is the configuration for the HttpClient AWS the SDK's API clients will use to invoke HTTP requests.
68+
HTTPClient HTTPClient `mapstructure:"http_client"`
69+
// Credentials is the configuration for the AWS credentials.
70+
Credentials CredentialsConfig `mapstructure:"credentials"`
71+
}
72+
73+
SQSConfig struct {
74+
// Region is the region to send requests to.
75+
Region string `mapstructure:"region"`
76+
// HTTPClient is the configuration for the HttpClient AWS the SDK's API clients will use to invoke HTTP requests.
77+
HTTPClient HTTPClient `mapstructure:"http_client"`
78+
// Credentials is the configuration for the AWS credentials.
79+
Credentials CredentialsConfig `mapstructure:"credentials"`
80+
}
81+
82+
Config struct {
83+
// AWSConfig is the configuration for the AWS SDK.
84+
AWSConfig AWSConfig `mapstructure:"config"`
85+
// CredentialsConfig is the configuration for the AWS credentials.
86+
CredentialsConfig CredentialsConfig `mapstructure:"credentials"`
87+
// S3 is the configuration for the S3 clients.
88+
S3 map[string]S3Config `mapstructure:"s3"`
89+
// SagemakerRuntime is the configuration for the SagemakerRuntime clients.
90+
SagemakerRuntime map[string]SagemakerRuntimeConfig `mapstructure:"sagemakerruntime"`
91+
// SFN is the configuration for the Sfn clients.
92+
SFN map[string]SFNConfig `mapstructure:"sfn"`
93+
// SQS is the configuration for the Sqs clients.
94+
SQS map[string]SQSConfig `mapstructure:"sqs"`
95+
}
96+
)
97+
98+
func NewConfig() (*Config, error) {
99+
config := &Config{}
100+
viperBuilder := cbuilder.New("aws")
101+
102+
appName := strings.ReplaceAll(os.Getenv("APP_SETTINGS_NAME"), "-", "_")
103+
viperBuilder.SetDefault("aws", fmt.Sprintf("%s_%s", appName, os.Getenv("APP_ENV")))
104+
105+
vConf, err := viperBuilder.Build()
106+
if err != nil {
107+
return config, err
108+
}
109+
110+
if err = vConf.Unmarshal(config); err != nil {
111+
return config, fmt.Errorf("unable to decode into struct: %s", err.Error())
112+
}
113+
114+
return config, nil
115+
}

pkg/aws/config_test.go

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package aws
2+
3+
import (
4+
"path/filepath"
5+
"runtime"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
"github.com/stretchr/testify/require"
10+
)
11+
12+
func TestNewConfig(t *testing.T) {
13+
testCases := []struct {
14+
name string
15+
wantError bool
16+
}{
17+
{
18+
name: "NewWithoutConfigFileFails",
19+
wantError: true,
20+
},
21+
}
22+
23+
for _, tc := range testCases {
24+
t.Run(tc.name, func(t *testing.T) {
25+
_, err := NewConfig()
26+
27+
gotError := err != nil
28+
assert.Equal(t, gotError, tc.wantError)
29+
})
30+
}
31+
}
32+
33+
func TestNewConfigWithAppRoot(t *testing.T) {
34+
testCases := []struct {
35+
name string
36+
env string
37+
cfg *Config
38+
wantErr bool
39+
40+
envOverrides [][]string
41+
}{
42+
{
43+
name: "NewWithConfigFileWorks",
44+
env: "test",
45+
cfg: &Config{
46+
AWSConfig: AWSConfig{
47+
Region: "us-east-2",
48+
},
49+
S3: map[string]S3Config{
50+
"default": {
51+
Region: "us-east-1",
52+
},
53+
"test": {
54+
Region: "us-west-2",
55+
},
56+
},
57+
},
58+
},
59+
{
60+
name: "NewWithConfigFileWorks, overrides",
61+
env: "test",
62+
cfg: &Config{
63+
AWSConfig: AWSConfig{
64+
Region: "us-west-2",
65+
},
66+
S3: map[string]S3Config{
67+
"default": {
68+
Region: "us-east-1",
69+
Credentials: CredentialsConfig{
70+
AssumeRole: AssumeRoleConfig{
71+
ARN: "test",
72+
},
73+
},
74+
},
75+
"test": {
76+
Region: "us-west-1",
77+
},
78+
},
79+
},
80+
envOverrides: [][]string{
81+
{"APP_AWS_CONFIG_REGION", "us-west-2"},
82+
{"APP_AWS_S3_TEST_REGION", "us-west-1"},
83+
{"APP_AWS_S3_DEFAULT_CREDENTIALS_ASSUME_ROLE_ARN", "test"}},
84+
},
85+
}
86+
87+
for _, tc := range testCases {
88+
t.Run(tc.name, func(t *testing.T) {
89+
90+
if len(tc.envOverrides) > 0 {
91+
for _, o := range tc.envOverrides {
92+
t.Setenv(o[0], o[1])
93+
}
94+
}
95+
96+
_, filename, _, _ := runtime.Caller(0)
97+
tmpRootParent := filepath.Dir(filename)
98+
t.Setenv("APP_ROOT", filepath.Join(tmpRootParent, "testdata"))
99+
100+
c, err := NewConfig()
101+
if tc.wantErr {
102+
require.NotNil(t, err)
103+
} else {
104+
require.Nil(t, err)
105+
}
106+
107+
assert.Equal(t, tc.cfg, c)
108+
})
109+
}
110+
}

pkg/aws/testdata/config/aws.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
common: &common
2+
config:
3+
region: "us-east-2"
4+
5+
test: &test
6+
<<: *common
7+
config:
8+
region: "us-east-2"
9+
s3:
10+
default:
11+
region: "us-east-1"
12+
credentials:
13+
assume_role:
14+
arn: ""
15+
test:
16+
region: "us-west-2"
17+
18+
development:
19+
<<: *test

0 commit comments

Comments
 (0)