|
1 | | -import { db } from '@sim/db' |
2 | | -import { credential } from '@sim/db/schema' |
3 | | -import { and, eq, isNull, or } from 'drizzle-orm' |
4 | 1 | import { z } from 'zod' |
5 | | -import { decryptSecret, encryptSecret } from '@/lib/core/security/encryption' |
| 2 | +import { encryptSecret } from '@/lib/core/security/encryption' |
| 3 | +import { |
| 4 | + type CredentialOwnerScope, |
| 5 | + readOwnerScopedServiceAccountSecret, |
| 6 | +} from '@/lib/credentials/owner-scoped-secret.server' |
6 | 7 | import { executeOracleStatements } from '@/lib/internal/oracledb/client' |
7 | 8 | import { oracleConnectionInputSchema } from '@/lib/internal/oracledb/schema' |
8 | 9 | import { ORACLE_DATABASE_SERVICE_ACCOUNT_PROVIDER_ID } from '@/lib/oauth/types' |
@@ -46,55 +47,21 @@ export async function verifyAndEncryptOracleDatabaseCredential(serialized: strin |
46 | 47 | /** Loads only an executor-authorized reference bound to its trusted owner scope and provider. */ |
47 | 48 | export async function getOracleDatabaseCredential( |
48 | 49 | credentialId: string, |
49 | | - scope: { workspaceId?: string; organizationId?: string } |
| 50 | + scope: CredentialOwnerScope |
50 | 51 | ) { |
51 | 52 | const unavailable = () => new Error('Oracle Database credential is unavailable') |
52 | | - if (!credentialId || (!scope.workspaceId && !scope.organizationId)) throw unavailable() |
53 | | - const [row] = await db |
54 | | - .select({ |
55 | | - workspaceId: credential.workspaceId, |
56 | | - organizationId: credential.organizationId, |
57 | | - type: credential.type, |
58 | | - providerId: credential.providerId, |
59 | | - revokedAt: credential.revokedAt, |
60 | | - encryptedServiceAccountKey: credential.encryptedServiceAccountKey, |
61 | | - }) |
62 | | - .from(credential) |
63 | | - .where( |
64 | | - and( |
65 | | - eq(credential.id, credentialId), |
66 | | - or( |
67 | | - scope.workspaceId |
68 | | - ? and(eq(credential.workspaceId, scope.workspaceId), isNull(credential.organizationId)) |
69 | | - : undefined, |
70 | | - scope.organizationId |
71 | | - ? and( |
72 | | - eq(credential.organizationId, scope.organizationId), |
73 | | - isNull(credential.workspaceId) |
74 | | - ) |
75 | | - : undefined |
76 | | - ) |
77 | | - ) |
| 53 | + let decrypted: string | null |
| 54 | + try { |
| 55 | + decrypted = await readOwnerScopedServiceAccountSecret( |
| 56 | + credentialId, |
| 57 | + ORACLE_DATABASE_SERVICE_ACCOUNT_PROVIDER_ID, |
| 58 | + scope |
78 | 59 | ) |
79 | | - .limit(1) |
80 | | - const ownerMatches = |
81 | | - row && |
82 | | - ((row.workspaceId === scope.workspaceId && |
83 | | - !!scope.workspaceId && |
84 | | - row.organizationId === null) || |
85 | | - (row.organizationId === scope.organizationId && |
86 | | - !!scope.organizationId && |
87 | | - row.workspaceId === null)) |
88 | | - if ( |
89 | | - !ownerMatches || |
90 | | - row.type !== 'service_account' || |
91 | | - row.providerId !== ORACLE_DATABASE_SERVICE_ACCOUNT_PROVIDER_ID || |
92 | | - row.revokedAt !== null || |
93 | | - !row.encryptedServiceAccountKey |
94 | | - ) |
| 60 | + } catch { |
95 | 61 | throw unavailable() |
| 62 | + } |
| 63 | + if (decrypted === null) throw unavailable() |
96 | 64 | try { |
97 | | - const { decrypted } = await decryptSecret(row.encryptedServiceAccountKey) |
98 | 65 | return secretSchema.parse(JSON.parse(decrypted)).connection |
99 | 66 | } catch { |
100 | 67 | throw unavailable() |
|
0 commit comments