@stacksjs/ts-cloud-core 0.1.1

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 (251) hide show
  1. package/LICENSE.md +21 -0
  2. package/README.md +321 -0
  3. package/package.json +31 -0
  4. package/src/advanced-features.test.ts +465 -0
  5. package/src/aws/cloudformation.ts +421 -0
  6. package/src/aws/cloudfront.ts +158 -0
  7. package/src/aws/credentials.test.ts +132 -0
  8. package/src/aws/credentials.ts +545 -0
  9. package/src/aws/index.ts +87 -0
  10. package/src/aws/s3.test.ts +188 -0
  11. package/src/aws/s3.ts +1088 -0
  12. package/src/aws/signature.test.ts +670 -0
  13. package/src/aws/signature.ts +1155 -0
  14. package/src/backup/disaster-recovery.test.ts +726 -0
  15. package/src/backup/disaster-recovery.ts +500 -0
  16. package/src/backup/index.ts +34 -0
  17. package/src/backup/manager.test.ts +498 -0
  18. package/src/backup/manager.ts +432 -0
  19. package/src/cicd/circleci.ts +430 -0
  20. package/src/cicd/github-actions.ts +424 -0
  21. package/src/cicd/gitlab-ci.ts +255 -0
  22. package/src/cicd/index.ts +8 -0
  23. package/src/cli/history.ts +396 -0
  24. package/src/cli/index.ts +10 -0
  25. package/src/cli/progress.ts +458 -0
  26. package/src/cli/repl.ts +454 -0
  27. package/src/cli/suggestions.ts +327 -0
  28. package/src/cli/table.test.ts +319 -0
  29. package/src/cli/table.ts +332 -0
  30. package/src/cloudformation/builder.test.ts +327 -0
  31. package/src/cloudformation/builder.ts +378 -0
  32. package/src/cloudformation/builders/api-gateway.ts +449 -0
  33. package/src/cloudformation/builders/cache.ts +334 -0
  34. package/src/cloudformation/builders/cdn.ts +278 -0
  35. package/src/cloudformation/builders/compute.ts +485 -0
  36. package/src/cloudformation/builders/database.ts +392 -0
  37. package/src/cloudformation/builders/functions.ts +343 -0
  38. package/src/cloudformation/builders/messaging.ts +140 -0
  39. package/src/cloudformation/builders/monitoring.ts +300 -0
  40. package/src/cloudformation/builders/network.ts +264 -0
  41. package/src/cloudformation/builders/queue.ts +147 -0
  42. package/src/cloudformation/builders/security.ts +399 -0
  43. package/src/cloudformation/builders/storage.ts +285 -0
  44. package/src/cloudformation/index.ts +30 -0
  45. package/src/cloudformation/types.ts +173 -0
  46. package/src/compliance/aws-config.ts +543 -0
  47. package/src/compliance/cloudtrail.ts +376 -0
  48. package/src/compliance/compliance.test.ts +423 -0
  49. package/src/compliance/guardduty.ts +446 -0
  50. package/src/compliance/index.ts +66 -0
  51. package/src/compliance/security-hub.ts +456 -0
  52. package/src/containers/build-optimization.ts +416 -0
  53. package/src/containers/containers.test.ts +508 -0
  54. package/src/containers/image-scanning.ts +360 -0
  55. package/src/containers/index.ts +9 -0
  56. package/src/containers/registry.ts +293 -0
  57. package/src/containers/service-mesh.ts +520 -0
  58. package/src/database/database.test.ts +762 -0
  59. package/src/database/index.ts +9 -0
  60. package/src/database/migrations.ts +444 -0
  61. package/src/database/performance.ts +528 -0
  62. package/src/database/replicas.ts +534 -0
  63. package/src/database/users.ts +494 -0
  64. package/src/dependency-graph.ts +143 -0
  65. package/src/deployment/ab-testing.ts +582 -0
  66. package/src/deployment/blue-green.ts +452 -0
  67. package/src/deployment/canary.ts +500 -0
  68. package/src/deployment/deployment.test.ts +526 -0
  69. package/src/deployment/index.ts +61 -0
  70. package/src/deployment/progressive.ts +62 -0
  71. package/src/dns/dns.test.ts +641 -0
  72. package/src/dns/dnssec.ts +315 -0
  73. package/src/dns/index.ts +8 -0
  74. package/src/dns/resolver.ts +496 -0
  75. package/src/dns/routing.ts +593 -0
  76. package/src/email/advanced/analytics.ts +445 -0
  77. package/src/email/advanced/index.ts +11 -0
  78. package/src/email/advanced/rules.ts +465 -0
  79. package/src/email/advanced/scheduling.ts +352 -0
  80. package/src/email/advanced/search.ts +412 -0
  81. package/src/email/advanced/shared-mailboxes.ts +404 -0
  82. package/src/email/advanced/templates.ts +455 -0
  83. package/src/email/advanced/threading.ts +281 -0
  84. package/src/email/analytics.ts +467 -0
  85. package/src/email/bounce-handling.ts +425 -0
  86. package/src/email/email.test.ts +431 -0
  87. package/src/email/handlers/__tests__/inbound.test.ts +38 -0
  88. package/src/email/handlers/__tests__/outbound.test.ts +37 -0
  89. package/src/email/handlers/converter.ts +227 -0
  90. package/src/email/handlers/feedback.ts +228 -0
  91. package/src/email/handlers/inbound.ts +169 -0
  92. package/src/email/handlers/outbound.ts +178 -0
  93. package/src/email/index.ts +15 -0
  94. package/src/email/reputation.ts +303 -0
  95. package/src/email/templates.ts +352 -0
  96. package/src/errors/index.test.ts +434 -0
  97. package/src/errors/index.ts +416 -0
  98. package/src/health-checks/index.ts +40 -0
  99. package/src/index.ts +360 -0
  100. package/src/intrinsic-functions.ts +118 -0
  101. package/src/lambda/concurrency.ts +330 -0
  102. package/src/lambda/destinations.ts +345 -0
  103. package/src/lambda/dlq.ts +425 -0
  104. package/src/lambda/index.ts +11 -0
  105. package/src/lambda/lambda.test.ts +840 -0
  106. package/src/lambda/layers.ts +263 -0
  107. package/src/lambda/versions.ts +376 -0
  108. package/src/lambda/vpc.ts +399 -0
  109. package/src/local/config.ts +114 -0
  110. package/src/local/index.ts +6 -0
  111. package/src/local/mock-aws.ts +351 -0
  112. package/src/modules/ai.ts +340 -0
  113. package/src/modules/api.ts +478 -0
  114. package/src/modules/auth.ts +805 -0
  115. package/src/modules/cache.ts +417 -0
  116. package/src/modules/cdn.ts +1062 -0
  117. package/src/modules/communication.ts +1094 -0
  118. package/src/modules/compute.ts +3348 -0
  119. package/src/modules/database.ts +554 -0
  120. package/src/modules/deployment.ts +1079 -0
  121. package/src/modules/dns.ts +337 -0
  122. package/src/modules/email.ts +1538 -0
  123. package/src/modules/filesystem.ts +515 -0
  124. package/src/modules/index.ts +32 -0
  125. package/src/modules/messaging.ts +486 -0
  126. package/src/modules/monitoring.ts +2086 -0
  127. package/src/modules/network.ts +664 -0
  128. package/src/modules/parameter-store.ts +325 -0
  129. package/src/modules/permissions.ts +1081 -0
  130. package/src/modules/phone.ts +494 -0
  131. package/src/modules/queue.ts +1260 -0
  132. package/src/modules/redirects.ts +464 -0
  133. package/src/modules/registry.ts +699 -0
  134. package/src/modules/search.ts +401 -0
  135. package/src/modules/secrets.ts +416 -0
  136. package/src/modules/security.ts +731 -0
  137. package/src/modules/sms.ts +389 -0
  138. package/src/modules/storage.ts +1120 -0
  139. package/src/modules/workflow.ts +680 -0
  140. package/src/multi-account/config.ts +521 -0
  141. package/src/multi-account/index.ts +7 -0
  142. package/src/multi-account/manager.ts +427 -0
  143. package/src/multi-region/cross-region.ts +410 -0
  144. package/src/multi-region/index.ts +8 -0
  145. package/src/multi-region/manager.ts +483 -0
  146. package/src/multi-region/regions.ts +435 -0
  147. package/src/network-security/index.ts +48 -0
  148. package/src/observability/index.ts +9 -0
  149. package/src/observability/logs.ts +522 -0
  150. package/src/observability/metrics.ts +460 -0
  151. package/src/observability/observability.test.ts +782 -0
  152. package/src/observability/synthetics.ts +568 -0
  153. package/src/observability/xray.ts +358 -0
  154. package/src/phone/advanced/analytics.ts +349 -0
  155. package/src/phone/advanced/callbacks.ts +428 -0
  156. package/src/phone/advanced/index.ts +8 -0
  157. package/src/phone/advanced/ivr-builder.ts +504 -0
  158. package/src/phone/advanced/recording.ts +310 -0
  159. package/src/phone/handlers/__tests__/incoming-call.test.ts +40 -0
  160. package/src/phone/handlers/incoming-call.ts +117 -0
  161. package/src/phone/handlers/missed-call.ts +116 -0
  162. package/src/phone/handlers/voicemail.ts +179 -0
  163. package/src/phone/index.ts +9 -0
  164. package/src/presets/api-backend.ts +134 -0
  165. package/src/presets/data-pipeline.ts +204 -0
  166. package/src/presets/extend.test.ts +295 -0
  167. package/src/presets/extend.ts +297 -0
  168. package/src/presets/fullstack-app.ts +144 -0
  169. package/src/presets/index.ts +27 -0
  170. package/src/presets/jamstack.ts +135 -0
  171. package/src/presets/microservices.ts +167 -0
  172. package/src/presets/ml-api.ts +208 -0
  173. package/src/presets/nodejs-server.ts +104 -0
  174. package/src/presets/nodejs-serverless.ts +114 -0
  175. package/src/presets/realtime-app.ts +184 -0
  176. package/src/presets/static-site.ts +64 -0
  177. package/src/presets/traditional-web-app.ts +339 -0
  178. package/src/presets/wordpress.ts +138 -0
  179. package/src/preview/github.test.ts +249 -0
  180. package/src/preview/github.ts +297 -0
  181. package/src/preview/index.ts +37 -0
  182. package/src/preview/manager.test.ts +440 -0
  183. package/src/preview/manager.ts +326 -0
  184. package/src/preview/notifications.test.ts +582 -0
  185. package/src/preview/notifications.ts +341 -0
  186. package/src/queue/batch-processing.ts +402 -0
  187. package/src/queue/dlq-monitoring.ts +402 -0
  188. package/src/queue/fifo.ts +342 -0
  189. package/src/queue/index.ts +9 -0
  190. package/src/queue/management.ts +428 -0
  191. package/src/queue/queue.test.ts +429 -0
  192. package/src/resource-mgmt/index.ts +39 -0
  193. package/src/resource-naming.ts +62 -0
  194. package/src/s3/index.ts +523 -0
  195. package/src/schema/cloud-config.schema.json +554 -0
  196. package/src/schema/index.ts +68 -0
  197. package/src/security/certificate-manager.ts +492 -0
  198. package/src/security/index.ts +9 -0
  199. package/src/security/scanning.ts +545 -0
  200. package/src/security/secrets-manager.ts +476 -0
  201. package/src/security/secrets-rotation.ts +456 -0
  202. package/src/security/security.test.ts +738 -0
  203. package/src/sms/advanced/ab-testing.ts +389 -0
  204. package/src/sms/advanced/analytics.ts +336 -0
  205. package/src/sms/advanced/campaigns.ts +523 -0
  206. package/src/sms/advanced/chatbot.ts +224 -0
  207. package/src/sms/advanced/index.ts +10 -0
  208. package/src/sms/advanced/link-tracking.ts +248 -0
  209. package/src/sms/advanced/mms.ts +308 -0
  210. package/src/sms/handlers/__tests__/send.test.ts +40 -0
  211. package/src/sms/handlers/delivery-status.ts +133 -0
  212. package/src/sms/handlers/receive.ts +162 -0
  213. package/src/sms/handlers/send.ts +174 -0
  214. package/src/sms/index.ts +9 -0
  215. package/src/stack-diff.ts +389 -0
  216. package/src/static-site/index.ts +85 -0
  217. package/src/template-builder.ts +110 -0
  218. package/src/template-validator.ts +574 -0
  219. package/src/utils/cache.ts +291 -0
  220. package/src/utils/diff.ts +269 -0
  221. package/src/utils/hash.ts +227 -0
  222. package/src/utils/index.ts +8 -0
  223. package/src/utils/parallel.ts +294 -0
  224. package/src/validators/credentials.test.ts +274 -0
  225. package/src/validators/credentials.ts +233 -0
  226. package/src/validators/quotas.test.ts +434 -0
  227. package/src/validators/quotas.ts +217 -0
  228. package/test/ai.test.ts +327 -0
  229. package/test/api.test.ts +511 -0
  230. package/test/auth.test.ts +632 -0
  231. package/test/cache.test.ts +406 -0
  232. package/test/cdn.test.ts +247 -0
  233. package/test/compute.test.ts +861 -0
  234. package/test/database.test.ts +523 -0
  235. package/test/deployment.test.ts +499 -0
  236. package/test/dns.test.ts +270 -0
  237. package/test/email.test.ts +439 -0
  238. package/test/filesystem.test.ts +382 -0
  239. package/test/integration.test.ts +350 -0
  240. package/test/messaging.test.ts +514 -0
  241. package/test/monitoring.test.ts +634 -0
  242. package/test/network.test.ts +425 -0
  243. package/test/permissions.test.ts +488 -0
  244. package/test/queue.test.ts +484 -0
  245. package/test/registry.test.ts +306 -0
  246. package/test/security.test.ts +462 -0
  247. package/test/storage.test.ts +463 -0
  248. package/test/template-validator.test.ts +559 -0
  249. package/test/workflow.test.ts +592 -0
  250. package/tsconfig.json +16 -0
  251. package/tsconfig.tsbuildinfo +1 -0
@@ -0,0 +1,462 @@
1
+ import { describe, expect, it } from 'bun:test'
2
+ import { Security } from '../src/modules/security'
3
+ import { TemplateBuilder } from '../src/template-builder'
4
+
5
+ describe('Security Module', () => {
6
+ describe('createCertificate', () => {
7
+ it('should create a basic SSL certificate', () => {
8
+ const { certificate, logicalId } = Security.createCertificate({
9
+ domain: 'example.com',
10
+ slug: 'my-app',
11
+ environment: 'production',
12
+ })
13
+
14
+ expect(certificate.Type).toBe('AWS::CertificateManager::Certificate')
15
+ expect(certificate.Properties.DomainName).toBe('example.com')
16
+ expect(certificate.Properties.ValidationMethod).toBe('DNS')
17
+ expect(certificate.Properties.SubjectAlternativeNames).toContain('example.com')
18
+ expect(logicalId).toBeDefined()
19
+ })
20
+
21
+ it('should create certificate with wildcard subdomain', () => {
22
+ const { certificate } = Security.createCertificate({
23
+ domain: 'example.com',
24
+ subdomains: ['*'],
25
+ slug: 'my-app',
26
+ environment: 'production',
27
+ })
28
+
29
+ expect(certificate.Properties.SubjectAlternativeNames).toContain('example.com')
30
+ expect(certificate.Properties.SubjectAlternativeNames).toContain('*.example.com')
31
+ expect(certificate.Properties.SubjectAlternativeNames).toHaveLength(2)
32
+ })
33
+
34
+ it('should create certificate with specific subdomains', () => {
35
+ const { certificate } = Security.createCertificate({
36
+ domain: 'example.com',
37
+ subdomains: ['www', 'api', 'cdn'],
38
+ slug: 'my-app',
39
+ environment: 'production',
40
+ })
41
+
42
+ expect(certificate.Properties.SubjectAlternativeNames).toContain('example.com')
43
+ expect(certificate.Properties.SubjectAlternativeNames).toContain('www.example.com')
44
+ expect(certificate.Properties.SubjectAlternativeNames).toContain('api.example.com')
45
+ expect(certificate.Properties.SubjectAlternativeNames).toContain('cdn.example.com')
46
+ })
47
+
48
+ it('should support email validation', () => {
49
+ const { certificate } = Security.createCertificate({
50
+ domain: 'example.com',
51
+ slug: 'my-app',
52
+ environment: 'production',
53
+ validationMethod: 'EMAIL',
54
+ })
55
+
56
+ expect(certificate.Properties.ValidationMethod).toBe('EMAIL')
57
+ expect(certificate.Properties.DomainValidationOptions).toBeUndefined()
58
+ })
59
+
60
+ it('should add DNS validation with Route53 when hostedZoneId provided', () => {
61
+ const { certificate } = Security.createCertificate({
62
+ domain: 'example.com',
63
+ subdomains: ['www'],
64
+ slug: 'my-app',
65
+ environment: 'production',
66
+ validationMethod: 'DNS',
67
+ hostedZoneId: 'Z1234567890ABC',
68
+ })
69
+
70
+ expect(certificate.Properties.DomainValidationOptions).toBeDefined()
71
+ expect(certificate.Properties.DomainValidationOptions).toHaveLength(2)
72
+ expect(certificate.Properties.DomainValidationOptions?.[0]).toEqual({
73
+ DomainName: 'example.com',
74
+ HostedZoneId: 'Z1234567890ABC',
75
+ })
76
+ expect(certificate.Properties.DomainValidationOptions?.[1]).toEqual({
77
+ DomainName: 'www.example.com',
78
+ HostedZoneId: 'Z1234567890ABC',
79
+ })
80
+ })
81
+ })
82
+
83
+ describe('createKmsKey', () => {
84
+ it('should create a KMS key with default settings', () => {
85
+ const { key, alias, logicalId, aliasId } = Security.createKmsKey({
86
+ description: 'Encryption key for user data',
87
+ slug: 'my-app',
88
+ environment: 'production',
89
+ })
90
+
91
+ expect(key.Type).toBe('AWS::KMS::Key')
92
+ expect(key.Properties.Description).toBe('Encryption key for user data')
93
+ expect(key.Properties.Enabled).toBe(true)
94
+ expect(key.Properties.EnableKeyRotation).toBe(true)
95
+ expect(key.Properties.KeySpec).toBe('SYMMETRIC_DEFAULT')
96
+ expect(key.Properties.KeyUsage).toBe('ENCRYPT_DECRYPT')
97
+ expect(key.Properties.MultiRegion).toBe(false)
98
+
99
+ expect(alias?.Type).toBe('AWS::KMS::Alias')
100
+ expect(alias?.Properties.AliasName).toBe('alias/my-app-production')
101
+ expect(logicalId).toBeDefined()
102
+ expect(aliasId).toBeDefined()
103
+ })
104
+
105
+ it('should create multi-region KMS key', () => {
106
+ const { key } = Security.createKmsKey({
107
+ description: 'Multi-region key',
108
+ slug: 'my-app',
109
+ environment: 'production',
110
+ multiRegion: true,
111
+ })
112
+
113
+ expect(key.Properties.MultiRegion).toBe(true)
114
+ })
115
+
116
+ it('should disable rotation when requested', () => {
117
+ const { key } = Security.createKmsKey({
118
+ description: 'No rotation key',
119
+ slug: 'my-app',
120
+ environment: 'production',
121
+ enableRotation: false,
122
+ })
123
+
124
+ expect(key.Properties.EnableKeyRotation).toBe(false)
125
+ })
126
+
127
+ it('should have valid key policy', () => {
128
+ const { key } = Security.createKmsKey({
129
+ description: 'Test key',
130
+ slug: 'my-app',
131
+ environment: 'production',
132
+ })
133
+
134
+ expect(key.Properties.KeyPolicy.Version).toBe('2012-10-17')
135
+ expect(key.Properties.KeyPolicy.Statement).toBeArray()
136
+ expect(key.Properties.KeyPolicy.Statement.length).toBeGreaterThan(0)
137
+
138
+ // Check root account permissions
139
+ const rootStatement = key.Properties.KeyPolicy.Statement[0]
140
+ expect(rootStatement.Effect).toBe('Allow')
141
+ expect(rootStatement.Action).toBe('kms:*')
142
+ })
143
+ })
144
+
145
+ describe('createFirewall', () => {
146
+ it('should create a WAF Web ACL with default settings', () => {
147
+ const { webAcl, logicalId } = Security.createFirewall({
148
+ slug: 'my-app',
149
+ environment: 'production',
150
+ })
151
+
152
+ expect(webAcl.Type).toBe('AWS::WAFv2::WebACL')
153
+ expect(webAcl.Properties.Scope).toBe('CLOUDFRONT')
154
+ expect(webAcl.Properties.DefaultAction.Allow).toBeDefined()
155
+ expect(webAcl.Properties.DefaultAction.Block).toBeUndefined()
156
+ expect(webAcl.Properties.VisibilityConfig.CloudWatchMetricsEnabled).toBe(true)
157
+ expect(logicalId).toBeDefined()
158
+ })
159
+
160
+ it('should create regional WAF', () => {
161
+ const { webAcl } = Security.createFirewall({
162
+ slug: 'my-app',
163
+ environment: 'production',
164
+ scope: 'REGIONAL',
165
+ })
166
+
167
+ expect(webAcl.Properties.Scope).toBe('REGIONAL')
168
+ })
169
+
170
+ it('should support block as default action', () => {
171
+ const { webAcl } = Security.createFirewall({
172
+ slug: 'my-app',
173
+ environment: 'production',
174
+ defaultAction: 'block',
175
+ })
176
+
177
+ expect(webAcl.Properties.DefaultAction.Block).toBeDefined()
178
+ expect(webAcl.Properties.DefaultAction.Allow).toBeUndefined()
179
+ })
180
+ })
181
+
182
+ describe('setRateLimit', () => {
183
+ it('should add rate limit rule', () => {
184
+ const { webAcl } = Security.createFirewall({
185
+ slug: 'my-app',
186
+ environment: 'production',
187
+ })
188
+
189
+ const updated = Security.setRateLimit(webAcl, {
190
+ name: 'RateLimit',
191
+ priority: 1,
192
+ requestsPerWindow: 2000,
193
+ })
194
+
195
+ expect(updated.Properties.Rules).toHaveLength(1)
196
+ expect(updated.Properties.Rules?.[0].Name).toBe('RateLimit')
197
+ expect(updated.Properties.Rules?.[0].Priority).toBe(1)
198
+ expect(updated.Properties.Rules?.[0].Statement.RateBasedStatement?.Limit).toBe(2000)
199
+ expect(updated.Properties.Rules?.[0].Statement.RateBasedStatement?.AggregateKeyType).toBe('IP')
200
+ expect(updated.Properties.Rules?.[0].Action?.Block).toBeDefined()
201
+ })
202
+
203
+ it('should support FORWARDED_IP aggregation', () => {
204
+ const { webAcl } = Security.createFirewall({
205
+ slug: 'my-app',
206
+ environment: 'production',
207
+ })
208
+
209
+ const updated = Security.setRateLimit(webAcl, {
210
+ name: 'RateLimit',
211
+ priority: 1,
212
+ requestsPerWindow: 1000,
213
+ aggregateKeyType: 'FORWARDED_IP',
214
+ })
215
+
216
+ expect(updated.Properties.Rules?.[0].Statement.RateBasedStatement?.AggregateKeyType).toBe('FORWARDED_IP')
217
+ })
218
+ })
219
+
220
+ describe('blockCountries', () => {
221
+ it('should add geo-blocking rule', () => {
222
+ const { webAcl } = Security.createFirewall({
223
+ slug: 'my-app',
224
+ environment: 'production',
225
+ })
226
+
227
+ const updated = Security.blockCountries(webAcl, {
228
+ name: 'BlockRiskyCountries',
229
+ priority: 2,
230
+ countryCodes: ['CN', 'RU', 'KP'],
231
+ })
232
+
233
+ expect(updated.Properties.Rules).toHaveLength(1)
234
+ expect(updated.Properties.Rules?.[0].Statement.GeoMatchStatement?.CountryCodes).toEqual(['CN', 'RU', 'KP'])
235
+ expect(updated.Properties.Rules?.[0].Action?.Block).toBeDefined()
236
+ })
237
+ })
238
+
239
+ describe('blockIpAddresses', () => {
240
+ it('should create IP set and add blocking rule', () => {
241
+ const { webAcl } = Security.createFirewall({
242
+ slug: 'my-app',
243
+ environment: 'production',
244
+ })
245
+
246
+ const { webAcl: updated, ipSet, ipSetLogicalId } = Security.blockIpAddresses(
247
+ webAcl,
248
+ {
249
+ name: 'BlockedIPs',
250
+ priority: 3,
251
+ ipAddresses: ['192.0.2.0/24', '198.51.100.42/32'],
252
+ },
253
+ 'my-app',
254
+ 'production',
255
+ )
256
+
257
+ expect(ipSet.Type).toBe('AWS::WAFv2::IPSet')
258
+ expect(ipSet.Properties.IPAddressVersion).toBe('IPV4')
259
+ expect(ipSet.Properties.Addresses).toEqual(['192.0.2.0/24', '198.51.100.42/32'])
260
+ expect(ipSetLogicalId).toBeDefined()
261
+
262
+ expect(updated.Properties.Rules).toHaveLength(1)
263
+ expect(updated.Properties.Rules?.[0].Statement.IPSetReferenceStatement).toBeDefined()
264
+ })
265
+
266
+ it('should support IPv6 addresses', () => {
267
+ const { webAcl } = Security.createFirewall({
268
+ slug: 'my-app',
269
+ environment: 'production',
270
+ })
271
+
272
+ const { ipSet } = Security.blockIpAddresses(
273
+ webAcl,
274
+ {
275
+ name: 'BlockedIPv6',
276
+ priority: 4,
277
+ ipAddresses: ['2001:0db8::/32'],
278
+ ipVersion: 'IPV6',
279
+ },
280
+ 'my-app',
281
+ 'production',
282
+ )
283
+
284
+ expect(ipSet.Properties.IPAddressVersion).toBe('IPV6')
285
+ expect(ipSet.Properties.Addresses).toContain('2001:0db8::/32')
286
+ })
287
+ })
288
+
289
+ describe('addManagedRules', () => {
290
+ it('should add AWS managed rule group', () => {
291
+ const { webAcl } = Security.createFirewall({
292
+ slug: 'my-app',
293
+ environment: 'production',
294
+ })
295
+
296
+ const updated = Security.addManagedRules(webAcl, {
297
+ name: 'AWSCoreRuleSet',
298
+ priority: 5,
299
+ vendorName: 'AWS',
300
+ ruleName: 'AWSManagedRulesCommonRuleSet',
301
+ })
302
+
303
+ expect(updated.Properties.Rules).toHaveLength(1)
304
+ expect(updated.Properties.Rules?.[0].Statement.ManagedRuleGroupStatement?.VendorName).toBe('AWS')
305
+ expect(updated.Properties.Rules?.[0].Statement.ManagedRuleGroupStatement?.Name).toBe('AWSManagedRulesCommonRuleSet')
306
+ })
307
+
308
+ it('should support excluding rules from managed rule group', () => {
309
+ const { webAcl } = Security.createFirewall({
310
+ slug: 'my-app',
311
+ environment: 'production',
312
+ })
313
+
314
+ const updated = Security.addManagedRules(webAcl, {
315
+ name: 'CoreRuleSet',
316
+ priority: 6,
317
+ vendorName: 'AWS',
318
+ ruleName: 'AWSManagedRulesCommonRuleSet',
319
+ excludedRules: ['SizeRestrictions_BODY', 'GenericRFI_BODY'],
320
+ })
321
+
322
+ expect(updated.Properties.Rules?.[0].Statement.ManagedRuleGroupStatement?.ExcludedRules).toHaveLength(2)
323
+ expect(updated.Properties.Rules?.[0].Statement.ManagedRuleGroupStatement?.ExcludedRules?.[0].Name).toBe('SizeRestrictions_BODY')
324
+ })
325
+
326
+ it('should have predefined managed rule groups', () => {
327
+ expect(Security.ManagedRuleGroups.CoreRuleSet.vendorName).toBe('AWS')
328
+ expect(Security.ManagedRuleGroups.CoreRuleSet.ruleName).toBe('AWSManagedRulesCommonRuleSet')
329
+ expect(Security.ManagedRuleGroups.SqlDatabase.ruleName).toBe('AWSManagedRulesSQLiRuleSet')
330
+ expect(Security.ManagedRuleGroups.AmazonIpReputation.ruleName).toBe('AWSManagedRulesAmazonIpReputationList')
331
+ })
332
+ })
333
+
334
+ describe('Integration with TemplateBuilder', () => {
335
+ it('should create complete security infrastructure', () => {
336
+ const template = new TemplateBuilder('Security Infrastructure')
337
+
338
+ // 1. Create SSL certificate
339
+ const { certificate, logicalId: certId } = Security.createCertificate({
340
+ domain: 'example.com',
341
+ subdomains: ['*'],
342
+ slug: 'my-app',
343
+ environment: 'production',
344
+ hostedZoneId: 'Z1234567890ABC',
345
+ })
346
+ template.addResource(certId, certificate)
347
+
348
+ // 2. Create KMS key
349
+ const { key, alias, logicalId: keyId, aliasId } = Security.createKmsKey({
350
+ description: 'Application encryption key',
351
+ slug: 'my-app',
352
+ environment: 'production',
353
+ })
354
+ template.addResource(keyId, key)
355
+ if (alias && aliasId) {
356
+ template.addResource(aliasId, alias)
357
+ }
358
+
359
+ // 3. Create WAF with multiple rules
360
+ const { webAcl, logicalId: wafId } = Security.createFirewall({
361
+ slug: 'my-app',
362
+ environment: 'production',
363
+ scope: 'CLOUDFRONT',
364
+ })
365
+
366
+ // Add rate limiting
367
+ Security.setRateLimit(webAcl, {
368
+ name: 'RateLimit',
369
+ priority: 1,
370
+ requestsPerWindow: 2000,
371
+ })
372
+
373
+ // Add managed rules
374
+ Security.addManagedRules(webAcl, {
375
+ name: 'CoreRules',
376
+ priority: 2,
377
+ ...Security.ManagedRuleGroups.CoreRuleSet,
378
+ })
379
+
380
+ // Add geo-blocking
381
+ Security.blockCountries(webAcl, {
382
+ name: 'GeoBlock',
383
+ priority: 3,
384
+ countryCodes: ['CN', 'RU'],
385
+ })
386
+
387
+ // Add IP blocking
388
+ const { webAcl: finalWebAcl, ipSet, ipSetLogicalId } = Security.blockIpAddresses(
389
+ webAcl,
390
+ {
391
+ name: 'IPBlock',
392
+ priority: 4,
393
+ ipAddresses: ['192.0.2.0/24'],
394
+ },
395
+ 'my-app',
396
+ 'production',
397
+ )
398
+
399
+ template.addResource(wafId, finalWebAcl)
400
+ template.addResource(ipSetLogicalId, ipSet)
401
+
402
+ const result = template.build()
403
+
404
+ // Certificate + Key + Alias + WebACL + IPSet = 5 resources
405
+ expect(Object.keys(result.Resources)).toHaveLength(5)
406
+ expect(result.Resources[certId].Type).toBe('AWS::CertificateManager::Certificate')
407
+ expect(result.Resources[keyId].Type).toBe('AWS::KMS::Key')
408
+ expect(result.Resources[wafId].Type).toBe('AWS::WAFv2::WebACL')
409
+ expect(result.Resources[wafId]!.Properties!.Rules).toHaveLength(4)
410
+ })
411
+
412
+ it('should generate valid JSON template', () => {
413
+ const template = new TemplateBuilder('Security Test')
414
+
415
+ const { certificate, logicalId } = Security.createCertificate({
416
+ domain: 'test.com',
417
+ slug: 'test',
418
+ environment: 'development',
419
+ })
420
+
421
+ template.addResource(logicalId, certificate)
422
+
423
+ const json = template.toJSON()
424
+ const parsed = JSON.parse(json)
425
+
426
+ expect(parsed.Resources[logicalId].Type).toBe('AWS::CertificateManager::Certificate')
427
+ expect(parsed.Resources[logicalId].Properties.DomainName).toBe('test.com')
428
+ })
429
+ })
430
+
431
+ describe('Multiple rules in WAF', () => {
432
+ it('should handle multiple rules with correct priorities', () => {
433
+ const { webAcl } = Security.createFirewall({
434
+ slug: 'my-app',
435
+ environment: 'production',
436
+ })
437
+
438
+ Security.setRateLimit(webAcl, {
439
+ name: 'RateLimit',
440
+ priority: 1,
441
+ requestsPerWindow: 2000,
442
+ })
443
+
444
+ Security.blockCountries(webAcl, {
445
+ name: 'GeoBlock',
446
+ priority: 2,
447
+ countryCodes: ['XX'],
448
+ })
449
+
450
+ Security.addManagedRules(webAcl, {
451
+ name: 'CoreRules',
452
+ priority: 3,
453
+ ...Security.ManagedRuleGroups.CoreRuleSet,
454
+ })
455
+
456
+ expect(webAcl.Properties.Rules).toHaveLength(3)
457
+ expect(webAcl.Properties.Rules?.[0].Priority).toBe(1)
458
+ expect(webAcl.Properties.Rules?.[1].Priority).toBe(2)
459
+ expect(webAcl.Properties.Rules?.[2].Priority).toBe(3)
460
+ })
461
+ })
462
+ })