forked from FormidableLabs/terraform-provider-serverless
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Assume AWS role when provided in AWS config
- Loading branch information
1 parent
538e285
commit 010aa22
Showing
4 changed files
with
105 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package serverless | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
|
||
"github.com/aws/aws-sdk-go/aws/credentials" | ||
"github.com/aws/aws-sdk-go/aws/credentials/stscreds" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
) | ||
|
||
func loadAWSCredentials(resource getter) (*credentials.Credentials, error) { | ||
configs := resource.Get("aws_config").([]interface{}) | ||
if len(configs) == 0 || configs[0] == nil { | ||
return nil, nil | ||
} | ||
awsConfig := configs[0].(map[string]interface{}) | ||
|
||
accountId := awsConfig["account_id"].(string) | ||
callerArn := awsConfig["caller_arn"].(string) | ||
|
||
re := regexp.MustCompile(`.*assumed-role/([\w+=,.@-]+)/.*`) | ||
var role string | ||
if result := re.FindStringSubmatch(callerArn); result != nil { | ||
role = result[1] | ||
} else { | ||
return nil, fmt.Errorf("Could not parse role name from callerArn %s", callerArn) | ||
} | ||
|
||
roleArn := fmt.Sprintf("arn:aws:iam::%s:role/%s", accountId, role) | ||
|
||
sess := session.Must(session.NewSession()) | ||
return stscreds.NewCredentials(sess, roleArn), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters