Skip to content

Commit

Permalink
build: Support dynamically injection of threat packs or mitigation pa…
Browse files Browse the repository at this point in the history
…cks (#130)
  • Loading branch information
jessieweiyi authored Aug 21, 2024
1 parent ebd037b commit af6c0ae
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 23 deletions.
10 changes: 6 additions & 4 deletions .projenrc.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import ThreatComposerMonorepoProject from "./projenrc/monorepo";
import ThreatComposerUIComponentsProject from "./projenrc/ui-components";
import ThreatComposerReactAppProject from "./projenrc/app";
import ThreatComposerBrowserExtensionProject from "./projenrc/browser-extension";
import ThreatComposerInfraProject from "./projenrc/infra";
import ThreatComposerBrowserExtensionProject from './projenrc/browser-extension';
import ThreatComposerMonorepoProject from "./projenrc/monorepo";
import ThreatComposerUIComponentsProject from "./projenrc/ui-components";

const monorepo = new ThreatComposerMonorepoProject();
const uiProject = new ThreatComposerUIComponentsProject(monorepo);
const appProject = new ThreatComposerReactAppProject(monorepo, uiProject);
const infraProject = new ThreatComposerInfraProject(monorepo);
const browserExtensionProject = new ThreatComposerBrowserExtensionProject(monorepo);
const browserExtensionProject = new ThreatComposerBrowserExtensionProject(
monorepo
);

monorepo.addImplicitDependency(appProject, uiProject);
monorepo.addImplicitDependency(infraProject, appProject);
Expand Down
19 changes: 0 additions & 19 deletions packages/threat-composer/src/data/mitigationPacks/Sample.json

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
******************************************************************************************************************** */
import GenAIChatbot from './generated/GenAIChatbot.json';
import { MitigationPack } from '../../customTypes';
// {IMPORT_PLACEHOLDER}

const mitigationPacks = [
GenAIChatbot,
// {ENTRY_PLACEHOLDER}
] as MitigationPack[];

export default mitigationPacks;
3 changes: 3 additions & 0 deletions packages/threat-composer/src/data/threatPacks/threatPacks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
******************************************************************************************************************** */
import GenAIChatbot from './generated/GenAIChatbot.json';
import { ThreatPack } from '../../customTypes';
// {IMPORT_PLACEHOLDER}

const threatPacks = [
GenAIChatbot,
// {ENTRY_PLACEHOLDER}
] as ThreatPack[];


export default threatPacks;
117 changes: 117 additions & 0 deletions scripts/packs/injectPacks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import fs from "fs";
import path from "path";

const IMPORT_PLACEHOLDER = "// {IMPORT_PLACEHOLDER}";
const ENTRY_PLACEHOLDER = "// {ENTRY_PLACEHOLDER}";

const DATA_FOLDER = path.join(
__dirname,
"..",
"..",
"packages",
"threat-composer",
"src",
"data"
);

const THREAT_PACKS_FOLDER = path.join(DATA_FOLDER, "threatPacks");

const THREAT_PACKS_FILE = path.join(THREAT_PACKS_FOLDER, "threatPacks.ts");

const MITIGATION_PACKS_FOLDER = path.join(DATA_FOLDER, "mitigationPacks");

const MITIGATION_PACKS_FILE = path.join(
MITIGATION_PACKS_FOLDER,
"mitigationPacks.ts"
);

const readFileContent = (filePath: string) => {
return fs.readFileSync(filePath, { encoding: "utf8", flag: "r" });
};

const writeFileContent = (filePath: string, content: string) => {
return fs.writeFileSync(filePath, content);
};

const injectPackEntry = (packDefinition: string, filePaths: string[]) => {
const fileNames: string[] = [];
const importFiles = filePaths
.map((filePath) => {
const fileName = path
.basename(filePath, path.extname(filePath))
.replace(/[^a-zA-Z0-9]+/gm, "_");
fileNames.push(fileName);
const importFile = `import ${fileName} from "${filePath}";`;
return importFile;
})
.join("\n");

let updatedPackDefiniton = packDefinition
.replace(IMPORT_PLACEHOLDER, importFiles)
.replace(
ENTRY_PLACEHOLDER,
`${fileNames.map((fn) => `${fn},`).join("\n")}`
);

return updatedPackDefiniton;
};

const listFilePaths = (dir: string) => {
const filePaths: string[] = [];

fs.readdirSync(dir).forEach((x) => {
const filePath = path.join(dir, x);
if (fs.lstatSync(filePath).isDirectory()) {
filePaths.push(...listFilePaths(filePath));
}

if (x.endsWith(".json")) {
filePaths.push(filePath);
}
});

return filePaths;
};

const main = () => {
const args = process.argv;

console.log("Arguments", args);

const lenArgs = args.length;

if (lenArgs !== 4) {
console.log(
"Usage: npx ts-node ./scripts/packs/injectPacks.ts <ThreatPack|MitigationPack> <SourceDir-relative path to the relative pack folder>"
);
return -1;
}

const input = {
type: args[lenArgs - 2],
dir: args[lenArgs - 1],
};

const packFilePath =
input.type === "ThreatPack" ? THREAT_PACKS_FILE : MITIGATION_PACKS_FILE;
const packDefinition = readFileContent(packFilePath);

console.log(`Pack file to be update: ${packFilePath}`);

const packFileFolder =
input.type === "ThreatPack" ? THREAT_PACKS_FOLDER : MITIGATION_PACKS_FOLDER;

const importPackFolder = path.join(packFileFolder, input.dir);

console.log(`Imported pack folder ${importPackFolder}`);

const filePaths = listFilePaths(importPackFolder);

const updatedPackDefiniton = injectPackEntry(packDefinition, filePaths);

writeFileContent(packFilePath, updatedPackDefiniton);

return 0;
};

main();

0 comments on commit af6c0ae

Please sign in to comment.