forked from akamai/cli-terraform
/
create_iam_role.go
196 lines (167 loc) · 5.11 KB
/
create_iam_role.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
package iam
import (
"context"
"errors"
"fmt"
"path/filepath"
"strconv"
"github.com/akamai/AkamaiOPEN-edgegrid-golang/v3/pkg/iam"
"github.com/akamai/cli-terraform/pkg/edgegrid"
"github.com/akamai/cli-terraform/pkg/templates"
"github.com/akamai/cli-terraform/pkg/tools"
"github.com/akamai/cli/pkg/terminal"
"github.com/fatih/color"
"github.com/urfave/cli/v2"
)
var (
// ErrFetchingRole is returned when fetching role fails
ErrFetchingRole = errors.New("unable to fetch role by role_id")
)
// CmdCreateIAMRole is an entrypoint to create-iam role command
func CmdCreateIAMRole(c *cli.Context) error {
ctx := c.Context
sess := edgegrid.GetSession(ctx)
client := iam.Client(sess)
// tfWorkPath is a target directory for generated terraform resources
var tfWorkPath = "./"
if c.IsSet("tfworkpath") {
tfWorkPath = c.String("tfworkpath")
}
tfWorkPath = filepath.FromSlash(tfWorkPath)
groupPath := filepath.Join(tfWorkPath, "groups.tf")
importPath := filepath.Join(tfWorkPath, "import.sh")
rolesPath := filepath.Join(tfWorkPath, "role.tf")
userPath := filepath.Join(tfWorkPath, "users.tf")
variablesPath := filepath.Join(tfWorkPath, "variables.tf")
err := tools.CheckFiles(userPath, groupPath, rolesPath, variablesPath, importPath)
if err != nil {
return cli.Exit(color.RedString(err.Error()), 1)
}
templateToFile := map[string]string{
"groups.tmpl": groupPath,
"imports.tmpl": importPath,
"roles.tmpl": rolesPath,
"users.tmpl": userPath,
"variables.tmpl": variablesPath,
}
processor := templates.FSTemplateProcessor{
TemplatesFS: templateFiles,
TemplateTargets: templateToFile,
}
section := edgegrid.GetEdgercSection(c)
roleID, err := strconv.ParseInt(c.Args().First(), 10, 64)
if err != nil {
return cli.Exit(color.RedString(fmt.Sprintf("Wrong format of role id %v must be a number: %s", roleID, err)), 1)
}
if err = createIAMRoleByID(ctx, roleID, section, client, processor); err != nil {
return cli.Exit(color.RedString(fmt.Sprintf("Error exporting HCL for IAM: %s", err)), 1)
}
return nil
}
func createIAMRoleByID(ctx context.Context, roleID int64, section string, client iam.IAM, templateProcessor templates.TemplateProcessor) error {
term := terminal.Get(ctx)
_, err := term.Writeln("Exporting Identity and Access Management role configuration with related users and groups")
if err != nil {
return err
}
term.Spinner().Start(fmt.Sprintf("Fetching role by role_id %d", roleID))
role, err := client.GetRole(ctx, iam.GetRoleRequest{
ID: roleID,
GrantedRoles: true,
Users: true,
})
if err != nil {
term.Spinner().Fail()
return fmt.Errorf("%w: %s", ErrFetchingRole, err)
}
term.Spinner().OK()
tfRole := TFRole{
RoleID: role.RoleID,
RoleName: role.RoleName,
RoleDescription: role.RoleDescription,
GrantedRoles: getGrantedRolesID(role.GrantedRoles),
}
term.Spinner().Start(fmt.Sprintf("Fetching users with the given role %d", roleID))
users, err := getUsersByRole(ctx, term, role.Users, client)
if err != nil {
term.Spinner().Fail()
return err
}
term.Spinner().OK()
tfUsers := make([]*TFUser, 0)
tfGroups := make([]TFGroup, 0)
term.Spinner().Start(fmt.Sprintf("Fetching groups for users related within role %d", roleID))
for _, user := range users {
userData, err := getTFUser(user)
if err != nil {
term.Spinner().Fail()
return err
}
tfUsers = append(tfUsers, userData)
authGrantsList := user.AuthGrants
if len(authGrantsList) > 0 {
groupsData, err := getTFUserGroups(ctx, client, authGrantsList)
if err != nil {
term.Spinner().Fail()
return err
}
tfGroups = appendUniqueGroups(tfGroups, groupsData)
}
}
term.Spinner().OK()
tfData := TFData{
TFUsers: tfUsers,
TFGroups: tfGroups,
TFRoles: []TFRole{
tfRole,
},
Section: section,
Subcommand: "role",
}
term.Spinner().Start("Saving TF configurations ")
if err = templateProcessor.ProcessTemplates(tfData); err != nil {
term.Spinner().Fail()
return err
}
term.Spinner().OK()
_, err = term.Writeln(fmt.Sprintf("Terraform configuration for role with id '%d' was saved successfully", tfRole.RoleID))
if err != nil {
return nil
}
return nil
}
func appendUniqueGroups(tfGroups []TFGroup, groupsData []TFGroup) []TFGroup {
for _, groupData := range groupsData {
neverSeenGroup := true
for _, tfGroup := range tfGroups {
if groupData.GroupID == tfGroup.GroupID {
neverSeenGroup = false
break
}
}
if neverSeenGroup {
tfGroups = append(tfGroups, groupData)
}
}
return tfGroups
}
func getUsersByRole(ctx context.Context, term terminal.Terminal, roleUsers []iam.RoleUser, client iam.IAM) ([]*iam.User, error) {
users := make([]*iam.User, 0)
for _, roleUser := range roleUsers {
user, err := client.GetUser(ctx, iam.GetUserRequest{
IdentityID: roleUser.UIIdentityID,
Actions: true,
AuthGrants: true,
Notifications: true,
})
if err != nil {
_, err := term.Writeln(fmt.Sprintf("[WARN] Unable to fetch user of ID '%s' - skipping:\n%s", roleUser.UIIdentityID, err))
if err != nil {
return nil, err
}
continue
}
users = append(users, user)
}
return users, nil
}