6bf8bebf51
CI / Test and Build (push) Failing after 1s
CI / Migrate Dev DB (push) Has been skipped
CI / Migrate DB (push) Has been skipped
CodeQL / Analyze actions (push) Has been cancelled
CodeQL / Analyze javascript-typescript (push) Has been cancelled
CI / Detect Version (push) Has been cancelled
CI / Detect Desktop Changes (push) Has been cancelled
CI / Build AMD64 (blacksmith-2vcpu-ubuntu-2404, ./docker/cron.Dockerfile, ubuntu-latest, ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Build AMD64 (blacksmith-2vcpu-ubuntu-2404, ./docker/db.Dockerfile, ECR_MIGRATIONS, ubuntu-latest, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (blacksmith-4vcpu-ubuntu-2404, ./docker/pii.Dockerfile, ECR_PII, ubuntu-latest, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (blacksmith-4vcpu-ubuntu-2404, ./docker/realtime.Dockerfile, ECR_REALTIME, ubuntu-latest, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build AMD64 (blacksmith-8vcpu-ubuntu-2404, ./docker/app.Dockerfile, ECR_APP, linux-x64-8-core, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/cron.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/db.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/pii.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/realtime.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-8vcpu-ubuntu-2404-arm, ./docker/app.Dockerfile, linux-arm64-8-core, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
Helm Chart / Lint, test, and validate chart (push) Has been cancelled
Helm Chart / Chart version bumped (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
CI / Build Dev ECR (blacksmith-8vcpu-ubuntu-2404, ./docker/app.Dockerfile, ECR_APP, linux-x64-8-core) (push) Has been cancelled
CI / Promote Images (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-2vcpu-ubuntu-2404, ./docker/db.Dockerfile, ECR_MIGRATIONS, ubuntu-latest) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-4vcpu-ubuntu-2404, ./docker/pii.Dockerfile, ECR_PII, ubuntu-latest) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-4vcpu-ubuntu-2404, ./docker/realtime.Dockerfile, ECR_REALTIME, ubuntu-latest) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Check Desktop Signing Secrets (push) Has been cancelled
CI / Desktop Release (push) Has been cancelled
CI / Create Desktop Prerelease (push) Has been cancelled
CI / Desktop Prerelease Build (push) Has been cancelled
CI / Publish Desktop Prerelease (push) Has been cancelled
CI / Prune Desktop Prereleases (push) Has been cancelled
Helm Chart / Install on kind and run helm test (push) Has been cancelled
151 lines
5.0 KiB
TypeScript
151 lines
5.0 KiB
TypeScript
import { AuditAction, AuditResourceType, recordAudit } from '@sim/audit'
|
|
import { db } from '@sim/db'
|
|
import { environment } from '@sim/db/schema'
|
|
import { createLogger } from '@sim/logger'
|
|
import { toError } from '@sim/utils/errors'
|
|
import { generateId } from '@sim/utils/id'
|
|
import { eq } from 'drizzle-orm'
|
|
import { type NextRequest, NextResponse } from 'next/server'
|
|
import { savePersonalEnvironmentContract } from '@/lib/api/contracts/environment'
|
|
import { parseRequest } from '@/lib/api/server'
|
|
import { getSession } from '@/lib/auth'
|
|
import { decryptSecret, encryptSecret } from '@/lib/core/security/encryption'
|
|
import { generateRequestId } from '@/lib/core/utils/request'
|
|
import { withRouteHandler } from '@/lib/core/utils/with-route-handler'
|
|
import { syncPersonalEnvCredentialsForUser } from '@/lib/credentials/environment'
|
|
import type { EnvironmentVariable } from '@/lib/environment/api'
|
|
import { captureServerEvent } from '@/lib/posthog/server'
|
|
|
|
const logger = createLogger('EnvironmentAPI')
|
|
|
|
export const POST = withRouteHandler(async (req: NextRequest) => {
|
|
const requestId = generateRequestId()
|
|
|
|
try {
|
|
const session = await getSession()
|
|
if (!session?.user?.id) {
|
|
logger.warn(`[${requestId}] Unauthorized environment variables update attempt`)
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
|
|
const parsed = await parseRequest(
|
|
savePersonalEnvironmentContract,
|
|
req,
|
|
{},
|
|
{
|
|
validationErrorResponse: (error) => {
|
|
logger.warn(`[${requestId}] Invalid environment variables data`, { errors: error.issues })
|
|
return NextResponse.json(
|
|
{ error: 'Invalid request data', details: error.issues },
|
|
{ status: 400 }
|
|
)
|
|
},
|
|
}
|
|
)
|
|
if (!parsed.success) return parsed.response
|
|
|
|
const { variables } = parsed.data.body
|
|
|
|
const encryptedVariables = await Promise.all(
|
|
Object.entries(variables).map(async ([key, value]) => {
|
|
const { encrypted } = await encryptSecret(value)
|
|
return [key, encrypted] as const
|
|
})
|
|
).then((entries) => Object.fromEntries(entries))
|
|
|
|
await db
|
|
.insert(environment)
|
|
.values({
|
|
id: generateId(),
|
|
userId: session.user.id,
|
|
variables: encryptedVariables,
|
|
updatedAt: new Date(),
|
|
})
|
|
.onConflictDoUpdate({
|
|
target: [environment.userId],
|
|
set: {
|
|
variables: encryptedVariables,
|
|
updatedAt: new Date(),
|
|
},
|
|
})
|
|
|
|
await syncPersonalEnvCredentialsForUser({
|
|
userId: session.user.id,
|
|
envKeys: Object.keys(variables),
|
|
})
|
|
|
|
recordAudit({
|
|
actorId: session.user.id,
|
|
actorName: session.user.name,
|
|
actorEmail: session.user.email,
|
|
action: AuditAction.ENVIRONMENT_UPDATED,
|
|
resourceType: AuditResourceType.ENVIRONMENT,
|
|
resourceId: session.user.id,
|
|
description: `Updated ${Object.keys(variables).length} personal environment variable(s)`,
|
|
metadata: {
|
|
variableCount: Object.keys(variables).length,
|
|
updatedKeys: Object.keys(variables),
|
|
scope: 'personal',
|
|
},
|
|
request: req,
|
|
})
|
|
|
|
captureServerEvent(session.user.id, 'environment_updated', {
|
|
key_count: Object.keys(variables).length,
|
|
scope: 'personal',
|
|
})
|
|
|
|
return NextResponse.json({ success: true })
|
|
} catch (error) {
|
|
logger.error(`[${requestId}] Error updating environment variables`, error)
|
|
return NextResponse.json({ error: 'Failed to update environment variables' }, { status: 500 })
|
|
}
|
|
})
|
|
|
|
export const GET = withRouteHandler(async (request: Request) => {
|
|
const requestId = generateRequestId()
|
|
|
|
try {
|
|
const session = await getSession()
|
|
if (!session?.user?.id) {
|
|
logger.warn(`[${requestId}] Unauthorized environment variables access attempt`)
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
|
|
const userId = session.user.id
|
|
|
|
const result = await db
|
|
.select()
|
|
.from(environment)
|
|
.where(eq(environment.userId, userId))
|
|
.limit(1)
|
|
|
|
if (!result.length || !result[0].variables) {
|
|
return NextResponse.json({ data: {} }, { status: 200 })
|
|
}
|
|
|
|
const encryptedVariables = result[0].variables as Record<string, string>
|
|
|
|
const decryptedEntries = await Promise.all(
|
|
Object.entries(encryptedVariables).map(async ([key, encryptedValue]) => {
|
|
try {
|
|
const { decrypted } = await decryptSecret(encryptedValue)
|
|
return [key, { key, value: decrypted }] as const
|
|
} catch (error) {
|
|
logger.error(`[${requestId}] Error decrypting variable ${key}`, error)
|
|
return [key, { key, value: '' }] as const
|
|
}
|
|
})
|
|
)
|
|
const decryptedVariables = Object.fromEntries(decryptedEntries) as Record<
|
|
string,
|
|
EnvironmentVariable
|
|
>
|
|
|
|
return NextResponse.json({ data: decryptedVariables }, { status: 200 })
|
|
} catch (error) {
|
|
logger.error(`[${requestId}] Environment fetch error`, error)
|
|
return NextResponse.json({ error: toError(error).message }, { status: 500 })
|
|
}
|
|
})
|