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+
3336func (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 {
0 commit comments