@stacksjs/ts-cloud 0.1.8 → 0.1.9

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 (75) hide show
  1. package/dist/bin/cli.js +1 -1
  2. package/package.json +18 -16
  3. package/src/aws/acm.ts +768 -0
  4. package/src/aws/application-autoscaling.ts +845 -0
  5. package/src/aws/bedrock.ts +4074 -0
  6. package/src/aws/client.ts +891 -0
  7. package/src/aws/cloudformation.ts +896 -0
  8. package/src/aws/cloudfront.ts +1531 -0
  9. package/src/aws/cloudwatch-logs.ts +154 -0
  10. package/src/aws/comprehend.ts +839 -0
  11. package/src/aws/connect.ts +1056 -0
  12. package/src/aws/deploy-imap.ts +384 -0
  13. package/src/aws/dynamodb.ts +340 -0
  14. package/src/aws/ec2.ts +1385 -0
  15. package/src/aws/ecr.ts +621 -0
  16. package/src/aws/ecs.ts +615 -0
  17. package/src/aws/elasticache.ts +301 -0
  18. package/src/aws/elbv2.ts +942 -0
  19. package/src/aws/email.ts +928 -0
  20. package/src/aws/eventbridge.ts +248 -0
  21. package/src/aws/iam.ts +1689 -0
  22. package/src/aws/imap-server.ts +2100 -0
  23. package/src/aws/index.ts +213 -0
  24. package/src/aws/kendra.ts +1097 -0
  25. package/src/aws/lambda.ts +786 -0
  26. package/src/aws/opensearch.ts +158 -0
  27. package/src/aws/personalize.ts +977 -0
  28. package/src/aws/polly.ts +559 -0
  29. package/src/aws/rds.ts +888 -0
  30. package/src/aws/rekognition.ts +846 -0
  31. package/src/aws/route53-domains.ts +359 -0
  32. package/src/aws/route53.ts +1046 -0
  33. package/src/aws/s3.ts +2334 -0
  34. package/src/aws/scheduler.ts +571 -0
  35. package/src/aws/secrets-manager.ts +769 -0
  36. package/src/aws/ses.ts +1081 -0
  37. package/src/aws/setup-phone.ts +104 -0
  38. package/src/aws/setup-sms.ts +580 -0
  39. package/src/aws/sms.ts +1735 -0
  40. package/src/aws/smtp-server.ts +531 -0
  41. package/src/aws/sns.ts +758 -0
  42. package/src/aws/sqs.ts +382 -0
  43. package/src/aws/ssm.ts +807 -0
  44. package/src/aws/sts.ts +92 -0
  45. package/src/aws/support.ts +391 -0
  46. package/src/aws/test-imap.ts +86 -0
  47. package/src/aws/textract.ts +780 -0
  48. package/src/aws/transcribe.ts +108 -0
  49. package/src/aws/translate.ts +641 -0
  50. package/src/aws/voice.ts +1379 -0
  51. package/src/config.ts +35 -0
  52. package/src/deploy/index.ts +7 -0
  53. package/src/deploy/static-site-external-dns.ts +945 -0
  54. package/src/deploy/static-site.ts +1175 -0
  55. package/src/dns/cloudflare.ts +548 -0
  56. package/src/dns/godaddy.ts +412 -0
  57. package/src/dns/index.ts +205 -0
  58. package/src/dns/porkbun.ts +362 -0
  59. package/src/dns/route53-adapter.ts +414 -0
  60. package/src/dns/types.ts +119 -0
  61. package/src/dns/validator.ts +369 -0
  62. package/src/generators/index.ts +5 -0
  63. package/src/generators/infrastructure.ts +1660 -0
  64. package/src/index.ts +163 -0
  65. package/src/push/apns.ts +452 -0
  66. package/src/push/fcm.ts +506 -0
  67. package/src/push/index.ts +58 -0
  68. package/src/security/pre-deploy-scanner.ts +655 -0
  69. package/src/ssl/acme-client.ts +478 -0
  70. package/src/ssl/index.ts +7 -0
  71. package/src/ssl/letsencrypt.ts +747 -0
  72. package/src/types.ts +2 -0
  73. package/src/utils/cli.ts +398 -0
  74. package/src/validation/index.ts +5 -0
  75. package/src/validation/template.ts +405 -0
@@ -0,0 +1,384 @@
1
+ #!/usr/bin/env bun
2
+ /**
3
+ * Deploy IMAP-to-S3 bridge server to EC2 via SSM
4
+ * Embeds all code directly in SSM commands to avoid S3 permission issues
5
+ *
6
+ * Security:
7
+ * - Uses EC2 instance IAM role for AWS credentials (no hardcoded keys)
8
+ * - Fetches IMAP passwords from AWS Secrets Manager at startup
9
+ * - Secret name: stacks/mail-server/credentials
10
+ * - Credentials are read from email config and synced to Secrets Manager
11
+ */
12
+
13
+ import { SSMClient } from './ssm'
14
+ import { AWSClient } from './client'
15
+ import * as fs from 'node:fs'
16
+ import * as path from 'node:path'
17
+
18
+ export interface MailboxConfig {
19
+ email: string
20
+ password?: string
21
+ }
22
+
23
+ export interface MailServerDeployConfig {
24
+ instanceId: string
25
+ region: string
26
+ secretName: string
27
+ domain: string
28
+ bucket: string
29
+ prefix: string
30
+ /** Mailboxes can be simple strings or objects with optional passwords */
31
+ mailboxes: Array<string | MailboxConfig>
32
+ }
33
+
34
+ /**
35
+ * Normalize mailbox config to object format with password lookup
36
+ * Supports:
37
+ * - Simple usernames: 'chris' -> 'chris@{domain}', looks up MAIL_PASSWORD_CHRIS
38
+ * - Full email strings: 'chris@stacksjs.com' -> looks up MAIL_PASSWORD_CHRIS
39
+ * - Objects with email: { email: 'chris@stacksjs.com', password: '...' }
40
+ * - Objects with address (deprecated): { address: 'chris@stacksjs.com' }
41
+ */
42
+ function normalizeMailbox(mailbox: string | MailboxConfig | { address: string, password?: string }, domain: string): MailboxConfig {
43
+ if (typeof mailbox === 'string') {
44
+ // If it's just a username (no @), append the domain
45
+ const email = mailbox.includes('@') ? mailbox : `${mailbox}@${domain}`
46
+ const username = email.split('@')[0].toUpperCase()
47
+ const envKey = `MAIL_PASSWORD_${username}`
48
+ const password = process.env[envKey]
49
+ return { email, password }
50
+ }
51
+
52
+ // Handle both 'email' and 'address' fields (address is deprecated)
53
+ let email = 'email' in mailbox ? mailbox.email : (mailbox as { address: string }).address
54
+ if (!email) {
55
+ throw new Error('Mailbox must have either "email" or "address" field')
56
+ }
57
+
58
+ // If it's just a username (no @), append the domain
59
+ if (!email.includes('@')) {
60
+ email = `${email}@${domain}`
61
+ }
62
+
63
+ // If object format but no password, try env lookup
64
+ if (!mailbox.password) {
65
+ const username = email.split('@')[0].toUpperCase()
66
+ const envKey = `MAIL_PASSWORD_${username}`
67
+ const password = process.env[envKey]
68
+ return { email, password }
69
+ }
70
+ return { email, password: mailbox.password }
71
+ }
72
+
73
+ const defaultConfig: MailServerDeployConfig = {
74
+ instanceId: 'i-032233d3e9839b78b',
75
+ region: 'us-east-1',
76
+ secretName: 'stacks/mail-server/credentials',
77
+ domain: 'stacksjs.com',
78
+ bucket: 'stacks-production-email',
79
+ prefix: 'incoming/',
80
+ mailboxes: [],
81
+ }
82
+
83
+ export async function deployImapServer(config: MailServerDeployConfig = defaultConfig): Promise<void> {
84
+ console.log('Deploying IMAP-to-S3 bridge server to EC2...')
85
+ console.log('')
86
+
87
+ const ssm = new SSMClient(config.region)
88
+ const awsClient = new AWSClient()
89
+
90
+ // Normalize all mailboxes to object format with password lookup
91
+ const normalizedMailboxes = config.mailboxes.map((m) => normalizeMailbox(m, config.domain))
92
+
93
+ // Build credentials from normalized mailboxes
94
+ const credentials: Record<string, string> = {}
95
+ for (const mailbox of normalizedMailboxes) {
96
+ // Extract username from email (chris@stacksjs.com -> chris)
97
+ const username = mailbox.email.split('@')[0]
98
+ if (mailbox.password) {
99
+ credentials[username] = mailbox.password
100
+ }
101
+ }
102
+
103
+ // Ensure the secret exists with IMAP credentials from config
104
+ console.log('0. Ensuring credentials secret exists in Secrets Manager...')
105
+ try {
106
+ const existingSecretResult = await awsClient.request({
107
+ service: 'secretsmanager',
108
+ region: config.region,
109
+ method: 'POST',
110
+ path: '/',
111
+ headers: {
112
+ 'Content-Type': 'application/x-amz-json-1.1',
113
+ 'X-Amz-Target': 'secretsmanager.GetSecretValue',
114
+ },
115
+ body: JSON.stringify({ SecretId: config.secretName }),
116
+ })
117
+ const existingSecret = existingSecretResult.SecretString || '{}'
118
+ console.log(' Secret already exists')
119
+
120
+ // Update if we have new credentials from config
121
+ if (Object.keys(credentials).length > 0) {
122
+ const existingCreds = JSON.parse(existingSecret)
123
+ const mergedCreds = { ...existingCreds, ...credentials }
124
+ await awsClient.request({
125
+ service: 'secretsmanager',
126
+ region: config.region,
127
+ method: 'POST',
128
+ path: '/',
129
+ headers: {
130
+ 'Content-Type': 'application/x-amz-json-1.1',
131
+ 'X-Amz-Target': 'secretsmanager.PutSecretValue',
132
+ },
133
+ body: JSON.stringify({
134
+ SecretId: config.secretName,
135
+ SecretString: JSON.stringify(mergedCreds),
136
+ }),
137
+ })
138
+ console.log(' Secret updated with config credentials')
139
+ }
140
+ }
141
+ catch {
142
+ // Create the secret with credentials from config
143
+ console.log(' Creating secret...')
144
+ if (Object.keys(credentials).length === 0) {
145
+ console.warn(' WARNING: No passwords configured in mailboxes - logins will fail')
146
+ console.warn(' Set MAIL_PASSWORD_<USER> environment variables in your config')
147
+ }
148
+ await awsClient.request({
149
+ service: 'secretsmanager',
150
+ region: config.region,
151
+ method: 'POST',
152
+ path: '/',
153
+ headers: {
154
+ 'Content-Type': 'application/x-amz-json-1.1',
155
+ 'X-Amz-Target': 'secretsmanager.CreateSecret',
156
+ },
157
+ body: JSON.stringify({
158
+ Name: config.secretName,
159
+ Description: `IMAP mail server credentials for ${config.domain}`,
160
+ SecretString: JSON.stringify(credentials),
161
+ ClientRequestToken: crypto.randomUUID(),
162
+ }),
163
+ })
164
+ console.log(' Secret created')
165
+ }
166
+
167
+ // Read the source files
168
+ const imapServerCode = fs.readFileSync(path.join(__dirname, 'imap-server.ts'), 'utf-8')
169
+ const s3ClientCode = fs.readFileSync(path.join(__dirname, 's3.ts'), 'utf-8')
170
+ const clientCode = fs.readFileSync(path.join(__dirname, 'client.ts'), 'utf-8')
171
+
172
+ // Build users config for server script from normalized mailboxes
173
+ const usersConfig = normalizedMailboxes.map((m) => {
174
+ const username = m.email.split('@')[0]
175
+ return ` ${username}: {
176
+ password: passwords.${username} || 'changeme',
177
+ email: '${m.email}',
178
+ },`
179
+ }).join('\n')
180
+
181
+ // Create the server startup script - fetches credentials from Secrets Manager using AWSClient directly
182
+ const serverScript = `#!/usr/bin/env bun
183
+ import * as fs from 'node:fs'
184
+ import { startImapServer } from './imap-server'
185
+ import { AWSClient } from './client'
186
+
187
+ const SECRET_NAME = '${config.secretName}'
188
+ const REGION = '${config.region}'
189
+
190
+ async function main() {
191
+ console.log('Starting IMAP-to-S3 bridge server...')
192
+
193
+ // Fetch credentials from Secrets Manager using AWSClient directly (uses EC2 instance IAM role)
194
+ console.log('Fetching credentials from Secrets Manager...')
195
+ const client = new AWSClient()
196
+ let passwords: Record<string, string> = {}
197
+
198
+ try {
199
+ const result = await client.request({
200
+ service: 'secretsmanager',
201
+ region: REGION,
202
+ method: 'POST',
203
+ path: '/',
204
+ headers: {
205
+ 'Content-Type': 'application/x-amz-json-1.1',
206
+ 'X-Amz-Target': 'secretsmanager.GetSecretValue'
207
+ },
208
+ body: JSON.stringify({ SecretId: SECRET_NAME })
209
+ })
210
+ passwords = JSON.parse(result.SecretString || '{}')
211
+ console.log('Credentials loaded for:', Object.keys(passwords).join(', '))
212
+ }
213
+ catch (error) {
214
+ console.error('Failed to fetch credentials from Secrets Manager:', error)
215
+ console.error('Using fallback empty passwords - logins will fail')
216
+ }
217
+
218
+ const hasTlsCerts = fs.existsSync('/etc/letsencrypt/live/mail.${config.domain}/privkey.pem')
219
+ console.log('TLS certificates available:', hasTlsCerts)
220
+
221
+ const server = await startImapServer({
222
+ port: 143,
223
+ sslPort: 993,
224
+ host: '0.0.0.0',
225
+ region: REGION,
226
+ bucket: '${config.bucket}',
227
+ prefix: '${config.prefix}',
228
+ domain: '${config.domain}',
229
+ users: {
230
+ ${usersConfig}
231
+ },
232
+ tls: hasTlsCerts ? {
233
+ key: '/etc/letsencrypt/live/mail.${config.domain}/privkey.pem',
234
+ cert: '/etc/letsencrypt/live/mail.${config.domain}/fullchain.pem',
235
+ } : undefined,
236
+ })
237
+
238
+ console.log('IMAP server running on port 143' + (hasTlsCerts ? ' and 993 (TLS)' : ''))
239
+
240
+ process.on('SIGINT', async () => {
241
+ console.log('Shutting down...')
242
+ await server.stop()
243
+ process.exit(0)
244
+ })
245
+
246
+ process.on('SIGTERM', async () => {
247
+ console.log('Shutting down...')
248
+ await server.stop()
249
+ process.exit(0)
250
+ })
251
+ }
252
+
253
+ main().catch(console.error)
254
+ `
255
+
256
+ // Create systemd service file - NO hardcoded credentials, uses IAM role
257
+ const systemdService = `[Unit]
258
+ Description=IMAP-to-S3 Bridge Server
259
+ After=network.target
260
+
261
+ [Service]
262
+ Type=simple
263
+ User=root
264
+ WorkingDirectory=/opt/imap-server
265
+ # AWS credentials come from EC2 instance IAM role - no hardcoded keys needed
266
+ Environment="AWS_REGION=${config.region}"
267
+ ExecStart=/root/.bun/bin/bun run /opt/imap-server/server.ts
268
+ Restart=always
269
+ RestartSec=10
270
+
271
+ [Install]
272
+ WantedBy=multi-user.target
273
+ `
274
+
275
+ // Step 1: Create directory structure
276
+ console.log('1. Creating directory structure on EC2...')
277
+ let result = await ssm.runShellCommand(config.instanceId, [
278
+ 'mkdir -p /opt/imap-server',
279
+ 'ls -la /opt/imap-server',
280
+ ], { maxWaitMs: 60000 })
281
+
282
+ if (!result.success) {
283
+ console.error('Failed to create directory:', result.error)
284
+ process.exit(1)
285
+ }
286
+ console.log(' Directory created')
287
+
288
+ // Step 2: Write client.ts (base64 encode to handle special chars)
289
+ console.log('2. Writing client.ts...')
290
+ const clientBase64 = Buffer.from(clientCode).toString('base64')
291
+ result = await ssm.runShellCommand(config.instanceId, [
292
+ `echo '${clientBase64}' | base64 -d > /opt/imap-server/client.ts`,
293
+ 'wc -l /opt/imap-server/client.ts',
294
+ ], { maxWaitMs: 60000 })
295
+
296
+ if (!result.success) {
297
+ console.error('Failed to write client.ts:', result.error)
298
+ process.exit(1)
299
+ }
300
+ console.log(' client.ts written')
301
+
302
+ // Step 3: Write s3.ts
303
+ console.log('3. Writing s3.ts...')
304
+ const s3Base64 = Buffer.from(s3ClientCode).toString('base64')
305
+ result = await ssm.runShellCommand(config.instanceId, [
306
+ `echo '${s3Base64}' | base64 -d > /opt/imap-server/s3.ts`,
307
+ 'wc -l /opt/imap-server/s3.ts',
308
+ ], { maxWaitMs: 60000 })
309
+
310
+ if (!result.success) {
311
+ console.error('Failed to write s3.ts:', result.error)
312
+ process.exit(1)
313
+ }
314
+ console.log(' s3.ts written')
315
+
316
+ // Step 4: Write imap-server.ts
317
+ console.log('4. Writing imap-server.ts...')
318
+ const imapBase64 = Buffer.from(imapServerCode).toString('base64')
319
+ result = await ssm.runShellCommand(config.instanceId, [
320
+ `echo '${imapBase64}' | base64 -d > /opt/imap-server/imap-server.ts`,
321
+ 'wc -l /opt/imap-server/imap-server.ts',
322
+ ], { maxWaitMs: 60000 })
323
+
324
+ if (!result.success) {
325
+ console.error('Failed to write imap-server.ts:', result.error)
326
+ process.exit(1)
327
+ }
328
+ console.log(' imap-server.ts written')
329
+
330
+ // Step 5: Write server.ts
331
+ console.log('5. Writing server.ts...')
332
+ const serverBase64 = Buffer.from(serverScript).toString('base64')
333
+ result = await ssm.runShellCommand(config.instanceId, [
334
+ `echo '${serverBase64}' | base64 -d > /opt/imap-server/server.ts`,
335
+ 'wc -l /opt/imap-server/server.ts',
336
+ ], { maxWaitMs: 60000 })
337
+
338
+ if (!result.success) {
339
+ console.error('Failed to write server.ts:', result.error)
340
+ process.exit(1)
341
+ }
342
+ console.log(' server.ts written')
343
+
344
+ // Step 6: Write systemd service and start
345
+ console.log('6. Setting up systemd service...')
346
+ const serviceBase64 = Buffer.from(systemdService).toString('base64')
347
+ result = await ssm.runShellCommand(config.instanceId, [
348
+ `echo '${serviceBase64}' | base64 -d > /etc/systemd/system/imap-server.service`,
349
+ 'systemctl daemon-reload',
350
+ 'systemctl stop imap-server 2>/dev/null || true',
351
+ 'systemctl enable imap-server',
352
+ 'systemctl start imap-server',
353
+ 'sleep 3',
354
+ 'systemctl status imap-server --no-pager || true',
355
+ 'ss -tlnp | grep -E ":143|:993" || netstat -tlnp | grep -E ":143|:993" || echo "Ports not yet listening"',
356
+ ], { maxWaitMs: 120000 })
357
+
358
+ console.log('')
359
+ console.log('Service status:')
360
+ console.log(result.output || result.error)
361
+
362
+ console.log('')
363
+ console.log('='.repeat(60))
364
+ console.log('IMAP Server Deployment Complete!')
365
+ console.log('='.repeat(60))
366
+ console.log('')
367
+ console.log('Mail.app Settings:')
368
+ console.log(' Account Type: IMAP')
369
+ console.log(` Incoming Server: mail.${config.domain}`)
370
+ console.log(' Port: 143 (or 993 with SSL)')
371
+ console.log(' Username: <email username>')
372
+ console.log(' Password: <from Secrets Manager>')
373
+ console.log('')
374
+ console.log('Credentials are stored in AWS Secrets Manager:')
375
+ console.log(` Secret: ${config.secretName}`)
376
+ console.log('')
377
+ }
378
+
379
+ // Main entry point for standalone execution
380
+ async function main() {
381
+ await deployImapServer(defaultConfig)
382
+ }
383
+
384
+ main().catch(console.error)