@@ -190,6 +190,12 @@ func (b *backend) stsRoleForAccount(ctx context.Context, s logical.Storage, acco
190190 if sts != nil {
191191 return sts .StsRole , nil
192192 }
193+
194+ // Return an error if there's no STS config for an account which is not the default one
195+ if b .defaultAWSAccountID != "" && b .defaultAWSAccountID != accountID {
196+ return "" , fmt .Errorf ("no STS configuration found for account ID %q" , accountID )
197+ }
198+
193199 return "" , nil
194200}
195201
@@ -200,20 +206,26 @@ func (b *backend) clientEC2(ctx context.Context, s logical.Storage, region, acco
200206 return nil , err
201207 }
202208 b .configMutex .RLock ()
203- if b .EC2ClientsMap [region ] != nil && b.EC2ClientsMap [region ][stsRole ] != nil {
209+ if b .EC2ClientsMap [region ] != nil &&
210+ b.EC2ClientsMap [region ][accountID ] != nil &&
211+ b.EC2ClientsMap [region ][accountID ][stsRole ] != nil {
204212 defer b .configMutex .RUnlock ()
205213 // If the client object was already created, return it
206- return b.EC2ClientsMap [region ][stsRole ], nil
214+ b .Logger ().Debug (fmt .Sprintf ("returning cached client for region %s, account %s and stsRole %s" , region , accountID , stsRole ))
215+ return b.EC2ClientsMap [region ][accountID ][stsRole ], nil
207216 }
217+ b .Logger ().Debug (fmt .Sprintf ("no cached client for region %s, account %s and stsRole %s" , region , accountID , stsRole ))
208218
209219 // Release the read lock and acquire the write lock
210220 b .configMutex .RUnlock ()
211221 b .configMutex .Lock ()
212222 defer b .configMutex .Unlock ()
213223
214224 // If the client gets created while switching the locks, return it
215- if b .EC2ClientsMap [region ] != nil && b.EC2ClientsMap [region ][stsRole ] != nil {
216- return b.EC2ClientsMap [region ][stsRole ], nil
225+ if b .EC2ClientsMap [region ] != nil &&
226+ b.EC2ClientsMap [region ][accountID ] != nil &&
227+ b.EC2ClientsMap [region ][accountID ][stsRole ] != nil {
228+ return b.EC2ClientsMap [region ][accountID ][stsRole ], nil
217229 }
218230
219231 // Create an AWS config object using a chain of providers
@@ -237,13 +249,16 @@ func (b *backend) clientEC2(ctx context.Context, s logical.Storage, region, acco
237249 if client == nil {
238250 return nil , fmt .Errorf ("could not obtain ec2 client" )
239251 }
252+
240253 if _ , ok := b .EC2ClientsMap [region ]; ! ok {
241- b .EC2ClientsMap [region ] = map [string ]* ec2.EC2 {stsRole : client }
242- } else {
243- b.EC2ClientsMap [region ][stsRole ] = client
254+ b .EC2ClientsMap [region ] = make (map [string ]map [string ]* ec2.EC2 )
244255 }
256+ if _ , ok := b.EC2ClientsMap [region ][accountID ]; ! ok {
257+ b.EC2ClientsMap [region ][accountID ] = make (map [string ]* ec2.EC2 )
258+ }
259+ b.EC2ClientsMap [region ][accountID ][stsRole ] = client
245260
246- return b.EC2ClientsMap [region ][stsRole ], nil
261+ return b.EC2ClientsMap [region ][accountID ][ stsRole ], nil
247262}
248263
249264// clientIAM creates a client to interact with AWS IAM API
@@ -258,22 +273,26 @@ func (b *backend) clientIAM(ctx context.Context, s logical.Storage, region, acco
258273 b .Logger ().Debug (fmt .Sprintf ("found stsRole %s for account %s" , stsRole , accountID ))
259274 }
260275 b .configMutex .RLock ()
261- if b .IAMClientsMap [region ] != nil && b.IAMClientsMap [region ][stsRole ] != nil {
276+ if b .IAMClientsMap [region ] != nil &&
277+ b.IAMClientsMap [region ][accountID ] != nil &&
278+ b.IAMClientsMap [region ][accountID ][stsRole ] != nil {
262279 defer b .configMutex .RUnlock ()
263280 // If the client object was already created, return it
264- b .Logger ().Debug (fmt .Sprintf ("returning cached client for region %s and stsRole %s" , region , stsRole ))
265- return b.IAMClientsMap [region ][stsRole ], nil
281+ b .Logger ().Debug (fmt .Sprintf ("returning cached client for region %s, account %s and stsRole %s" , region , accountID , stsRole ))
282+ return b.IAMClientsMap [region ][accountID ][ stsRole ], nil
266283 }
267- b .Logger ().Debug (fmt .Sprintf ("no cached client for region %s and stsRole %s" , region , stsRole ))
284+ b .Logger ().Debug (fmt .Sprintf ("no cached client for region %s, account %s and stsRole %s" , region , accountID , stsRole ))
268285
269286 // Release the read lock and acquire the write lock
270287 b .configMutex .RUnlock ()
271288 b .configMutex .Lock ()
272289 defer b .configMutex .Unlock ()
273290
274291 // If the client gets created while switching the locks, return it
275- if b .IAMClientsMap [region ] != nil && b.IAMClientsMap [region ][stsRole ] != nil {
276- return b.IAMClientsMap [region ][stsRole ], nil
292+ if b .IAMClientsMap [region ] != nil &&
293+ b.IAMClientsMap [region ][accountID ] != nil &&
294+ b.IAMClientsMap [region ][accountID ][stsRole ] != nil {
295+ return b.IAMClientsMap [region ][accountID ][stsRole ], nil
277296 }
278297
279298 // Create an AWS config object using a chain of providers
@@ -297,10 +316,14 @@ func (b *backend) clientIAM(ctx context.Context, s logical.Storage, region, acco
297316 if client == nil {
298317 return nil , fmt .Errorf ("could not obtain iam client" )
299318 }
319+
300320 if _ , ok := b .IAMClientsMap [region ]; ! ok {
301- b .IAMClientsMap [region ] = map [string ]* iam.IAM {stsRole : client }
302- } else {
303- b.IAMClientsMap [region ][stsRole ] = client
321+ b .IAMClientsMap [region ] = make (map [string ]map [string ]* iam.IAM )
304322 }
305- return b.IAMClientsMap [region ][stsRole ], nil
323+ if _ , ok := b.IAMClientsMap [region ][accountID ]; ! ok {
324+ b.IAMClientsMap [region ][accountID ] = make (map [string ]* iam.IAM )
325+ }
326+ b.IAMClientsMap [region ][accountID ][stsRole ] = client
327+
328+ return b.IAMClientsMap [region ][accountID ][stsRole ], nil
306329}
0 commit comments