Skip to content

Commit ca96b47

Browse files
authored
fix hostname validation (#317)
1 parent c6017fe commit ca96b47

File tree

2 files changed

+23
-17
lines changed

2 files changed

+23
-17
lines changed

packages/databricks-vscode/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@
524524
"package": "vsce package",
525525
"package:cli:fetch": "BRICKS_VERSION=v0.0.12 && bash ./scripts/fetch-bricks-cli.sh ${BRICKS_VERSION} ${BRICKS_ARCH:-}",
526526
"package:cli:link": "rm -f ./bin/bricks && ln -s ../../../../bricks/bricks bin",
527-
"package:compile": "yarn run esbuild:base --minify",
527+
"package:compile": "yarn run esbuild:base",
528528
"package:copy-webview-toolkit": "cp ./node_modules/@vscode/webview-ui-toolkit/dist/toolkit.js ./out/toolkit.js",
529529
"esbuild:base": "esbuild ./src/extension.ts --bundle --outfile=out/extension.js --external:vscode --format=cjs --platform=node --sourcemap --target=es2019",
530530
"build": "tsc",

packages/databricks-vscode/src/configuration/configureWorkspaceWizard.ts

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ interface AuthTypeQuickPickItem extends QuickPickItem {
1616
}
1717

1818
interface State {
19-
host: string;
19+
host: URL;
2020
authType: AuthType;
2121
profile?: string;
2222
token?: string;
@@ -48,7 +48,7 @@ export async function configureWorkspaceWizard(
4848
},
4949
});
5050

51-
state.host = host;
51+
state.host = normalizeHost(host);
5252
return (input: MultiStepInput) => selectAuthMethod(input, state);
5353
}
5454

@@ -59,7 +59,7 @@ export async function configureWorkspaceWizard(
5959
const items: Array<AuthTypeQuickPickItem> = [];
6060
let profiles: Profiles = {};
6161

62-
for (const authMethod of authMethodsForHostname(new URL(state.host!))) {
62+
for (const authMethod of authMethodsForHostname(state.host!)) {
6363
switch (authMethod) {
6464
case "azure-cli":
6565
items.push({
@@ -108,8 +108,7 @@ export async function configureWorkspaceWizard(
108108
.filter(
109109
(label) =>
110110
(profiles[label] as Profile).host
111-
.hostname ===
112-
new URL(state.host!).hostname
111+
.hostname === state.host!.hostname
113112
)
114113
.map((label) => ({
115114
label,
@@ -195,36 +194,43 @@ export async function configureWorkspaceWizard(
195194
return;
196195
}
197196

198-
state.host = `https://${new URL(state.host).hostname}`;
199-
200197
return {
201198
authProvider: AuthProvider.fromJSON(state),
202199
};
203200
}
204201

205-
async function validateDatabricksHost(
206-
host: string
207-
): Promise<string | undefined> {
208-
let url;
202+
function normalizeHost(host: string): URL {
203+
let url: URL;
209204

210-
if (!host.startsWith("https://")) {
205+
if (!host.startsWith("http")) {
211206
host = `https://${host}`;
212207
}
213-
214208
try {
215209
url = new URL(host);
216210
} catch (e) {
217-
return "Invalid host name";
211+
throw new Error("Invalid host name");
218212
}
219213
if (url.protocol !== "https:") {
220-
return "Invalid protocol";
214+
throw new Error("Invalid protocol");
221215
}
222216
if (
223217
!url.hostname.match(
224218
/(\.azuredatabricks\.net|\.gcp\.databricks\.com|\.cloud\.databricks\.com)$/
225219
)
226220
) {
227-
return "Not a Databricks host";
221+
throw new Error("Not a Databricks host");
222+
}
223+
224+
return new URL(`https://${url.hostname}`);
225+
}
226+
227+
async function validateDatabricksHost(
228+
host: string
229+
): Promise<string | undefined> {
230+
try {
231+
normalizeHost(host);
232+
} catch (e: any) {
233+
return e.message;
228234
}
229235
}
230236

0 commit comments

Comments
 (0)