Skip to content

Commit 9be8ceb

Browse files
committed
move verification to parserSDK
Signed-off-by: Johannes Zahn <johannes.zahn@iteratec.com>
1 parent fe448f7 commit 9be8ceb

7 files changed

Lines changed: 119 additions & 149 deletions

File tree

File renamed without changes.

‎parser-sdk/nodejs/package-lock.json‎

Lines changed: 92 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎parser-sdk/nodejs/package.json‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
"license": "Apache-2.0",
99
"dependencies": {
1010
"@kubernetes/client-node": "^0.14.3",
11+
"ajv": "^8.6.0",
12+
"ajv-draft-04": "^1.0.0",
1113
"axios": "^0.21.1",
1214
"uuid": "^8.3.2",
1315
"ws": "^7.4.6"

‎parser-sdk/nodejs/validate.js‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const fs = require("fs");
2+
const Ajv = require("ajv-draft-04");
3+
const ajv = new Ajv();
4+
const util = require('util')
5+
// eslint-disable-next-line security/detect-non-literal-fs-filename
6+
const readFile = util.promisify(fs.readFile);
7+
8+
// eslint-disable-next-line
9+
async function validate(jsonData) {
10+
const jsonSchemaString = await readFile(
11+
__dirname + "/findings-schema.json","utf8"
12+
);
13+
const jsonSchema = JSON.parse(jsonSchemaString)
14+
const validater = ajv.compile(jsonSchema);
15+
const valid = validater(jsonData);
16+
if(!valid){
17+
throw new Error(JSON.stringify(validater.errors,null,2))
18+
}
19+
}
20+
21+
module.exports.validate = validate;

‎scanners/nmap/parser/parser.test.js‎

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,13 @@
44

55
const fs = require("fs");
66
const util = require("util");
7-
const Ajv = require("ajv-draft-04")
7+
const { validate } = require("../../../parser-sdk/nodejs/validate.js");
88

99

1010
// eslint-disable-next-line security/detect-non-literal-fs-filename
1111
const readFile = util.promisify(fs.readFile);
1212

1313
const { parse } = require("./parser");
14-
const ajv = new Ajv()
15-
16-
17-
test("should comply with findings format", async () => {
18-
const jsonSchemaString = await readFile(
19-
__dirname + "/__testFiles__/findings-schema.json","utf8"
20-
);
21-
const findingsString = await readFile(
22-
__dirname + "/__testFiles__/findings.json","utf8"
23-
);
24-
const jsonSchema = JSON.parse(jsonSchemaString)
25-
const validate = ajv.compile(jsonSchema)
26-
const data = JSON.parse(findingsString)
27-
28-
const valid = validate(data)
29-
if (!valid) {
30-
console.log(validate.errors)
31-
}
32-
expect(valid).toBeTruthy();
33-
});
3414

3515
test("should properly parse nmap xml file", async () => {
3616
const xmlContent = await readFile(
@@ -39,8 +19,9 @@ test("should properly parse nmap xml file", async () => {
3919
encoding: "utf8"
4020
}
4121
);
42-
43-
expect(await parse(xmlContent)).toMatchInlineSnapshot(`
22+
const findings = await parse(xmlContent)
23+
await expect(validate(findings)).resolves.toBeUndefined()
24+
expect(findings).toMatchInlineSnapshot(`
4425
Array [
4526
Object {
4627
"attributes": Object {

0 commit comments

Comments
 (0)