/
main.go
131 lines (104 loc) · 3.31 KB
/
main.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
125
126
127
128
129
130
131
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"os/user"
"path/filepath"
log "github.com/Sirupsen/logrus"
"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/fatih/color"
flag "github.com/ogier/pflag"
open "github.com/petermbenjamin/go-open"
)
var (
debugFlag = flag.BoolP("debug", "d", false, "Debug")
helpFlag = flag.BoolP("help", "h", false, "Print Help")
awsCredFileFlag string
awsProfileFlag string
currentUser *user.User
userName string
homeDir string
)
const (
consoleURL = "https://console.aws.amazon.com/"
signinURL = "https://signin.aws.amazon.com/federation"
)
// Session represents AWS Console session
type Session struct {
SessionID string `json:"sessionId"`
SessionKey string `json:"sessionKey"`
SessionToken string `json:"sessionToken"`
}
// Token represents AWS SigninToken object
type Token struct {
SigninToken string `json:"SigninToken"`
}
func setup() {
currentUser, err := user.Current()
if err != nil {
color.Red(err.Error())
}
userName = currentUser.Username
homeDir = currentUser.HomeDir
}
func init() {
setup()
flag.StringVarP(&awsCredFileFlag, "credentials", "c", filepath.Join(homeDir, ".aws", "credentials"), "Path to AWS credentials file")
flag.StringVarP(&awsProfileFlag, "profile", "p", "default", "AWS Profile")
}
func main() {
flag.Parse()
if *debugFlag {
log.SetLevel(log.DebugLevel)
}
if *helpFlag {
fmt.Println("Usage: aws-console <options>")
fmt.Println("Options:")
flag.PrintDefaults()
os.Exit(1)
}
creds := credentials.NewSharedCredentials(awsCredFileFlag, awsProfileFlag)
sess := session.Must(session.NewSession())
svc := sts.New(sess, aws.NewConfig().WithCredentials(creds))
tokenOutput, err := svc.GetFederationToken(&sts.GetFederationTokenInput{Name: aws.String(userName), Policy: aws.String("{\"Version\": \"2012-10-17\", \"Statement\": [{\"Sid\": \"Stmt1437414476731\", \"Action\": \"*\",\"Effect\": \"Allow\", \"Resource\": \"*\" }]}")})
if err != nil {
color.Red(err.Error())
}
log.Debugf("Credentials from AWS Federation: %+v\n", *tokenOutput.Credentials)
loginSession := &Session{
SessionID: *tokenOutput.Credentials.AccessKeyId,
SessionKey: *tokenOutput.Credentials.SecretAccessKey,
SessionToken: *tokenOutput.Credentials.SessionToken,
}
loginSessionJSON, err := json.Marshal(loginSession)
if err != nil {
color.Red(err.Error())
}
signinTokenURL := signinURL + "?Action=getSigninToken" + "&SessionType=json" + "&Session=" + url.QueryEscape(string(loginSessionJSON))
log.Debugf("Signin Token URL: %+v\n", signinTokenURL)
resp, err := http.Get(signinTokenURL)
if err != nil {
color.Red(err.Error())
}
defer resp.Body.Close()
respJSON, err := ioutil.ReadAll(resp.Body)
if err != nil {
color.Red(err.Error())
}
var token Token
err = json.Unmarshal(respJSON, &token)
if err != nil {
color.Red(err.Error())
}
log.Debugf("Token from AWS Federation: %+v\n", token)
awsConsoleURL := signinURL + "?Action=login" + "&SigninToken=" + url.QueryEscape(token.SigninToken) + "&Issuer=" + userName + "&Destination=" + url.QueryEscape(consoleURL)
log.Debugf("AWS Console URL to be launched: %+v\n", awsConsoleURL)
open.Open([]string{awsConsoleURL})
}