-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from nathandines/assume-role
Added support for assuming roles
- Loading branch information
Showing
10 changed files
with
148 additions
and
35 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
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,74 @@ | ||
package forgelib | ||
|
||
import ( | ||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/credentials" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/aws/aws-sdk-go/service/cloudformation" | ||
"github.com/aws/aws-sdk-go/service/cloudformation/cloudformationiface" | ||
"github.com/aws/aws-sdk-go/service/sts" | ||
"github.com/aws/aws-sdk-go/service/sts/stsiface" | ||
"strings" | ||
) | ||
|
||
var originalSession *session.Session | ||
var cfnClient cloudformationiface.CloudFormationAPI // CloudFormation service | ||
var stsClient stsiface.STSAPI // STS Service | ||
|
||
func init() { | ||
originalSession = session.Must(session.NewSessionWithOptions(session.Options{ | ||
SharedConfigState: session.SharedConfigEnable, | ||
})) | ||
setupClients(originalSession) | ||
} | ||
|
||
func setupClients(sess *session.Session) { | ||
cfnClient = cloudformation.New(sess) | ||
stsClient = sts.New(sess) | ||
} | ||
|
||
// AssumeRole will change your credentials for Forge to those of an assumed role | ||
// as specific by the ARN specified in the arguments to AssumeRole | ||
func AssumeRole(roleArn string) error { | ||
roleSessionName, err := getRoleSessionName() | ||
if err != nil { | ||
return err | ||
} | ||
assumeOut, err := stsClient.AssumeRole(&sts.AssumeRoleInput{ | ||
RoleSessionName: aws.String(roleSessionName), | ||
RoleArn: aws.String(roleArn), | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
session, err := session.NewSessionWithOptions(session.Options{ | ||
Config: aws.Config{ | ||
Credentials: credentials.NewStaticCredentials( | ||
*assumeOut.Credentials.AccessKeyId, | ||
*assumeOut.Credentials.SecretAccessKey, | ||
*assumeOut.Credentials.SessionToken, | ||
), | ||
}, | ||
SharedConfigState: session.SharedConfigEnable, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
setupClients(session) | ||
return nil | ||
} | ||
|
||
// UnassumeAllRoles will change your credentials back to their original state | ||
// after using AssumeRole | ||
func UnassumeAllRoles() { | ||
setupClients(originalSession) | ||
} | ||
|
||
func getRoleSessionName() (string, error) { | ||
callerIdentity, err := stsClient.GetCallerIdentity(&sts.GetCallerIdentityInput{}) | ||
if err != nil { | ||
return "", err | ||
} | ||
roleSessionNameSlice := strings.Split(*callerIdentity.Arn, "/") | ||
return roleSessionNameSlice[len(roleSessionNameSlice)-1], 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package forgelib | ||
|
||
import "testing" | ||
|
||
func TestGetRoleSessionName(t *testing.T) { | ||
oldSTSClient := stsClient | ||
defer func() { stsClient = oldSTSClient }() | ||
stsClient = mockSTS{ | ||
callerArn: "arn:aws:iam::111111111111:user/nathan", | ||
accountID: "111111111111", | ||
} | ||
expectedName := "nathan" | ||
|
||
roleSessionName, err := getRoleSessionName() | ||
if err != nil { | ||
t.Fatalf("unexpected error, %v", err) | ||
} | ||
|
||
if e, g := expectedName, roleSessionName; e != g { | ||
t.Errorf("expected \"%s\", got \"%s\"", e, g) | ||
} | ||
} |
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
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
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