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

Introduce auth config to Driver.executeQuery #1177

Merged
merged 5 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 18 additions & 1 deletion packages/core/src/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ class QueryConfig<T = EagerResult> {
bookmarkManager?: BookmarkManager | null
resultTransformer?: ResultTransformer<T>
transactionConfig?: TransactionConfig
auth?: AuthToken

/**
* @constructor
Expand Down Expand Up @@ -413,6 +414,21 @@ class QueryConfig<T = EagerResult> {
*
*/
this.transactionConfig = undefined

/**
* The {@link AuthToken} which will be used for executing the query.
*
* By default, the query executor will use connections authenticated with {@link AuthToken} configured in the
* driver creation. This configuration allows switch user and/or authorization information for the
* execution lifetime.
*
* **Warning**: This option is only enable when the driver is connected with Neo4j Database servers
* which supports Bolt 5.1 and onwards.
bigmontz marked this conversation as resolved.
Show resolved Hide resolved
*
* @type {AuthToken|undefined}
* @see {@link driver}
*/
this.auth = undefined
}
}

Expand Down Expand Up @@ -578,7 +594,8 @@ class Driver {
routing: routingConfig,
database: config.database,
impersonatedUser: config.impersonatedUser,
transactionConfig: config.transactionConfig
transactionConfig: config.transactionConfig,
auth: config.auth
}, query, parameters)
}

Expand Down
8 changes: 5 additions & 3 deletions packages/core/src/internal/query-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ import BookmarkManager from '../bookmark-manager'
import Session, { TransactionConfig } from '../session'
import Result from '../result'
import ManagedTransaction from '../transaction-managed'
import { Query } from '../types'
import { AuthToken, Query } from '../types'
import { TELEMETRY_APIS } from './constants'

type SessionFactory = (config: { database?: string, bookmarkManager?: BookmarkManager, impersonatedUser?: string }) => Session
type SessionFactory = (config: { database?: string, bookmarkManager?: BookmarkManager, impersonatedUser?: string, auth?: AuthToken }) => Session

type TransactionFunction<T> = (transactionWork: (tx: ManagedTransaction) => Promise<T>, transactionConfig?: TransactionConfig) => Promise<T>

Expand All @@ -32,6 +32,7 @@ interface ExecutionConfig<T> {
impersonatedUser?: string
bookmarkManager?: BookmarkManager
transactionConfig?: TransactionConfig
auth?: AuthToken
resultTransformer: (result: Result) => Promise<T>
}

Expand All @@ -44,7 +45,8 @@ export default class QueryExecutor {
const session = this._createSession({
database: config.database,
bookmarkManager: config.bookmarkManager,
impersonatedUser: config.impersonatedUser
impersonatedUser: config.impersonatedUser,
auth: config.auth
})

// @ts-expect-error The method is private for external users
Expand Down
1 change: 1 addition & 0 deletions packages/core/test/driver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,7 @@ describe('Driver', () => {
['config.routing=READ', 'create num $d', { d: 1 }, { routing: routing.READ }, extendsDefaultWith({ routing: routing.READ })],
['config.database="dbname"', 'q', {}, { database: 'dbname' }, extendsDefaultWith({ database: 'dbname' })],
['config.impersonatedUser="the_user"', 'q', {}, { impersonatedUser: 'the_user' }, extendsDefaultWith({ impersonatedUser: 'the_user' })],
['config.auth={ scheme: "none", credentials: "" }', 'q', {}, { auth: { scheme: 'none', credentials: '' } }, extendsDefaultWith({ auth: { scheme: 'none', credentials: '' } })],
['config.bookmarkManager=null', 'q', {}, { bookmarkManager: null }, extendsDefaultWith({ bookmarkManager: undefined })],
['config.bookmarkManager set to non-null/empty', 'q', {}, { bookmarkManager: theBookmarkManager }, extendsDefaultWith({ bookmarkManager: theBookmarkManager })],
['config.resultTransformer set', 'q', {}, { resultTransformer: aTransformer }, extendsDefaultWith({ resultTransformer: aTransformer })],
Expand Down
4 changes: 3 additions & 1 deletion packages/core/test/internal/query-executor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ describe('QueryExecutor', () => {
['database set', { database: 'adb' }, { database: 'adb' }],
['database undefined', { database: undefined }, { database: undefined }],
['impersonatedUser set', { impersonatedUser: 'anUser' }, { impersonatedUser: 'anUser' }],
['impersonatedUser undefined', { impersonatedUser: undefined }, { impersonatedUser: undefined }]
['impersonatedUser undefined', { impersonatedUser: undefined }, { impersonatedUser: undefined }],
['auth set', { auth: { scheme: 'none', credentials: '' } }, { auth: { scheme: 'none', credentials: '' } }],
['auth undefined', { auth: undefined }, { auth: undefined }]
])('should redirect % to the session creation', async (_, executorConfig, expectConfig) => {
const { queryExecutor, createSession } = createExecutor()

Expand Down
19 changes: 18 additions & 1 deletion packages/neo4j-driver-deno/lib/core/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ class QueryConfig<T = EagerResult> {
bookmarkManager?: BookmarkManager | null
resultTransformer?: ResultTransformer<T>
transactionConfig?: TransactionConfig
auth?: AuthToken

/**
* @constructor
Expand Down Expand Up @@ -413,6 +414,21 @@ class QueryConfig<T = EagerResult> {
*
*/
this.transactionConfig = undefined

/**
* The {@link AuthToken} which will be used for executing the query.
*
* By default, the query executor will use connections authenticated with {@link AuthToken} configured in the
* driver creation. This configuration allows switch user and/or authorization information for the
* execution lifetime.
*
* **Warning**: This option is only enable when the driver is connected with Neo4j Database servers
* which supports Bolt 5.1 and onwards.
*
* @type {AuthToken|undefined}
* @see {@link driver}
*/
this.auth = undefined
}
}

Expand Down Expand Up @@ -578,7 +594,8 @@ class Driver {
routing: routingConfig,
database: config.database,
impersonatedUser: config.impersonatedUser,
transactionConfig: config.transactionConfig
transactionConfig: config.transactionConfig,
auth: config.auth
}, query, parameters)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ import BookmarkManager from '../bookmark-manager.ts'
import Session, { TransactionConfig } from '../session.ts'
import Result from '../result.ts'
import ManagedTransaction from '../transaction-managed.ts'
import { Query } from '../types.ts'
import { AuthToken, Query } from '../types.ts'
import { TELEMETRY_APIS } from './constants.ts'

type SessionFactory = (config: { database?: string, bookmarkManager?: BookmarkManager, impersonatedUser?: string }) => Session
type SessionFactory = (config: { database?: string, bookmarkManager?: BookmarkManager, impersonatedUser?: string, auth?: AuthToken }) => Session

type TransactionFunction<T> = (transactionWork: (tx: ManagedTransaction) => Promise<T>, transactionConfig?: TransactionConfig) => Promise<T>

Expand All @@ -32,6 +32,7 @@ interface ExecutionConfig<T> {
impersonatedUser?: string
bookmarkManager?: BookmarkManager
transactionConfig?: TransactionConfig
auth?: AuthToken
resultTransformer: (result: Result) => Promise<T>
}

Expand All @@ -44,7 +45,8 @@ export default class QueryExecutor {
const session = this._createSession({
database: config.database,
bookmarkManager: config.bookmarkManager,
impersonatedUser: config.impersonatedUser
impersonatedUser: config.impersonatedUser,
auth: config.auth
})

// @ts-expect-error The method is private for external users
Expand Down
1 change: 1 addition & 0 deletions packages/testkit-backend/src/feature/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const features = [
'Feature:Bolt:Patch:UTC',
'Feature:API:ConnectionAcquisitionTimeout',
'Feature:API:Driver.ExecuteQuery',
'Feature:API:Driver.ExecuteQuery:WithAuth',
'Feature:API:Driver:NotificationsConfig',
'Feature:API:Driver:GetServerInfo',
'Feature:API:Driver.SupportsSessionAuth',
Expand Down
4 changes: 4 additions & 0 deletions packages/testkit-backend/src/request-handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,10 @@ export function ExecuteQuery ({ neo4j }, context, { driverId, cypher, params, co
timeout: config.timeout
}
}

if (config.authorizationToken != null) {
configuration.auth = context.binder.parseAuthToken(config.authorizationToken.data)
}
}

driver.executeQuery(cypher, params, configuration)
Expand Down