Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add validation to verify control and kit module ids in "new" #264

Merged
merged 2 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/cli/validation.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { validateIsPrefixedId } from "./validation.ts";
import { assertEquals } from "std/testing/assert";

Deno.test("non-prefixed ids fail", () => {
const result = validateIsPrefixedId("abc");
assertEquals(result, false);
});

Deno.test("prefixed ids pass", () => {
const result = validateIsPrefixedId("abc/123");
assertEquals(result, true);
});

Deno.test("prefixed ids with paths pass", () => {
const result = validateIsPrefixedId("abc/123/xyz");
assertEquals(result, true);
});
5 changes: 5 additions & 0 deletions src/cli/validation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const prefixedId = /.+\/.+/;

export function validateIsPrefixedId(id: string) {
return prefixedId.test(id);
}
22 changes: 19 additions & 3 deletions src/commands/compliance/new.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,33 @@ import { MarkdownDocument } from "../../model/MarkdownDocument.ts";
import { GlobalCommandOptions } from "../GlobalCommandOptions.ts";
import { ComplianceControl } from "../../compliance/ComplianceControl.ts";
import { TopLevelCommand } from "../TopLevelCommand.ts";
import { validateIsPrefixedId } from "../../cli/validation.ts";
import { ValidationError } from "x/cliffy/command";
import { CLI } from "../../info.ts";

const idValidationMessage =
"Control ids must contain a prefix for a control framework path like 'framework/control'.";

export function registerNewCmd(program: TopLevelCommand) {
program
.command("new <control> [name]")
.description("generate a new compliance control")
.description(
`Gnerate a new compliance control with the specified id.\n${idValidationMessage}`,
)
.example(
"create a new control",
`${CLI} compliance new iso27001/8.2-privileged-access-rights`,
)
.action(
async (opts: GlobalCommandOptions, module: string, name?: string) => {
async (opts: GlobalCommandOptions, control: string, name?: string) => {
if (!validateIsPrefixedId(control)) {
throw new ValidationError(idValidationMessage);
}

const kit = await CollieRepository.load();
const logger = new Logger(kit, opts);

const controlPath = kit.resolvePath("compliance", module + ".md");
const controlPath = kit.resolvePath("compliance", control + ".md");
if (!name) {
name = await Input.prompt({
message: `Choose a human-friendly name for this control`,
Expand Down
13 changes: 12 additions & 1 deletion src/commands/kit/new.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,24 @@ import { CollieRepository } from "../../model/CollieRepository.ts";
import { GlobalCommandOptions } from "../GlobalCommandOptions.ts";
import { TopLevelCommand } from "../TopLevelCommand.ts";
import { newKitDirectoryCreation } from "./kit-utilities.ts";
import { ValidationError } from "x/cliffy/command";
import { validateIsPrefixedId } from "../../cli/validation.ts";

const idValidationMessage =
"Kit module ids must contain a prefix for a platform like 'platform/module'";

export function registerNewCmd(program: TopLevelCommand) {
program
.command("new <module> [name]")
.description("Generate a new kit module terraform template")
.description(
`Generate a new kit module terraform template.\n${idValidationMessage}`,
)
.action(
async (opts: GlobalCommandOptions, module: string, name?: string) => {
if (!validateIsPrefixedId(module)) {
throw new ValidationError(idValidationMessage);
}

const collie = await CollieRepository.load();
const logger = new Logger(collie, opts);

Expand Down