Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export SPARK_REMOTE env var for profile authentication type #825

Merged
merged 2 commits into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,10 @@ export class DatabricksEnvFileManager implements Disposable {

const data = Object.entries({
...(this.getDatabrickseEnvVars() || {}),
...(EnvVarGenerators.getDbConnectEnvVars(
...((await EnvVarGenerators.getDbConnectEnvVars(
this.connectionManager,
this.workspacePath
) || {}),
)) || {}),
...this.getIdeEnvVars(),
...((await this.getUserEnvVars()) || {}),
...(await this.getNotebookEnvVars()),
Expand Down Expand Up @@ -219,10 +219,10 @@ export class DatabricksEnvFileManager implements Disposable {
Object.entries({
...(this.getDatabrickseEnvVars() || {}),
...this.getIdeEnvVars(),
...(EnvVarGenerators.getDbConnectEnvVars(
...((await EnvVarGenerators.getDbConnectEnvVars(
this.connectionManager,
this.workspacePath
) || {}),
)) || {}),
...(await this.getNotebookEnvVars()),
}).forEach(([key, value]) => {
if (value === undefined) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,10 @@ export class NotebookInitScriptManager implements Disposable {
...(EnvVarGenerators.getCommonDatabricksEnvVars(
this.connectionManager
) ?? {}),
...(EnvVarGenerators.getDbConnectEnvVars(
...((await EnvVarGenerators.getDbConnectEnvVars(
this.connectionManager,
this.workspacePath
) ?? {}),
)) ?? {}),
...(EnvVarGenerators.getIdeEnvVars() ?? {}),
...((await this.getUserEnvVars()) ?? {}),
};
Expand Down
34 changes: 31 additions & 3 deletions packages/databricks-vscode/src/utils/envVarGenerators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,44 @@ export function getCommonDatabricksEnvVars(
/* eslint-enable @typescript-eslint/naming-convention */
}

export function getDbConnectEnvVars(
async function getPatToken(connectionManager: ConnectionManager) {
const headers: Record<string, string> = {};
await connectionManager.workspaceClient?.apiClient.config.authenticate(
headers
);
return headers["Authorization"]?.split(" ")[1];
}

async function getSparkRemoteEnvVar(connectionManager: ConnectionManager) {
const host = connectionManager.databricksWorkspace?.host.authority;
const authType =
connectionManager.databricksWorkspace?.authProvider.authType;

// We export spark remote only for profile auth type. This is to support
// SparkSession builder in oss spark connect (and also dbconnect).
// For all other auth types, we don't export spark remote and expect users
// to use DatabricksSession for full functionality.
if (host && connectionManager.cluster && authType === "profile") {
const pat = await getPatToken(connectionManager);
if (pat) {
return {
// eslint-disable-next-line @typescript-eslint/naming-convention
SPARK_REMOTE: `sc://${host}:443/;token=${pat};use_ssl=true;x-databricks-cluster-id=${connectionManager.cluster.id}`,
};
}
}
}

export async function getDbConnectEnvVars(
connectionManager: ConnectionManager,
workspacePath: Uri
) {
const userAgent = getUserAgent(connectionManager);

/* eslint-disable @typescript-eslint/naming-convention */
return {
SPARK_CONNECT_USER_AGENT: userAgent,
DATABRICKS_PROJECT_ROOT: workspacePath.fsPath,
...((await getSparkRemoteEnvVar(connectionManager)) || {}),
};
/* eslint-enable @typescript-eslint/naming-convention */
}
Expand All @@ -114,7 +142,7 @@ export function getProxyEnvVars() {
}

export function removeUndefinedKeys<
T extends Record<string, string | undefined>
T extends Record<string, string | undefined>,
>(envVarMap?: T): T | undefined {
if (envVarMap === undefined) {
return;
Expand Down
Loading