|
| 1 | +/* |
| 2 | +Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | +SPDX-License-Identifier: Apache-2.0 |
| 4 | +Modifications copyright (c) 2026 NHS Digital – see THIRD_PARTY_NOTICES.md |
| 5 | +*/ |
| 6 | +import {Aspects, Stack} from "aws-cdk-lib" |
| 7 | +import {beforeEach, test} from "vitest" |
| 8 | +import {TestPack, TestType, validateStack} from "./utils" |
| 9 | +import {APIGWStructuredLogging} from "../../src/nag/rules" |
| 10 | +import {describe} from "node:test" |
| 11 | +import {CfnDeployment, CfnStage} from "aws-cdk-lib/aws-apigateway" |
| 12 | +import {CfnApi, CfnHttpApi} from "aws-cdk-lib/aws-sam" |
| 13 | +import {CfnStage as CfnV2Stage} from "aws-cdk-lib/aws-apigatewayv2" |
| 14 | + |
| 15 | +// Copied from https://github.com/cdklabs/cdk-nag/blob/main/test/rules/APIGW.test.ts |
| 16 | +// with minor adjustments to handle CfnDeployment access log settings possibly being undefined |
| 17 | +// only copied relevant tests for structured logging |
| 18 | +// see https://github.com/cdklabs/cdk-nag/issues/2267 |
| 19 | +// and https://github.com/cdklabs/cdk-nag/pull/2268 |
| 20 | + |
| 21 | +const testPack = new TestPack([ |
| 22 | + APIGWStructuredLogging |
| 23 | +]) |
| 24 | +let stack: Stack |
| 25 | +beforeEach(() => { |
| 26 | + stack = new Stack() |
| 27 | + Aspects.of(stack).add(testPack) |
| 28 | +}) |
| 29 | + |
| 30 | +describe("APIGWStructuredLogging: API Gateway stages use JSON-formatted structured logging", () => { |
| 31 | + const ruleId = "APIGWStructuredLogging" |
| 32 | + |
| 33 | + test("Noncompliance 1: Non-JSON format (CfnStage)", () => { |
| 34 | + new CfnStage(stack, "RestApiStageNonJsonFormat", { |
| 35 | + restApiId: "foo", |
| 36 | + stageName: "prod", |
| 37 | + accessLogSetting: { |
| 38 | + destinationArn: |
| 39 | + "arn:aws:logs:us-east-1:123456789012:log-group:API-Gateway-Execution-Logs_abc123/prod", |
| 40 | + format: |
| 41 | + // eslint-disable-next-line max-len |
| 42 | + '$context.identity.sourceIp $context.identity.caller $context.identity.user [$context.requestTime] "$context.httpMethod $context.resourcePath $context.protocol" $context.status $context.responseLength $context.requestId' |
| 43 | + } |
| 44 | + }) |
| 45 | + validateStack(stack, ruleId, TestType.NON_COMPLIANCE) |
| 46 | + }) |
| 47 | + |
| 48 | + test("Noncompliance 2: No access log settings (CfnDeployment)", () => { |
| 49 | + new CfnDeployment(stack, "RestApiDeploymentNoLogs", { |
| 50 | + restApiId: "foo", |
| 51 | + stageDescription: { |
| 52 | + accessLogSetting: { |
| 53 | + destinationArn: |
| 54 | + "arn:aws:logs:us-east-1:123456789012:log-group:API-Gateway-Execution-Logs_abc123/prod" |
| 55 | + } |
| 56 | + } |
| 57 | + }) |
| 58 | + validateStack(stack, ruleId, TestType.NON_COMPLIANCE) |
| 59 | + }) |
| 60 | + |
| 61 | + test("Noncompliance 3: No access log settings (CfnApi)", () => { |
| 62 | + new CfnApi(stack, "SamApiNoLogs", { |
| 63 | + stageName: "MyApi", |
| 64 | + accessLogSetting: { |
| 65 | + destinationArn: |
| 66 | + "arn:aws:logs:us-east-1:123456789012:log-group:API-Gateway-Execution-Logs_abc123/prod" |
| 67 | + } |
| 68 | + }) |
| 69 | + validateStack(stack, ruleId, TestType.NON_COMPLIANCE) |
| 70 | + }) |
| 71 | + |
| 72 | + test("Noncompliance 4: No access log settings (CfnHttpApi)", () => { |
| 73 | + new CfnHttpApi(stack, "SamHttpApiNoLogs", { |
| 74 | + stageName: "MyApi", |
| 75 | + accessLogSetting: { |
| 76 | + destinationArn: |
| 77 | + "arn:aws:logs:us-east-1:123456789012:log-group:API-Gateway-Execution-Logs_abc123/prod" |
| 78 | + } |
| 79 | + }) |
| 80 | + validateStack(stack, ruleId, TestType.NON_COMPLIANCE) |
| 81 | + }) |
| 82 | + |
| 83 | + test("Compliance 1: JSON-formatted log (CfnStage)", () => { |
| 84 | + new CfnStage(stack, "RestApiStageJsonFormat", { |
| 85 | + restApiId: "foo", |
| 86 | + stageName: "prod", |
| 87 | + accessLogSetting: { |
| 88 | + destinationArn: |
| 89 | + "arn:aws:logs:us-east-1:123456789012:log-group:API-Gateway-Execution-Logs_abc123/prod", |
| 90 | + format: |
| 91 | + // eslint-disable-next-line max-len |
| 92 | + '{"requestId":"$context.requestId", "ip": "$context.identity.sourceIp", "caller":"$context.identity.caller", "user":"$context.identity.user","requestTime":"$context.requestTime", "httpMethod":"$context.httpMethod","resourcePath":"$context.resourcePath", "status":"$context.status","protocol":"$context.protocol", "responseLength":"$context.responseLength"}' |
| 93 | + } |
| 94 | + }) |
| 95 | + validateStack(stack, ruleId, TestType.COMPLIANCE) |
| 96 | + }) |
| 97 | + |
| 98 | + test("Compliance 2: HTTP API with JSON-formatted log (CfnStageV2)", () => { |
| 99 | + new CfnV2Stage(stack, "HttpApiStageJsonFormat", { |
| 100 | + apiId: "bar", |
| 101 | + stageName: "prod", |
| 102 | + accessLogSettings: { |
| 103 | + destinationArn: |
| 104 | + "arn:aws:logs:us-east-1:123456789012:log-group:API-Gateway-Execution-Logs_abc123/prod", |
| 105 | + format: |
| 106 | + // eslint-disable-next-line max-len |
| 107 | + '{"requestId":"$context.requestId", "ip": "$context.identity.sourceIp", "requestTime":"$context.requestTime", "httpMethod":"$context.httpMethod","routeKey":"$context.routeKey", "status":"$context.status","protocol":"$context.protocol", "responseLength":"$context.responseLength"}' |
| 108 | + } |
| 109 | + }) |
| 110 | + validateStack(stack, ruleId, TestType.COMPLIANCE) |
| 111 | + }) |
| 112 | + |
| 113 | + test("Compliance 3: JSON-formatted log (CfnDeployment)", () => { |
| 114 | + new CfnDeployment(stack, "RestApiDeploymentJsonFormat", { |
| 115 | + restApiId: "foo", |
| 116 | + stageDescription: { |
| 117 | + accessLogSetting: { |
| 118 | + destinationArn: |
| 119 | + "arn:aws:logs:us-east-1:123456789012:log-group:API-Gateway-Execution-Logs_abc123/prod", |
| 120 | + format: |
| 121 | + // eslint-disable-next-line max-len |
| 122 | + '{"requestId":"$context.requestId", "ip": "$context.identity.sourceIp", "caller":"$context.identity.caller", "user":"$context.identity.user","requestTime":"$context.requestTime", "httpMethod":"$context.httpMethod","resourcePath":"$context.resourcePath", "status":"$context.status","protocol":"$context.protocol", "responseLength":"$context.responseLength"}' |
| 123 | + } |
| 124 | + } |
| 125 | + }) |
| 126 | + validateStack(stack, ruleId, TestType.COMPLIANCE) |
| 127 | + }) |
| 128 | + |
| 129 | + test("Compliance 4: JSON-formatted log (CfnApi)", () => { |
| 130 | + new CfnApi(stack, "SamApiJsonFormat", { |
| 131 | + stageName: "MyApi", |
| 132 | + accessLogSetting: { |
| 133 | + destinationArn: |
| 134 | + "arn:aws:logs:us-east-1:123456789012:log-group:API-Gateway-Execution-Logs_abc123/prod", |
| 135 | + format: |
| 136 | + // eslint-disable-next-line max-len |
| 137 | + '{"requestId":"$context.requestId", "ip": "$context.identity.sourceIp", "caller":"$context.identity.caller", "user":"$context.identity.user","requestTime":"$context.requestTime", "httpMethod":"$context.httpMethod","resourcePath":"$context.resourcePath", "status":"$context.status","protocol":"$context.protocol", "responseLength":"$context.responseLength"}' |
| 138 | + } |
| 139 | + }) |
| 140 | + validateStack(stack, ruleId, TestType.COMPLIANCE) |
| 141 | + }) |
| 142 | + |
| 143 | + test("Compliance 5: JSON-formatted log (CfnHttpApi)", () => { |
| 144 | + new CfnHttpApi(stack, "SamHttpApiJsonFormat", { |
| 145 | + stageName: "MyApi", |
| 146 | + accessLogSetting: { |
| 147 | + destinationArn: |
| 148 | + "arn:aws:logs:us-east-1:123456789012:log-group:API-Gateway-Execution-Logs_abc123/prod", |
| 149 | + format: |
| 150 | + // eslint-disable-next-line max-len |
| 151 | + '{"requestId":"$context.requestId", "ip": "$context.identity.sourceIp", "requestTime":"$context.requestTime", "httpMethod":"$context.httpMethod","routeKey":"$context.routeKey", "status":"$context.status","protocol":"$context.protocol", "responseLength":"$context.responseLength"}' |
| 152 | + } |
| 153 | + }) |
| 154 | + validateStack(stack, ruleId, TestType.COMPLIANCE) |
| 155 | + }) |
| 156 | + |
| 157 | + test("Compliance 6: No stageDescription (CfnDeployment)", () => { |
| 158 | + new CfnDeployment(stack, "RestApiDeploymentNoStageDescription", { |
| 159 | + restApiId: "foo" |
| 160 | + }) |
| 161 | + validateStack(stack, ruleId, TestType.COMPLIANCE) |
| 162 | + }) |
| 163 | +}) |
0 commit comments