Skip to content

Commit e49b45d

Browse files
authored
Fix: use alternate STS endpoint for STS interaction if given (#214)
1 parent 57d47f0 commit e49b45d

File tree

5 files changed

+52
-8
lines changed

5 files changed

+52
-8
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
All notable changes to this project will be documented in this file.
44

5+
## 0.33.1
6+
- Fix: use alternate STS endpoint for STS interaction if given by @njvrzm in https://github.com/grafana/grafana-aws-sdk/pull/214
7+
58
## 0.33.0
69

710
- Update CodeBuild metrics and dimensions by @hectorruiz-it in https://github.com/grafana/grafana-aws-sdk/pull/209

pkg/awsauth/auth.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"github.com/aws/aws-sdk-go-v2/aws"
77
"github.com/grafana/grafana-plugin-sdk-go/backend"
8+
"strings"
89
)
910

1011
type ConfigProvider interface {
@@ -73,3 +74,7 @@ func (rcp *awsConfigProvider) GetConfig(ctx context.Context, authSettings Settin
7374
rcp.cache[key] = cfg
7475
return cfg, nil
7576
}
77+
78+
func isStsEndpoint(ep *string) bool {
79+
return ep != nil && (strings.HasPrefix(*ep, "sts.") || strings.HasPrefix(*ep, "sts-fips."))
80+
}

pkg/awsauth/auth_test.go

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ func (tc testCase) Run(t *testing.T) {
5252
assert.Equal(t, accessKey, creds.AccessKeyID)
5353
assert.Equal(t, secret, creds.SecretAccessKey)
5454
}
55+
if isStsEndpoint(&tc.authSettings.Endpoint) {
56+
assert.Equal(t, tc.authSettings.Endpoint, *client.assumeRoleClient.stsConfig.BaseEndpoint)
57+
assert.Nil(t, cfg.BaseEndpoint)
58+
}
5559
}
5660

5761
func (tc testCase) assertConfig(t *testing.T, cfg aws.Config) {
@@ -134,6 +138,15 @@ func TestGetAWSConfig_Keys(t *testing.T) {
134138
Region: "ap-south-1",
135139
},
136140
},
141+
{
142+
name: "static credentials, sts endpoint",
143+
authSettings: Settings{
144+
LegacyAuthType: awsds.AuthTypeKeys,
145+
AccessKey: "ubiquitous",
146+
SecretKey: "malevolent",
147+
Region: "ap-south-1",
148+
},
149+
},
137150
}.runAll(t)
138151
}
139152

@@ -155,6 +168,23 @@ func TestGetAWSConfig_Keys_AssumeRule(t *testing.T) {
155168
Expiration: aws.Time(time.Now().Add(time.Hour)),
156169
},
157170
},
171+
{
172+
name: "static assume role with sts endpoint - endpoint is nil",
173+
authSettings: Settings{
174+
AuthType: AuthTypeKeys,
175+
AccessKey: "tensile",
176+
SecretKey: "diaphanous",
177+
Region: "us-east-1",
178+
Endpoint: "sts.us-east-1.amazonaws.com",
179+
AssumeRoleARN: "arn:aws:iam::1234567890:role/aws-service-role",
180+
},
181+
assumedCredentials: &ststypes.Credentials{
182+
AccessKeyId: aws.String("assumed"),
183+
SecretAccessKey: aws.String("role"),
184+
SessionToken: aws.String("session"),
185+
Expiration: aws.Time(time.Now().Add(time.Hour)),
186+
},
187+
},
158188
{
159189
name: "static assume role with failure",
160190
authSettings: Settings{
@@ -227,25 +257,26 @@ func TestGetAWSConfig_Shared(t *testing.T) {
227257
func TestGetAWSConfig_UnknownOrMissing(t *testing.T) {
228258
testSuite{
229259
{
230-
name: "shared reads from specified file",
260+
name: "unknown auth type fails",
231261
authSettings: Settings{
232262
AuthType: AuthTypeUnknown,
233263
},
234264
shouldError: true,
235265
},
236266
{
237-
name: "grafana assume role uses the shared mechanism",
267+
name: "random auth type fails",
238268
authSettings: Settings{
239-
AuthType: AuthTypeMissing,
269+
AuthType: "rainbows",
240270
},
241271
shouldError: true,
242272
},
243273
{
244-
name: "grafana assume role uses the shared mechanism",
245-
authSettings: Settings{
246-
AuthType: "rainbows",
274+
name: "missing auth type fails back to legacy default (and does not fail)",
275+
authSettings: Settings{},
276+
environment: map[string]string{
277+
"AWS_SHARED_CREDENTIALS_FILE": testDataPath("credentials"),
247278
},
248-
shouldError: true,
279+
shouldError: false,
249280
},
250281
}.runAll(t)
251282
}

pkg/awsauth/settings.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ func (s Settings) WithAssumeRole(cfg aws.Config, client AWSAPIClient) LoadOption
127127
cache := client.NewCredentialsCache(provider)
128128
return func(options *config.LoadOptions) error {
129129
options.Credentials = cache
130+
if isStsEndpoint(cfg.BaseEndpoint) {
131+
options.BaseEndpoint = ""
132+
}
130133
return nil
131134
}
132135
}

pkg/awsauth/test_utils.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ func (m *mockAWSAPIClient) NewStaticCredentialsProvider(key, secret, session str
3535
return credentials.NewStaticCredentialsProvider(key, secret, session)
3636
}
3737

38-
func (m *mockAWSAPIClient) NewSTSClientFromConfig(_ aws.Config) stscreds.AssumeRoleAPIClient {
38+
func (m *mockAWSAPIClient) NewSTSClientFromConfig(cfg aws.Config) stscreds.AssumeRoleAPIClient {
39+
m.assumeRoleClient.stsConfig = cfg
3940
return m.assumeRoleClient
4041
}
4142

@@ -54,6 +55,7 @@ func (m *mockAWSAPIClient) NewEC2RoleCreds() aws.CredentialsProvider {
5455

5556
type mockAssumeRoleAPIClient struct {
5657
mock.Mock
58+
stsConfig aws.Config
5759
}
5860

5961
func (m *mockAssumeRoleAPIClient) AssumeRole(_ context.Context, params *sts.AssumeRoleInput, _ ...func(*sts.Options)) (*sts.AssumeRoleOutput, error) {

0 commit comments

Comments
 (0)