-
Notifications
You must be signed in to change notification settings - Fork 10
[DATABRICKS-2] PLU-600: add databricks app skeleton, auth, and client setup #1527
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
Open
pregnantboy
wants to merge
1
commit into
databricks/1-config
Choose a base branch
from
databricks/2-app-skeleton
base: databricks/1-config
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export default [] |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
65 changes: 65 additions & 0 deletions
65
packages/backend/src/apps/databricks/auth/create-client.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| import { IGlobalVariable } from '@plumber/types' | ||
|
|
||
| import { DBSQLClient, LogLevel } from '@databricks/sql' | ||
| import IDBSQLClient, { | ||
| ConnectionOptions, | ||
| } from '@databricks/sql/dist/contracts/IDBSQLClient' | ||
|
|
||
| import { databricksConfig } from '@/config/app-env-vars/databricks' | ||
| import logger from '@/helpers/logger' | ||
|
|
||
| import { constructSchemaName } from '../common/construct-schema-name' | ||
|
|
||
| import { getDatabricksToken } from './token-persistence' | ||
|
|
||
| export const createSession = async ($: IGlobalVariable) => { | ||
| const client: DBSQLClient = new DBSQLClient({ | ||
| logger: { | ||
| log(level: LogLevel, message: string) { | ||
| logger[level]({ | ||
| userId: $.user?.id, | ||
| stepId: $.step?.id, | ||
| flowId: $.flow?.id, | ||
| testRun: $.execution?.testRun, | ||
| event: 'databricks-client-log', | ||
| message, | ||
| }) | ||
| }, | ||
| }, | ||
| }) | ||
|
|
||
| const token = await getDatabricksToken() | ||
| const connectOptions = { | ||
| authType: 'access-token', | ||
| host: databricksConfig.serverHostname, | ||
| path: databricksConfig.httpPath, | ||
| token, | ||
| } satisfies ConnectionOptions | ||
|
|
||
| const schemaName = constructSchemaName($) | ||
|
|
||
| let connectedClient: IDBSQLClient | ||
| try { | ||
| connectedClient = await client.connect(connectOptions) | ||
| const session = await connectedClient.openSession({ | ||
| initialSchema: schemaName, | ||
| initialCatalog: databricksConfig.catalog, | ||
| }) | ||
| const endSession = async () => { | ||
| await session.close() | ||
| await connectedClient.close() | ||
| } | ||
|
|
||
| return { session, endSession } | ||
| } catch (error) { | ||
| // Clean up the connected client if it was created | ||
| if (connectedClient) { | ||
| await connectedClient.close().catch(() => {}) | ||
| } | ||
| logger.error('Failed to connect to Databricks', { | ||
| event: 'databricks-connect-error', | ||
| error, | ||
| }) | ||
| throw new Error('Failed to connect to Databricks') | ||
| } | ||
| } | ||
62 changes: 62 additions & 0 deletions
62
packages/backend/src/apps/databricks/auth/token-persistence.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import axios from 'axios' | ||
|
|
||
| import { databricksConfig } from '@/config/app-env-vars/databricks' | ||
| import { createRedisClient, REDIS_DB_INDEX } from '@/config/redis' | ||
| import logger from '@/helpers/logger' | ||
|
|
||
| const redisClient = createRedisClient(REDIS_DB_INDEX.APP_DATA) | ||
|
|
||
| const DATABRICKS_AUTH_TOKEN_REDIS_PREFIX = 'databricks:authToken:' | ||
|
|
||
| export async function getDatabricksToken(): Promise<string> { | ||
| const redisKey = | ||
| DATABRICKS_AUTH_TOKEN_REDIS_PREFIX + databricksConfig.serverHostname | ||
| try { | ||
| const cachedToken = await redisClient.get(redisKey) | ||
| if (cachedToken) { | ||
| logger.info('Databricks OAuth token read', { | ||
| event: 'databricks-oauth-token-read', | ||
| redisKey, | ||
| }) | ||
| return cachedToken | ||
| } | ||
|
|
||
| const response = await axios.post( | ||
| '/oidc/v1/token', | ||
| new URLSearchParams({ | ||
| grant_type: 'client_credentials', | ||
| scope: 'all-apis', | ||
| }), | ||
| { | ||
| baseURL: `https://${databricksConfig.serverHostname}`, | ||
| auth: { | ||
| username: databricksConfig.clientId, | ||
| password: databricksConfig.clientSecret, | ||
| }, | ||
| headers: { | ||
| 'Content-Type': 'application/x-www-form-urlencoded', | ||
| }, | ||
| }, | ||
| ) | ||
| const accessToken = response.data.access_token | ||
| logger.info('Databricks OAuth token response', { | ||
| event: 'databricks-oauth-token-response', | ||
| redisKey, | ||
| accessToken: accessToken.slice(0, 10) + '...', | ||
| }) | ||
|
|
||
| // expires_in is in seconds, minus 1 minute of buffer | ||
| const expiresIn = response.data.expires_in - 60 | ||
| // Write it into Redis | ||
| await redisClient.set(redisKey, accessToken, 'EX', expiresIn) | ||
|
|
||
| return accessToken | ||
| } catch (e) { | ||
| logger.error('Databricks OAuth token read error', { | ||
| event: 'databricks-oauth-token-read-error', | ||
| error: e, | ||
| redisKey, | ||
| }) | ||
| throw new Error('Failed to get Databricks OAuth token') | ||
| } | ||
| } |
10 changes: 10 additions & 0 deletions
10
packages/backend/src/apps/databricks/common/construct-schema-name.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import { IGlobalVariable } from '@plumber/types' | ||
|
|
||
| export const constructSchemaName = ($: IGlobalVariable) => { | ||
| const userEmail = $.user?.email | ||
| if (!userEmail) { | ||
| throw new Error('User email is required') | ||
| } | ||
| // replace non-alphanumeric characters with underscore | ||
| return userEmail.replace(/[^a-zA-Z0-9]/g, '_').toLowerCase() | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| export interface DatabrickColumnRes { | ||
| TABLE_CAT: string | ||
| TABLE_SCHEM: string | ||
| TABLE_NAME: string | ||
| COLUMN_NAME: string | ||
| DATA_TYPE: number | ||
| TYPE_NAME: string | ||
| NULLABLE: number | ||
| IS_NULLABLE: 'YES' | 'NO' | ||
| } | ||
|
|
||
| export interface DatabrickTableRes { | ||
| TABLE_CAT: string | ||
| TABLE_SCHEM: string | ||
| TABLE_NAME: string | ||
| TABLE_TYPE: string | ||
| REMARKS: string | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export default [] |
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import { IApp } from '@plumber/types' | ||
|
|
||
| import actions from './actions' | ||
| import dynamicData from './dynamic-data' | ||
|
|
||
| const app: IApp = { | ||
| name: 'Databricks', | ||
| key: 'databricks', | ||
| description: 'Store data for analytics and machine learning', | ||
| iconUrl: '{BASE_URL}/apps/databricks/assets/favicon.svg', | ||
| authDocUrl: '', | ||
| beforeRequest: [], | ||
| baseUrl: '', | ||
| apiBaseUrl: '', | ||
| primaryColor: '0059F7', | ||
| actions, | ||
| dynamicData, | ||
| category: 'data', | ||
| } | ||
|
|
||
| export default app |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.