-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add validations and update node version
- Loading branch information
Geovanni Pacheco
committed
Jan 31, 2024
1 parent
2d3d590
commit 546e360
Showing
4 changed files
with
503 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,11 +2,21 @@ | |
"name": "@jupiterone/jupiterone-alert-rules", | ||
"version": "0.23.0", | ||
"description": "Alert rule packages for the JupiterOne platform", | ||
"scripts": { | ||
"validate": "node --loader tsx ./scripts/validate.ts" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/JupiterOne/jupiterone-alert-rules" | ||
}, | ||
"license": "MIT", | ||
"main": "index.js", | ||
"author": "JupiterOne <[email protected]>" | ||
"author": "JupiterOne <[email protected]>", | ||
"devDependencies": { | ||
"@jupiterone/query-language-parser": "^3.16.1", | ||
"@types/node": "^20.11.13", | ||
"euberlog": "^2.5.1", | ||
"tsx": "^4.7.0", | ||
"typescript": "^5.3.3" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { readdirSync, readFileSync } from "node:fs"; | ||
import { resolve } from "node:path"; | ||
import { cwd } from "node:process"; | ||
import { Logger } from "euberlog"; | ||
import ts from "typescript"; | ||
import { parse } from "@jupiterone/query-language-parser"; | ||
|
||
const logger = new Logger(); | ||
|
||
const validateExports = () => { | ||
const files = readdirSync(resolve(cwd(), "rule-packs")); | ||
|
||
const indexPath = resolve(cwd(), "rule-packs", "index.js"); | ||
|
||
const file = ts.preProcessFile(readFileSync(indexPath, "utf-8"), true, true); | ||
|
||
const importedFiles = file.importedFiles.map((f) => | ||
f.fileName.replace("./", "") | ||
); | ||
|
||
for (const file of files) { | ||
if (file === "index.js" || importedFiles.includes(file)) { | ||
continue; | ||
} | ||
throw new Error(`File ${file} is not imported in index.js.`); | ||
} | ||
}; | ||
|
||
const validateJsonContent = () => { | ||
const files = readdirSync(resolve(cwd(), "rule-packs")).filter( | ||
(file) => file !== "index.js" | ||
); | ||
for (const file of files) { | ||
const rulePack = require(resolve(cwd(), "rule-packs", file)); | ||
if (!(rulePack instanceof Array)) { | ||
throw new Error(`File ${file} does not export an array.`); | ||
} | ||
for (const rule of rulePack) { | ||
validateRule(rule); | ||
} | ||
} | ||
}; | ||
|
||
const validateRule = (rule) => { | ||
if ( | ||
!["MEDIUM", "HIGH", "LOW", "INFO", "CRITICAL", undefined].includes( | ||
rule.alertLevel | ||
) | ||
) { | ||
throw new Error( | ||
`Rule ${rule.name} has an invalid alertLevel. ${rule.alertLevel} is not a valid alertLevel.` | ||
); | ||
} | ||
for (const queryObj of rule.queries) { | ||
if (RegExp(/[^A-Za-z0-9_]/g).test(queryObj.name)) { | ||
throw new Error( | ||
`Rule ${rule.name} has an invalid query name. "${queryObj.name}" is not a valid query name.` | ||
); | ||
} | ||
try { | ||
parse(queryObj.query); | ||
} catch (e) { | ||
throw new Error( | ||
`Rule ${rule.name} has an invalid query. ${queryObj.name} is not a valid query. | ||
${e.message} | ||
` | ||
); | ||
} | ||
} | ||
}; | ||
|
||
try { | ||
logger.info("Starting validation..."); | ||
logger.info("Validating exports..."); | ||
validateExports(); | ||
logger.info("Validating json contents..."); | ||
validateJsonContent(); | ||
logger.success("Everything valid"); | ||
} catch (e) { | ||
logger.error(e.message); | ||
process.exit(1); | ||
} |
Oops, something went wrong.