Skip to content

Commit f40dca2

Browse files
authored
Merge pull request #22 from cloudgraphdev/fix/EP-1988-remove-nodejq-implementation
fix: remove node-jq implementation
2 parents fe6f0b1 + c614842 commit f40dca2

File tree

8 files changed

+29
-1069
lines changed

8 files changed

+29
-1069
lines changed

.github/workflows/publish.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,6 @@ jobs:
4141
# See https://github.com/actions/setup-node/blob/main/docs/advanced-usage.md#use-private-packages
4242
run: yarn install --frozen-lockfile --prefer-offline --ignore-scripts
4343

44-
- name: Install jq for tests
45-
run: node -r node-jq/scripts/install-binary.js
46-
4744
- name: Build
4845
run: yarn prepack
4946

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
"inquirer": "^8.1.2",
3030
"jsonpath": "^1.1.1",
3131
"lodash": "^4.17.21",
32-
"node-jq": "^2.3.0",
3332
"ora": "^5.4.1"
3433
},
3534
"devDependencies": {

src/plugins/policyPack/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ export default class PolicyPackPlugin extends Plugin {
316316
])
317317

318318
// Format metadata and link connections
319-
const linkedData = getLinkedData(providerData)
319+
const linkedData = getLinkedData(providerData, this.provider.schemasMap)
320320

321321
const findings = await this.executeRule({
322322
data: linkedData,

src/rules-engine/evaluators/json-evaluator.ts

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import lodash from 'lodash'
2-
import * as jqNode from 'node-jq'
2+
33
import {
44
Condition,
55
JsonRule,
@@ -122,10 +122,9 @@ export default class JsonEvaluator implements RuleEvaluator<JsonRule> {
122122
_data: _ResourceData
123123
): Promise<number | boolean> {
124124
const condition = { ..._condition }
125-
const { path, value, jq: jqQuery } = condition
125+
const { path, value } = condition
126126
delete condition.path
127127
delete condition.value
128-
delete condition.jq
129128
// remaining field should be the op name
130129
const op = Object.keys(condition)[0] //
131130
const operator = this.operators[op]
@@ -158,25 +157,6 @@ export default class JsonEvaluator implements RuleEvaluator<JsonRule> {
158157
firstArg = value
159158
}
160159

161-
if (firstArg && jqQuery) {
162-
firstArg = await this.runJq(firstArg, jqQuery)
163-
data.data = lodash.cloneDeep(data.data)
164-
lodash.set(data.data, data.elementPath, firstArg)
165-
}
166-
167160
return operator(firstArg, otherArgs, data)
168161
}
169-
170-
async runJq(data: unknown, jqQuery: string): Promise<unknown> {
171-
try {
172-
const json = (await jqNode.run(jqQuery, data, {
173-
input: 'json',
174-
output: 'json',
175-
})) as unknown
176-
177-
return json || data
178-
} catch (e) {
179-
return data
180-
}
181-
}
182162
}

src/rules-engine/types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ export type ResourceData = {
99
export type Condition = {
1010
path?: string
1111
value?: string | number | Condition | (string | number)[]
12-
jq?: string
1312
[operationId: string]: any
1413
}
1514

src/utils/data.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { ProviderData } from '../types'
1+
import { ProviderData, SchemaMap } from '../types'
22

3-
const getLinkedData = (providerData: ProviderData): any => {
3+
const getLinkedData = (providerData: ProviderData, schemasMap?: SchemaMap): any => {
44
const linkedData = {}
55
const allEntities = providerData?.entities || []
66
const allConnections = providerData?.connections || {}
@@ -42,11 +42,12 @@ const getLinkedData = (providerData: ProviderData): any => {
4242
}
4343
entity[conn.field].push(targetEntity)
4444
// 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)
45+
// eslint-disable-next-line no-underscore-dangle
46+
const inverseConnField = schemasMap && schemasMap[entity.__typename] || 'account' // @TODO: account doesn't have a name
47+
if (!targetEntity[inverseConnField]) {
48+
targetEntity[inverseConnField] = []
49+
}
50+
targetEntity[inverseConnField].push(entity)
5051
} // else parent relation.. is not used atm
5152
}
5253
}

tests/evaluators/json-evaluator.test.ts

Lines changed: 0 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -369,125 +369,6 @@ describe('JsonEvaluator', () => {
369369
expect(finding.result).toBe(Result.PASS)
370370
})
371371

372-
test('should support jq on array operators', async () => {
373-
const data = {
374-
data: {
375-
a: {
376-
b: [{ color: 'red' }, { color: 'green' }, { color: 'blue' }],
377-
c: [{ fruit: 'apple' }, { fruit: 'orange' }, { fruit: 'banana' }],
378-
},
379-
},
380-
resource: {
381-
id: cuid(),
382-
},
383-
} as any
384-
const rule: any = {
385-
jq: '[.b[] + .c[]]',
386-
path: 'a',
387-
array_any: {
388-
path: '[*]',
389-
and: [
390-
{
391-
path: '[*].color',
392-
equal: 'green',
393-
},
394-
{
395-
path: '[*].fruit',
396-
equal: 'orange',
397-
},
398-
],
399-
},
400-
}
401-
402-
const finding = await evaluator.evaluateSingleResource(
403-
{ conditions: rule } as any,
404-
data
405-
)
406-
407-
expect(finding.result).toBe(Result.PASS)
408-
})
409-
410-
test('should support jq on array with nested operators', async () => {
411-
const data = {
412-
data: {
413-
cloudwatchLog: [
414-
{
415-
metricFilters: [
416-
{
417-
filterName: 'KmsDeletion',
418-
filterPattern:
419-
'{($.eventSource = kms.amazonaws.com) && (($.eventName=DisableKey)||($.eventName=ScheduleKeyDeletion)) }',
420-
metricTransformations: [
421-
{
422-
metricName: 'KmsDeletionCount',
423-
},
424-
],
425-
},
426-
{
427-
filterName: 'ConsoleSignInFailures',
428-
filterPattern:
429-
'{ ($.eventName = ConsoleLogin) && ($.errorMessage = "Failed authentication") }',
430-
metricTransformations: [
431-
{
432-
metricName: 'ConsoleSignInFailureCount',
433-
},
434-
],
435-
},
436-
],
437-
cloudwatch: [
438-
{
439-
metric: 'KmsDeletionCount',
440-
sns: [
441-
{
442-
arn: 'arn:aws:sns:us-east-1:...',
443-
subscriptions: [
444-
{
445-
arn: 'arn:aws:...',
446-
},
447-
],
448-
},
449-
],
450-
},
451-
],
452-
},
453-
],
454-
},
455-
resource: {
456-
id: cuid(),
457-
},
458-
} as any
459-
const rule: any = {
460-
path: 'cloudwatchLog',
461-
jq: '[.[].metricFilters[] + .[].cloudwatch[] | select(.metricTransformations[].metricName == .metric)]',
462-
array_any: {
463-
and: [
464-
{
465-
path: '[*].filterPattern',
466-
equal:
467-
'{($.eventSource = kms.amazonaws.com) && (($.eventName=DisableKey)||($.eventName=ScheduleKeyDeletion)) }',
468-
},
469-
{
470-
path: '[*].sns',
471-
array_any: {
472-
path: '[*].subscriptions',
473-
array_any: {
474-
path: '[*].arn',
475-
match: /^arn:aws:.*$/,
476-
},
477-
},
478-
},
479-
],
480-
},
481-
}
482-
483-
const finding = await evaluator.evaluateSingleResource(
484-
{ conditions: rule } as any,
485-
data
486-
)
487-
488-
expect(finding.result).toBe(Result.PASS)
489-
})
490-
491372
test('Should pass using the same value for the equal operator with the same path', async () => {
492373
const data = {
493374
data: { a: { b: [0, { e: 'same value', d: 'same value' }] } },

0 commit comments

Comments
 (0)