feat: EC2 UI polished + storage persisting

This commit is contained in:
Zakaria
2026-09-25 18:18:33 -04:00
parent 1b129e9dcf
commit 093afb9b2b
40 changed files with 2586 additions and 102 deletions
+84
View File
@@ -17,6 +17,8 @@ import {awsKmsSchema} from '../cloud-spi/kmsSchema'
import {awsStorageSchema, azureStorageSchema, gcpStorageSchema} from '../cloud-spi/storageSchema'
import {awsSesEmailSchema} from '../cloud-spi/emailSchema'
import {awsAppConfigSchema} from '../cloud-spi/appConfigSchema'
import {awsEventBridgeSchema} from '../cloud-spi/eventbridgeSchema'
import {awsNotificationsSchema} from '../cloud-spi/notificationsSchema'
import type {
AppConfigConfigurationProfile,
AppConfigDeployment,
@@ -99,6 +101,88 @@ function appWithRoutes(
return app
}
describe('EventBridge rule routes', () => {
test('routes rule lifecycle requests through the Events adapter using the event bus resource id', async () => {
const calls: Array<{operation: string; args: unknown[]}> = []
const adapter = mockAdapter('aws', {
service: 'events',
schema: awsEventBridgeSchema,
listEventBridgeRules: async (...args) => {
calls.push({operation: 'list', args})
return []
},
createEventBridgeRule: async (...args) => {
calls.push({operation: 'create', args})
return {
name: 'hourly', busName: 'orders', busId: 'event-bus:orders', arn: null,
scheduleExpression: 'rate(1 hour)', state: 'ENABLED', targets: [],
}
},
deleteEventBridgeRule: async (...args) => {
calls.push({operation: 'delete', args})
},
})
const app = appWithRoutes([adapter])
const create = await app.request('/api/clouds/aws/services/events/resources/event-bus:orders/rules', {
method: 'POST',
headers: {'content-type': 'application/json'},
body: JSON.stringify({
name: 'hourly', scheduleExpression: 'rate(1 hour)',
targets: [{id: 'worker', arn: 'arn:aws:lambda:us-east-1:000000000000:function:worker'}],
}),
})
expect(create.status).toBe(201)
await app.request('/api/clouds/aws/services/events/resources/event-bus:orders/rules')
const remove = await app.request('/api/clouds/aws/services/events/resources/event-bus:orders/rules/hourly', {method: 'DELETE'})
expect(remove.status).toBe(200)
expect(calls).toEqual([
{operation: 'create', args: ['event-bus:orders', {name: 'hourly', scheduleExpression: 'rate(1 hour)', eventPattern: undefined, state: undefined, description: undefined, targets: [{id: 'worker', arn: 'arn:aws:lambda:us-east-1:000000000000:function:worker'}]}]},
{operation: 'list', args: ['event-bus:orders']},
{operation: 'delete', args: ['event-bus:orders', 'hourly']},
])
})
})
describe('SNS subscription routes', () => {
test('routes subscription lifecycle requests through the Notifications adapter', async () => {
const calls: Array<{operation: string; args: unknown[]}> = []
const adapter = mockAdapter('aws', {
service: 'notifications',
schema: awsNotificationsSchema,
createSnsSubscription: async (...args) => {
calls.push({operation: 'create', args})
return {arn: 'subscription-1', protocol: 'sqs', endpoint: 'arn:aws:sqs:us-east-1:000000000000:orders', status: 'Confirmed', filterPolicy: null}
},
listSnsSubscriptions: async (...args) => {
calls.push({operation: 'list', args})
return []
},
deleteSnsSubscription: async (...args) => {
calls.push({operation: 'delete', args})
},
})
const app = appWithRoutes([adapter])
const create = await app.request('/api/clouds/aws/services/notifications/resources/orders/subscriptions', {
method: 'POST',
headers: {'content-type': 'application/json'},
body: JSON.stringify({protocol: 'sqs', endpoint: 'arn:aws:sqs:us-east-1:000000000000:orders'}),
})
expect(create.status).toBe(201)
await app.request('/api/clouds/aws/services/notifications/resources/orders/subscriptions')
const remove = await app.request('/api/clouds/aws/services/notifications/resources/orders/subscriptions/subscription-1', {method: 'DELETE'})
expect(remove.status).toBe(200)
expect(calls).toEqual([
{operation: 'create', args: ['orders', {protocol: 'sqs', endpoint: 'arn:aws:sqs:us-east-1:000000000000:orders'}]},
{operation: 'list', args: ['orders']},
{operation: 'delete', args: ['subscription-1']},
])
})
})
const databaseSnapshot: DatabaseSnapshot = {
id: 'orders-db-snapshot-1',
name: 'orders-db-snapshot-1',
+97
View File
@@ -620,6 +620,42 @@ export function createCloudRoutes(injectedService?: CloudProxyService) {
})
})
app.get('/:cloud/services/events/resources/:id/rules', async (c) => {
const cloud = c.req.param('cloud') as CloudProvider
if (cloud !== 'aws') return c.json({error: 'Unknown cloud or service'}, 404)
return withRuntime(c, async () => c.json(await svc(c).listEventBridgeRules(cloud, c.req.param('id'))))
})
app.post('/:cloud/services/events/resources/:id/rules', async (c) => {
const cloud = c.req.param('cloud') as CloudProvider
if (cloud !== 'aws') return c.json({error: 'Unknown cloud or service'}, 404)
return withRuntime(c, async () => {
const input = await c.req.json<Record<string, unknown>>()
const rule = await svc(c).createEventBridgeRule(cloud, c.req.param('id'), {
name: typeof input.name === 'string' ? input.name : '',
scheduleExpression: typeof input.scheduleExpression === 'string' ? input.scheduleExpression : undefined,
eventPattern: typeof input.eventPattern === 'string' ? input.eventPattern : undefined,
state: input.state === 'DISABLED' ? 'DISABLED' : input.state === 'ENABLED' ? 'ENABLED' : undefined,
description: typeof input.description === 'string' ? input.description : undefined,
targets: Array.isArray(input.targets) ? input.targets.flatMap((target) => {
if (!target || typeof target !== 'object' || Array.isArray(target)) return []
const value = target as {id?: unknown; arn?: unknown}
return typeof value.id === 'string' && typeof value.arn === 'string' ? [{id: value.id, arn: value.arn}] : []
}) : [],
})
return c.json(rule, 201)
})
})
app.delete('/:cloud/services/events/resources/:id/rules/:ruleName', async (c) => {
const cloud = c.req.param('cloud') as CloudProvider
if (cloud !== 'aws') return c.json({error: 'Unknown cloud or service'}, 404)
return withRuntime(c, async () => {
await svc(c).deleteEventBridgeRule(cloud, c.req.param('id'), c.req.param('ruleName'))
return c.json({ok: true})
})
})
app.get('/:cloud/services/serverless/resources/:id/sqs-event-source-mappings', async (c) => {
const cloud = c.req.param('cloud') as CloudProvider
if (cloud !== 'aws') return c.json({error: 'Unknown cloud or service'}, 404)
@@ -867,6 +903,60 @@ export function createCloudRoutes(injectedService?: CloudProxyService) {
})
})
app.post('/:cloud/services/notifications/resources/:id/publish', async (c) => {
const cloud = c.req.param('cloud') as CloudProvider
if (!isCloudProvider(cloud)) return c.json({error: 'Unknown cloud'}, 404)
return withRuntime(c, async () => {
const body = await jsonBody<{message?: unknown; subject?: unknown; messageAttributes?: unknown}>(c)
if (typeof body.message !== 'string' || !body.message) {
throw new ValidationError('message is required')
}
if (body.subject !== undefined && typeof body.subject !== 'string') {
throw new ValidationError('subject must be a string')
}
if (body.messageAttributes !== undefined && !isStringRecord(body.messageAttributes)) {
throw new ValidationError('messageAttributes must be an object of string values')
}
const result = await svc(c).publishNotification(cloud, c.req.param('id'), {
message: body.message,
...(body.subject ? {subject: body.subject} : {}),
...(body.messageAttributes ? {messageAttributes: body.messageAttributes} : {}),
})
return c.json(result)
})
})
app.get('/:cloud/services/notifications/resources/:id/subscriptions', async (c) => {
const cloud = c.req.param('cloud') as CloudProvider
if (!isCloudProvider(cloud)) return c.json({error: 'Unknown cloud'}, 404)
return withRuntime(c, async () => c.json(await svc(c).listSnsSubscriptions(cloud, c.req.param('id'))))
})
app.post('/:cloud/services/notifications/resources/:id/subscriptions', async (c) => {
const cloud = c.req.param('cloud') as CloudProvider
if (!isCloudProvider(cloud)) return c.json({error: 'Unknown cloud'}, 404)
return withRuntime(c, async () => {
const body = await jsonBody<{protocol?: unknown; endpoint?: unknown}>(c)
if (typeof body.protocol !== 'string' || !body.protocol.trim()) throw new ValidationError('protocol is required')
if (typeof body.endpoint !== 'string' || !body.endpoint.trim()) throw new ValidationError('endpoint is required')
const subscription = await svc(c).createSnsSubscription(cloud, c.req.param('id'), {
protocol: body.protocol,
endpoint: body.endpoint,
})
return c.json(subscription, 201)
})
})
app.delete('/:cloud/services/notifications/resources/:id/subscriptions/:subscriptionArn', async (c) => {
const cloud = c.req.param('cloud') as CloudProvider
if (!isCloudProvider(cloud)) return c.json({error: 'Unknown cloud'}, 404)
return withRuntime(c, async () => {
await svc(c).deleteSnsSubscription(cloud, c.req.param('subscriptionArn'))
return c.json({ok: true})
})
})
app.post('/:cloud/services/kms/resources/:id/encrypt', async (c) => {
const cloud = c.req.param('cloud') as CloudProvider
if (!isCloudProvider(cloud)) return c.json({error: 'Unknown cloud'}, 404)
@@ -984,6 +1074,13 @@ function encryptionContext(value: unknown): Record<string, string> | undefined {
return entries.length === 0 ? undefined : Object.fromEntries(entries) as Record<string, string>
}
function isStringRecord(value: unknown): value is Record<string, string> {
return typeof value === 'object'
&& value !== null
&& !Array.isArray(value)
&& Object.values(value).every((item) => typeof item === 'string')
}
function stringHeaders(value: unknown): Record<string, string> {
if (value === undefined) return {}
if (!value || typeof value !== 'object' || Array.isArray(value)) {
+42 -1
View File
@@ -12,6 +12,7 @@ import type {
Ec2IpPermission,
Ec2KeyPair,
Ec2KeyPairMaterial,
Ec2KeyPairType,
Ec2NatGateway,
Ec2RouteTable,
Ec2SecurityGroup,
@@ -75,7 +76,7 @@ const fakeService = {
getConsoleOutput: async (instanceId: string): Promise<Ec2ConsoleOutput> => ({instanceId, output: 'boot log'}),
listAmis: async (): Promise<Ec2Image[]> => [],
listKeyPairs: async (): Promise<Ec2KeyPair[]> => [],
createKeyPair: async (name: string): Promise<Ec2KeyPairMaterial> => ({keyPairId: 'key-0a1b2c3d4e5f6a7b', keyName: name, keyMaterial: '---PEM---'}),
createKeyPair: async (name: string, _keyType: Ec2KeyPairType = 'rsa'): Promise<Ec2KeyPairMaterial> => ({keyPairId: 'key-0a1b2c3d4e5f6a7b', keyName: name, keyMaterial: '---PEM---'}),
deleteKeyPair: async () => {},
listSecurityGroups: async (): Promise<Ec2SecurityGroup[]> => [],
createSecurityGroup: async () => ({groupId: 'sg-0a1b2c3d4e5f6a7b'}),
@@ -257,6 +258,46 @@ describe('POST /key-pairs', () => {
expect(body.keyName).toBe('my-key')
expect(body.keyMaterial).toBe('---PEM---')
})
test('accepts ed25519 key type', async () => {
let receivedKeyType: Ec2KeyPairType | undefined
const router = createEc2Router({...fakeService, createKeyPair: async (name: string, keyType: Ec2KeyPairType) => {
receivedKeyType = keyType
return {keyPairId: 'key-0a1b2c3d4e5f6a7b', keyName: name, keyMaterial: '---PEM---'}
}})
const res = await router.request('/key-pairs', {
method: 'POST',
headers: {'content-type': 'application/json'},
body: JSON.stringify({name: 'my-ed25519-key', keyType: 'ed25519'}),
})
expect(res.status).toBe(200)
expect(receivedKeyType).toBe('ed25519')
})
test('defaults key type to rsa', async () => {
let receivedKeyType: Ec2KeyPairType | undefined
const router = createEc2Router({...fakeService, createKeyPair: async (name: string, keyType: Ec2KeyPairType) => {
receivedKeyType = keyType
return {keyPairId: 'key-0a1b2c3d4e5f6a7b', keyName: name, keyMaterial: '---PEM---'}
}})
const res = await router.request('/key-pairs', {
method: 'POST',
headers: {'content-type': 'application/json'},
body: JSON.stringify({name: 'my-rsa-key'}),
})
expect(res.status).toBe(200)
expect(receivedKeyType).toBe('rsa')
})
test('rejects unsupported key types', async () => {
const res = await app.request('/key-pairs', {
method: 'POST',
headers: {'content-type': 'application/json'},
body: JSON.stringify({name: 'my-key', keyType: 'dsa'}),
})
expect(res.status).toBe(400)
expect((await res.json() as {error: string}).error).toBe('keyType must be rsa or ed25519')
})
})
describe('GET /internet-gateways', () => {
+11 -2
View File
@@ -4,6 +4,7 @@ import type {
CreateAmiInput,
CreateNatGatewayInput,
CreateRouteInput,
Ec2KeyPairType,
Ec2Tag,
IpPermissionInput,
RunInstanceInput,
@@ -12,6 +13,11 @@ import type {
import {ec2Service} from '../services/ec2'
type Ec2Service = typeof ec2Service
type CreateKeyPairInput = {name: string; keyType?: Ec2KeyPairType}
function isEc2KeyPairType(value: unknown): value is Ec2KeyPairType {
return value === 'rsa' || value === 'ed25519'
}
function handleError(c: Context, error: unknown) {
const msg = error instanceof Error ? error.message : 'Unknown error'
@@ -124,8 +130,11 @@ export function createEc2Router(svc: Ec2Service = ec2Service) {
app.post('/key-pairs', async (c) => {
try {
const {name} = await c.req.json<{name: string}>()
return c.json(await svc.createKeyPair(name))
const {name, keyType = 'rsa'} = await c.req.json<CreateKeyPairInput>()
if (!isEc2KeyPairType(keyType)) {
return c.json({error: 'keyType must be rsa or ed25519'}, 400)
}
return c.json(await svc.createKeyPair(name, keyType))
} catch (error) {
return handleError(c, error)
}