diff --git a/backend/package.json b/backend/package.json index 5f8ce9fc..4f095b68 100644 --- a/backend/package.json +++ b/backend/package.json @@ -15,6 +15,7 @@ "dev-collector": "nodemon -r tsconfig-paths/register src/collector.ts", "start-collector": "TS_NODE_BASEURL=./dist/ node -r tsconfig-paths/register dist/collector.js", "sync-endpoints": "ts-node -r tsconfig-paths/register src/scripts/generate-endpoints.ts", + "generate-alerts": "ts-node -r tsconfig-paths/register src/scripts/generate-alerts.ts", "format": "prettier --write './src/**/*.{ts,tsx}'" }, "keywords": [], @@ -55,7 +56,8 @@ "typeorm": "^0.3.7", "uuid": "^8.3.2", "validator": "^13.7.0", - "whatwg-mimetype": "^3.0.0" + "whatwg-mimetype": "^3.0.0", + "yargs": "^17.5.1" }, "devDependencies": { "@types/express": "^4.17.13", diff --git a/backend/src/scripts/generate-alerts.ts b/backend/src/scripts/generate-alerts.ts new file mode 100644 index 00000000..caeda578 --- /dev/null +++ b/backend/src/scripts/generate-alerts.ts @@ -0,0 +1,37 @@ +import yargs from "yargs" +import { AlertType } from "@common/enums" +import { ALERT_TYPE_TO_RISK_SCORE } from "@common/maps" +import { AppDataSource } from "data-source" +import { Alert } from "models" +import { DatabaseService } from "services/database" + +const generateAlert = async (alertType: string, apiEndpointUuid: string, description: string, context: any) => { + try{ + const newAlert = new Alert() + newAlert.apiEndpointUuid = apiEndpointUuid + newAlert.type = AlertType[alertType] + newAlert.description = description + newAlert.riskScore = ALERT_TYPE_TO_RISK_SCORE[AlertType[alertType]] + newAlert.context = JSON.parse(context || "{}") + await DatabaseService.executeTransactions([[newAlert]], [], false) + } catch (err) { + console.error(`Error generating new alert from script: ${err}`) + } +} + +const main = async () => { + const datasource = await AppDataSource.initialize() + if (!datasource.isInitialized) { + console.error("Couldn't initialize datasource...") + return + } + console.log("AppDataSource Initialized...") + const args = yargs.argv + const alertType = args["alertType"] + const apiEndpointUuid = args["endpointUuid"] + const description = args["description"] + const context = args["context"] + await generateAlert(alertType, apiEndpointUuid, description, context) +} + +main() diff --git a/backend/yarn.lock b/backend/yarn.lock index 43e049d2..1e1f9ecb 100644 --- a/backend/yarn.lock +++ b/backend/yarn.lock @@ -4011,7 +4011,7 @@ yargs@^16.0.0, yargs@^16.2.0: y18n "^5.0.5" yargs-parser "^20.2.2" -yargs@^17.3.1: +yargs@^17.3.1, yargs@^17.5.1: version "17.5.1" resolved "https://registry.yarnpkg.com/yargs/-/yargs-17.5.1.tgz#e109900cab6fcb7fd44b1d8249166feb0b36e58e" integrity sha512-t6YAJcxDkNX7NFYiVtKvWUz8l+PaKTLiL63mJYWR2GnHq2gjEWISzsLp9wg3aY36dY1j+gfIEL3pIF+XlJJfbA==