-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
277 lines (238 loc) · 8.18 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package main
import (
"bytes"
"context"
"crypto/sha1"
"encoding/hex"
"encoding/json"
"fmt"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/aws/external"
"github.com/aws/aws-sdk-go-v2/service/sso"
"github.com/mitchellh/go-homedir"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"gopkg.in/ini.v1"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
)
type AWSSSOCredential struct {
StartURL string `json:"startUrl"`
Region string `json:"region"`
AccessToken string `json:"accessToken"`
ExpiresAt AWSTime `json:"expiresAt"`
}
type CredentialProcessJson struct {
Version int `json:"Version"`
AccessKeyID string `json:"AccessKeyId"`
SecretAccessKey string `json:"SecretAccessKey"`
SessionToken string `json:"SessionToken"`
Expiration AWSTime `json:"Expiration"`
}
type Profile struct {
SSOAccountID string
SSORegion string
SSORoleName string
SSOStartUrl string
}
type AWSTime struct {
time.Time
}
func (it *AWSTime) UnmarshalJSON(data []byte) error {
t, err := time.Parse("2006-01-02T15:04:05Z07:00", strings.Trim(strings.Replace(string(data), "UTC", "Z", 1), `"`))
if err == nil {
*it = AWSTime{t}
}
return err
}
func (it AWSTime) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf("\"%sZ\"", it.Time.UTC().Format("2006-01-02T15:04:05"))), nil
}
func main() {
zerolog.SetGlobalLevel(zerolog.InfoLevel)
_, ok := os.LookupEnv("DEBUG")
if ok {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
zerolog.SetGlobalLevel(zerolog.DebugLevel)
}
argsWithoutProg := os.Args[1:]
if len(argsWithoutProg) != 1 {
fmt.Println("should only be 1 argument, and should be the name of a profile")
os.Exit(1)
}
profileName := fmt.Sprintf("profile %s", argsWithoutProg[0])
log.Debug().Str("profileName", profileName).Msg("profile name")
homePath, err := homedir.Dir()
if err != nil {
log.Fatal().Err(err).Msg("error getting home dir")
}
log.Debug().Str("path", homePath).Msg("home")
awsConfigPath := filepath.Join(homePath, ".aws", "config")
log.Debug().Str("path", awsConfigPath).Msg("config")
awsSsoCachePath := filepath.Join(homePath, ".aws", "sso", "cache")
log.Debug().Str("path", awsSsoCachePath).Msg("sso_cache")
cachedProfile, err := getCachedFile(awsSsoCachePath, profileName)
if err != nil {
log.Error().Err(err).Msg("error accessing cached profile")
}
if cachedProfile != nil {
printProfile(*cachedProfile)
os.Exit(0)
}
profileSection, err := getAwsProfile(awsConfigPath, profileName)
if err != nil {
log.Fatal().Err(err).Str("profile", profileName).Str("path", awsConfigPath).Msg("error getting aws profile")
}
profile, err := parseProfile(profileSection)
if err != nil {
log.Fatal().Err(err).Msg("error creating profile, that's not great")
}
awsSSOCredential, err := getSsoCachedLogin(profile, awsSsoCachePath)
if err != nil {
log.Fatal().Err(err).Msg("could not get sso cached login")
}
credentialProcessJson, err := getSsoRoleCredentials(profile, awsSSOCredential)
if err != nil {
log.Fatal().Err(err).Msg("could not get role credentials with SSO credentials")
}
err = writeCachedFile(awsSsoCachePath, profileName, credentialProcessJson)
if err != nil {
log.Error().Err(err).Msg("could not write cache file")
}
printProfile(credentialProcessJson)
}
func writeCachedFile(awsSsoCachePath, awsSSOProfileName string, credentialProcessJson CredentialProcessJson) error {
cachedFileName := getCachedFileName(awsSSOProfileName)
cachedFilePath := filepath.Join(awsSsoCachePath, cachedFileName)
buffer, err := jsonEncode(credentialProcessJson)
if err != nil {
return err
}
if err != nil {
return err
}
err = ioutil.WriteFile(cachedFilePath, buffer.Bytes(), 0600)
if err != nil {
return err
}
return nil
}
func getCachedFile(awsSsoCachePath, awsSSOProfileName string) (*CredentialProcessJson, error) {
cachedFileName := getCachedFileName(awsSSOProfileName)
cachedFilePath := filepath.Join(awsSsoCachePath, cachedFileName)
var credentialProcessJson CredentialProcessJson
bytes, err := ioutil.ReadFile(cachedFilePath)
if err != nil {
return nil, err
}
err = json.Unmarshal(bytes, &credentialProcessJson)
if err != nil {
return nil, err
}
if time.Now().After(credentialProcessJson.Expiration.Time) {
log.Debug().Str("expire", credentialProcessJson.Expiration.String()).Msg("credentials expired")
return nil, nil
}
log.Debug().Str("path", cachedFilePath).Msg("using cache file")
return &credentialProcessJson, nil
}
func getCachedFileName(awsSSOProfileName string) string {
profileNameSha1 := sha1.Sum([]byte(awsSSOProfileName))
return fmt.Sprintf("aws-sso-fetcher-%s.json", hex.EncodeToString(profileNameSha1[:]))
}
func printProfile(credentialProcessJson CredentialProcessJson) {
buffer, err := jsonEncode(credentialProcessJson)
if err != nil {
log.Fatal().Err(err).Msg("encoding json exploded")
}
fmt.Printf("%s", buffer.String())
}
func jsonEncode(credentialProcessJson CredentialProcessJson) (*bytes.Buffer, error) {
buffer := new(bytes.Buffer)
encoder := json.NewEncoder(buffer)
encoder.SetIndent("", " ")
err := encoder.Encode(credentialProcessJson)
if err != nil {
return nil, err
}
return buffer, nil
}
func getSsoRoleCredentials(profile Profile, awsSSOCredential AWSSSOCredential) (CredentialProcessJson, error) {
var credentialProcessJson CredentialProcessJson
config, err := external.LoadDefaultAWSConfig(external.WithRegion(profile.SSORegion))
if err != nil {
log.Fatal().Err(err).Msg("error loading default config")
}
client := sso.New(config)
resp, err := client.GetRoleCredentialsRequest(&sso.GetRoleCredentialsInput{
AccessToken: aws.String(awsSSOCredential.AccessToken),
AccountId: aws.String(profile.SSOAccountID),
RoleName: aws.String(profile.SSORoleName),
}).Send(context.TODO())
if err != nil {
return credentialProcessJson, err
}
return CredentialProcessJson{
Version: 1,
AccessKeyID: *resp.RoleCredentials.AccessKeyId,
SecretAccessKey: *resp.RoleCredentials.SecretAccessKey,
SessionToken: *resp.RoleCredentials.SessionToken,
Expiration: AWSTime{aws.MillisecondsTimeValue(resp.RoleCredentials.Expiration)},
}, nil
}
func getSsoCachedLogin(profile Profile, ssoCachePath string) (AWSSSOCredential, error) {
var awsSSOCredential AWSSSOCredential
bs := sha1.Sum([]byte(profile.SSOStartUrl))
cachedFilePath := filepath.Join(ssoCachePath, fmt.Sprintf("%x.json", bs))
bytes, err := ioutil.ReadFile(cachedFilePath)
if err != nil {
return awsSSOCredential, err
}
err = json.Unmarshal(bytes, &awsSSOCredential)
if err != nil {
return awsSSOCredential, err
}
if time.Now().After(awsSSOCredential.ExpiresAt.Time) {
log.Debug().Str("ExpiresAt", awsSSOCredential.ExpiresAt.String()).Msg("credential is expired")
return awsSSOCredential, fmt.Errorf("Credentials expired")
}
return awsSSOCredential, nil
}
func parseProfile(section *ini.Section) (Profile, error) {
var profile Profile
profileAccountId, err := section.GetKey("sso_account_id")
if err != nil {
return profile, fmt.Errorf("error getting sso_account_id from profile: %w", err)
}
log.Debug().Str("id", profileAccountId.String()).Msg("found account id")
profile.SSOAccountID = profileAccountId.String()
profileRegionKey, err := section.GetKey("sso_region")
if err != nil {
return profile, fmt.Errorf("error getting sso_region from profile: %w", err)
}
log.Debug().Str("value", profileRegionKey.String()).Msg("profile Region")
profile.SSORegion = profileRegionKey.String()
profileRoleName, err := section.GetKey("sso_role_name")
if err != nil {
return profile, fmt.Errorf("error getting sso_role_name from profile: %w", err)
}
log.Debug().Str("name", profileRoleName.String()).Msg("found role name")
profile.SSORoleName = profileRoleName.String()
profileSSOStartURLKey, err := section.GetKey("sso_start_url")
if err != nil {
return profile, fmt.Errorf("error getting sso_start_url from profile: %w", err)
}
log.Debug().Str("value", profileSSOStartURLKey.String()).Msg("profile StartUrl")
profile.SSOStartUrl = profileSSOStartURLKey.String()
return profile, nil
}
func getAwsProfile(configPath, profileName string) (*ini.Section, error) {
cfg, err := ini.Load(configPath)
if err != nil {
return nil, err
}
return cfg.GetSection(profileName)
}