-
Notifications
You must be signed in to change notification settings - Fork 2
/
session.go
51 lines (39 loc) · 1.3 KB
/
session.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package client
import (
"fmt"
"github.com/Appkube-awsx/awsx-common/awssession"
util "github.com/Appkube-awsx/awsx-common/utils"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sts"
"log"
)
var sessionName = util.RandomString(10)
func GetSessionWithAssumeRole(auth Auth) *session.Session {
sess, err := awssession.GetSessionByCreds(auth.Region, auth.AccessKey, auth.SecretKey, "")
if err != nil {
fmt.Printf("failed to create aws session, %v\n", err)
log.Fatal(err)
}
svc := sts.New(sess)
assumeRoleInput := sts.AssumeRoleInput{
RoleArn: aws.String(auth.CrossAccountRoleArn),
RoleSessionName: aws.String(sessionName),
DurationSeconds: aws.Int64(60 * 60 * 1),
}
if auth.ExternalId != "nil" {
fmt.Println("Trying to fetch external id to assume new role")
assumeRoleInput.ExternalId = aws.String(auth.ExternalId)
}
result, err := svc.AssumeRole(&assumeRoleInput)
if err != nil {
fmt.Printf("failed to assume role, %v\n", err)
log.Fatal(err)
}
awsSession, err := awssession.GetSessionByCreds(auth.Region, *result.Credentials.AccessKeyId, *result.Credentials.SecretAccessKey, *result.Credentials.SessionToken)
if err != nil {
fmt.Printf("failed to assume role, %v\n", err)
log.Fatal(err)
}
return awsSession
}