-
Notifications
You must be signed in to change notification settings - Fork 3
/
target.go
60 lines (47 loc) · 1.68 KB
/
target.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
52
53
54
55
56
57
58
59
60
package assumer
import (
"errors"
"fmt"
"os"
"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/sts"
"github.com/spf13/viper"
)
// TargetPlane represents the AWS Target Plane Account
type TargetPlane struct {
Plane
}
// Assume assumes a role in the target account and returns the assumed role creds
func (t *TargetPlane) Assume(c *sts.AssumeRoleOutput) (*sts.AssumeRoleOutput, error) {
if err := t.GetDefaults(); err != nil {
return nil, errors.New("Error: Could not get Target Plane defaults")
}
targetParams := &sts.AssumeRoleInput{
RoleArn: aws.String(t.Plane.RoleArn),
RoleSessionName: aws.String(fmt.Sprintf("%s_AssumedRole", os.Getenv("USER"))),
}
controlCreds := credentials.NewStaticCredentials(*c.Credentials.AccessKeyId, *c.Credentials.SecretAccessKey, *c.Credentials.SessionToken)
stsClient := sts.New(session.New(), aws.NewConfig().WithRegion(t.Region).WithCredentials(controlCreds))
resp, err := stsClient.AssumeRole(targetParams)
if err != nil {
return nil, errors.New("Error: Could not assume Target Plane Role. " + err.Error())
}
return resp, nil
}
// GetDefaults will get TargetPlane default values from assumer config file
func (t *TargetPlane) GetDefaults() error {
if t.AccountNumber == "" {
t.AccountNumber = viper.GetString("default.account")
}
if t.RoleArn == "" {
t.RoleArn = "arn:aws:iam::" + viper.GetString("default.account") + ":role/" + viper.GetString("default.role")
} else {
t.RoleArn = "arn:aws:iam::" + t.AccountNumber + ":role/" + t.RoleArn
}
if t.Region == "" {
t.Region = viper.GetString("default.region")
}
return nil
}