Skip to content

Commit 9280fd5

Browse files
authored
Fix: use configured externalID when not using Grafana Assume Role (#248)
1 parent 4b6cdad commit 9280fd5

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

pkg/awsauth/auth.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package awsauth
33
import (
44
"context"
55
"fmt"
6+
"github.com/grafana/grafana-aws-sdk/pkg/awsds"
67

78
"github.com/aws/aws-sdk-go-v2/aws"
89
"github.com/grafana/grafana-plugin-sdk-go/backend"
@@ -48,6 +49,8 @@ func (rcp *awsConfigProvider) GetConfig(ctx context.Context, authSettings Settin
4849
case AuthTypeSharedCreds:
4950
options = append(options, authSettings.WithSharedCredentials())
5051
case AuthTypeGrafanaAssumeRole:
52+
settings, _ := awsds.ReadAuthSettingsFromContext(ctx)
53+
authSettings.ExternalID = settings.ExternalID
5154
options = append(options, authSettings.WithGrafanaAssumeRole(ctx, rcp.client))
5255
default:
5356
return aws.Config{}, fmt.Errorf("unknown auth type: %s", authType)

pkg/awsauth/auth_test.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/aws/aws-sdk-go-v2/aws"
77
ststypes "github.com/aws/aws-sdk-go-v2/service/sts/types"
88
"github.com/grafana/grafana-aws-sdk/pkg/awsds"
9+
"github.com/grafana/grafana-plugin-sdk-go/backend"
910
"github.com/stretchr/testify/assert"
1011
"github.com/stretchr/testify/require"
1112
"os"
@@ -30,8 +31,11 @@ type testCase struct {
3031
environment map[string]string
3132
}
3233

34+
const StackID = "42"
35+
3336
func (tc testCase) Run(t *testing.T) {
34-
ctx := context.Background()
37+
ctx := backend.WithGrafanaConfig(context.Background(),
38+
backend.NewGrafanaCfg(map[string]string{awsds.GrafanaAssumeRoleExternalIdKeyName: StackID}))
3539
client := &mockAWSAPIClient{&mockAssumeRoleAPIClient{}}
3640

3741
if tc.authSettings.AssumeRoleARN != "" {
@@ -50,10 +54,16 @@ func (tc testCase) Run(t *testing.T) {
5054
if tc.assumeRoleShouldFail {
5155
require.Error(t, err)
5256
} else {
57+
require.NoError(t, err)
5358
tc.assertConfig(t, cfg)
5459
if tc.authSettings.GetAuthType() == AuthTypeKeys && tc.authSettings.SessionToken != "" {
5560
assert.Equal(t, tc.authSettings.SessionToken, creds.SessionToken)
5661
}
62+
if tc.authSettings.GetAuthType() == AuthTypeGrafanaAssumeRole {
63+
assert.Equal(t, client.assumeRoleClient.calledExternalId, StackID)
64+
} else if tc.authSettings.AssumeRoleARN != "" && tc.authSettings.ExternalID != "" {
65+
assert.Equal(t, client.assumeRoleClient.calledExternalId, tc.authSettings.ExternalID)
66+
}
5767
accessKey, secret := tc.getExpectedKeyAndSecret(t)
5868
assert.Equal(t, accessKey, creds.AccessKeyID)
5969
assert.Equal(t, secret, creds.SecretAccessKey)
@@ -185,6 +195,23 @@ func TestGetAWSConfig_Keys_AssumeRule(t *testing.T) {
185195
Expiration: aws.Time(time.Now().Add(time.Hour)),
186196
},
187197
},
198+
{
199+
name: "static assume role with external ID - external ID is used",
200+
authSettings: Settings{
201+
AuthType: AuthTypeKeys,
202+
AccessKey: "tensile",
203+
SecretKey: "diaphanous",
204+
Region: "eu-north-1",
205+
AssumeRoleARN: "arn:aws:iam::1234567890:role/aws-service-role",
206+
ExternalID: "cows_with_parasols",
207+
},
208+
assumedCredentials: &ststypes.Credentials{
209+
AccessKeyId: aws.String("assumed"),
210+
SecretAccessKey: aws.String("role"),
211+
SessionToken: aws.String("session"),
212+
Expiration: aws.Time(time.Now().Add(time.Hour)),
213+
},
214+
},
188215
{
189216
name: "static assume role with sts endpoint - endpoint is nil",
190217
authSettings: Settings{

pkg/awsauth/test_utils.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,15 @@ func (m *mockAWSAPIClient) NewEC2RoleCreds() aws.CredentialsProvider {
5555

5656
type mockAssumeRoleAPIClient struct {
5757
mock.Mock
58-
stsConfig aws.Config
58+
stsConfig aws.Config
59+
calledExternalId string
5960
}
6061

6162
func (m *mockAssumeRoleAPIClient) AssumeRole(_ context.Context, params *sts.AssumeRoleInput, _ ...func(*sts.Options)) (*sts.AssumeRoleOutput, error) {
6263
args := m.Called()
64+
if params.ExternalId != nil {
65+
m.calledExternalId = *params.ExternalId
66+
}
6367
if args.Bool(0) { // shouldError
6468
return &sts.AssumeRoleOutput{}, fmt.Errorf("assume role failed")
6569
}

0 commit comments

Comments
 (0)