You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat(core): template validation after synthesis (#23951)
Integrate policy as code tools into CDK synthesis via a plugin mechanism. Immediately after synthesis, the framework invokes all the registered plugins, collect the results and, if there are any violations, show a report to the user.
Application developers register plugins to a `Stage`:
```ts
const app = new App({
validationPlugins: [
new SomePolicyAgentPlugin(),
new AnotherPolicyAgentPugin(),
]
});
```
Plugin authors must implement the `IPolicyValidationPlugin` interface. Hypothetical example of a CloudFormation Guard plugin:
```ts
export class CfnGuardValidator implements IPolicyValidationPlugin {
public readonly name = 'cfn-guard-validator';
constructor() {}
validate(context: IPolicyValidationContext): PolicyValidationPluginReport {
// execute the cfn-guard cli and get the JSON response from the tool
const cliResultJson = executeCfnGuardCli();
// parse the results and return the violations format
// that the framework expects
const violations = parseGuardResults(cliResultJson);
// construct the report and return it to the framework
// this is a vastly over simplified example that is only
// meant to show the structure of the report that is returned
return {
success: false,
violations: [{
ruleName: violations.ruleName,
recommendation: violations.recommendation,
fix: violations.fix,
violatingResources: [{
resourceName: violations.resourceName,
locations: violations.locations,
templatePath: violations.templatePath,
}],
}],
};
}
}
```
Co-authored-by: corymhall <[email protected]>
For more details see the [Permissions Boundary](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_iam-readme.html#permissions-boundaries) section in the IAM guide.
1304
1304
1305
+
## Policy Validation
1306
+
1307
+
If you or your organization use (or would like to use) any policy validation tool, such as
1308
+
[CloudFormation
1309
+
Guard](https://docs.aws.amazon.com/cfn-guard/latest/ug/what-is-guard.html) or
1310
+
[OPA](https://www.openpolicyagent.org/), to define constraints on your
1311
+
CloudFormation template, you can incorporate them into the CDK application.
1312
+
By using the appropriate plugin, you can make the CDK application check the
1313
+
generated CloudFormation templates against your policies immediately after
1314
+
synthesis. If there are any violations, the synthesis will fail and a report
1315
+
will be printed to the console or to a file (see below).
1316
+
1317
+
> **Note**
1318
+
> This feature is considered experimental, and both the plugin API and the
1319
+
> format of the validation report are subject to change in the future.
1320
+
1321
+
### For application developers
1322
+
1323
+
To use one or more validation plugins in your application, use the
1324
+
`policyValidationBeta1` property of `Stage`:
1325
+
1326
+
```ts
1327
+
// globally for the entire app (an app is a stage)
1328
+
const app = new App({
1329
+
policyValidationBeta1: [
1330
+
// These hypothetical classes implement IValidationPlugin:
1331
+
new ThirdPartyPluginX(),
1332
+
new ThirdPartyPluginY(),
1333
+
],
1334
+
});
1335
+
1336
+
// only apply to a particular stage
1337
+
const prodStage = new Stage(app, 'ProdStage', {
1338
+
policyValidationBeta1: [...],
1339
+
});
1340
+
```
1341
+
1342
+
Immediately after synthesis, all plugins registered this way will be invoked to
1343
+
validate all the templates generated in the scope you defined. In particular, if
1344
+
you register the templates in the `App` object, all templates will be subject to
1345
+
validation.
1346
+
1347
+
> **Warning**
1348
+
> Other than modifying the cloud assembly, plugins can do anything that your CDK
1349
+
> application can. They can read data from the filesystem, access the network
1350
+
> etc. It's your responsibility as the consumer of a plugin to verify that it is
1351
+
> secure to use.
1352
+
1353
+
By default, the report will be printed in a human readable format. If you want a
1354
+
report in JSON format, enable it using the `@aws-cdk/core:validationReportJson`
0 commit comments