Skip to content

Commit 42a74af

Browse files
committed
refactor(credentials): share the owner-scoped service-account secret read
Oracle Database's saved-connection lookup now reads its row through one owner-scoped helper, so the saved database connections that follow bind to their owner and provider the same way instead of copying the query.
1 parent bcadf23 commit 42a74af

2 files changed

Lines changed: 86 additions & 48 deletions

File tree

‎apps/sim/lib/credentials/oracledb-service-account.server.ts‎

Lines changed: 15 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { db } from '@sim/db'
2-
import { credential } from '@sim/db/schema'
3-
import { and, eq, isNull, or } from 'drizzle-orm'
41
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'
67
import { executeOracleStatements } from '@/lib/internal/oracledb/client'
78
import { oracleConnectionInputSchema } from '@/lib/internal/oracledb/schema'
89
import { ORACLE_DATABASE_SERVICE_ACCOUNT_PROVIDER_ID } from '@/lib/oauth/types'
@@ -46,55 +47,21 @@ export async function verifyAndEncryptOracleDatabaseCredential(serialized: strin
4647
/** Loads only an executor-authorized reference bound to its trusted owner scope and provider. */
4748
export async function getOracleDatabaseCredential(
4849
credentialId: string,
49-
scope: { workspaceId?: string; organizationId?: string }
50+
scope: CredentialOwnerScope
5051
) {
5152
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
7859
)
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 {
9561
throw unavailable()
62+
}
63+
if (decrypted === null) throw unavailable()
9664
try {
97-
const { decrypted } = await decryptSecret(row.encryptedServiceAccountKey)
9865
return secretSchema.parse(JSON.parse(decrypted)).connection
9966
} catch {
10067
throw unavailable()
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { db } from '@sim/db'
2+
import { credential } from '@sim/db/schema'
3+
import { and, eq, isNull, or } from 'drizzle-orm'
4+
import { decryptSecret } from '@/lib/core/security/encryption'
5+
6+
/** The workspace or organization an executor-authorized credential must belong to. */
7+
export interface CredentialOwnerScope {
8+
workspaceId?: string
9+
organizationId?: string
10+
}
11+
12+
/**
13+
* Decrypts a live service-account credential of `providerId` owned by exactly
14+
* `scope`: a workspace credential when the workspace matches, or an
15+
* organization credential when the organization matches. Returns `null` for
16+
* anything else (no scope, another owner, another provider, revoked, or no
17+
* stored secret) without decrypting, so callers fail closed with their own
18+
* provider-specific message.
19+
*/
20+
export async function readOwnerScopedServiceAccountSecret(
21+
credentialId: string,
22+
providerId: string,
23+
scope: CredentialOwnerScope
24+
): Promise<string | null> {
25+
if (!credentialId || (!scope.workspaceId && !scope.organizationId)) return null
26+
const [row] = await db
27+
.select({
28+
workspaceId: credential.workspaceId,
29+
organizationId: credential.organizationId,
30+
type: credential.type,
31+
providerId: credential.providerId,
32+
revokedAt: credential.revokedAt,
33+
encryptedServiceAccountKey: credential.encryptedServiceAccountKey,
34+
})
35+
.from(credential)
36+
.where(
37+
and(
38+
eq(credential.id, credentialId),
39+
or(
40+
scope.workspaceId
41+
? and(eq(credential.workspaceId, scope.workspaceId), isNull(credential.organizationId))
42+
: undefined,
43+
scope.organizationId
44+
? and(
45+
eq(credential.organizationId, scope.organizationId),
46+
isNull(credential.workspaceId)
47+
)
48+
: undefined
49+
)
50+
)
51+
)
52+
.limit(1)
53+
const ownerMatches =
54+
row &&
55+
((row.workspaceId === scope.workspaceId &&
56+
!!scope.workspaceId &&
57+
row.organizationId === null) ||
58+
(row.organizationId === scope.organizationId &&
59+
!!scope.organizationId &&
60+
row.workspaceId === null))
61+
if (
62+
!ownerMatches ||
63+
row.type !== 'service_account' ||
64+
row.providerId !== providerId ||
65+
row.revokedAt !== null ||
66+
!row.encryptedServiceAccountKey
67+
)
68+
return null
69+
const { decrypted } = await decryptSecret(row.encryptedServiceAccountKey)
70+
return decrypted
71+
}

0 commit comments

Comments
 (0)