Skip to content

Commit c3d62b7

Browse files
authored
Merge pull request #5 from cloudgraphdev/feature/CG-1026
feat: RulesEngine Optimization
2 parents ba33ba2 + ab75afa commit c3d62b7

File tree

9 files changed

+289
-244
lines changed

9 files changed

+289
-244
lines changed

src/plugins/policyPack/index.ts

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -186,19 +186,18 @@ export default class PolicyPackPlugin extends Plugin {
186186
}
187187

188188
// TODO: Generalize data processor moving storage module to SDK with its interfaces
189-
private getDataProcessor({
190-
entity,
191-
provider,
192-
}: {
193-
entity: string
194-
provider: string
189+
private getDataProcessor(config: {
190+
providerName: string
191+
entityName: string
192+
typenameToFieldMap?: { [tn: string]: string }
193+
extraFields?: string[]
195194
}): DataProcessor {
196-
const dataProcessorKey = `${provider}${entity}`
195+
const dataProcessorKey = `${config.providerName}${config.entityName}`
197196
if (this.dataProcessors[dataProcessorKey]) {
198197
return this.dataProcessors[dataProcessorKey]
199198
}
200199

201-
const dataProcessor = new DgraphDataProcessor(provider, entity)
200+
const dataProcessor = new DgraphDataProcessor(config)
202201
this.dataProcessors[dataProcessorKey] = dataProcessor
203202
return dataProcessor
204203
}
@@ -256,14 +255,17 @@ export default class PolicyPackPlugin extends Plugin {
256255
continue // eslint-disable-line no-continue
257256
}
258257

259-
// Initialize RulesEngine
260-
const rulesEngine = new RulesEngine({
258+
// Initialize Data Processor
259+
const dataProcessor = this.getDataProcessor({
261260
providerName: this.provider.name,
262261
entityName: policyPackPlugin.entity,
263262
typenameToFieldMap: resourceTypeNamesToFieldsMap,
264263
extraFields: policyPackPlugin.extraFields,
265264
})
266265

266+
// Initialize RulesEngine
267+
const rulesEngine = new RulesEngine(dataProcessor)
268+
267269
this.policyPacksPlugins[policyPack] = {
268270
engine: rulesEngine,
269271
entity: policyPackPlugin.entity,
@@ -318,14 +320,8 @@ export default class PolicyPackPlugin extends Plugin {
318320
storageEngine,
319321
})
320322

321-
// Data Processor
322-
const dataProcessor = this.getDataProcessor({
323-
entity,
324-
provider: this.provider.name,
325-
})
326-
327323
// Prepare mutations
328-
const mutations = dataProcessor.prepareMutations(findings)
324+
const mutations = engine.prepareMutations(findings, rules)
329325

330326
// Save connections
331327
processConnectionsBetweenEntities({
Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,28 @@
11
import { Entity } from '../../types'
2-
import { RuleFinding } from '../types'
2+
import { Rule, RuleFinding } from '../types'
33

44
export default interface DataProcessor {
5+
readonly typenameToFieldMap: { [typeName: string]: string }
6+
7+
readonly extraFields: string[]
8+
9+
/**
10+
* Returns an GraphQL schema build dynamically based on the provider and existing resources
11+
* @returns new schemas and extensions for existing ones
12+
*/
13+
getSchema: () => string[]
14+
515
/**
616
* Transforms RuleFinding array into a mutation array for GraphQL
717
* @param findings resulted findings during rules execution
818
* @returns {Entity[]} Array of generated mutations
919
*/
10-
prepareMutations: (findings: RuleFinding[]) => Entity[]
20+
prepareFindingsMutations: (findings: RuleFinding[]) => Entity[]
21+
22+
/**
23+
* Transforms Rules array into a mutation array for GraphQL
24+
* @param rules rules metadata
25+
* @returns {Entity[]} Array of generated mutations
26+
*/
27+
prepareRulesMetadataMutations: (rules: Rule[]) => Entity[]
1128
}

0 commit comments

Comments
 (0)