-
Notifications
You must be signed in to change notification settings - Fork 13
/
copy-role.js
236 lines (183 loc) · 8.52 KB
/
copy-role.js
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
const AWS = require("aws-sdk")
let iam
;(async () => {
try {
const [sourceRoleName, targetRoleName] = loadArguments()
checkAwsCredentials()
const sourceRole = await fetchRole(sourceRoleName)
const inlinePolicies = await fetchInlinePolicies(sourceRoleName)
const managedPolicies = await fetchManagedPolicies(sourceRoleName)
await createRoleFromExisting(sourceRole, targetRoleName)
if (inlinePolicies.length > 0) {
await addInlinePolicies(targetRoleName, inlinePolicies)
}
if (managedPolicies.length > 0) {
await addManagedPolicies(targetRoleName, managedPolicies)
}
log('\nDone!')
} catch (e) {
error(e.message)
}
})()
function loadArguments() {
log('\n--> Parsing arguments from command line...')
const cmdArgs = process.argv.slice(2)
if (cmdArgs.length !== 2) {
throw new TypeError("<-- Usage: node copy-role.js SOURCE_ROLE_NAME TARGET_ROLE_NAME")
}
log(`<-- Arguments loaded. Source role name: ${cmdArgs[0]}, target role name: ${cmdArgs[1]}`)
return cmdArgs
}
function checkAwsCredentials() {
log('\n--> Checking if AWS credentials are loaded...')
if (!AWS.config.credentials) {
throw new Error(`<-- Failed to find AWS credentials. Consider providing them with environment variables.`)
}
log('<-- AWS credentials found.')
}
async function fetchRole(roleName) {
log('\n--> Fetching source role...')
let role
try {
role = (await getIam().getRole({RoleName: roleName}).promise()).Role
} catch (e) {
throw new Error(`<-- Failed to fetch source role: "${e.message}"`)
}
log('<-- Source role loaded.')
return role
}
async function fetchInlinePolicies(roleName) {
log(`\n--> Fetching inline policy names for ${roleName}...`)
let inlinePolicyNames
try {
inlinePolicyNames = await fetchInlinePoliciesRecursive()
} catch (e) {
throw new Error(`<-- Failed to fetch inline policy names: "${e.message}"`)
}
log(`<-- Loaded ${inlinePolicyNames.length} inline policy names.`)
if (inlinePolicyNames.length === 0) {
return []
}
log('--> Fetching inline policies...')
let inlinePolies = []
try {
for (const inlinePolicyName of inlinePolicyNames) {
inlinePolies.push(await getIam().getRolePolicy({RoleName: roleName, PolicyName: inlinePolicyName}).promise())
}
} catch (e) {
throw new Error(`<-- Failed to fetch inline policy: "${e.message}"`)
}
log(`<-- Loaded inline policies.`)
return inlinePolies
async function fetchInlinePoliciesRecursive(marker) {
let inlinePolicyNames
const response = await getIam().listRolePolicies({RoleName: roleName, Marker: marker}).promise()
inlinePolicyNames = response.PolicyNames
if (response.IsTruncated) {
inlinePolicyNames = inlinePolicyNames.concat(await fetchInlinePoliciesRecursive(response.Marker))
}
return inlinePolicyNames
}
}
async function fetchManagedPolicies(roleName) {
log(`\n--> Fetching managed policies for ${roleName}...`)
let managedPolicies
try {
managedPolicies = await fetchManagedPoliciesRecursive()
} catch (e) {
throw new Error(`<-- Failed to fetch managed policies: "${e.message}"`)
}
log(`<-- Loaded ${managedPolicies.length} managed policies.`)
return managedPolicies
async function fetchManagedPoliciesRecursive(marker) {
let managedPolicies
const response = await getIam().listAttachedRolePolicies({RoleName: roleName, Marker: marker}).promise()
managedPolicies = response.AttachedPolicies
if (response.IsTruncated) {
managedPolicies = managedPolicies.concat(await fetchManagedPoliciesRecursive(response.Marker))
}
return managedPolicies
}
}
async function createRoleFromExisting(sourceRole, targetRoleName) {
log(`\n--> Creating a new role ${targetRoleName}...`)
let targetRole
try {
targetRole = (await getIam().createRole({
Path: sourceRole.Path,
RoleName: targetRoleName,
AssumeRolePolicyDocument: decodeURIComponent(sourceRole.AssumeRolePolicyDocument),
Description: sourceRole.Description,
MaxSessionDuration: sourceRole.MaxSessionDuration,
PermissionsBoundary: sourceRole.PermissionsBoundary ? sourceRole.PermissionsBoundary.PermissionsBoundaryArn: undefined,
Tags: sourceRole.Tags,
}).promise()).Role
} catch (e) {
throw new Error(`<-- Failed to create target role: "${e.message}"`)
}
log(`<-- Created role ${targetRoleName}.`)
return targetRole
}
async function addInlinePolicies(targetRoleName, policies) {
log(`\n--> Adding inline policies to ${targetRoleName}...`)
try {
for (const policy of policies) {
await getIam().putRolePolicy({
RoleName: targetRoleName,
PolicyName: policy.PolicyName,
PolicyDocument: decodeURIComponent(policy.PolicyDocument),
}).promise()
}
} catch (e) {
throw new Error(`<-- Failed to add inline policies: "${e.message}"`)
}
log(`<-- Added ${policies.length} inline policies.`)
}
async function addManagedPolicies(targetRoleName, policies) {
log(`\n--> Adding managed policies to ${targetRoleName}...`)
try {
for (const policy of policies) {
await getIam().attachRolePolicy({
RoleName: targetRoleName,
PolicyArn: policy.PolicyArn,
}).promise()
}
} catch (e) {
throw new Error(`<-- Failed to add managed policies: "${e.message}"`)
}
log(`<-- Added ${policies.length} managed policies.`)
}
function getIam() {
if (!iam) {
iam = new AWS.IAM()
}
return iam
}
function log(message) {
console.log(message)
}
function error(message) {
console.log(`
████████████
████ ██████████
████████████████
████████ ████
████████████ ████
██ ████████ ████
████ ██████████████ ████ ████ ████ ████
██████████████████ ██ ████ ██ ████ ████ ████
██████████████████ ████ ██ ████ ████ ████
████████████████ ██ ████ ██ ████ ████ ████
████████████ ██ ████████ ████████████████
████████ ██ ████ ████████████
████ ██ ████████ ████
██ ██ ████ ████ ████ ████
████ ████ ██ ██ ██ ██ ████ ████
████████████████ ██████ ████████████ ████ ██████████ ████ ████
████ ████ ████ ████
████ ████ ████ ████ ████
████ ████ ████
`)
console.error(message)
process.exitCode = 1
}