This repository has been archived by the owner on Jan 14, 2020. It is now read-only.
/
auth.go
124 lines (105 loc) · 3.34 KB
/
auth.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package tasks
import (
"fmt"
"os"
"strings"
printer "github.com/KablamoOSS/go-cli-printer"
"github.com/aws/aws-sdk-go/aws"
"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"
"github.com/aws/aws-sdk-go/service/cloudformation"
"github.com/aws/aws-sdk-go/service/sts"
)
// GetCloudformationClient from the standard credential chain
func GetCloudformationClient(profile string, region string) (string, *cloudformation.CloudFormation) {
var awsConfig *aws.Config
if region == "" {
printer.Fatal(
fmt.Errorf("No region has been set"),
"Pass a region in via the cli with `--region us-east-1` or set a default `region` in kombustion.yaml",
"http://kombustion.io/manifest/#region",
)
}
// If we have a custom env var for assuming a role, lets assume
if len(os.Getenv("ASSUMED_ROLE")) > 0 {
// TODO: Check for all env params, and err if not all exist
creds := getAWSCredentials()
awsConfig = &aws.Config{Credentials: creds, Region: aws.String(region), MaxRetries: aws.Int(5)}
} else {
awsConfig = &aws.Config{Region: aws.String(region), MaxRetries: aws.Int(5)}
}
awsSession := getSession(profile)
stsClient := sts.New(awsSession, awsConfig)
callerID, err := stsClient.GetCallerIdentity(&sts.GetCallerIdentityInput{})
if err != nil {
printer.Fatal(
err,
"Check that you have configured valid AWS credentials",
"",
)
}
cf := cloudformation.New(awsSession, awsConfig)
return *callerID.Account, cf
}
func getAWSCredentials() *credentials.Credentials {
assumedRole := os.Getenv("ASSUMED_ROLE")
mfaSerial := os.Getenv("MFA_SERIAL")
awsMfaToken := os.Getenv("TOKEN")
sess := must(session.NewSession())
if len(mfaSerial) > 0 {
return stscreds.NewCredentials(sess, assumedRole, func(p *stscreds.AssumeRoleProvider) {
p.SerialNumber = aws.String(mfaSerial)
p.TokenCode = aws.String(awsMfaToken)
p.Duration = 3600
})
}
return stscreds.NewCredentials(sess, assumedRole)
}
// Get a session, or fatal error out explaining why we didn't get one
func getSession(profile string) *session.Session {
var options session.Options
options = session.Options{
// We pass a custom token provider here
// to ensure we can stop the printer while we wait for
// the mfa token
AssumeRoleTokenProvider: mfaTokenProvider,
Config: aws.Config{
CredentialsChainVerboseErrors: aws.Bool(true),
},
}
if len(profile) > 0 {
options.Profile = profile
options.SharedConfigState = session.SharedConfigEnable
}
awsSession := must(session.NewSessionWithOptions(options))
return awsSession
}
// Custom token provider to ensure we can stop the printer while we wait for
// the mfa token
func mfaTokenProvider() (string, error) {
var v string
printer.Stop()
fmt.Printf("Enter MFA code: ")
_, err := fmt.Scanln(&v)
return v, err
}
// Ensure a session is returned, else fatal with an error explaning why no session was found
func must(sess *session.Session, err error) *session.Session {
if err != nil {
if strings.Contains(err.Error(), "NoCredentialProviders") {
printer.Fatal(
err,
"You need to provide access credentials to your AWS account.",
"https://www.kombustion.io/docs/getting-started/#ensuring-your-credentials-are-available",
)
}
// TODO: Make this error more helpful
printer.Fatal(
err,
"",
"",
)
}
return sess
}