This repository has been archived by the owner on Apr 29, 2024. It is now read-only.
/
main.go
172 lines (148 loc) · 3.97 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
package main
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"os"
"path/filepath"
"regexp"
"strings"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/sso"
"gopkg.in/ini.v1"
)
func main() {
session, err := findSSOSession()
if err != nil {
log.Fatal(err)
}
profiles, err := listSSOProfiles(session)
if err != nil {
log.Fatal(err)
}
configFile := os.Getenv("AWS_CONFIG_FILE")
if configFile == "" {
configFile = os.ExpandEnv("$HOME/.aws/config")
}
var in io.Reader
in, err = os.Open(configFile)
if err != nil {
if !os.IsNotExist(err) {
log.Fatal(err)
}
in = bytes.NewBufferString("")
}
if err := mergeProfiles(in, configFile, profiles); err != nil {
log.Fatal(err)
}
}
type CacheFile struct {
StartURL string `json:"startUrl"`
Region string `json:"region"`
AccessToken string `json:"accessToken"`
ExpiresAt time.Time `json:"expiresAt"`
}
func findSSOSession() (CacheFile, error) {
logbuf := &bytes.Buffer{}
verbose := log.New(logbuf, "", log.LstdFlags)
dir := os.ExpandEnv("$HOME/.aws/sso/cache")
files, err := os.ReadDir(dir)
if err != nil {
log.Println(err)
}
var best CacheFile
for _, file := range files {
if !strings.HasSuffix(file.Name(), ".json") {
continue
}
data, err := os.ReadFile(filepath.Join(dir, file.Name()))
if err != nil {
verbose.Printf("readfile %v: %v", file.Name(), err)
continue
}
var cacheFile CacheFile
if err := json.Unmarshal(data, &cacheFile); err != nil {
verbose.Printf("parse json %v: %v", file.Name(), err)
continue
}
verbose.Printf("%v: %v (%d)", file.Name(), cacheFile.ExpiresAt, len(cacheFile.AccessToken))
if cacheFile.ExpiresAt.After(best.ExpiresAt) && cacheFile.AccessToken != "" {
best = cacheFile
}
}
if best.AccessToken == "" {
verbose.Println("no tokens")
return CacheFile{}, errors.New(logbuf.String())
}
return best, nil
}
func listSSOProfiles(ssoSession CacheFile) ([]Profile, error) {
ctx := context.Background()
cfg := aws.NewConfig()
cfg.Region = ssoSession.Region
ssosvc := sso.NewFromConfig(*cfg)
accounts, err := ssosvc.ListAccounts(ctx, &sso.ListAccountsInput{
AccessToken: &ssoSession.AccessToken,
})
if err != nil {
return nil, err
}
var profiles []Profile
for _, acc := range accounts.AccountList {
roles, err := ssosvc.ListAccountRoles(ctx, &sso.ListAccountRolesInput{
AccessToken: &ssoSession.AccessToken,
AccountId: acc.AccountId,
})
if err != nil {
return nil, err
}
for _, role := range roles.RoleList {
profiles = append(profiles, Profile{
Name: profileName(*acc.AccountName, *role.RoleName),
SSORoleName: *role.RoleName,
SSOStartURL: ssoSession.StartURL,
SSORegion: ssoSession.Region,
SSOAccountID: *role.AccountId,
Region: ssoSession.Region,
})
}
}
return profiles, nil
}
func profileName(accountName, roleName string) string {
combined := fmt.Sprintf("%s/%s", strings.TrimSpace(accountName), strings.TrimSpace(roleName))
return strings.ToLower(regexp.MustCompile("[^a-zA-Z0-9-/]").ReplaceAllString(combined, "-"))
}
type Profile struct {
Name string
SSOStartURL string
SSORoleName string
SSORegion string
SSOAccountID string
Region string
// output = json
// cli_pager=
}
func mergeProfiles(in io.Reader, outFile string, profiles []Profile) error {
cfg, err := ini.Load(in)
if err != nil {
return err
}
updateProfiles(cfg, profiles)
return cfg.SaveTo(outFile)
}
func updateProfiles(cfg *ini.File, profiles []Profile) {
for _, profile := range profiles {
sectionName := "profile " + profile.Name
cfg.Section(sectionName).Key("sso_start_url").SetValue(profile.SSOStartURL)
cfg.Section(sectionName).Key("sso_account_id").SetValue(profile.SSOAccountID)
cfg.Section(sectionName).Key("sso_role_name").SetValue(profile.SSORoleName)
cfg.Section(sectionName).Key("sso_region").SetValue(profile.SSORegion)
cfg.Section(sectionName).Key("region").SetValue(profile.Region)
}
}