d25d482dc2
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (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 / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
67 lines
2.3 KiB
TypeScript
67 lines
2.3 KiB
TypeScript
import { db } from '@sim/db'
|
|
import { account } from '@sim/db/schema'
|
|
import { createLogger } from '@sim/logger'
|
|
import { eq } from 'drizzle-orm'
|
|
import { getCredentialActorContext } from '@/lib/credentials/access'
|
|
import { getServiceAccountToken, refreshTokenIfNeeded } from '@/app/api/auth/oauth/utils'
|
|
|
|
const logger = createLogger('VertexCredential')
|
|
|
|
/**
|
|
* Resolves a Vertex AI OAuth credential to an access token.
|
|
* Shared across agent, evaluator, and router handlers. Authorizes the executing
|
|
* user against the credential first — workspace credentials are usable by their
|
|
* members and by derived workspace admins, matching `authorizeCredentialUse`.
|
|
*/
|
|
export async function resolveVertexCredential(
|
|
credentialId: string,
|
|
actingUserId: string | undefined,
|
|
callerLabel = 'vertex'
|
|
): Promise<string> {
|
|
const requestId = `${callerLabel}-${Date.now()}`
|
|
|
|
logger.info(`[${requestId}] Resolving Vertex AI credential: ${credentialId}`)
|
|
|
|
if (!actingUserId) {
|
|
throw new Error('Vertex AI credential use requires an authenticated user')
|
|
}
|
|
|
|
const access = await getCredentialActorContext(credentialId, actingUserId)
|
|
const cred = access.credential
|
|
if (!cred) {
|
|
throw new Error(`Vertex AI credential not found: ${credentialId}`)
|
|
}
|
|
if (!access.hasWorkspaceAccess || (!access.member && !access.isAdmin)) {
|
|
throw new Error('Not authorized to use this Vertex AI credential')
|
|
}
|
|
|
|
if (cred.type === 'service_account') {
|
|
const accessToken = await getServiceAccountToken(cred.id, [
|
|
'https://www.googleapis.com/auth/cloud-platform',
|
|
])
|
|
logger.info(`[${requestId}] Successfully resolved Vertex AI service account credential`)
|
|
return accessToken
|
|
}
|
|
|
|
if (cred.type !== 'oauth' || !cred.accountId) {
|
|
throw new Error(`Vertex AI credential is not a valid OAuth credential: ${credentialId}`)
|
|
}
|
|
|
|
const accountRow = await db.query.account.findFirst({
|
|
where: eq(account.id, cred.accountId),
|
|
})
|
|
|
|
if (!accountRow) {
|
|
throw new Error(`Vertex AI credential not found: ${credentialId}`)
|
|
}
|
|
|
|
const { accessToken } = await refreshTokenIfNeeded(requestId, accountRow, cred.accountId)
|
|
|
|
if (!accessToken) {
|
|
throw new Error('Failed to get Vertex AI access token')
|
|
}
|
|
|
|
logger.info(`[${requestId}] Successfully resolved Vertex AI credential`)
|
|
return accessToken
|
|
}
|