/
rootCmd.go
216 lines (179 loc) · 6.03 KB
/
rootCmd.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
package main
import (
"context"
"errors"
"fmt"
"os"
"strings"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/organizations"
"github.com/aws/aws-sdk-go-v2/service/organizations/types"
"github.com/aws/aws-sdk-go-v2/service/ssoadmin"
"github.com/go-ini/ini"
"github.com/spf13/cobra"
)
const DEFAULT_FILENAME string = "aws.config"
var (
ssoSession string
profile string
ssoRegion string
mapping string
filename string
)
func parseNicknameMapping(mapping string) map[string]string {
nicknameMapping := make(map[string]string)
if len(mapping) == 0 {
return nicknameMapping
}
tokens := strings.Split(mapping, ",")
for _, token := range tokens {
parts := strings.Split(token, "=")
nicknameMapping[parts[0]] = parts[1]
}
return nicknameMapping
}
func handleRoot(cmd *cobra.Command, args []string) error {
ctx := context.TODO()
if err := cmd.MarkFlagRequired("sso-session"); err != nil {
return err
}
if err := cmd.MarkFlagRequired("sso-region"); err != nil {
return err
}
var cfg aws.Config
var err error
// check if a profile is specified
if profile != "" {
// create a config with the specified profile
cfg, err = config.LoadDefaultConfig(ctx, config.WithRegion(ssoRegion), config.WithSharedConfigProfile(profile))
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
} else {
// create a config with static credentials
cfg, err = config.LoadDefaultConfig(ctx, config.WithRegion(ssoRegion))
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
nicknameMapping := parseNicknameMapping(mapping)
// get the SSO instance ARN (there's only one allowed)
ssoClient := ssoadmin.NewFromConfig(cfg)
resp, err := ssoClient.ListInstances(ctx, nil)
if err != nil {
return err
}
if len(resp.Instances) == 0 {
return errors.New("SSO is not enabled. No SSO instances exist.")
}
instanceArn := resp.Instances[0].InstanceArn
// list all accounts
var accounts []types.Account
orgClient := organizations.NewFromConfig(cfg)
var token *string
for {
orgOutput, err := orgClient.ListAccounts(ctx, &organizations.ListAccountsInput{NextToken: token})
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
for _, v := range orgOutput.Accounts {
accounts = append(accounts, v)
}
if orgOutput.NextToken == nil {
break
}
token = orgOutput.NextToken
}
// print out the SSO session configuration to file
payload := ini.Empty()
ssoSection := payload.Section(fmt.Sprintf("sso-session %s", ssoSession))
if _, err := ssoSection.NewKey("sso_start_url", fmt.Sprintf("https://%s.awsapps.com/start", *resp.Instances[0].IdentityStoreId)); err != nil {
return err
}
if _, err := ssoSection.NewKey("sso_region", ssoRegion); err != nil {
return err
}
if _, err := ssoSection.NewKey("sso_registration_scopes", "sso:account:access"); err != nil {
return err
}
// loop through each account
for _, account := range accounts {
// list-permission-sets-provisioned-to-account
params := &ssoadmin.ListPermissionSetsProvisionedToAccountInput{
InstanceArn: instanceArn,
AccountId: account.Id,
}
resp, err := ssoClient.ListPermissionSetsProvisionedToAccount(ctx, params)
if err != nil {
return err
}
// loop through permissions sets
for _, permissionSetArn := range resp.PermissionSets {
// get permission set name
params := &ssoadmin.DescribePermissionSetInput{
InstanceArn: instanceArn,
PermissionSetArn: aws.String(permissionSetArn),
}
resp, err := ssoClient.DescribePermissionSet(ctx, params)
if err != nil {
return err
}
// create section for AccountId-PermissionSet profile
section1 := payload.Section(fmt.Sprintf("profile %s-%s", *account.Id, *resp.PermissionSet.Name))
section1.Comment = fmt.Sprintf("# %s. Session Duration: %s", *resp.PermissionSet.Description, *resp.PermissionSet.SessionDuration)
if _, err := section1.NewKey("sso_session", ssoSession); err != nil {
return err
}
if _, err := section1.NewKey("sso_account_id", *account.Id); err != nil {
return err
}
if _, err := section1.NewKey("sso_role_name", *resp.PermissionSet.Name); err != nil {
return err
}
if len(nicknameMapping) == 0 {
continue
}
// create section for AccountNickname-PermissionSet profile
section2 := payload.Section(fmt.Sprintf("profile %s-%s", nicknameFor(*account.Id, nicknameMapping), *resp.PermissionSet.Name))
section2.Comment = fmt.Sprintf("# %s. Session Duration: %s", *resp.PermissionSet.Description, *resp.PermissionSet.SessionDuration)
if _, err := section2.NewKey("sso_session", ssoSession); err != nil {
return err
}
if _, err := section2.NewKey("sso_account_id", *account.Id); err != nil {
return err
}
if _, err := section2.NewKey("sso_role_name", *resp.PermissionSet.Name); err != nil {
return err
}
}
}
if err := payload.SaveTo(filename); err != nil {
return err
}
fmt.Printf("Wrote to %s\n", filename)
return nil
}
func nicknameFor(accountId string, nicknameMapping map[string]string) string {
v := nicknameMapping[accountId]
if v == "" {
v = fmt.Sprintf("NoNickname-%s", accountId)
}
return v
}
var rootCmd = &cobra.Command{
Use: "aws-config-creator",
Short: "Creates an AWS config file from AWS SSO configuration",
Long: "Parses an AWS organizations permission set structure to build a complete .aws/config file with all permission sets provisioned across all AWS member accounts",
RunE: handleRoot,
}
func init() {
rootCmd.PersistentFlags().StringVarP(&ssoSession, "sso-session", "s", "", "Nickname to give the SSO Session (e.g. org name)")
rootCmd.PersistentFlags().StringVarP(&profile, "profile", "p", "", "Profile")
rootCmd.PersistentFlags().StringVarP(&ssoRegion, "sso-region", "r", "", "AWS region where AWS SSO resides")
rootCmd.PersistentFlags().StringVarP(&mapping, "mapping", "m", "", "Comma-delimited Account Nickname Mapping (id=nickname)")
rootCmd.PersistentFlags().StringVarP(&filename, "output", "o", DEFAULT_FILENAME, "Where the AWS config file will be written")
}