-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathauth-m2m.test.ts
More file actions
232 lines (206 loc) · 8.78 KB
/
Copy pathauth-m2m.test.ts
File metadata and controls
232 lines (206 loc) · 8.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// Copyright (c) 2026 Databricks, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import { expect } from 'chai';
import expectNativeConnectionOptions from './_helpers/nativeOptions';
import KernelBackend from '../../../lib/kernel/KernelBackend';
import { buildKernelConnectionOptions } from '../../../lib/kernel/KernelAuth';
import { ConnectionOptions } from '../../../lib/contracts/IDBSQLClient';
import AuthenticationError from '../../../lib/errors/AuthenticationError';
import HiveDriverError from '../../../lib/errors/HiveDriverError';
import { makeFakeBinding, makeFakeContext } from './_helpers/fakeBinding';
describe('KernelAuth + KernelBackend — OAuth M2M auth flow', () => {
describe('buildKernelConnectionOptions', () => {
it('accepts databricks-oauth + oauthClientId + oauthClientSecret', () => {
const opts: ConnectionOptions = {
host: 'example.cloud.databricks.com',
path: '/sql/1.0/warehouses/abc',
authType: 'databricks-oauth',
oauthClientId: 'client-uuid',
oauthClientSecret: 'dose-fake-secret',
};
const native = buildKernelConnectionOptions(opts);
expectNativeConnectionOptions(native, {
hostName: 'example.cloud.databricks.com',
httpPath: '/sql/1.0/warehouses/abc',
intervalsAsString: true,
authMode: 'OAuthM2m',
oauthClientId: 'client-uuid',
oauthClientSecret: 'dose-fake-secret',
oauthScopes: ['all-apis'],
});
});
it('defaults M2M oauthScopes to all-apis (Thrift + kernel parity)', () => {
const native = buildKernelConnectionOptions({
host: 'example.cloud.databricks.com',
path: '/sql/1.0/warehouses/abc',
authType: 'databricks-oauth',
oauthClientId: 'client-uuid',
oauthClientSecret: 'dose-fake-secret',
});
expect(native.authMode).to.equal('OAuthM2m');
expect((native as { oauthScopes?: string[] }).oauthScopes).to.deep.equal(['all-apis']);
});
it('honors a caller-supplied M2M oauthScopes override (parity with pyo3)', () => {
const native = buildKernelConnectionOptions({
host: 'example.cloud.databricks.com',
path: '/sql/1.0/warehouses/abc',
authType: 'databricks-oauth',
oauthClientId: 'client-uuid',
oauthClientSecret: 'dose-fake-secret',
oauthScopes: ['sql', 'offline_access'],
} as ConnectionOptions);
expect((native as { oauthScopes?: string[] }).oauthScopes).to.deep.equal(['sql', 'offline_access']);
});
it('prepends `/` to the path on the M2M branch too', () => {
const opts: ConnectionOptions = {
host: 'example.cloud.databricks.com',
path: 'sql/1.0/warehouses/abc',
authType: 'databricks-oauth',
oauthClientId: 'client-uuid',
oauthClientSecret: 'dose-fake-secret',
};
const native = buildKernelConnectionOptions(opts);
expect(native.httpPath).to.equal('/sql/1.0/warehouses/abc');
});
it('rejects missing oauthClientId with AuthenticationError', () => {
const opts = {
host: 'example.cloud.databricks.com',
path: '/sql/1.0/warehouses/abc',
authType: 'databricks-oauth',
oauthClientSecret: 'dose-fake-secret',
} as unknown as ConnectionOptions;
expect(() => buildKernelConnectionOptions(opts)).to.throw(AuthenticationError, /oauthClientId.*required/);
});
it('rejects empty oauthClientId with AuthenticationError', () => {
const opts = {
host: 'example.cloud.databricks.com',
path: '/sql/1.0/warehouses/abc',
authType: 'databricks-oauth',
oauthClientId: '',
oauthClientSecret: 'dose-fake-secret',
} as unknown as ConnectionOptions;
expect(() => buildKernelConnectionOptions(opts)).to.throw(AuthenticationError, /oauthClientId.*required/);
});
it('rejects empty oauthClientSecret with AuthenticationError when oauthClientId is set (M2M intent)', () => {
const opts: ConnectionOptions = {
host: 'example.cloud.databricks.com',
path: '/sql/1.0/warehouses/abc',
authType: 'databricks-oauth',
oauthClientId: 'client-uuid',
oauthClientSecret: '',
};
// Presence of `oauthClientId` signals M2M intent; an empty secret
// is a typo/missing-env, not a request to fall back to U2M.
// Surface the M2M "secret required" error so the user knows the
// real problem instead of getting routed to a different flow.
expect(() => buildKernelConnectionOptions(opts)).to.throw(
AuthenticationError,
/oauthClientSecret.*non-empty.*OAuth M2M/,
);
});
it('rejects azureTenantId with a clear Entra-direct-out-of-scope error', () => {
const opts: ConnectionOptions = {
host: 'adb-12345.0.azuredatabricks.net',
path: '/sql/1.0/warehouses/abc',
authType: 'databricks-oauth',
oauthClientId: 'client-uuid',
oauthClientSecret: 'dose-fake-secret',
azureTenantId: 'tenant-uuid',
};
expect(() => buildKernelConnectionOptions(opts)).to.throw(
HiveDriverError,
/Azure-direct OAuth.*is not supported/,
);
});
it('rejects useDatabricksOAuthInAzure with the same Entra-direct error', () => {
const opts: ConnectionOptions = {
host: 'adb-12345.0.azuredatabricks.net',
path: '/sql/1.0/warehouses/abc',
authType: 'databricks-oauth',
oauthClientId: 'client-uuid',
oauthClientSecret: 'dose-fake-secret',
useDatabricksOAuthInAzure: true,
};
expect(() => buildKernelConnectionOptions(opts)).to.throw(
HiveDriverError,
/Azure-direct OAuth.*is not supported/,
);
});
it('rejects a `persistence` hook on M2M (no cache needed)', () => {
const opts: ConnectionOptions = {
host: 'example.cloud.databricks.com',
path: '/sql/1.0/warehouses/abc',
authType: 'databricks-oauth',
oauthClientId: 'client-uuid',
oauthClientSecret: 'dose-fake-secret',
persistence: {
read: async () => undefined,
persist: async () => undefined,
},
};
expect(() => buildKernelConnectionOptions(opts)).to.throw(
HiveDriverError,
/`persistence` is not supported on OAuth M2M/,
);
});
});
describe('KernelBackend.connect + openSession (M2M)', () => {
it('round-trips M2M options through to the napi binding', async () => {
const { binding, calls } = makeFakeBinding();
const backend = new KernelBackend({ nativeBinding: binding, context: makeFakeContext() });
await backend.connect({
host: 'example.cloud.databricks.com',
path: '/sql/1.0/warehouses/abc',
authType: 'databricks-oauth',
oauthClientId: 'client-uuid',
oauthClientSecret: 'dose-fake-secret',
});
const session = await backend.openSession({});
// Post-integration: KernelSessionBackend generates UUIDv4 ids; the
// earlier auth-only counter-id scheme was superseded.
expect(session.id).to.match(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i);
expect(calls).to.have.lengthOf(1);
expect(calls[0].method).to.equal('openSession');
expectNativeConnectionOptions(calls[0].args[0], {
hostName: 'example.cloud.databricks.com',
httpPath: '/sql/1.0/warehouses/abc',
intervalsAsString: true,
authMode: 'OAuthM2m',
oauthClientId: 'client-uuid',
oauthClientSecret: 'dose-fake-secret',
oauthScopes: ['all-apis'],
});
await session.close();
await backend.close();
});
it('rejects connect() for missing oauthClientId before touching the binding', async () => {
const { binding, calls } = makeFakeBinding();
const backend = new KernelBackend({ nativeBinding: binding, context: makeFakeContext() });
let caught: unknown;
try {
await backend.connect({
host: 'example.cloud.databricks.com',
path: '/sql/1.0/warehouses/abc',
authType: 'databricks-oauth',
// eslint-disable-next-line @typescript-eslint/no-explicit-any
oauthClientSecret: 'dose-fake-secret',
} as any);
} catch (e) {
caught = e;
}
expect(caught).to.be.instanceOf(AuthenticationError);
expect(calls).to.have.lengthOf(0);
});
});
});