Skip to content

Commit 171d937

Browse files
committed
feat: Move getLinkedData method to utils folder
1 parent a9a4ccb commit 171d937

File tree

2 files changed

+60
-55
lines changed

2 files changed

+60
-55
lines changed

src/plugins/policyPack/index.ts

Lines changed: 3 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { Result, Rule, Severity } from '../../rules-engine/types'
1515
import Plugin, { ConfiguredPlugin, PluginManager } from '../types'
1616
import DgraphDataProcessor from '../../rules-engine/data-processors/dgraph-data-processor'
1717
import DataProcessor from '../../rules-engine/data-processors/data-processor'
18+
import getLinkedData from '../../utils/data'
1819

1920
export default class PolicyPackPlugin extends Plugin {
2021
constructor({
@@ -272,60 +273,6 @@ export default class PolicyPackPlugin extends Plugin {
272273
}
273274
}
274275

275-
private getLinkedData({ providerData }: { providerData: ProviderData }): any {
276-
const linkedData = {}
277-
const allEntities = providerData?.entities || []
278-
const allConnections = providerData?.connections || {}
279-
const entitiesById: { [key: string]: any } = {}
280-
281-
for (const entity of allEntities) {
282-
// AddawsEc2Input! => queryawsEc2
283-
const mutationName = /(?<=\[)(.*?)(?=\])/
284-
.exec(entity.mutation as any)[0]
285-
.replace('Add', 'query')
286-
.replace('Input!', '')
287-
288-
linkedData[mutationName] = entity.data
289-
290-
for (const entityData of entity.data) {
291-
entitiesById[entityData.id] = entityData
292-
// eslint-disable-next-line no-underscore-dangle
293-
entityData.__typename = mutationName.replace('query', '') // or entity.name?
294-
}
295-
}
296-
297-
// connect data on a second pass
298-
for (const entityId of Object.keys(allConnections)) {
299-
const entityConnections = allConnections[entityId]
300-
const entity = entitiesById[entityId]
301-
if (!entity) {
302-
// eslint-disable-next-line no-continue
303-
continue
304-
}
305-
for (const conn of entityConnections) {
306-
const targetEntity = entitiesById[conn.id]
307-
if (!targetEntity) {
308-
// eslint-disable-next-line no-continue
309-
continue
310-
}
311-
if (conn.relation === 'child') {
312-
if (!entity[conn.field]) {
313-
entity[conn.field] = []
314-
}
315-
entity[conn.field].push(targetEntity)
316-
// inverse relation
317-
// const inverseConnField = this.schemasMap[entity.__typename] || 'account' // @TODO: account doesn't have a name
318-
// if (!targetEntity[inverseConnField]) {
319-
// targetEntity[inverseConnField] = []
320-
// }
321-
// targetEntity[inverseConnField].push(entity)
322-
} // else parent relation.. is not used atm
323-
}
324-
}
325-
326-
return linkedData
327-
}
328-
329276
async execute({
330277
storageRunning,
331278
storageEngine,
@@ -368,7 +315,8 @@ export default class PolicyPackPlugin extends Plugin {
368315
mergeSchemas(currentSchema, findingsSchema),
369316
])
370317

371-
const linkedData = this.getLinkedData({ providerData })
318+
// Format metadata and link connections
319+
const linkedData = getLinkedData(providerData)
372320

373321
const findings = await this.executeRule({
374322
data: linkedData,

src/utils/data.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { ProviderData } from '../types'
2+
3+
const getLinkedData = (providerData: ProviderData): any => {
4+
const linkedData = {}
5+
const allEntities = providerData?.entities || []
6+
const allConnections = providerData?.connections || {}
7+
const entitiesById: { [key: string]: any } = {}
8+
9+
for (const entity of allEntities) {
10+
// AddawsEc2Input! => queryawsEc2
11+
const mutationName = /(?<=\[)(.*?)(?=\])/
12+
.exec(entity.mutation as any)[0]
13+
.replace('Add', 'query')
14+
.replace('Input!', '')
15+
16+
linkedData[mutationName] = entity.data
17+
18+
for (const entityData of entity.data) {
19+
entitiesById[entityData.id] = entityData
20+
// eslint-disable-next-line no-underscore-dangle
21+
entityData.__typename = mutationName.replace('query', '') // or entity.name?
22+
}
23+
}
24+
25+
// connect data on a second pass
26+
for (const entityId of Object.keys(allConnections)) {
27+
const entityConnections = allConnections[entityId]
28+
const entity = entitiesById[entityId]
29+
if (!entity) {
30+
// eslint-disable-next-line no-continue
31+
continue
32+
}
33+
for (const conn of entityConnections) {
34+
const targetEntity = entitiesById[conn.id]
35+
if (!targetEntity) {
36+
// eslint-disable-next-line no-continue
37+
continue
38+
}
39+
if (conn.relation === 'child') {
40+
if (!entity[conn.field]) {
41+
entity[conn.field] = []
42+
}
43+
entity[conn.field].push(targetEntity)
44+
// inverse relation
45+
// const inverseConnField = this.schemasMap[entity.__typename] || 'account' // @TODO: account doesn't have a name
46+
// if (!targetEntity[inverseConnField]) {
47+
// targetEntity[inverseConnField] = []
48+
// }
49+
// targetEntity[inverseConnField].push(entity)
50+
} // else parent relation.. is not used atm
51+
}
52+
}
53+
54+
return linkedData
55+
}
56+
57+
export default getLinkedData

0 commit comments

Comments
 (0)