Skip to content

Commit dac87b7

Browse files
committed
fix(credentials): tell an interrupted Databricks token reply apart from an oversized one
1 parent e7bfe31 commit dac87b7

2 files changed

Lines changed: 27 additions & 5 deletions

File tree

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,23 @@ describe('getting a Databricks token for a run', () => {
198198
})
199199
})
200200

201+
it('reports a token reply cut off mid-body as the provider being unavailable, not a bad credential', async () => {
202+
const body = new ReadableStream<Uint8Array>({
203+
start(controller) {
204+
controller.enqueue(new TextEncoder().encode('{"error":"invalid_cl'))
205+
controller.error(new Error('socket hang up'))
206+
},
207+
})
208+
fetch.mockResolvedValue(
209+
new Response(body, { status: 401, headers: { 'Content-Type': 'application/json' } })
210+
)
211+
212+
await expect(resolveDatabricksCredentialToken('cred-1')).rejects.toMatchObject({
213+
code: 'provider_unavailable',
214+
logDetail: { reason: 'Databricks token response could not be read' },
215+
})
216+
})
217+
201218
it('reports a token reply without a token as the provider misbehaving, not a bad credential', async () => {
202219
fetch.mockResolvedValue(jsonResponse({}, 200))
203220

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { eq } from 'drizzle-orm'
55
import { z } from 'zod'
66
import { decryptSecret, encryptSecret } from '@/lib/core/security/encryption'
77
import { validateDatabricksWorkspaceHost } from '@/lib/core/security/input-validation'
8-
import { readResponseJsonWithLimit } from '@/lib/core/utils/stream-limits'
8+
import { isPayloadSizeLimitError, readResponseJsonWithLimit } from '@/lib/core/utils/stream-limits'
99
import { parseServiceAccountConfig } from '@/lib/credentials/service-account-config'
1010
import {
1111
providerFailureCode,
@@ -151,10 +151,15 @@ async function requestWorkspaceToken(
151151
label: 'Databricks token response',
152152
}).catch((error: unknown) => {
153153
if (error instanceof SyntaxError) return {}
154-
throw new DatabricksCredentialError(
155-
'Databricks token response exceeded the size limit',
156-
response.status
157-
)
154+
signal?.throwIfAborted()
155+
if (isPayloadSizeLimitError(error)) {
156+
throw new DatabricksCredentialError(
157+
'Databricks token response exceeded the size limit',
158+
response.status
159+
)
160+
}
161+
// An interrupted body says nothing about the credential, whatever the status line was.
162+
throw new DatabricksCredentialError('Databricks token response could not be read', 502)
158163
})) as {
159164
access_token?: unknown
160165
error_description?: unknown

0 commit comments

Comments
 (0)