Repository navigation
Expand file tree
/
Copy pathses-regions.ts
More file actions
191 lines (190 loc) · 17.5 KB
/
Copy pathses-regions.ts
File metadata and controls
191 lines (190 loc) · 17.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import { createRoute, z } from '@hono/zod-openapi';
import { and, eq, inArray, isNull, sql } from 'drizzle-orm';
import { actor, ApiError, assertSesConfigured, isSimulatedSes, errors, id, json, response, security, type Actor, type App, type Database, type JobHandler, type Runtime } from './core.js';
import { recordAudit } from './audit.js';
import { apiKeys, jobs } from './db/core.js';
import { domains } from './db/operations.js';
import { sesRegions } from './db/ses-regions.js';
import { approvedOriginUser } from './sending.js';
import { getMcpGrantActor } from './mcp-auth.js';
import { enqueue } from './jobs.js';
import { workspaceConnection, workspaceCredentials, type ReadyConnection } from './provider-credentials.js';
import { assertProvisionable, AutoValidationModeSchema, discoverSes, provisionSes, SesDiscoverySchema, setAutoValidation, type SesDiscovery } from './ses-setup.js';
import { configureRegion, credentialFingerprint, discoveryTarget, DISCOVERY_TTL, getRegionSettings, regionCatalog, regionPattern, regionWhere } from './ses-region-state.js';
import { productName } from './brand.js';
export { ensureRegionSettings, resolveRegionRuntime } from './ses-region-state.js';
const Params = z.object({ region: z.string().max(32).regex(regionPattern) }).openapi('SesRegionParams');
const Catalog = z.object({ defaultRegion: z.string(), data: z.array(z.object({
region: z.string(), enabled: z.boolean(), isDefault: z.boolean(),
discoveryStatus: z.enum(['not_discovered', 'discovering', 'stale', 'ready', 'needs_provisioning', 'blocked']),
lastDiscoveredAt: z.string().nullable(), provisionJobId: z.string().nullable(),
provisionStatus: z.enum(['pending', 'running', 'completed', 'failed']).nullable(), provisionError: z.string().nullable(),
})) }).openapi('RegionCatalog');
const Receipt = z.object({ jobId: z.string(), status: z.enum(['pending', 'running']) }).openapi('RegionProvisionReceipt');
const ValidationSettings = z.object({ stream: z.enum(['transactional', 'marketing']), mode: AutoValidationModeSchema.exclude(['inherit', 'unknown']) }).openapi('SesAutoValidationSettings');
function access(c: Parameters<typeof actor>[0], mutate = false, aws = false) {
const value = actor(c, mutate ? 'manage' : 'read');
if (value.domains.length) throw new ApiError(403, 'UNRESTRICTED_KEY_REQUIRED', 'Region configuration requires unrestricted workspace access.');
if (aws) assertSesConfigured(c.env);
if (c.env.config.multiTenant && mutate) throw new ApiError(409, 'CONNECTION_MANAGED_REGION', 'Use the workspace sending connection to manage its AWS region.');
return value;
}
async function saveDiscovery(runtime: Runtime, report: SesDiscovery, jobId?: string, workspaceId = runtime.config.workspaceId, connection?: ReadyConnection) {
if (runtime.config.multiTenant && connection?.kind !== 'byo') throw new ApiError(409, 'CONNECTION_MANAGED_REGION', 'Shared account discovery is not available.');
const fingerprint = await credentialFingerprint(runtime.config);
await runtime.db.transaction(async tx => {
if (connection && !(await tx.execute(sql`SELECT 1 FROM provider_connections WHERE id=${connection.id} AND workspace_id=${workspaceId} FOR SHARE`)).rowCount) throw new ApiError(409, 'AWS_CONNECTION_CHANGED', 'The sending connection changed during discovery.');
await tx.insert(sesRegions).values({ workspaceId, region: report.region }).onConflictDoNothing();
const updated = await tx.update(sesRegions).set({ report, credentialsFingerprint: fingerprint, publicUrl: discoveryTarget(runtime.config), lastDiscoveredAt: report.checkedAt })
.where(and(regionWhere(workspaceId, report.region), jobId ? eq(sesRegions.provisionJobId, jobId) : undefined,
sql`(${sesRegions.lastDiscoveredAt} IS NULL OR ${sesRegions.lastDiscoveredAt} <= ${report.checkedAt}::timestamptz)`)).returning({ region: sesRegions.region });
if (!updated.length) return;
if (connection) return; // Discovery is observational; only BYO onboarding imports account identities.
for (const domain of report.domains) await tx.insert(domains).values({ id: id('domain'), workspaceId, region: report.region, name: domain.name }).onConflictDoNothing();
});
}
export function registerSesRegions(app: App) {
app.openapi(createRoute({ method: 'get', path: '/v1/regions', operationId: 'listRegions', tags: ['Regions'], security,
description: 'Lists persisted region settings and cached discovery/job status without AWS calls. The environment default only seeds installation settings during migration.',
responses: { 200: response(Catalog), ...errors },
}), async c => { const value = access(c); return c.json(Catalog.parse(await regionCatalog(c.env, value.workspaceId)), 200); });
app.openapi(createRoute({ method: 'put', path: '/v1/regions/{region}', operationId: 'configureRegion', tags: ['Regions'], security,
description: 'Enable/disable a region or choose the default. Requires unrestricted management access. Disabling never deletes AWS resources or historical mail.',
request: { params: Params, body: json(z.object({ enabled: z.boolean().optional(), makeDefault: z.boolean().optional() }).strict().refine(value => value.enabled !== undefined || value.makeDefault === true, 'Choose enabled or makeDefault.').openapi('ConfigureRegion')) },
responses: { 200: response(Catalog), ...errors },
}), async c => {
const value = access(c, true), { region } = c.req.valid('param'), input = c.req.valid('json');
// configureRegion's own transaction becomes a savepoint, so the change and its audit row commit together.
const catalog = await c.env.db.transaction(async tx => {
const catalog = await configureRegion({ ...c.env, db: tx as unknown as Database }, value.workspaceId, region, input);
await recordAudit(tx, c, { action: 'region.configure', workspaceId: value.workspaceId, target: { type: 'region', id: region }, after: { ...input, defaultRegion: catalog.defaultRegion } });
return catalog;
});
return c.json(Catalog.parse(catalog), 200);
});
app.openapi(createRoute({ method: 'get', path: '/v1/regions/{region}/discovery', operationId: 'discoverRegion', tags: ['Regions'], security,
description: 'Read-only AWS discovery. Caches observations in PostgreSQL for 15 minutes; refresh=true forces AWS reads. Never creates resources or subscribes endpoints. Credential or public-origin changes invalidate the cache.',
request: { params: Params, query: z.object({ refresh: z.enum(['true', 'false']).optional() }) }, responses: { 200: response(SesDiscoverySchema), ...errors },
}), async c => {
const value = access(c, false, true); const { region } = c.req.valid('param');
const settings = await getRegionSettings(c.env.db, value.workspaceId);
if (!settings.enabledRegions.includes(region)) throw new ApiError(422, 'REGION_NOT_CONFIGURED', 'Enable this SES region before discovery.', 'region');
let runtime = c.env, connection: ReadyConnection | undefined;
if (runtime.config.multiTenant) {
runtime = { ...runtime, config: { ...runtime.config, workspaceId: value.workspaceId } };
connection = await workspaceConnection(runtime, region);
if (connection.kind !== 'byo') throw new ApiError(409, 'CONNECTION_MANAGED_REGION', 'Shared account discovery is not available.');
runtime.config.aws = await workspaceCredentials(runtime, region, connection);
runtime.config.awsAccountId = connection.aws_account_id;
}
const [cached] = await c.env.db.select().from(sesRegions).where(regionWhere(value.workspaceId, region)).limit(1);
const fingerprint = await credentialFingerprint(runtime.config);
if (c.req.valid('query').refresh !== 'true' && cached?.report && cached.credentialsFingerprint === fingerprint && cached.publicUrl === discoveryTarget(runtime.config) && cached.lastDiscoveredAt && Date.now() - Date.parse(cached.lastDiscoveredAt) < DISCOVERY_TTL) return c.json(SesDiscoverySchema.parse(cached.report), 200);
const report = await discoverSes(runtime, { region, installationId: connection?.id ?? settings.installationId,namespace:connection?.legacy===false?'byo':undefined });
await saveDiscovery(runtime, report, undefined, value.workspaceId, connection); return c.json(report, 200);
});
app.openapi(createRoute({ method: 'post', path: '/v1/regions/{region}/provision', operationId: 'provisionRegion', tags: ['Regions'], security,
description: `Explicitly queue idempotent setup of ${productName}-owned SES configuration sets, SNS topic/policy/event destinations and signed HTTPS subscription. Never sends email, changes DNS, or grants SES production approval. Repeated requests reuse the active job; inspect listRegions and discovery for completion and remaining blockers.`,
request: { params: Params, body: json(z.object({ confirm: z.literal(true) }).strict().openapi('ProvisionRegion')) }, responses: { 202: response(Receipt), ...errors },
}), async c => {
const value = access(c, true, true); const { region } = c.req.valid('param');
const receipt = await c.env.db.transaction(async tx => {
const settings = await getRegionSettings(tx, value.workspaceId, 'share');
assertProvisionable(c.env);
if (!settings.enabledRegions.includes(region)) throw new ApiError(422, 'REGION_NOT_CONFIGURED', 'The requested SES region is not enabled.', 'region');
await tx.insert(sesRegions).values({ workspaceId: value.workspaceId, region }).onConflictDoNothing();
const [row] = await tx.select().from(sesRegions).where(regionWhere(value.workspaceId, region)).for('update');
if (row!.provisionJobId) {
const [job] = await tx.select().from(jobs).where(and(eq(jobs.id, row!.provisionJobId), eq(jobs.workspaceId, value.workspaceId))).limit(1);
if (job && ['pending', 'running'].includes(job.status)) return { jobId: job.id, status: job.status as 'pending' | 'running' };
}
const jobId = await enqueue(tx, { type: 'ses.provision', workspaceId: value.workspaceId, payload: {
region, actorKeyId: value.keyId, credentialsFingerprint: await credentialFingerprint(c.env.config), publicUrl: discoveryTarget(c.env.config), requestId: c.get('requestId'),
} });
await tx.update(sesRegions).set({ provisionJobId: jobId }).where(regionWhere(value.workspaceId, region));
await recordAudit(tx, c, { action: 'region.provision', workspaceId: value.workspaceId, target: { type: 'region', id: region }, details: { jobId } });
return { jobId, status: 'pending' as const };
});
return c.json(receipt, 202);
});
app.openapi(createRoute({ method: 'put', path: '/v1/regions/{region}/auto-validation', operationId: 'configureRegionAutoValidation', tags: ['Regions'], security,
description: `Set SES Auto Validation for one ${productName}-owned configuration set. This changes real AWS behavior and requires unrestricted management access plus explicit confirmation.`,
request: { params: Params, body: json(ValidationSettings.extend({ confirm: z.literal(true) }).strict().openapi('ConfigureSesAutoValidation')) }, responses: { 200: response(ValidationSettings), ...errors },
}), async c => {
const value = access(c, true, true); const { region } = c.req.valid('param'); const input = c.req.valid('json');
const settings = await getRegionSettings(c.env.db, value.workspaceId);
if (!settings.enabledRegions.includes(region)) throw new ApiError(422, 'REGION_NOT_CONFIGURED', 'Enable this SES region before configuring Auto Validation.', 'region');
// The change is in SES, not in a database row. Fail closed: the audit row is written first and committed only once SES
// accepted the change, so a change without its row needs a lost commit (the client then sees 500 and retries).
await c.env.db.transaction(async tx => {
await recordAudit(tx, c, { action: 'region.auto_validation', workspaceId: value.workspaceId, target: { type: 'region', id: region }, after: { stream: input.stream, mode: input.mode } });
await setAutoValidation(c.env, { region, installationId: settings.installationId }, input.stream, input.mode);
});
const [row] = await c.env.db.select().from(sesRegions).where(regionWhere(value.workspaceId, region)).limit(1);
if (row?.report) {
const report = SesDiscoverySchema.parse(row.report);
report.resources[input.stream].autoValidation = input.mode;
await saveDiscovery(c.env, { ...report, checkedAt: new Date().toISOString() }, undefined, value.workspaceId);
}
return c.json(ValidationSettings.parse(input), 200);
});
}
async function approvedOrigin(runtime: Runtime, keyId: string) {
if (keyId.startsWith('user_')) return (await runtime.db.execute(approvedOriginUser(runtime, sql`${runtime.config.workspaceId}`, sql`${keyId.slice(5)}`))).rows.length > 0;
if (keyId.startsWith('mcp_')) {
const grant = await getMcpGrantActor(runtime, keyId, runtime.config.workspaceId);
return !!grant && (!runtime.config.multiTenant || grant.workspaceId === runtime.config.workspaceId) && grant.permissions.includes('manage') && !grant.domains.length;
}
const [key] = await runtime.db.select().from(apiKeys).where(and(eq(apiKeys.id, keyId), eq(apiKeys.workspaceId, runtime.config.workspaceId), isNull(apiKeys.revokedAt))).limit(1);
return Boolean(key?.permissions.includes('manage') && !key.domains.length);
}
const provision: JobHandler = async (runtime, payload, job) => {
if (runtime.config.multiTenant) throw new ApiError(409, 'CONNECTION_MANAGED_REGION', 'Use connection setup for hosted workspaces.');
const parsed = Params.safeParse(payload);
if (!parsed.success || job.workspaceId !== runtime.config.workspaceId || typeof payload.actorKeyId !== 'string') throw new ApiError(500, 'JOB_PAYLOAD_INVALID', 'Invalid SES provisioning job.');
const { region } = parsed.data;
if (payload.credentialsFingerprint !== await credentialFingerprint(runtime.config) || payload.publicUrl !== discoveryTarget(runtime.config)) throw new ApiError(409, 'AWS_CONNECTION_CHANGED', 'AWS credentials or public URL changed; request provisioning again.');
if (!await approvedOrigin(runtime, payload.actorKeyId)) throw new ApiError(403, 'ORIGIN_KEY_REVOKED', 'The provisioning principal is no longer authorized.');
const settings = await getRegionSettings(runtime.db, job.workspaceId);
if (!settings.enabledRegions.includes(region)) throw new ApiError(422, 'REGION_NOT_CONFIGURED', 'The provisioning region is disabled.');
const [row] = await runtime.db.select().from(sesRegions).where(regionWhere(job.workspaceId, region)).limit(1);
if (row?.provisionJobId !== job.id) throw new ApiError(409, 'PROVISION_SUPERSEDED', 'A newer provisioning job superseded this request.');
const report = await provisionSes(runtime, { region, installationId: settings.installationId,
registerTopic: async (accountId, topicArn) => {
const updated = await runtime.db.update(sesRegions).set({ trustedAccountId: accountId, trustedTopicArn: topicArn, trustedCredentialsFingerprint: await credentialFingerprint(runtime.config) })
.where(and(regionWhere(job.workspaceId, region), eq(sesRegions.provisionJobId, job.id), sql`EXISTS (SELECT 1 FROM jobs WHERE id=${job.id} AND status='running' AND attempts=${job.attempts})`)).returning({ region: sesRegions.region });
if (!updated.length) throw new ApiError(409, 'PROVISION_SUPERSEDED', 'This provisioning job no longer owns the region.');
},
});
await saveDiscovery(runtime, report, job.id);
};
// Startup schedules only read-only discovery. Concurrent worker starts reuse active jobs.
export async function queueStartupDiscovery(runtime: Runtime): Promise<number> {
if (isSimulatedSes(runtime) || runtime.config.multiTenant) return 0;
const fingerprint = await credentialFingerprint(runtime.config), target = discoveryTarget(runtime.config);
return runtime.db.transaction(async tx => {
const settings = await getRegionSettings(tx, runtime.config.workspaceId, 'update');
const rows = await tx.select().from(sesRegions).where(eq(sesRegions.workspaceId, runtime.config.workspaceId));
const active = await tx.select({ payload: jobs.payload }).from(jobs).where(and(eq(jobs.workspaceId, runtime.config.workspaceId), eq(jobs.type, 'ses.discover'), inArray(jobs.status, ['pending', 'running'])));
let count = 0;
for (const region of settings.enabledRegions) {
const row = rows.find(value => value.region === region);
if (row?.lastDiscoveredAt && row.credentialsFingerprint === fingerprint && row.publicUrl === target && Date.now() - Date.parse(row.lastDiscoveredAt) < DISCOVERY_TTL) continue;
if (active.some(job => job.payload.region === region && job.payload.credentialsFingerprint === fingerprint && job.payload.publicUrl === target)) continue;
await enqueue(tx, { type: 'ses.discover', workspaceId: runtime.config.workspaceId, payload: { region, credentialsFingerprint: fingerprint, publicUrl: target } });
count++;
}
return count;
});
}
const discover: JobHandler = async (runtime, payload, job) => {
if (runtime.config.multiTenant) return;
const parsed = Params.safeParse(payload);
if (!parsed.success || job.workspaceId !== runtime.config.workspaceId) throw new ApiError(500, 'JOB_PAYLOAD_INVALID', 'Invalid SES discovery job.');
if (payload.credentialsFingerprint !== await credentialFingerprint(runtime.config) || payload.publicUrl !== discoveryTarget(runtime.config)) throw new ApiError(409, 'AWS_CONNECTION_CHANGED', 'Discovery connection settings changed.');
const settings = await getRegionSettings(runtime.db, job.workspaceId);
if (!settings.enabledRegions.includes(parsed.data.region)) return;
const report = await discoverSes(runtime, { region: parsed.data.region, installationId: settings.installationId });
await saveDiscovery(runtime, report);
};
export const sesRegionJobs = { 'ses.provision': provision, 'ses.discover': discover };