forked from Azure/Azure-Sentinel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplaybooksValidator.ts
54 lines (45 loc) · 2.72 KB
/
playbooksValidator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import fs from "fs";
import { runCheckOverChangedFiles } from "./utils/changedFilesValidator";
import { ExitCode } from "./utils/exitCode";
import { isValidSchema } from "./utils/jsonSchemaChecker";
import * as logger from "./utils/logger";
import { ArmTemplate, ArmTemplateResource } from "./utils/playbookCheckers/Models/armTemplateModels";
import { PlaybookTemplateMetadata } from "./utils/playbookCheckers/Models/playbookTemplateMetadata";
import { validateTemplateMetadata } from "./utils/playbookCheckers/playbookArmTemplateMetadataChecker";
import { validateTemplateParameters } from "./utils/playbookCheckers/playbookArmTemplateParametersChecker";
import { getTemplatePlaybookResources } from "./utils/playbookCheckers/playbookARMTemplateUtils";
import { validatePlaybookResource } from "./utils/playbookCheckers/playbookResourceChecker";
export async function IsValidTemplate(filePath: string): Promise<ExitCode> {
let playbookARMTemplate: ArmTemplate<PlaybookTemplateMetadata> = JSON.parse(fs.readFileSync(filePath, "utf8"));
validateARMTemplateSchema(playbookARMTemplate);
// Some ARM template files deploy external resources required by playbooks (e.g custom connector) and not the actual playbook, so they don't require playbook-specific validations
let templatePlaybookResources: ArmTemplateResource[] = getTemplatePlaybookResources(playbookARMTemplate);
if (templatePlaybookResources.length > 0) {
await validateARMTemplateWithPlaybookResource(filePath, playbookARMTemplate);
}
return ExitCode.SUCCESS;
}
function validateARMTemplateSchema(playbookARMTemplate: ArmTemplate<PlaybookTemplateMetadata>): void {
let schema = JSON.parse(fs.readFileSync(".script/utils/schemas/ARM_DeploymentTemplateSchema.json", "utf8"));
isValidSchema(playbookARMTemplate, schema);
}
function validateARMTemplateWithPlaybookResource(filePath: string, playbookARMTemplate: ArmTemplate<PlaybookTemplateMetadata>): void {
validateTemplateParameters(filePath, playbookARMTemplate);
validateTemplateMetadata(filePath, playbookARMTemplate);
validatePlaybookResource(filePath, playbookARMTemplate);
}
let fileTypeSuffixes = ["azuredeploy.json"];
let filePathFolderPrefixes = ["Playbooks","Solutions"];
let fileKinds = ["Modified"];
let CheckOptions = {
onCheckFile: (filePath: string) => {
return IsValidTemplate(filePath);
},
onExecError: async (e: any, filePath: string) => {
console.log(`Playbooks validation failed. File path: ${filePath}. Error message: ${e.message}`);
},
onFinalFailed: async () => {
logger.logError("An error occurred, please open an issue");
},
};
runCheckOverChangedFiles(CheckOptions, fileKinds, fileTypeSuffixes, filePathFolderPrefixes);