Skip to content

Commit

Permalink
report nag errors
Browse files Browse the repository at this point in the history
  • Loading branch information
sam-goodwin committed Oct 17, 2023
1 parent c73e9fa commit 04579a4
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 46 deletions.
5 changes: 5 additions & 0 deletions apps/tests/aws-runtime-cdk/eventual.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"projectType": "aws-cdk",
"synth": "npx cdk synth --app \"ts-node --esm ./src/app.mts\"",
"deploy": "../aws-runtime/scripts/deploy"
}
4 changes: 3 additions & 1 deletion apps/tests/aws-runtime-cdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
"version": "0.0.0",
"main": "lib/index.js",
"scripts": {
"cdk": "cdk"
"cdk": "cdk",
"nag": "ts-node-esm ./scripts/report-violations.ts"
},
"dependencies": {
"@aws-cdk/aws-apigatewayv2-alpha": "^2.80.0-alpha.0",
"aws-cdk-lib": "2.80.0",
"cdk-nag": "^2.27.164",
"constructs": "10.1.154"
},
"devDependencies": {
Expand Down
45 changes: 45 additions & 0 deletions apps/tests/aws-runtime-cdk/scripts/report-violations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import hipaa from "../cdk.out/HIPAA.Security-eventual-tests-NagReport.json" assert { type: "json" };
import awsSolutions from "../cdk.out/HIPAA.Security-eventual-tests-NagReport.json" assert { type: "json" };

type Report = typeof hipaa | typeof awsSolutions;

report(hipaa);
report(awsSolutions);

function report(report: Report) {
const nonCompliant = report.lines.filter(
(line) => line.compliance === "Non-Compliant"
);
type Violation = (typeof nonCompliant)[number];

const byResource = nonCompliant.reduce<{
[resourceID: string]: Violation[];
}>((resources, line) => {
resources[line.resourceId] = resources[line.resourceId] ?? [];
resources[line.resourceId]!.push(line);
return resources;
}, {});

const resourceIDs = Object.keys(byResource).sort();
for (const resourceID of resourceIDs) {
console.log(
`# ${resourceID
.replace("eventual-tests/testService", "")
.replace("eventual-tests/", "")}`
);
printErrors(byResource[resourceID]!);
}

// printErrors(nonCompliant);

function printErrors(error: Violation[]) {
const uniqueErrors = Array.from(
new Set(error.map((line) => format(line.ruleInfo)))
);
console.log(uniqueErrors.sort().join("\n"));
}

function format(line: string) {
return `- [ ] ${line.replace(/- \(Control.*/g, "")}`;
}
}
26 changes: 25 additions & 1 deletion apps/tests/aws-runtime-cdk/src/app.mts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,17 @@ import {
} from "aws-cdk-lib/aws-iam";
import { NodejsFunction } from "aws-cdk-lib/aws-lambda-nodejs";
import { Queue } from "aws-cdk-lib/aws-sqs";
import { Duration } from "aws-cdk-lib/core";
import { Aspects, Duration } from "aws-cdk-lib/core";
import { createRequire as topLevelCreateRequire } from "module";
import path from "path";
import { ChaosExtension } from "./chaos-extension.js";
import {
AwsSolutionsChecks,
HIPAASecurityChecks,
NagReportFormat,
NagPack,
NagPackProps,
} from "cdk-nag";

import type * as testServiceRuntime from "tests-runtime";

Expand All @@ -33,6 +40,23 @@ if (!assumeRoleArn) {

const app = new App();

// these run linting rules on the CDK code and should all pass to enforce compliance
enableNagPack(AwsSolutionsChecks);
enableNagPack(HIPAASecurityChecks);
function enableNagPack<P extends NagPackProps>(
Pack: new (props: P) => NagPack,
props?: P
) {
Aspects.of(app).add(
new Pack({
reports: true,
reportFormats: [NagReportFormat.CSV, NagReportFormat.JSON],
verbose: true,
...props,
} as P)
);
}

const stack = new Stack(app, "eventual-tests");

const role = new Role(stack, "testRole", {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"test:local": "pnpm -r --filter tests-runtime run test:local-start",
"typecheck": "tsc -b",
"watch": "tsc -b -w",
"nag": "pnpm --filter tests-cdk run nag",
"export": "turbo run export"
},
"devDependencies": {
Expand Down
45 changes: 24 additions & 21 deletions packages/@eventual/aws-cdk/src/command-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,19 +162,13 @@ export class CommandService<Service = any> {
"Commands"
);

this.integrationRole = new Role(commandsSystemScope, "IntegrationRole", {
assumedBy: new ServicePrincipal("apigateway.amazonaws.com"),
});

// Service => Commands
const commandsScope = new Construct(props.serviceScope, "Commands");

const policies = new ManagedPolicies(
commandsSystemScope,
"Policies",
props
);
this.invokeHttpServicePolicy = policies.createManagedPolicy(
"invoke-http-service-policy"
);
this.grantInvokeHttpServiceApiInline(this.invokeHttpServicePolicy);

this.serviceCommands = synthesizeAPI(
commandsScope,
[...this.props.build.commands, this.props.build.commandDefault].map(
Expand Down Expand Up @@ -208,10 +202,26 @@ export class CommandService<Service = any> {
this.configureSystemCommandHandler();
});

this.integrationRole = new Role(commandsSystemScope, "IntegrationRole", {
assumedBy: new ServicePrincipal("apigateway.amazonaws.com"),
this.specification = createSpecification();

// Service => Gateway
this.gateway = new SpecHttpApi(props.serviceScope, "Gateway", {
apiDefinition: ApiDefinition.fromInline(this.specification),
...props.apiOverrides,
});

const policies = new ManagedPolicies(
commandsSystemScope,
"Policies",
props
);
this.invokeHttpServicePolicy = policies.createManagedPolicy(
"invoke-http-service-policy"
);
this.grantInvokeHttpServiceApiInline(
this.invokeHttpServicePolicy.grantPrincipal
);

policies.createManagedPolicy("integration-policy", {
description:
"Allows API Gateway to invoke the service's Lambda Functions",
Expand All @@ -231,14 +241,6 @@ export class CommandService<Service = any> {
],
});

this.specification = createSpecification();

// Service => Gateway
this.gateway = new SpecHttpApi(props.serviceScope, "Gateway", {
apiDefinition: ApiDefinition.fromInline(this.specification),
...props.apiOverrides,
});

this.gateway.node.addDependency(this.integrationRole);

this.finalize();
Expand Down Expand Up @@ -447,9 +449,10 @@ export class CommandService<Service = any> {

@grant()
public grantInvokeHttpServiceApiInline(grantable: IGrantable) {
grantable.grantPrincipal.addToPrincipalPolicy(
const result = grantable.grantPrincipal.addToPrincipalPolicy(
this.executeApiPolicyStatement()
);
return result;
}

private executeApiPolicyStatement() {
Expand Down
44 changes: 24 additions & 20 deletions packages/@eventual/aws-cdk/src/entity-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export class EntityService<Service> {
public readonly transactions: ServiceTransactions<Service>;
public readonly transactionWorker?: Function;

private readonly invokeTransactionsPolicy: ManagedPolicy;
private readonly invokeTransactionsPolicy: ManagedPolicy | undefined;
private readonly readWritePolicy: ManagedPolicy;

constructor(private props: EntityServiceProps<Service>) {
Expand All @@ -107,6 +107,26 @@ export class EntityService<Service> {
"EntityService"
);

const policies = new ManagedPolicies(
entityServiceConstruct,
"Policies",
props
);
if (this.transactionWorker) {
this.invokeTransactionsPolicy = policies.createManagedPolicy(
"invoke-transactions-policy",
{
description:
"Allows the service to invoke the Entity Transaction worker",
}
);
this.grantInvokeTransactionsInline(this.invokeTransactionsPolicy);
}
this.readWritePolicy = policies.createManagedPolicy("read-write-data", {
description: "Allows access to read/write entity data stored in DynamoDB",
});
this.grantReadWriteEntityTablesInline(this.readWritePolicy);

this.entities = Object.fromEntries(
props.build.entities.entities.map((d) => [
d.name,
Expand Down Expand Up @@ -153,24 +173,6 @@ export class EntityService<Service> {
this.transactionWorker
);
}

const policies = new ManagedPolicies(
entityServiceConstruct,
"Policies",
props
);
this.invokeTransactionsPolicy = policies.createManagedPolicy(
"invoke-http-service-policy",
{
description:
"Allows the service to invoke the Entity Transaction worker",
}
);
this.grantInvokeTransactionsInline(this.invokeTransactionsPolicy);
this.readWritePolicy = policies.createManagedPolicy("read-write-data", {
description: "Allows access to read/write entity data stored in DynamoDB",
});
this.grantReadWriteEntityTablesInline(this.readWritePolicy);
}

public configureReadWriteEntityTable(func: Function) {
Expand Down Expand Up @@ -221,7 +223,9 @@ export class EntityService<Service> {
}

public grantInvokeTransactions(grantee: IGrantable) {
attachPolicy(grantee, this.invokeTransactionsPolicy);
if (this.invokeTransactionsPolicy) {
attachPolicy(grantee, this.invokeTransactionsPolicy);
}
}

public grantInvokeTransactionsInline(grantee: IGrantable) {
Expand Down
19 changes: 16 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 04579a4

Please sign in to comment.