@aws/nx-plugin 0.1.5 → 0.2.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.
Files changed (115) hide show
  1. package/LICENSE-THIRD-PARTY +794 -496
  2. package/generators.json +1 -7
  3. package/package.json +7 -7
  4. package/src/cloudscape-website/app/README.md +84 -48
  5. package/src/cloudscape-website/app/__snapshots__/generator.spec.ts.snap +157 -218
  6. package/src/cloudscape-website/app/files/app/README.md.template +44 -0
  7. package/src/cloudscape-website/app/files/app/src/layouts/App/index.tsx.template +40 -43
  8. package/src/cloudscape-website/app/files/app/src/layouts/App/navitems.ts.template +3 -3
  9. package/src/cloudscape-website/app/files/app/src/layouts/Routes/index.tsx.template +4 -6
  10. package/src/cloudscape-website/app/files/app/src/main.tsx.template +7 -10
  11. package/src/cloudscape-website/app/files/app/src/pages/Home/index.tsx.template +0 -2
  12. package/src/cloudscape-website/app/files/common/constructs/src/app/static-websites/__websiteNameKebabCase__.ts.template +13 -0
  13. package/src/cloudscape-website/app/files/common/constructs/src/{__websiteNameKebabCase__ → core}/static-website.ts.template +74 -144
  14. package/src/cloudscape-website/app/generator.js +83 -65
  15. package/src/cloudscape-website/app/generator.js.map +1 -1
  16. package/src/cloudscape-website/app/schema.d.ts +3 -6
  17. package/src/cloudscape-website/app/schema.json +1 -29
  18. package/src/cloudscape-website/cognito-auth/README.md +53 -32
  19. package/src/cloudscape-website/cognito-auth/__snapshots__/generator.spec.ts.snap +161 -125
  20. package/src/cloudscape-website/cognito-auth/files/app/components/CognitoAuth/index.tsx.template +53 -39
  21. package/src/cloudscape-website/cognito-auth/files/common/constructs/src/core/user-identity.ts.template +168 -0
  22. package/src/cloudscape-website/cognito-auth/generator.js +129 -46
  23. package/src/cloudscape-website/cognito-auth/generator.js.map +1 -1
  24. package/src/cloudscape-website/cognito-auth/schema.d.ts +1 -0
  25. package/src/cloudscape-website/cognito-auth/schema.json +7 -1
  26. package/src/cloudscape-website/runtime-config/__snapshots__/generator.spec.ts.snap +15 -17
  27. package/src/cloudscape-website/runtime-config/files/app/components/RuntimeConfig/index.tsx.template +7 -10
  28. package/src/cloudscape-website/runtime-config/files/app/hooks/useRuntimeConfig.tsx.template +13 -0
  29. package/src/cloudscape-website/runtime-config/generator.js +3 -1
  30. package/src/cloudscape-website/runtime-config/generator.js.map +1 -1
  31. package/src/infra/app/README.md +71 -46
  32. package/src/infra/app/__snapshots__/generator.spec.ts.snap +120 -187
  33. package/src/infra/app/files/app/README.md.template +76 -0
  34. package/src/infra/app/files/app/src/main.ts.template +18 -0
  35. package/src/infra/app/files/common/constructs/src/core/cfn-guard-rules/aws-prototyping.guard +1282 -0
  36. package/src/infra/app/files/common/constructs/src/core/cfn-guard-rules/cfn-nag.guard +6839 -0
  37. package/src/infra/app/files/common/constructs/src/core/cfn-guard-rules/hipaa-security.guard +2807 -0
  38. package/src/infra/app/files/common/constructs/src/core/cfn-guard-rules/nist-csf.guard +2585 -0
  39. package/src/infra/app/files/common/constructs/src/core/cfn-guard-rules/pci-dss-3-2-1.guard +2236 -0
  40. package/src/infra/app/files/common/constructs/src/core/cfn-guard-rules/wa-reliability-pillar.guard +885 -0
  41. package/src/infra/app/files/common/constructs/src/core/cfn-guard-rules/wa-security-pillar.guard +2205 -0
  42. package/src/infra/app/files/common/constructs/src/core/cfn-guard.ts.template +63 -0
  43. package/src/infra/app/generator.js +17 -3
  44. package/src/infra/app/generator.js.map +1 -1
  45. package/src/infra/app/schema.d.ts +10 -1
  46. package/src/infra/app/schema.json +16 -8
  47. package/src/trpc/backend/README.md +102 -80
  48. package/src/trpc/backend/__snapshots__/generator.spec.ts.snap +37 -17
  49. package/src/trpc/backend/files/backend/README.md.template +33 -0
  50. package/src/trpc/backend/files/common/constructs/src/app/trpc-apis/__apiNameKebabCase__.ts.template +18 -0
  51. package/src/trpc/backend/files/common/constructs/src/{__apiNameKebabCase__/index.ts.template → core/trpc-api.ts.template} +12 -16
  52. package/src/trpc/backend/files/schema/README.md.template +33 -0
  53. package/src/trpc/backend/generator.js +29 -43
  54. package/src/trpc/backend/generator.js.map +1 -1
  55. package/src/trpc/backend/schema.d.ts +3 -1
  56. package/src/trpc/backend/schema.json +8 -13
  57. package/src/trpc/react/README.md +46 -66
  58. package/src/trpc/react/__snapshots__/generator.spec.ts.snap +104 -65
  59. package/src/trpc/react/files/src/components/TrpcClients/IsolatedTrpcProvider.tsx.template +75 -0
  60. package/src/trpc/react/files/src/components/TrpcClients/TrpcApis.tsx.template +1 -0
  61. package/src/trpc/react/files/src/components/TrpcClients/TrpcClientProviders.tsx.template +10 -0
  62. package/src/trpc/react/files/src/components/TrpcClients/index.tsx.template +5 -0
  63. package/src/trpc/react/files/src/hooks/useSigV4.tsx.template +38 -0
  64. package/src/trpc/react/files/src/hooks/use__apiNameClassName__.tsx.template +3 -0
  65. package/src/trpc/react/generator.js +123 -24
  66. package/src/trpc/react/generator.js.map +1 -1
  67. package/src/trpc/react/schema.json +2 -2
  68. package/src/ts/cjs-to-esm/generator.js.map +1 -1
  69. package/src/ts/lib/eslint.d.ts +1 -1
  70. package/src/ts/lib/eslint.js +59 -11
  71. package/src/ts/lib/eslint.js.map +1 -1
  72. package/src/ts/lib/files/README.md.template +33 -0
  73. package/src/ts/lib/generator.js +11 -4
  74. package/src/ts/lib/generator.js.map +1 -1
  75. package/src/ts/lib/schema.d.ts +1 -3
  76. package/src/ts/lib/schema.json +2 -15
  77. package/src/ts/lib/ts-project-utils.js.map +1 -1
  78. package/src/ts/lib/vitest.js +14 -0
  79. package/src/ts/lib/vitest.js.map +1 -1
  80. package/src/utils/ast.d.ts +13 -0
  81. package/src/utils/ast.js +102 -0
  82. package/src/utils/ast.js.map +1 -0
  83. package/src/utils/files/common/constructs/src/app/index.ts.template +0 -0
  84. package/src/utils/files/common/constructs/src/{runtime-config → core}/runtime-config.ts.template +3 -5
  85. package/src/utils/files/common/constructs/src/index.ts.template +2 -1
  86. package/src/utils/files/common/readme/README.md.template +33 -0
  87. package/src/utils/files/common/types/src/runtime-config.ts.template +1 -13
  88. package/src/utils/format.js.map +1 -1
  89. package/src/utils/names.d.ts +2 -0
  90. package/src/utils/names.js +27 -0
  91. package/src/utils/names.js.map +1 -0
  92. package/src/utils/npm-scope.js.map +1 -1
  93. package/src/utils/paths.js.map +1 -1
  94. package/src/utils/shared-constructs.js +37 -4
  95. package/src/utils/shared-constructs.js.map +1 -1
  96. package/src/utils/versions.d.ts +15 -9
  97. package/src/utils/versions.js +14 -8
  98. package/src/utils/versions.js.map +1 -1
  99. package/src/cloudscape-website/app/files/common/constructs/src/__websiteNameKebabCase__/cloudfront-web-acl.ts.template +0 -317
  100. package/src/cloudscape-website/app/files/common/constructs/src/__websiteNameKebabCase__/index.ts.template +0 -4
  101. package/src/cloudscape-website/app/files/common/constructs/src/__websiteNameKebabCase__/webacl_event_handler/index.ts.template +0 -301
  102. package/src/cloudscape-website/cognito-auth/files/common/constructs/src/identity/index.ts.template +0 -4
  103. package/src/cloudscape-website/cognito-auth/files/common/constructs/src/identity/user-identity.ts.template +0 -66
  104. package/src/cloudscape-website/cognito-auth/files/common/constructs/src/identity/userpool-with-mfa.ts.template +0 -70
  105. package/src/gitlab/generator.d.ts +0 -8
  106. package/src/gitlab/generator.js +0 -16
  107. package/src/gitlab/generator.js.map +0 -1
  108. package/src/gitlab/schema.d.ts +0 -9
  109. package/src/gitlab/schema.json +0 -52
  110. package/src/infra/app/files/src/main.ts.template +0 -37
  111. package/src/trpc/react/files/src/components/TRPCClientProvider/index.tsx.template +0 -34
  112. package/src/trpc/react/files/src/hooks/useTrpc.tsx.template +0 -5
  113. /package/src/infra/app/files/{cdk.json → app/cdk.json} +0 -0
  114. /package/src/infra/app/files/{src → app/src}/stacks/application-stack.ts.template +0 -0
  115. /package/src/utils/files/common/constructs/src/{runtime-config → core}/index.ts.template +0 -0
@@ -1,317 +0,0 @@
1
- /*! Copyright [Amazon.com](http://amazon.com/), Inc. or its affiliates. All Rights Reserved.
2
- SPDX-License-Identifier: Apache-2.0 */
3
- import * as url from "url";
4
- import { PDKNag } from "@aws/pdk/pdk-nag";
5
- import { CustomResource, Duration, Stack } from "aws-cdk-lib";
6
- import {
7
- Effect,
8
- PolicyDocument,
9
- PolicyStatement,
10
- Role,
11
- ServicePrincipal,
12
- } from "aws-cdk-lib/aws-iam";
13
- import { Runtime } from "aws-cdk-lib/aws-lambda";
14
- import { Provider } from "aws-cdk-lib/custom-resources";
15
- import { NagSuppressions } from "cdk-nag";
16
- import { Construct } from "constructs";
17
- import { NodejsFunction } from "aws-cdk-lib/aws-lambda-nodejs";
18
-
19
- /**
20
- * Represents a WAF V2 managed rule.
21
- */
22
- export interface ManagedRule {
23
- /**
24
- * The name of the managed rule group vendor. You use this, along with the rule group name, to identify the rule group.
25
- */
26
- readonly vendor: string;
27
-
28
- /**
29
- * The name of the managed rule group. You use this, along with the vendor name, to identify the rule group.
30
- */
31
- readonly name: string;
32
- }
33
-
34
- /**
35
- * Type of Cidr.
36
- */
37
- export type CidrType = "IPV4" | "IPV6";
38
-
39
- /**
40
- * Representation of a CIDR range.
41
- */
42
- export interface CidrAllowList {
43
- /**
44
- * Type of CIDR range.
45
- */
46
- readonly cidrType: CidrType;
47
-
48
- /**
49
- * Specify an IPv4 address by using CIDR notation. For example:
50
- * To configure AWS WAF to allow, block, or count requests that originated from the IP address 192.0.2.44, specify 192.0.2.44/32 .
51
- * To configure AWS WAF to allow, block, or count requests that originated from IP addresses from 192.0.2.0 to 192.0.2.255, specify 192.0.2.0/24 .
52
- *
53
- * For more information about CIDR notation, see the Wikipedia entry Classless Inter-Domain Routing .
54
- *
55
- * Specify an IPv6 address by using CIDR notation. For example:
56
- * To configure AWS WAF to allow, block, or count requests that originated from the IP address 1111:0000:0000:0000:0000:0000:0000:0111, specify 1111:0000:0000:0000:0000:0000:0000:0111/128 .
57
- * To configure AWS WAF to allow, block, or count requests that originated from IP addresses 1111:0000:0000:0000:0000:0000:0000:0000 to 1111:0000:0000:0000:ffff:ffff:ffff:ffff, specify 1111:0000:0000:0000:0000:0000:0000:0000/64 .
58
- */
59
- readonly cidrRanges: string[];
60
- }
61
-
62
- /**
63
- * Properties to configure the web acl.
64
- */
65
- export interface CloudFrontWebAclProps {
66
- /**
67
- * List of managed rules to apply to the web acl.
68
- *
69
- * @default - [{ vendor: "AWS", name: "AWSManagedRulesCommonRuleSet" }]
70
- */
71
- readonly managedRules?: ManagedRule[];
72
-
73
- /**
74
- * List of cidr ranges to allow.
75
- *
76
- * @default - undefined
77
- */
78
- readonly cidrAllowList?: CidrAllowList;
79
-
80
- /**
81
- * Set to true to prevent creation of a web acl for the static website
82
- * @default false
83
- */
84
- readonly disable?: boolean;
85
- }
86
-
87
- /**
88
- * This construct creates a WAFv2 Web ACL for cloudfront in the us-east-1 region (required for cloudfront) no matter the
89
- * region of the parent cdk stack.
90
- */
91
- export class CloudfrontWebAcl extends Construct {
92
- public readonly webAclId: string;
93
- public readonly webAclArn: string;
94
-
95
- constructor(scope: Construct, id: string, props?: CloudFrontWebAclProps) {
96
- super(scope, id);
97
-
98
- const stack = Stack.of(this);
99
- const aclName = `${stack.stackName}-${id}-${this.node.addr.slice(-4)}`;
100
- const onEventHandler = this.createOnEventHandler(stack, aclName);
101
- const customResource = this.createAclCustomResource(
102
- stack,
103
- aclName,
104
- onEventHandler,
105
- props
106
- );
107
-
108
- this.webAclId = customResource.getAttString("WebAclId");
109
- this.webAclArn = customResource.getAttString("WebAclArn");
110
- }
111
-
112
- /**
113
- * Creates an event handler for managing an ACL in us-east-1.
114
- *
115
- * @param stack containing Stack instance.
116
- * @param aclName name of the ACL to manage.
117
- * @private
118
- */
119
- private createOnEventHandler(stack: Stack, aclName: string): NodejsFunction {
120
- // NB without manually defining a name, the cdk generated name for the Provider function can become too long and
121
- // deployments fail. This is because the Provider's name references the onEvent handler name and appends "-Provider"
122
- // rather than being generated by cdk and truncated appropriately
123
- const onEventHandlerName = `${PDKNag.getStackPrefix(stack)
124
- .split("/")
125
- .join("-")}AclEvent-${this.node.addr.slice(-6)}`;
126
- const onEventHandlerRole = new Role(this, "OnEventHandlerRole", {
127
- assumedBy: new ServicePrincipal("lambda.amazonaws.com"),
128
- inlinePolicies: {
129
- logs: new PolicyDocument({
130
- statements: [
131
- new PolicyStatement({
132
- effect: Effect.ALLOW,
133
- actions: [
134
- "logs:CreateLogGroup",
135
- "logs:CreateLogStream",
136
- "logs:PutLogEvents",
137
- ],
138
- resources: [
139
- `arn:aws:logs:${stack.region}:${stack.account}:log-group:/aws/lambda/${onEventHandlerName}`,
140
- `arn:aws:logs:${stack.region}:${stack.account}:log-group:/aws/lambda/${onEventHandlerName}:*`,
141
- ],
142
- }),
143
- ],
144
- }),
145
- wafv2: new PolicyDocument({
146
- statements: [
147
- new PolicyStatement({
148
- effect: Effect.ALLOW,
149
- actions: [
150
- "wafv2:CreateWebACL",
151
- "wafv2:DeleteWebACL",
152
- "wafv2:UpdateWebACL",
153
- "wafv2:GetWebACL",
154
- ],
155
- resources: [
156
- `arn:aws:wafv2:us-east-1:${stack.account}:global/ipset/${aclName}-IPSet/*`,
157
- `arn:aws:wafv2:us-east-1:${stack.account}:global/webacl/${aclName}/*`,
158
- `arn:aws:wafv2:us-east-1:${stack.account}:global/managedruleset/*/*`,
159
- ],
160
- }),
161
- new PolicyStatement({
162
- effect: Effect.ALLOW,
163
- actions: [
164
- "wafv2:CreateIPSet",
165
- "wafv2:DeleteIPSet",
166
- "wafv2:UpdateIPSet",
167
- "wafv2:GetIPSet",
168
- ],
169
- resources: [
170
- `arn:aws:wafv2:us-east-1:${stack.account}:global/ipset/${aclName}-IPSet/*`,
171
- ],
172
- }),
173
- ],
174
- }),
175
- },
176
- });
177
-
178
- const onEventHandler = new NodejsFunction(
179
- this,
180
- "CloudfrontWebAclOnEventHandler",
181
- {
182
- entry: url.fileURLToPath(new URL('./webacl_event_handler/index.ts', import.meta.url)),
183
- role: onEventHandlerRole,
184
- functionName: onEventHandlerName,
185
- handler: "onEvent",
186
- runtime: Runtime.NODEJS_18_X,
187
- timeout: Duration.seconds(300),
188
- }
189
- );
190
-
191
- ["AwsSolutions-IAM5", "AwsPrototyping-IAMNoWildcardPermissions"].forEach(
192
- (RuleId) => {
193
- NagSuppressions.addResourceSuppressions(
194
- onEventHandlerRole,
195
- [
196
- {
197
- id: RuleId,
198
- reason:
199
- "WafV2 resources have been scoped down to the ACL/IPSet level, however * is still needed as resource id's are created just in time.",
200
- appliesTo: [
201
- {
202
- regex: `/^Resource::arn:aws:wafv2:us-east-1:${PDKNag.getStackAccountRegex(
203
- stack
204
- )}:global/(.*)$/g`,
205
- },
206
- ],
207
- },
208
- {
209
- id: RuleId,
210
- reason:
211
- "Cloudwatch resources have been scoped down to the LogGroup level, however * is still needed as stream names are created just in time.",
212
- appliesTo: [
213
- {
214
- regex: `/^Resource::arn:aws:logs:${PDKNag.getStackRegionRegex(
215
- stack
216
- )}:${PDKNag.getStackAccountRegex(
217
- stack
218
- )}:log-group:/aws/lambda/${onEventHandlerName}:*/g`,
219
- },
220
- ],
221
- },
222
- ],
223
- true
224
- );
225
- }
226
- );
227
-
228
- return onEventHandler;
229
- }
230
-
231
- /**
232
- * Creates a Custom resource to manage the deployment of the ACL.
233
- *
234
- * @param stack containing Stack instance.
235
- * @param aclName name of the ACL to manage.
236
- * @param onEventHandler event handler to use for deployment.
237
- * @param props user provided properties for configuring the ACL.
238
- * @private
239
- */
240
- private createAclCustomResource(
241
- stack: Stack,
242
- aclName: string,
243
- onEventHandler: NodejsFunction,
244
- props?: CloudFrontWebAclProps
245
- ): CustomResource {
246
- const providerFunctionName = `${onEventHandler.functionName}-Provider`;
247
- const providerRole = new Role(this, "CloudfrontWebAclProviderRole", {
248
- assumedBy: new ServicePrincipal("lambda.amazonaws.com"),
249
- inlinePolicies: {
250
- logs: new PolicyDocument({
251
- statements: [
252
- new PolicyStatement({
253
- effect: Effect.ALLOW,
254
- actions: [
255
- "logs:CreateLogGroup",
256
- "logs:CreateLogStream",
257
- "logs:PutLogEvents",
258
- ],
259
- resources: [
260
- `arn:aws:logs:${stack.region}:${stack.account}:log-group:/aws/lambda/${providerFunctionName}`,
261
- `arn:aws:logs:${stack.region}:${stack.account}:log-group:/aws/lambda/${providerFunctionName}:*`,
262
- ],
263
- }),
264
- ],
265
- }),
266
- },
267
- });
268
- const provider = new Provider(this, "CloudfrontAclProvider", {
269
- onEventHandler,
270
- role: providerRole,
271
- providerFunctionName,
272
- });
273
-
274
- ["AwsSolutions-IAM5", "AwsPrototyping-IAMNoWildcardPermissions"].forEach(
275
- (RuleId) => {
276
- NagSuppressions.addResourceSuppressions(
277
- providerRole,
278
- [
279
- {
280
- id: RuleId,
281
- reason:
282
- "Cloudwatch resources have been scoped down to the LogGroup level, however * is still needed as stream names are created just in time.",
283
- },
284
- ],
285
- true
286
- );
287
- }
288
- );
289
-
290
- ["AwsSolutions-L1", "AwsPrototyping-LambdaLatestVersion"].forEach(
291
- (RuleId) => {
292
- NagSuppressions.addResourceSuppressions(
293
- provider,
294
- [
295
- {
296
- id: RuleId,
297
- reason:
298
- "Latest runtime cannot be configured. CDK will need to upgrade the Provider construct accordingly.",
299
- },
300
- ],
301
- true
302
- );
303
- }
304
- );
305
-
306
- return new CustomResource(this, "CFAclCustomResource", {
307
- serviceToken: provider.serviceToken,
308
- properties: {
309
- ID: aclName,
310
- MANAGED_RULES: props?.managedRules ?? [
311
- { vendor: "AWS", name: "AWSManagedRulesCommonRuleSet" },
312
- ],
313
- CIDR_ALLOW_LIST: props?.cidrAllowList,
314
- },
315
- });
316
- }
317
- }
@@ -1,4 +0,0 @@
1
- /*! Copyright [Amazon.com](http://amazon.com/), Inc. or its affiliates. All Rights Reserved.
2
- SPDX-License-Identifier: Apache-2.0 */
3
- export * from './cloudfront-web-acl.js';
4
- export * from './static-website.js';
@@ -1,301 +0,0 @@
1
- /* eslint-disable @typescript-eslint/no-non-null-asserted-optional-chain */
2
- /* eslint-disable @typescript-eslint/no-non-null-assertion */
3
- /*! Copyright [Amazon.com](http://amazon.com/), Inc. or its affiliates. All Rights Reserved.
4
- SPDX-License-Identifier: Apache-2.0 */
5
- import { CreateIPSetCommandOutput, Rule, WAFUnavailableEntityException, WAFV2 } from "@aws-sdk/client-wafv2"; // eslint-disable-line
6
-
7
- const DELIMITER = ":";
8
- const SCOPE = "CLOUDFRONT";
9
- const client = new WAFV2({
10
- region: "us-east-1",
11
- customUserAgent: "aws-pdk/static-website/waf",
12
- });
13
-
14
- const MAX_CREATE_RETRY = 10;
15
- const RETRY_INTERVAL = 2000;
16
-
17
- /**
18
- * Handler for creating a WAF V2 ACL in US-EAST-1.
19
- */
20
- export const onEvent = async (event: any) => {
21
- const { ID, MANAGED_RULES, CIDR_ALLOW_LIST } = event.ResourceProperties;
22
- const [WEB_ACL_ID, IP_SET_ID] = event.PhysicalResourceId
23
- ? event.PhysicalResourceId.split(DELIMITER)
24
- : [];
25
- let response = {};
26
-
27
- switch (event.RequestType) {
28
- case "Create":
29
- response = await createWaf(ID, MANAGED_RULES, CIDR_ALLOW_LIST);
30
- break;
31
- case "Update":
32
- response = await updateWaf(
33
- WEB_ACL_ID,
34
- IP_SET_ID,
35
- ID,
36
- getIpSetName(ID),
37
- MANAGED_RULES,
38
- CIDR_ALLOW_LIST
39
- );
40
- break;
41
- case "Delete":
42
- response = await deleteWaf(WEB_ACL_ID, IP_SET_ID, ID, getIpSetName(ID));
43
- break;
44
- default:
45
- throw new Error(`Invalid RequestType: ${event.RequestType}`);
46
- }
47
-
48
- return response;
49
- };
50
-
51
- /**
52
- * Generates the name of the IP Set.
53
- *
54
- * @param id param passed in.
55
- * @returns name of IP Set.
56
- */
57
- const getIpSetName = (id: string) => `${id}-IPSet`;
58
-
59
- /**
60
- * Returns a set of rules to apply.
61
- *
62
- * @param ipSetArn ip set arn
63
- * @param ipSetName ip set name
64
- * @param managedRules managed rules
65
- * @param cidrAllowList cidr allow list
66
- * @returns set of rules to apply.
67
- */
68
- const getWafRules = (
69
- ipSetArn: string,
70
- ipSetName: string,
71
- managedRules?: any,
72
- cidrAllowList?: any
73
- ): Array<Rule> => {
74
- const rules: Array<Rule> = [];
75
-
76
- if (cidrAllowList) {
77
- rules.push({
78
- Name: ipSetName,
79
- Priority: 1,
80
- VisibilityConfig: {
81
- MetricName: ipSetName,
82
- CloudWatchMetricsEnabled: true,
83
- SampledRequestsEnabled: true,
84
- },
85
- Action: {
86
- Block: {},
87
- },
88
- Statement: {
89
- NotStatement: {
90
- Statement: {
91
- IPSetReferenceStatement: {
92
- ARN: ipSetArn,
93
- },
94
- },
95
- },
96
- },
97
- });
98
- }
99
-
100
- if (managedRules) {
101
- rules.push(
102
- ...managedRules
103
- .map((r: any) => ({ VendorName: r.vendor, Name: r.name }))
104
- .map((rule: any, Priority: any) => ({
105
- Name: `${rule.VendorName}-${rule.Name}`,
106
- Priority,
107
- Statement: { ManagedRuleGroupStatement: rule },
108
- OverrideAction: { None: {} },
109
- VisibilityConfig: {
110
- MetricName: `${rule.VendorName}-${rule.Name}`,
111
- CloudWatchMetricsEnabled: true,
112
- SampledRequestsEnabled: true,
113
- },
114
- }))
115
- );
116
- }
117
-
118
- return rules;
119
- };
120
-
121
- const createWaf = async (
122
- id: string,
123
- managedRules?: any,
124
- cidrAllowList?: any
125
- ) => {
126
- const ipSetName = getIpSetName(id);
127
- const createIpSetResponse = await client.createIPSet({
128
- Name: ipSetName,
129
- Scope: SCOPE,
130
- Addresses: cidrAllowList?.cidrRanges ?? [],
131
- IPAddressVersion: cidrAllowList?.cidrType ?? "IPV4",
132
- });
133
-
134
- const createWebAclResponse = await createWafAcl(
135
- id,
136
- ipSetName,
137
- createIpSetResponse,
138
- managedRules,
139
- cidrAllowList
140
- );
141
-
142
- return {
143
- PhysicalResourceId: `${createWebAclResponse.Summary?.Id}${DELIMITER}${createIpSetResponse.Summary?.Id}`,
144
- Data: {
145
- WebAclArn: createWebAclResponse.Summary?.ARN,
146
- WebAclId: createWebAclResponse.Summary?.Id,
147
- IPSetArn: createIpSetResponse.Summary?.ARN,
148
- IPSetId: createIpSetResponse.Summary?.Id,
149
- },
150
- };
151
- };
152
-
153
- const createWafAcl = async (
154
- id: string,
155
- ipSetName: string,
156
- createIpSetResponse: CreateIPSetCommandOutput,
157
- managedRules?: any,
158
- cidrAllowList?: any
159
- ) => {
160
- let counter = 0;
161
-
162
- while (true) {
163
- try {
164
- const createWebAclResponse = await client.createWebACL({
165
- Name: id,
166
- DefaultAction: { Allow: {} },
167
- Scope: SCOPE,
168
- VisibilityConfig: {
169
- CloudWatchMetricsEnabled: true,
170
- MetricName: id,
171
- SampledRequestsEnabled: true,
172
- },
173
- Rules: getWafRules(
174
- createIpSetResponse.Summary!.ARN!,
175
- ipSetName,
176
- managedRules,
177
- cidrAllowList
178
- ),
179
- });
180
-
181
- return createWebAclResponse;
182
- } catch (e) {
183
- if (
184
- e instanceof WAFUnavailableEntityException &&
185
- counter < MAX_CREATE_RETRY
186
- ) {
187
- counter++;
188
- console.log(
189
- `Received error: ${e.message}; Waiting for retrying ${counter}`
190
- );
191
- await sleep(RETRY_INTERVAL);
192
- continue;
193
- }
194
-
195
- throw e;
196
- }
197
- }
198
- };
199
-
200
- const updateWaf = async (
201
- webAclId: string,
202
- ipSetId: string,
203
- id: string,
204
- ipSetName: string,
205
- managedRules?: any,
206
- cidrAllowList?: any
207
- ) => {
208
- const getIpSetResponse = await client.getIPSet({
209
- Id: ipSetId,
210
- Name: ipSetName,
211
- Scope: SCOPE,
212
- });
213
-
214
- await client.updateIPSet({
215
- Id: ipSetId,
216
- Name: ipSetName,
217
- Addresses: cidrAllowList?.cidrRanges ?? [],
218
- Scope: SCOPE,
219
- LockToken: getIpSetResponse.LockToken!,
220
- });
221
-
222
- const getWebAclResponse = await client.getWebACL({
223
- Id: webAclId,
224
- Name: id,
225
- Scope: SCOPE,
226
- });
227
-
228
- await client.updateWebACL({
229
- Name: id,
230
- DefaultAction: { Allow: {} },
231
- Scope: SCOPE,
232
- VisibilityConfig: {
233
- CloudWatchMetricsEnabled: true,
234
- MetricName: id,
235
- SampledRequestsEnabled: true,
236
- },
237
- Rules: getWafRules(
238
- getIpSetResponse.IPSet?.ARN!,
239
- ipSetName,
240
- managedRules,
241
- cidrAllowList
242
- ),
243
- Id: getWebAclResponse.WebACL?.Id!,
244
- LockToken: getWebAclResponse.LockToken!,
245
- });
246
-
247
- return {
248
- Data: {
249
- WebAclArn: getWebAclResponse.WebACL?.ARN,
250
- WebAclId: getWebAclResponse.WebACL?.Id,
251
- IPSetArn: getIpSetResponse.IPSet?.ARN,
252
- IPSetId: getIpSetResponse.IPSet?.Id,
253
- },
254
- };
255
- };
256
-
257
- const deleteWaf = async (
258
- webAclId: string,
259
- ipSetId: string,
260
- id: string,
261
- ipSetName: string
262
- ) => {
263
- const getWebAclResponse = await client.getWebACL({
264
- Id: webAclId,
265
- Name: id,
266
- Scope: SCOPE,
267
- });
268
-
269
- await client.deleteWebACL({
270
- Id: webAclId,
271
- Name: id,
272
- Scope: SCOPE,
273
- LockToken: getWebAclResponse.LockToken!,
274
- });
275
-
276
- const getIpSetResponse = await client.getIPSet({
277
- Id: ipSetId,
278
- Name: ipSetName,
279
- Scope: SCOPE,
280
- });
281
-
282
- await client.deleteIPSet({
283
- Id: ipSetId,
284
- Name: ipSetName,
285
- Scope: SCOPE,
286
- LockToken: getIpSetResponse.LockToken!,
287
- });
288
-
289
- return {
290
- Data: {
291
- WebAclArn: getWebAclResponse.WebACL?.ARN,
292
- WebAclId: getWebAclResponse.WebACL?.Id,
293
- IPSetArn: getIpSetResponse.IPSet?.ARN,
294
- IPSetId: getIpSetResponse.IPSet?.Id,
295
- },
296
- };
297
- };
298
-
299
- const sleep = async (duration: number) => {
300
- return new Promise((resolve) => setTimeout(resolve, duration));
301
- };
@@ -1,4 +0,0 @@
1
- /*! Copyright [Amazon.com](http://amazon.com/), Inc. or its affiliates. All Rights Reserved.
2
- SPDX-License-Identifier: Apache-2.0 */
3
- export * from './user-identity.js';
4
- export * from './userpool-with-mfa.js';
@@ -1,66 +0,0 @@
1
- /*! Copyright [Amazon.com](http://amazon.com/), Inc. or its affiliates. All Rights Reserved.
2
- SPDX-License-Identifier: Apache-2.0 */
3
- import {
4
- IdentityPool,
5
- UserPoolAuthenticationProvider,
6
- } from '@aws-cdk/aws-cognito-identitypool-alpha';
7
- import { CfnOutput, Stack } from 'aws-cdk-lib';
8
- import { UserPool, UserPoolClient } from 'aws-cdk-lib/aws-cognito';
9
- import { Construct } from 'constructs';
10
- import { UserPoolWithMfa } from './userpool-with-mfa.js';
11
- import { RuntimeConfig } from '../runtime-config/index.js';
12
-
13
- const WEB_CLIENT_ID = 'WebClient';
14
-
15
- /**
16
- * Creates a UserPool and Identity Pool with sane defaults configured intended for usage from a web client.
17
- */
18
- export class UserIdentity extends Construct {
19
- public readonly identityPool: IdentityPool;
20
- public readonly userPool: UserPool;
21
- public readonly userPoolClient: UserPoolClient;
22
-
23
- constructor(scope: Construct, id: string) {
24
- super(scope, id);
25
-
26
- // Unless explicitly stated, created a default Cognito User Pool and Web Client.
27
- this.userPool = new UserPoolWithMfa(this, 'UserPool');
28
-
29
- this.identityPool = new IdentityPool(this, 'IdentityPool');
30
-
31
- const existingClient = this.userPool.node.children.find(
32
- (e) => e.node.id === WEB_CLIENT_ID && e instanceof UserPoolClient
33
- ) as UserPoolClient | undefined;
34
-
35
- this.userPoolClient =
36
- existingClient ??
37
- this.userPool.addClient(WEB_CLIENT_ID, {
38
- authFlows: {
39
- userPassword: true,
40
- userSrp: true,
41
- },
42
- });
43
-
44
- this.identityPool.addUserPoolAuthentication(
45
- new UserPoolAuthenticationProvider({
46
- userPool: this.userPool,
47
- userPoolClient: this.userPoolClient,
48
- })
49
- );
50
-
51
- new CfnOutput(this, `${id}-UserPoolId`, {
52
- value: this.userPool.userPoolId,
53
- });
54
-
55
- new CfnOutput(this, `${id}-IdentityPoolId`, {
56
- value: this.identityPool.identityPoolId,
57
- });
58
-
59
- RuntimeConfig.ensure(this).config.cognitoProps = {
60
- region: Stack.of(this).region,
61
- identityPoolId: this.identityPool.identityPoolId,
62
- userPoolId: this.userPool?.userPoolId,
63
- userPoolWebClientId: this.userPoolClient?.userPoolClientId,
64
- };
65
- }
66
- }