This repository has been archived by the owner on Aug 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmainV2.js
271 lines (179 loc) · 9.35 KB
/
mainV2.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
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
const { graphListMod, graphListModBeta } = require('./src/graphf')
const fs = require('fs')
const { graphBatching } = require('./src/batcher')
const { randomUUID } = require('crypto')
const { argv } = require('yargs')
const { azBatch } = require('./src/azbatch')
const { getAzRoles } = require('./roleassignments')
module.exports = { mainV2 }
async function mainV2(token) {
// Group main objects
let opsPromarra = []
let ops = [
{
type: "appsOwner",
objects: [],
query: ['/applications?$filter=owners/$count ne 0&$count=true&$top=999']
},
{
type: "appsNoOwner",
objects: [],
query: ['/applications?$filter=owners/$count eq 0&$count=true&$top=999']
},
{
type: "spnOwner",
objects: [],
query: ['/servicePrincipals?$filter=owners/$count ne 0&$count=true&$top=999']
},
{
type: "spnNoOwner",
objects: [],
query: ['/servicePrincipals?$filter=owners/$count eq 0&$count=true&$top=999']
},
{
type: "oauth2PermissionGrants",
objects: [],
query:"oauth2PermissionGrants?$top=999"
},
{
type: "applications",
objects: [],
},
{
type: "serviceprincipals",
objects: [],
}
]
ops.filter(s =>s?.query).forEach(op => {
console.log('checking ', op)
opsPromarra.push(graphListMod(token?.access_token, op?.query, undefined, op.objects))
})
try {
await Promise.all(opsPromarra)
} catch (err) {
console.log(err)
}
fs.writeFileSync('ops.json', JSON.stringify(ops))
// Check for betaSign-ins
const lastSignIn = []
try {
await graphListModBeta(token?.access_token, '/reports/servicePrincipalSignInActivities', undefined,lastSignIn)
} catch (err) {
console.log('skipping lastsignins, user likely not in required role')
}
//
//store the raw ops file, so no further processing is needed for development
/*
ops = require('./ops.json') */
// Write all of the files from the ops
/* ops.forEach(f => fs.writeFileSync(`${f?.type}.json`, JSON.stringify(f?.objects))) */
// Transform new unified object with just owners and servicePrincipals
ops.filter(s => s?.type.match('apps') || s?.type.match('spn')).forEach(s => s.objects.map(ob => ob.HasOwner = s?.type))
ops.filter(s => s?.type.match('apps') || s?.type.match('spn')).forEach(s => s.objects.forEach(ob => {
if (ob?.HasOwner.match('apps')) {
ops.find(s => s?.type == 'applications').objects.push(ob)
}
if (ob?.HasOwner.match('spn')) {
ops.find(s => s?.type == 'serviceprincipals').objects.push(ob)
}
}))
let clean = ops.filter( s =>!s?.type.match('Owner'))
// Array destruct
let [oauth2PermissionGrantsOp, applicationsOp, servicePrincipalsOp] = clean
// further Destructure
let { objects: applications } = applicationsOp;
let { objects: servicePrincipals } = servicePrincipalsOp;
let { objects: oauth2PermissionGrants } = oauth2PermissionGrantsOp;
let usersBatch = Array.from(new Set(oauth2PermissionGrants.map(user => user.principalId))).filter(s => s)
.map(s => s = { url: `/users/${s}`, method: "GET" })
// Symmetric return filter is implemented for users (assumes all values are symmetric)
let users = await graphBatching(usersBatch, token?.access_token, (item) => item?.map(s => s?.body))
fs.writeFileSync('users.json', JSON.stringify(users))
// FedCreds
let filteredOpsPromArra = []
let filteredOpsNext = [
{
type:"spnFedCred",
query: "/servicePrincipals?$filter=federatedIdentityCredentials/$count ne 0&$count=true&$select=id,appId",
objects: []
},
{
type:"appFedCred",
query: "/applications?$filter=federatedIdentityCredentials/$count ne 0&$count=true&$select=id,appId",
objects: []
}
]
try {
filteredOpsNext.forEach(op => {
console.log('checking ', op)
filteredOpsPromArra.push(graphListMod(token?.access_token, op?.query, undefined, op.objects))
/* console.log() */
})
await Promise.all(filteredOpsPromArra)
console.log()
// Map federated credentials to applications and servicePrincipals
} catch (err) {
console.log(err)
}
let [spnFedCred,appFedCred] = filteredOpsNext
let appFedCredsListBatch = appFedCred?.objects
.map(s => s = { url: `/applications/${s?.id}/federatedIdentityCredentials?$top=999`, method: "GET", providedId: s?.id })
// modify so, that the ID created in MAP can be mapped back to result
let appFedCreds = await graphBatching(appFedCredsListBatch, token?.access_token, (item) => item?.map(s => s = { content: s?.body?.value, id: s?.id }), undefined, 5, 200)
console.log(appFedCreds?.length)
let spnFedCredsListBatch = spnFedCred?.objects
.map(s => s = { url: `/servicePrincipals/${s?.id}/federatedIdentityCredentials?$top=999`, method: "GET", providedId: s?.id })
// modify so, that the ID created in MAP can be mapped back to result
let spnFedCreds = await graphBatching(spnFedCredsListBatch, token?.access_token, (item) => item?.map(s => s = { content: s?.body?.value, id: s?.id }), undefined, 5, 200)
console.log(spnFedCreds?.length)
applications.map(s => s.federatedCredentials = appFedCreds.find(app => app?.id == s?.id)?.content ||[])
servicePrincipals.map(s => s.federatedCredentials = spnFedCreds.find(spn => spn?.id == s?.id)?.content ||[])
// AppOwnersOwners and
let AppOwnersOwnersBatch = applications.filter(app => app.HasOwner == 'appsOwner')
.map(s => s = { url: `/applications/${s?.id}/owners?$select=id,displayName&$top=999`, method: "GET", providedId: s?.appId })
// modify so, that the ID created in MAP can be mapped back to result
let appOwners = await graphBatching(AppOwnersOwnersBatch, token?.access_token, (item) => item?.map(s => s = { content: s?.body?.value, id: s?.id }), undefined, 5, 200)
console.log(appOwners?.length)
let SPNOwnersOwnersBatch = servicePrincipals.filter(spn => spn.HasOwner == 'spnOwner')
.map(s => s = { url: `/servicePrincipals/${s?.id}/owners?$select=id,displayName,AppId&$top=999`, method: "GET", providedId: s?.appId })
// modify so, that the ID created in MAP can be mapped back to result
let SPNOwners = await graphBatching(SPNOwnersOwnersBatch, token?.access_token, (item) => item?.map(s => s = { content: s?.body?.value, id: s?.id }), undefined, 5, 200)
console.log(SPNOwners?.length)
/* fs.writeFileSync('SPNOwners.json', JSON.stringify(SPNOwners)) */
//Map Owners to SPNS
servicePrincipals.map( s=> {
let ownersInApplication = appOwners.find(app => app?.id == s?.appId)?.content.map(owner => `AppOwner:${owner?.displayName}`) || []
let ownersInSPN = SPNOwners.find(app => app?.id == s?.appId)?.content.map(owner => `SPNOwner:${owner?.displayName}`) || []
s.owners = [ownersInApplication,ownersInSPN].flat()
})
// AppRoleAssignedTo
// This was changed to appRoleAssignments from the "AppRoleAssignedTo" as the latter in case of Graph can easily have more than 999 instances. Otherwise it is more rare to have single app given more than 999 AppRole Assignments
let spnAppRoleAssignedBatch = servicePrincipals.map(app => app.id)
.map(s => s = { url: `/servicePrincipals/${s}/appRoleAssignments?$top=999`, method: "GET", providedId: s })
/*
// Allows to debug higher batch sizes for logic throttling testing
servicePrincipals.map(app => app.id).forEach( s => {
for (let index = 0; index < 3; index++) {
if (index % 6 == 0 || index % 5 == 0) {
spnAppRoleAssignedBatch.push({ url: `/servicePrincipals/${s}/appRoleAssignedTo`, method: "GET", providedId: randomUUID() })
}
spnAppRoleAssignedBatch.push({ url: `/servicePrincipals/${s}/appRoleAssignedTo`, method: "GET", providedId: randomUUID() })
}
}) */
// modify so, that the ID created in MAP can be mapped back to result
let spnAppRoleAssigned = await graphBatching(spnAppRoleAssignedBatch, token?.access_token, (item) => item?.map(s => s = s?.body?.value).flat(), undefined, 5, 200)
console.log(spnAppRoleAssigned?.length)
if (argv?.azRbac) {
console.log('getting AzRoles')
await getAzRoles(servicePrincipals).catch(err => {
console.log()
})
console.log()
}
fs.writeFileSync(`roles.json`, JSON.stringify(spnAppRoleAssigned))
fs.writeFileSync('servicePrincipals.json',JSON.stringify(servicePrincipals))
fs.writeFileSync('oauth2PermissionGrants.json',JSON.stringify(oauth2PermissionGrants))
fs.writeFileSync('lastSignin.json',JSON.stringify(lastSignIn))
fs.writeFileSync('applications.json',JSON.stringify(applications))
console.log()
}