@guardian/cdk 63.0.0 → 63.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/experimental/constructs/iam/policies/developer-policy.d.ts +17 -11
- package/lib/experimental/constructs/iam/policies/developer-policy.d.ts.map +1 -1
- package/lib/experimental/constructs/iam/policies/developer-policy.js +36 -15
- package/lib/experimental/constructs/iam/policies/developer-policy.js.map +1 -1
- package/package.json +1 -1
|
@@ -1,29 +1,35 @@
|
|
|
1
1
|
import { type PolicyStatement } from "aws-cdk-lib/aws-iam";
|
|
2
2
|
import { ManagedPolicy } from "aws-cdk-lib/aws-iam";
|
|
3
3
|
import type { GuStack } from "../../../../constructs/core";
|
|
4
|
-
export type
|
|
4
|
+
export type GuDeveloperPolicyExperimentalProps = {
|
|
5
5
|
/**
|
|
6
|
-
*
|
|
7
|
-
* You can also use `addPermission(statement)` to add permissions later.
|
|
6
|
+
* IAM policy statements to include in the developer policy.
|
|
8
7
|
*
|
|
9
|
-
*
|
|
8
|
+
* At least one statement is required.
|
|
10
9
|
*/
|
|
11
|
-
readonly statements
|
|
10
|
+
readonly statements: [PolicyStatement, ...PolicyStatement[]];
|
|
12
11
|
/**
|
|
13
|
-
*
|
|
12
|
+
* Unique identifier of the developer policy grant.
|
|
13
|
+
* This must match the grant ID used in Janus DeveloperPolicyGrant!
|
|
14
14
|
*/
|
|
15
|
-
readonly
|
|
15
|
+
readonly grantId: string;
|
|
16
16
|
/**
|
|
17
|
-
*
|
|
17
|
+
* A brief description to help choose the correct developer policy in Janus or elsewhere.
|
|
18
|
+
*
|
|
19
|
+
* In AWS this will appear as the managed policy description.
|
|
20
|
+
*/
|
|
21
|
+
readonly friendlyName: string;
|
|
22
|
+
/**
|
|
23
|
+
* An optional marker which suppresses the check and warnings on overbroad permissions
|
|
18
24
|
*/
|
|
19
|
-
readonly
|
|
25
|
+
readonly withoutPolicyChecks?: boolean;
|
|
20
26
|
};
|
|
21
27
|
/**
|
|
22
28
|
* Creates a structured `AWS::IAM::ManagedPolicy` resource to manage arbitrary permissions on general account
|
|
23
29
|
* resources which can then be used to create limited permission credentials for use with specific activities.
|
|
24
30
|
*
|
|
25
31
|
* The permission scope is not controlled. This class should be used with care to create minimal permissions.
|
|
26
|
-
* To that end, broad ALLOW permissions can pruned with narrower optional DENY permissions.
|
|
32
|
+
* To that end, broad ALLOW permissions can be pruned with narrower optional DENY permissions.
|
|
27
33
|
*
|
|
28
34
|
* `permission` is prefixed with `/developer-policy/` and postfixed with `/` to construct the path. This will
|
|
29
35
|
* be used for discovery and display. It is restricted to the same character set as AWS `path`.
|
|
@@ -52,6 +58,6 @@ export type GuWorkloadPolicyProps = {
|
|
|
52
58
|
* @experimental
|
|
53
59
|
*/
|
|
54
60
|
export declare class GuDeveloperPolicyExperimental extends ManagedPolicy {
|
|
55
|
-
constructor(scope: GuStack, id: string, props:
|
|
61
|
+
constructor(scope: GuStack, id: string, props: GuDeveloperPolicyExperimentalProps);
|
|
56
62
|
}
|
|
57
63
|
//# sourceMappingURL=developer-policy.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"developer-policy.d.ts","sourceRoot":"","sources":["../../../../../src/experimental/constructs/iam/policies/developer-policy.ts"],"names":[],"mappings":"AAEA,OAAO,
|
|
1
|
+
{"version":3,"file":"developer-policy.d.ts","sourceRoot":"","sources":["../../../../../src/experimental/constructs/iam/policies/developer-policy.ts"],"names":[],"mappings":"AAEA,OAAO,EAA+B,KAAK,eAAe,EAAE,MAAM,qBAAqB,CAAC;AACxF,OAAO,EAAoB,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEtE,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,6BAA6B,CAAC;AAE3D,MAAM,MAAM,kCAAkC,GAAG;IAC/C;;;;OAIG;IACH,QAAQ,CAAC,UAAU,EAAE,CAAC,eAAe,EAAE,GAAG,eAAe,EAAE,CAAC,CAAC;IAC7D;;;OAGG;IACH,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB;;;;OAIG;IACH,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;IAC9B;;OAEG;IACH,QAAQ,CAAC,mBAAmB,CAAC,EAAE,OAAO,CAAC;CACxC,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,qBAAa,6BAA8B,SAAQ,aAAa;gBAClD,KAAK,EAAE,OAAO,EAAE,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,kCAAkC;CAelF"}
|
|
@@ -9,7 +9,7 @@ const aws_iam_2 = require("aws-cdk-lib/aws-iam");
|
|
|
9
9
|
* resources which can then be used to create limited permission credentials for use with specific activities.
|
|
10
10
|
*
|
|
11
11
|
* The permission scope is not controlled. This class should be used with care to create minimal permissions.
|
|
12
|
-
* To that end, broad ALLOW permissions can pruned with narrower optional DENY permissions.
|
|
12
|
+
* To that end, broad ALLOW permissions can be pruned with narrower optional DENY permissions.
|
|
13
13
|
*
|
|
14
14
|
* `permission` is prefixed with `/developer-policy/` and postfixed with `/` to construct the path. This will
|
|
15
15
|
* be used for discovery and display. It is restricted to the same character set as AWS `path`.
|
|
@@ -40,20 +40,36 @@ const aws_iam_2 = require("aws-cdk-lib/aws-iam");
|
|
|
40
40
|
class GuDeveloperPolicyExperimental extends aws_iam_2.ManagedPolicy {
|
|
41
41
|
constructor(scope, id, props) {
|
|
42
42
|
super(scope, id, {
|
|
43
|
-
description: `${props.permission} developer policy`,
|
|
44
43
|
...props,
|
|
45
|
-
path
|
|
44
|
+
// Bear in mind that path has max length 512 chars
|
|
45
|
+
path: `/developer-policy/${scope.repositoryName}/${scope.stack}/${scope.stage}/${props.grantId}/`,
|
|
46
|
+
description: props.friendlyName,
|
|
46
47
|
});
|
|
47
|
-
|
|
48
|
-
if (props.
|
|
49
|
-
|
|
48
|
+
aws_cdk_lib_1.Aspects.of(this).add(new GuDeveloperPolicyExperimentalRequiredChecker());
|
|
49
|
+
if (!props.withoutPolicyChecks) {
|
|
50
|
+
// Later, apply to the stack and check for specific errors
|
|
51
|
+
aws_cdk_lib_1.Aspects.of(this).add(new GuDeveloperPolicyExperimentalOptionalChecker());
|
|
50
52
|
}
|
|
51
|
-
// Later, apply to the stack and check for specific errors
|
|
52
|
-
aws_cdk_lib_1.Aspects.of(this).add(new GuDeveloperPolicyExperimentalChecker());
|
|
53
53
|
}
|
|
54
54
|
}
|
|
55
55
|
exports.GuDeveloperPolicyExperimental = GuDeveloperPolicyExperimental;
|
|
56
|
-
|
|
56
|
+
// Add checks here that we require and don't allow users to opt out of.
|
|
57
|
+
class GuDeveloperPolicyExperimentalRequiredChecker {
|
|
58
|
+
visit(node) {
|
|
59
|
+
if (node instanceof aws_iam_2.CfnManagedPolicy) {
|
|
60
|
+
const description = node.description;
|
|
61
|
+
const maxLength = 60;
|
|
62
|
+
if (!description?.length) {
|
|
63
|
+
aws_cdk_lib_1.Annotations.of(node).addError("friendlyName must be filled in");
|
|
64
|
+
}
|
|
65
|
+
else if (description.length > maxLength) {
|
|
66
|
+
aws_cdk_lib_1.Annotations.of(node).addError(`friendlyName must be no more than ${maxLength} characters long, but was ${description.length} characters`);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
// Add checks here that we recommend for most scenarios but don't require.
|
|
72
|
+
class GuDeveloperPolicyExperimentalOptionalChecker {
|
|
57
73
|
visit(node) {
|
|
58
74
|
if (node instanceof aws_iam_2.CfnManagedPolicy) {
|
|
59
75
|
const policyDocumentJson = node.policyDocument.toJSON();
|
|
@@ -64,7 +80,10 @@ class GuDeveloperPolicyExperimentalChecker {
|
|
|
64
80
|
if (!("Statement" in policyDocumentJson)) {
|
|
65
81
|
return;
|
|
66
82
|
}
|
|
67
|
-
if (!Array.isArray(policyDocumentJson.Statement)
|
|
83
|
+
if (!Array.isArray(policyDocumentJson.Statement)) {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
if (policyDocumentJson.Statement.length === 0) {
|
|
68
87
|
return;
|
|
69
88
|
}
|
|
70
89
|
// For the following conditions, we want to make a strong assertions: you cannot "Allow" with wildcards.
|
|
@@ -84,7 +103,8 @@ class GuDeveloperPolicyExperimentalChecker {
|
|
|
84
103
|
}
|
|
85
104
|
}
|
|
86
105
|
if (!("Resource" in statement)) {
|
|
87
|
-
|
|
106
|
+
// Typescript requires us to check this, because the data model doesn't guarantee it
|
|
107
|
+
aws_cdk_lib_1.Annotations.of(node).addError("Statement is missing a Resource");
|
|
88
108
|
}
|
|
89
109
|
else {
|
|
90
110
|
const resource = statement.Resource;
|
|
@@ -110,13 +130,14 @@ class GuDeveloperPolicyExperimentalChecker {
|
|
|
110
130
|
* arn:aws:dynamodb:us-east-2:account-ID-without-hyphens:table/*
|
|
111
131
|
* arn:aws:s3:::*
|
|
112
132
|
*
|
|
113
|
-
* @param checkString
|
|
114
|
-
* @param
|
|
133
|
+
* @param checkString Action or resource value to validate.
|
|
134
|
+
* @param checkType Label used in validation error messages (for example, Action or Resource).
|
|
135
|
+
* @param node Policy node used to attach validation annotations.
|
|
115
136
|
* @private
|
|
116
137
|
*/
|
|
117
138
|
check(checkString, checkType, node) {
|
|
118
|
-
if (checkString === "*" || checkString.
|
|
119
|
-
aws_cdk_lib_1.Annotations.of(node).addError(`Statement ${checkType} is too broad: ${checkString}`);
|
|
139
|
+
if (checkString === "*" || checkString.includes(":*") || checkString.includes("/*")) {
|
|
140
|
+
aws_cdk_lib_1.Annotations.of(node).addError(`Statement ${checkType} is too broad: ${checkString}. If this is necessary and intended, use withoutPolicyChecks: true in properties to turn off this check`);
|
|
120
141
|
}
|
|
121
142
|
}
|
|
122
143
|
checkAction(checkString, node) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"developer-policy.js","sourceRoot":"","sources":["../../../../../src/experimental/constructs/iam/policies/developer-policy.ts"],"names":[],"mappings":";;;AACA,6CAAmD;AACnD,iDAAmE;AACnE,iDAA2F;AA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E,IAAI,WAAW,KAAK,GAAG,IAAI,WAAW,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,WAAW,CAAC,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE;YACrF,yBAAW,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC,QAAQ,CAAC,aAAa,SAAS,kBAAkB,WAAW,EAAE,CAAC,CAAC;SACtF;IACH,CAAC;IACO,WAAW,CAAC,WAAmB,EAAE,IAAsB;QAC7D,IAAI,CAAC,KAAK,CAAC,WAAW,EAAE,QAAQ,EAAE,IAAI,CAAC,CAAC;IAC1C,CAAC;IACO,aAAa,CAAC,WAAmB,EAAE,IAAsB;QAC/D,IAAI,CAAC,KAAK,CAAC,WAAW,EAAE,UAAU,EAAE,IAAI,CAAC,CAAC;IAC5C,CAAC;CACF","sourcesContent":["import type { IAspect } from \"aws-cdk-lib\";\nimport { Annotations, Aspects } from \"aws-cdk-lib\";\nimport { Effect, type PolicyStatement } from \"aws-cdk-lib/aws-iam\";\nimport { CfnManagedPolicy, ManagedPolicy, type PolicyDocument } from \"aws-cdk-lib/aws-iam\";\nimport type { IConstruct } from \"constructs\";\nimport type { GuStack } from \"../../../../constructs/core\";\n\nexport type GuWorkloadPolicyProps = {\n /**\n * Initial set of permissions to add to this policy document.\n * You can also use `addPermission(statement)` to add permissions later.\n *\n * @default - No statements.\n */\n readonly statements?: PolicyStatement[];\n /**\n * The unique identifier of the policy, which will be displayed when creating credentials.\n */\n readonly permission: string;\n /**\n * An optional description of the policy which will be displayed if present.\n */\n readonly description?: string;\n};\n\n/**\n * Creates a structured `AWS::IAM::ManagedPolicy` resource to manage arbitrary permissions on general account\n * resources which can then be used to create limited permission credentials for use with specific activities.\n *\n * The permission scope is not controlled. This class should be used with care to create minimal permissions.\n * To that end, broad ALLOW permissions can pruned with narrower optional DENY permissions.\n *\n * `permission` is prefixed with `/developer-policy/` and postfixed with `/` to construct the path. This will\n * be used for discovery and display. It is restricted to the same character set as AWS `path`.\n *\n * `description` is optionally used to construct the AWS Managed Policy description and used for display.\n *\n * ```yaml\n * TestingECAE2E87:\n * Type: AWS::IAM::ManagedPolicy\n * Properties:\n * Description: This is testing stuff\n * Path: /developer-policy/read-from-mybucket-under-mypath/\n * PolicyDocument:\n * Statement:\n * - Action: s3:GetObject\n * Effect: Allow\n * Resource: arn:aws:s3:::mybucket/mypath\n * - Action: s3:GetObject\n * Effect: Deny\n * Resource: arn:aws:s3:::mybucket/mypath/butnotthispath\n * Version: \"2012-10-17\"\n * Metadata:\n * aws:cdk:path: janus-resources-for-testing-managed-policy-tagging/justin-testing/Resource* ```\n * ```\n *\n * @experimental\n */\nexport class GuDeveloperPolicyExperimental extends ManagedPolicy {\n constructor(scope: GuStack, id: string, props: GuWorkloadPolicyProps) {\n super(scope, id, {\n description: `${props.permission} developer policy`,\n ...props,\n path: `/developer-policy/${props.permission}/`,\n });\n\n // Don't mind if it's missing, but if it's used it must not be empty\n if (props.statements?.length == 0) {\n throw new Error(\"Empty statements array passed to GuDeveloperPolicyExperimental\");\n }\n\n // Later, apply to the stack and check for specific errors\n Aspects.of(this).add(new GuDeveloperPolicyExperimentalChecker());\n }\n}\n\nclass GuDeveloperPolicyExperimentalChecker implements IAspect {\n public visit(node: IConstruct): void {\n if (node instanceof CfnManagedPolicy) {\n const policyDocumentJson: unknown = (node.policyDocument as PolicyDocument).toJSON();\n\n // These conditions will result in failures from the AWS classes themselves, so we don't need to validate them.\n if (policyDocumentJson === null || typeof policyDocumentJson !== \"object\") {\n return;\n }\n if (!(\"Statement\" in policyDocumentJson)) {\n return;\n }\n if (!Array.isArray(policyDocumentJson.Statement) || policyDocumentJson.Statement.length == 0) {\n return;\n }\n\n // For the following conditions, we want to make a strong assertions: you cannot \"Allow\" with wildcards.\n for (const statement of policyDocumentJson.Statement) {\n if (!(\"Effect\" in statement) || (statement as { Effect: unknown }).Effect === Effect.ALLOW) {\n if (!(\"Action\" in statement)) {\n Annotations.of(node).addError(\"Statement is missing an Action\");\n } else {\n const action = (statement as { Action: unknown }).Action;\n\n if (typeof action === \"string\") {\n this.checkAction(action, node);\n } else if (Array.isArray(action)) {\n const actionArray = action as string[];\n actionArray.forEach((action) => this.checkAction(action, node));\n }\n }\n\n if (!(\"Resource\" in statement)) {\n Annotations.of(node).addError(\"Statement is missing an Resource\");\n } else {\n const resource = (statement as { Resource: unknown }).Resource;\n\n if (typeof resource === \"string\") {\n this.checkResource(resource, node);\n } else if (Array.isArray(resource)) {\n const resourceArray = resource as string[];\n resourceArray.forEach((resource) => {\n this.checkResource(resource, node);\n });\n }\n }\n }\n }\n }\n }\n\n /**\n * Ensure that we don't have either actions or resources of the following forms:\n *\n * *\n * s3:*\n * arn:aws:dynamodb:us-east-2:account-ID-without-hyphens:table/*\n * arn:aws:s3:::*\n *\n * @param checkString resource or action\n * @param node\n * @private\n */\n private check(checkString: string, checkType: string, node: CfnManagedPolicy) {\n if (checkString === \"*\" || checkString.indexOf(\":*\") || checkString.indexOf(\"/*\") > 0) {\n Annotations.of(node).addError(`Statement ${checkType} is too broad: ${checkString}`);\n }\n }\n private checkAction(checkString: string, node: CfnManagedPolicy) {\n this.check(checkString, \"Action\", node);\n }\n private checkResource(checkString: string, node: CfnManagedPolicy) {\n this.check(checkString, \"Resource\", node);\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"developer-policy.js","sourceRoot":"","sources":["../../../../../src/experimental/constructs/iam/policies/developer-policy.ts"],"names":[],"mappings":";;;AACA,6CAAmD;AACnD,iDAAwF;AACxF,iDAAsE;AA4BtE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,MAAa,6BAA8B,SAAQ,uBAAa;IAC9D,YAAY,KAAc,EAAE,EAAU,EAAE,KAAyC;QAC/E,KAAK,CAAC,KAAK,EAAE,EAAE,EAAE;YACf,GAAG,KAAK;YACR,kDAAkD;YAClD,IAAI,EAAE,qBAAqB,KAAK,CAAC,cAAc,IAAI,KAAK,CAAC,KAAK,IAAI,KAAK,CAAC,K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C,CAAC;yBACpC;6BAAM,IAAI,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE;4BAClC,MAAM,aAAa,GAAG,QAAoB,CAAC;4BAC3C,aAAa,CAAC,OAAO,CAAC,CAAC,QAAQ,EAAE,EAAE;gCACjC,IAAI,CAAC,aAAa,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAC;4BACrC,CAAC,CAAC,CAAC;yBACJ;qBACF;iBACF;aACF;SACF;IACH,CAAC;IAED;;;;;;;;;;;;OAYG;IACK,KAAK,CAAC,WAAmB,EAAE,SAAiB,EAAE,IAAsB;QAC1E,IAAI,WAAW,KAAK,GAAG,IAAI,WAAW,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,WAAW,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE;YACnF,yBAAW,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC,QAAQ,CAC3B,aAAa,SAAS,kBAAkB,WAAW,yGAAyG,CAC7J,CAAC;SACH;IACH,CAAC;IACO,WAAW,CAAC,WAAmB,EAAE,IAAsB;QAC7D,IAAI,CAAC,KAAK,CAAC,WAAW,EAAE,QAAQ,EAAE,IAAI,CAAC,CAAC;IAC1C,CAAC;IACO,aAAa,CAAC,WAAmB,EAAE,IAAsB;QAC/D,IAAI,CAAC,KAAK,CAAC,WAAW,EAAE,UAAU,EAAE,IAAI,CAAC,CAAC;IAC5C,CAAC;CACF","sourcesContent":["import type { IAspect } from \"aws-cdk-lib\";\nimport { Annotations, Aspects } from \"aws-cdk-lib\";\nimport { Effect, type PolicyDocument, type PolicyStatement } from \"aws-cdk-lib/aws-iam\";\nimport { CfnManagedPolicy, ManagedPolicy } from \"aws-cdk-lib/aws-iam\";\nimport type { IConstruct } from \"constructs\";\nimport type { GuStack } from \"../../../../constructs/core\";\n\nexport type GuDeveloperPolicyExperimentalProps = {\n /**\n * IAM policy statements to include in the developer policy.\n *\n * At least one statement is required.\n */\n readonly statements: [PolicyStatement, ...PolicyStatement[]];\n /**\n * Unique identifier of the developer policy grant.\n * This must match the grant ID used in Janus DeveloperPolicyGrant!\n */\n readonly grantId: string;\n /**\n * A brief description to help choose the correct developer policy in Janus or elsewhere.\n *\n * In AWS this will appear as the managed policy description.\n */\n readonly friendlyName: string;\n /**\n * An optional marker which suppresses the check and warnings on overbroad permissions\n */\n readonly withoutPolicyChecks?: boolean;\n};\n\n/**\n * Creates a structured `AWS::IAM::ManagedPolicy` resource to manage arbitrary permissions on general account\n * resources which can then be used to create limited permission credentials for use with specific activities.\n *\n * The permission scope is not controlled. This class should be used with care to create minimal permissions.\n * To that end, broad ALLOW permissions can be pruned with narrower optional DENY permissions.\n *\n * `permission` is prefixed with `/developer-policy/` and postfixed with `/` to construct the path. This will\n * be used for discovery and display. It is restricted to the same character set as AWS `path`.\n *\n * `description` is optionally used to construct the AWS Managed Policy description and used for display.\n *\n * ```yaml\n * TestingECAE2E87:\n * Type: AWS::IAM::ManagedPolicy\n * Properties:\n * Description: This is testing stuff\n * Path: /developer-policy/read-from-mybucket-under-mypath/\n * PolicyDocument:\n * Statement:\n * - Action: s3:GetObject\n * Effect: Allow\n * Resource: arn:aws:s3:::mybucket/mypath\n * - Action: s3:GetObject\n * Effect: Deny\n * Resource: arn:aws:s3:::mybucket/mypath/butnotthispath\n * Version: \"2012-10-17\"\n * Metadata:\n * aws:cdk:path: janus-resources-for-testing-managed-policy-tagging/justin-testing/Resource* ```\n * ```\n *\n * @experimental\n */\nexport class GuDeveloperPolicyExperimental extends ManagedPolicy {\n constructor(scope: GuStack, id: string, props: GuDeveloperPolicyExperimentalProps) {\n super(scope, id, {\n ...props,\n // Bear in mind that path has max length 512 chars\n path: `/developer-policy/${scope.repositoryName}/${scope.stack}/${scope.stage}/${props.grantId}/`,\n description: props.friendlyName,\n });\n\n Aspects.of(this).add(new GuDeveloperPolicyExperimentalRequiredChecker());\n\n if (!props.withoutPolicyChecks) {\n // Later, apply to the stack and check for specific errors\n Aspects.of(this).add(new GuDeveloperPolicyExperimentalOptionalChecker());\n }\n }\n}\n\n// Add checks here that we require and don't allow users to opt out of.\nclass GuDeveloperPolicyExperimentalRequiredChecker implements IAspect {\n public visit(node: IConstruct): void {\n if (node instanceof CfnManagedPolicy) {\n const description = node.description;\n const maxLength = 60;\n\n if (!description?.length) {\n Annotations.of(node).addError(\"friendlyName must be filled in\");\n } else if (description.length > maxLength) {\n Annotations.of(node).addError(\n `friendlyName must be no more than ${maxLength} characters long, but was ${description.length} characters`,\n );\n }\n }\n }\n}\n\n// Add checks here that we recommend for most scenarios but don't require.\nclass GuDeveloperPolicyExperimentalOptionalChecker implements IAspect {\n public visit(node: IConstruct): void {\n if (node instanceof CfnManagedPolicy) {\n const policyDocumentJson: unknown = (node.policyDocument as PolicyDocument).toJSON();\n\n // These conditions will result in failures from the AWS classes themselves, so we don't need to validate them.\n if (policyDocumentJson === null || typeof policyDocumentJson !== \"object\") {\n return;\n }\n if (!(\"Statement\" in policyDocumentJson)) {\n return;\n }\n if (!Array.isArray(policyDocumentJson.Statement)) {\n return;\n }\n if (policyDocumentJson.Statement.length === 0) {\n return;\n }\n\n // For the following conditions, we want to make a strong assertions: you cannot \"Allow\" with wildcards.\n for (const statement of policyDocumentJson.Statement) {\n if (!(\"Effect\" in statement) || (statement as { Effect: unknown }).Effect === Effect.ALLOW) {\n if (!(\"Action\" in statement)) {\n Annotations.of(node).addError(\"Statement is missing an Action\");\n } else {\n const action = (statement as { Action: unknown }).Action;\n\n if (typeof action === \"string\") {\n this.checkAction(action, node);\n } else if (Array.isArray(action)) {\n const actionArray = action as string[];\n actionArray.forEach((action) => this.checkAction(action, node));\n }\n }\n\n if (!(\"Resource\" in statement)) {\n // Typescript requires us to check this, because the data model doesn't guarantee it\n Annotations.of(node).addError(\"Statement is missing a Resource\");\n } else {\n const resource = (statement as { Resource: unknown }).Resource;\n\n if (typeof resource === \"string\") {\n this.checkResource(resource, node);\n } else if (Array.isArray(resource)) {\n const resourceArray = resource as string[];\n resourceArray.forEach((resource) => {\n this.checkResource(resource, node);\n });\n }\n }\n }\n }\n }\n }\n\n /**\n * Ensure that we don't have either actions or resources of the following forms:\n *\n * *\n * s3:*\n * arn:aws:dynamodb:us-east-2:account-ID-without-hyphens:table/*\n * arn:aws:s3:::*\n *\n * @param checkString Action or resource value to validate.\n * @param checkType Label used in validation error messages (for example, Action or Resource).\n * @param node Policy node used to attach validation annotations.\n * @private\n */\n private check(checkString: string, checkType: string, node: CfnManagedPolicy) {\n if (checkString === \"*\" || checkString.includes(\":*\") || checkString.includes(\"/*\")) {\n Annotations.of(node).addError(\n `Statement ${checkType} is too broad: ${checkString}. If this is necessary and intended, use withoutPolicyChecks: true in properties to turn off this check`,\n );\n }\n }\n private checkAction(checkString: string, node: CfnManagedPolicy) {\n this.check(checkString, \"Action\", node);\n }\n private checkResource(checkString: string, node: CfnManagedPolicy) {\n this.check(checkString, \"Resource\", node);\n }\n}\n"]}
|