From f72838d3753c0d477aa3a842183f82418a304034 Mon Sep 17 00:00:00 2001 From: Bandini Bhopi Date: Fri, 15 Mar 2024 06:43:27 +0000 Subject: [PATCH] Revert client pool changes from authentication method Signed-off-by: Bandini Bhopi --- .../authentication_methods_registry.test.ts | 14 +---- .../data_source/server/client/client_pool.ts | 6 +- .../server/client/configure_client.test.ts | 53 ++++++++--------- .../server/client/configure_client.ts | 50 +++++++++------- .../server/client/configure_client_utils.ts | 31 ++++++---- .../legacy/configure_legacy_client.test.ts | 58 +++++++++---------- .../server/legacy/configure_legacy_client.ts | 47 ++++++++------- src/plugins/data_source/server/types.ts | 13 +++-- .../server/util/credential_provider.ts | 14 +++-- 9 files changed, 148 insertions(+), 138 deletions(-) diff --git a/src/plugins/data_source/server/auth_registry/authentication_methods_registry.test.ts b/src/plugins/data_source/server/auth_registry/authentication_methods_registry.test.ts index ce8e13f7cdff..948641870a8a 100644 --- a/src/plugins/data_source/server/auth_registry/authentication_methods_registry.test.ts +++ b/src/plugins/data_source/server/auth_registry/authentication_methods_registry.test.ts @@ -5,22 +5,12 @@ import { AuthenticationMethodRegistery } from './authentication_methods_registry'; import { AuthenticationMethod } from '../../server/types'; -import { AuthType } from '../../common/data_sources'; -import { OpenSearchClientPoolSetup } from '../client'; - -const clientPoolSetup: OpenSearchClientPoolSetup = { - getClientFromPool: jest.fn(), - addClientToPool: jest.fn(), -}; const createAuthenticationMethod = ( authMethod: Partial ): AuthenticationMethod => ({ name: 'unknown', - authType: AuthType.NoAuth, credentialProvider: jest.fn(), - clientPoolSetup, - legacyClientPoolSetup: clientPoolSetup, ...authMethod, }); @@ -69,14 +59,14 @@ describe('AuthenticationMethodRegistery', () => { registry.registerAuthenticationMethod( createAuthenticationMethod({ name: 'typeA', - authType: AuthType.NoAuth, + credentialProvider: jest.fn(), }) ); const typeA = registry.getAuthenticationMethod('typeA')!; expect(() => { - typeA.authType = AuthType.SigV4; + typeA.credentialProvider = jest.fn(); }).toThrow(); expect(() => { typeA.name = 'foo'; diff --git a/src/plugins/data_source/server/client/client_pool.ts b/src/plugins/data_source/server/client/client_pool.ts index 0231833bdda9..02ad665718df 100644 --- a/src/plugins/data_source/server/client/client_pool.ts +++ b/src/plugins/data_source/server/client/client_pool.ts @@ -82,11 +82,7 @@ export class OpenSearchClientPool { }); this.logger.info(`Created data source aws client pool of size ${size}`); - const getClientFromPool = ( - key: string, - authType: AuthType, - request?: OpenSearchDashboardsRequest - ) => { + const getClientFromPool = (key: string, authType: AuthType) => { const selectedCache = authType === AuthType.SigV4 ? this.awsClientCache : this.clientCache; return selectedCache!.get(key); diff --git a/src/plugins/data_source/server/client/configure_client.test.ts b/src/plugins/data_source/server/client/configure_client.test.ts index 2030793c2d61..f01db28080e6 100644 --- a/src/plugins/data_source/server/client/configure_client.test.ts +++ b/src/plugins/data_source/server/client/configure_client.test.ts @@ -26,7 +26,7 @@ import { ClientOptions } from '@opensearch-project/opensearch'; import { opensearchClientMock } from '../../../../core/server/opensearch/client/mocks'; import { cryptographyServiceSetupMock } from '../cryptography_service.mocks'; import { CryptographyServiceSetup } from '../cryptography_service'; -import { DataSourceClientParams, AuthenticationMethod } from '../types'; +import { DataSourceClientParams, AuthenticationMethod, ClientParameters } from '../types'; import { CustomApiSchemaRegistry } from '../schema_registry'; import { IAuthenticationMethodRegistery } from '../auth_registry'; import { authenticationMethodRegisteryMock } from '../auth_registry/authentication_methods_registry.mock'; @@ -47,6 +47,7 @@ describe('configureClient', () => { let sigV4AuthContent: SigV4Content; let customApiSchemaRegistry: CustomApiSchemaRegistry; let authenticationMethodRegistery: jest.Mocked; + let clientParameters: ClientParameters; const customAuthContent = { region: 'us-east-1', @@ -60,10 +61,7 @@ describe('configureClient', () => { const authMethod: AuthenticationMethod = { name: 'typeA', - authType: AuthType.SigV4, credentialProvider: jest.fn(), - clientPoolSetup, - legacyClientPoolSetup: clientPoolSetup, }; beforeEach(() => { @@ -122,12 +120,21 @@ describe('configureClient', () => { customApiSchemaRegistryPromise: Promise.resolve(customApiSchemaRegistry), }; + clientParameters = { + authType: AuthType.SigV4, + endpoint: dataSourceAttr.endpoint, + cacheKeySuffix: '', + credentials: sigV4AuthContent, + }; + ClientMock.mockImplementation(() => dsClient); authenticationMethodRegistery.getAuthenticationMethod.mockImplementation(() => authMethod); + authRegistryCredentialProviderMock.mockReturnValue(clientParameters); }); afterEach(() => { ClientMock.mockReset(); + authRegistryCredentialProviderMock.mockReset(); }); test('configure client with auth.type == no_auth, will call new Client() to create client', async () => { @@ -291,11 +298,6 @@ describe('configureClient', () => { references: [], }); - authRegistryCredentialProviderMock.mockReturnValue({ - credential: sigV4AuthContent, - type: AuthType.SigV4, - }); - const client = await configureClient( { ...dataSourceClientParams, authRegistry: authenticationMethodRegistery }, clientPoolSetup, @@ -336,8 +338,8 @@ describe('configureClient', () => { }); authRegistryCredentialProviderMock.mockReturnValue({ - credential: mockCredentials, - type: AuthType.SigV4, + ...clientParameters, + credentials: mockCredentials, }); const client = await configureClient( @@ -376,11 +378,6 @@ describe('configureClient', () => { references: [], }); - authRegistryCredentialProviderMock.mockReturnValue({ - credential: sigV4AuthContent, - type: AuthType.SigV4, - }); - const client = await configureClient( { ...dataSourceClientParams, authRegistry: authenticationMethodRegistery }, clientPoolSetup, @@ -556,10 +553,7 @@ describe('configureClient', () => { beforeEach(() => { const authMethodWithClientPool: AuthenticationMethod = { name: 'clientPoolTest', - authType: AuthType.SigV4, credentialProvider: jest.fn(), - clientPoolSetup: opensearchClientPoolSetup, - legacyClientPoolSetup: clientPoolSetup, }; authenticationMethodRegistery.getAuthenticationMethod .mockReset() @@ -577,22 +571,18 @@ describe('configureClient', () => { }, references: [], }); - authRegistryCredentialProviderMock.mockReturnValue({ - credential: sigV4AuthContent, - type: AuthType.SigV4, - }); }); - test('Auth Method from Registry: If endpoint is same for multiple requests client pool size should be 1', async () => { + test('If endpoint is same for multiple requests client pool size should be 1', async () => { await configureClient( { ...dataSourceClientParams, authRegistry: authenticationMethodRegistery }, - clientPoolSetup, + opensearchClientPoolSetup, config, logger ); await configureClient( { ...dataSourceClientParams, authRegistry: authenticationMethodRegistery }, - clientPoolSetup, + opensearchClientPoolSetup, config, logger ); @@ -600,10 +590,10 @@ describe('configureClient', () => { expect(ClientMock).toHaveBeenCalledTimes(1); }); - test('Auth Method from Registry: If endpoint is different for two requests client pool size should be 2', async () => { + test('If endpoint is different for two requests client pool size should be 2', async () => { await configureClient( { ...dataSourceClientParams, authRegistry: authenticationMethodRegistery }, - clientPoolSetup, + opensearchClientPoolSetup, config, logger ); @@ -625,10 +615,15 @@ describe('configureClient', () => { }, references: [], }); + authRegistryCredentialProviderMock.mockReturnValue({ + ...clientParameters, + endpoint: 'http://test.com', + cacheKeySuffix: 'test', + }); await configureClient( { ...dataSourceClientParams, authRegistry: authenticationMethodRegistery }, - clientPoolSetup, + opensearchClientPoolSetup, config, logger ); diff --git a/src/plugins/data_source/server/client/configure_client.ts b/src/plugins/data_source/server/client/configure_client.ts index a0592c8799aa..7425bf42bb56 100644 --- a/src/plugins/data_source/server/client/configure_client.ts +++ b/src/plugins/data_source/server/client/configure_client.ts @@ -4,6 +4,7 @@ */ import { Client, ClientOptions } from '@opensearch-project/opensearch'; +import { Client as LegacyClient } from 'elasticsearch'; import { AwsSigv4Signer } from '@opensearch-project/opensearch/aws'; import { Logger, OpenSearchDashboardsRequest } from '../../../../../src/core/server'; import { @@ -16,7 +17,7 @@ import { import { DataSourcePluginConfigType } from '../../config'; import { CryptographyServiceSetup } from '../cryptography_service'; import { createDataSourceError } from '../lib/error'; -import { AuthenticationMethod, DataSourceClientParams } from '../types'; +import { DataSourceClientParams, ClientParameters } from '../types'; import { parseClientOptions } from './client_config'; import { OpenSearchClientPoolSetup } from './client_pool'; import { @@ -25,6 +26,7 @@ import { getCredential, getDataSource, getAuthenticationMethod, + generateCacheKey, } from './configure_client_utils'; import { authRegistryCredentialProvider } from '../util/credential_provider'; @@ -44,6 +46,7 @@ export const configureClient = async ( ): Promise => { let dataSource; let requireDecryption = true; + let clientParams; try { // configure test client @@ -66,25 +69,32 @@ export const configureClient = async ( dataSource = await getDataSource(dataSourceId!, savedObjects); } - let clientPool = openSearchClientPoolSetup; const authenticationMethod = getAuthenticationMethod(dataSource, authRegistry); if (authenticationMethod !== undefined) { - clientPool = authenticationMethod.clientPoolSetup; + clientParams = await authRegistryCredentialProvider(authenticationMethod, { + dataSourceAttr: dataSource, + request, + cryptography, + }); } - const rootClient = getRootClient(dataSource, clientPool.getClientFromPool, request) as Client; + const rootClient = getRootClient( + dataSource, + openSearchClientPoolSetup.getClientFromPool, + clientParams + ) as Client; const registeredSchema = (await customApiSchemaRegistryPromise).getAll(); return await getQueryClient( dataSource, - clientPool, + openSearchClientPoolSetup.addClientToPool, config, registeredSchema, cryptography, rootClient, dataSourceId, request, - authenticationMethod, + clientParams, requireDecryption ); } catch (error: any) { @@ -113,43 +123,41 @@ export const configureClient = async ( */ const getQueryClient = async ( dataSourceAttr: DataSourceAttributes, - clientPool: OpenSearchClientPoolSetup, + addClientToPool: (endpoint: string, authType: AuthType, client: Client | LegacyClient) => void, config: DataSourcePluginConfigType, registeredSchema: any[], cryptography?: CryptographyServiceSetup, rootClient?: Client, dataSourceId?: string, request?: OpenSearchDashboardsRequest, - authenticationMethod?: AuthenticationMethod, + clientParams?: ClientParameters, requireDecryption: boolean = true ): Promise => { let credential; + let cacheKeySuffix; let { auth: { type }, + endpoint, } = dataSourceAttr; - const { endpoint } = dataSourceAttr; const clientOptions = parseClientOptions(config, endpoint, registeredSchema); - if (authenticationMethod !== undefined) { - const credentialProvider = await authRegistryCredentialProvider(authenticationMethod, { - dataSourceAttr, - request, - cryptography, - }); - credential = credentialProvider.credential; - type = credentialProvider.type; + if (clientParams !== undefined) { + credential = clientParams.credentials; + type = clientParams.authType; + cacheKeySuffix = clientParams.cacheKeySuffix; + endpoint = clientParams.endpoint; if (credential.service === undefined) { credential = { ...credential, service: dataSourceAttr.auth.credentials?.service }; } } - const cacheKey = endpoint; + const cacheKey = generateCacheKey(endpoint, cacheKeySuffix); switch (type) { case AuthType.NoAuth: if (!rootClient) rootClient = new Client(clientOptions); - clientPool.addClientToPool(cacheKey, type, rootClient, request); + addClientToPool(cacheKey, type, rootClient); return rootClient.child(); @@ -161,7 +169,7 @@ const getQueryClient = async ( : (dataSourceAttr.auth.credentials as UsernamePasswordTypedContent)); if (!rootClient) rootClient = new Client(clientOptions); - clientPool.addClientToPool(cacheKey, type, rootClient, request); + addClientToPool(cacheKey, type, rootClient); return getBasicAuthClient(rootClient, credential); @@ -175,7 +183,7 @@ const getQueryClient = async ( if (!rootClient) { rootClient = getAWSClient(credential, clientOptions); } - clientPool.addClientToPool(cacheKey, type, rootClient, request); + addClientToPool(cacheKey, type, rootClient); return getAWSChildClient(rootClient, credential); diff --git a/src/plugins/data_source/server/client/configure_client_utils.ts b/src/plugins/data_source/server/client/configure_client_utils.ts index 6fcdc1e2565e..a7229a0d32fa 100644 --- a/src/plugins/data_source/server/client/configure_client_utils.ts +++ b/src/plugins/data_source/server/client/configure_client_utils.ts @@ -19,7 +19,7 @@ import { import { CryptographyServiceSetup } from '../cryptography_service'; import { createDataSourceError } from '../lib/error'; import { IAuthenticationMethodRegistery } from '../auth_registry'; -import { AuthenticationMethod } from '../types'; +import { AuthenticationMethod, ClientParameters } from '../types'; /** * Get the root client of datasource from @@ -32,20 +32,24 @@ import { AuthenticationMethod } from '../types'; */ export const getRootClient = ( dataSourceAttr: DataSourceAttributes, - getClientFromPool: ( - endpoint: string, - authType: AuthType, - request?: OpenSearchDashboardsRequest - ) => Client | LegacyClient | undefined, - request?: OpenSearchDashboardsRequest + getClientFromPool: (endpoint: string, authType: AuthType) => Client | LegacyClient | undefined, + clientParams?: ClientParameters ): Client | LegacyClient | undefined => { - const { + let cacheKeySuffix; + let { auth: { type }, endpoint, } = dataSourceAttr; - const cacheKey = endpoint; - return getClientFromPool(cacheKey, type, request); + if (clientParams !== undefined) { + endpoint = clientParams.endpoint; + cacheKeySuffix = clientParams.cacheKeySuffix; + type = clientParams.authType; + } + + const cacheKey = generateCacheKey(endpoint, cacheKeySuffix); + + return getClientFromPool(cacheKey, type); }; export const getDataSource = async ( @@ -129,6 +133,13 @@ export const getAWSCredential = async ( return credential; }; +export const generateCacheKey = (endpoint: string, cacheKeySuffix?: string) => { + const CACHE_KEY_DELIMITER = ','; + let key = endpoint; + if (cacheKeySuffix) key += CACHE_KEY_DELIMITER + cacheKeySuffix; + return key; +}; + export const getAuthenticationMethod = ( dataSourceAttr: DataSourceAttributes, authRegistry?: IAuthenticationMethodRegistery diff --git a/src/plugins/data_source/server/legacy/configure_legacy_client.test.ts b/src/plugins/data_source/server/legacy/configure_legacy_client.test.ts index c35434417456..581e545315e2 100644 --- a/src/plugins/data_source/server/legacy/configure_legacy_client.test.ts +++ b/src/plugins/data_source/server/legacy/configure_legacy_client.test.ts @@ -15,7 +15,12 @@ import { import { DataSourcePluginConfigType } from '../../config'; import { cryptographyServiceSetupMock } from '../cryptography_service.mocks'; import { CryptographyServiceSetup } from '../cryptography_service'; -import { DataSourceClientParams, LegacyClientCallAPIParams, AuthenticationMethod } from '../types'; +import { + DataSourceClientParams, + LegacyClientCallAPIParams, + AuthenticationMethod, + ClientParameters, +} from '../types'; import { OpenSearchClientPool, OpenSearchClientPoolSetup } from '../client'; import { ConfigOptions } from 'elasticsearch'; import { ClientMock, parseClientOptionsMock } from './configure_legacy_client.test.mocks'; @@ -37,6 +42,7 @@ describe('configureLegacyClient', () => { let dataSourceAttr: DataSourceAttributes; let sigV4AuthContent: SigV4Content; let authenticationMethodRegistery: jest.Mocked; + let clientParameters: ClientParameters; let mockOpenSearchClientInstance: { close: jest.Mock; @@ -60,10 +66,7 @@ describe('configureLegacyClient', () => { const authMethod: AuthenticationMethod = { name: 'typeA', - authType: AuthType.SigV4, credentialProvider: jest.fn(), - clientPoolSetup, - legacyClientPoolSetup: clientPoolSetup, }; beforeEach(() => { @@ -125,6 +128,13 @@ describe('configureLegacyClient', () => { customApiSchemaRegistryPromise: Promise.resolve(customApiSchemaRegistry), }; + clientParameters = { + authType: AuthType.SigV4, + endpoint: dataSourceAttr.endpoint, + cacheKeySuffix: '', + credentials: sigV4AuthContent, + }; + ClientMock.mockImplementation(() => mockOpenSearchClientInstance); mockOpenSearchClientInstance.ping.mockImplementation(function mockCall(this: any) { @@ -135,10 +145,12 @@ describe('configureLegacyClient', () => { }); authenticationMethodRegistery.getAuthenticationMethod.mockImplementation(() => authMethod); + authRegistryCredentialProviderMock.mockReturnValue(clientParameters); }); afterEach(() => { ClientMock.mockReset(); + authRegistryCredentialProviderMock.mockReset(); jest.resetAllMocks(); }); @@ -305,11 +317,6 @@ describe('configureLegacyClient', () => { references: [], }); - authRegistryCredentialProviderMock.mockReturnValue({ - credential: sigV4AuthContent, - type: AuthType.SigV4, - }); - await configureLegacyClient( { ...dataSourceClientParams, authRegistry: authenticationMethodRegistery }, callApiParams, @@ -353,8 +360,8 @@ describe('configureLegacyClient', () => { }); authRegistryCredentialProviderMock.mockReturnValue({ - credential: mockCredentials, - type: AuthType.SigV4, + ...clientParameters, + credentials: mockCredentials, }); await configureLegacyClient( @@ -397,11 +404,6 @@ describe('configureLegacyClient', () => { references: [], }); - authRegistryCredentialProviderMock.mockReturnValue({ - credential: sigV4AuthContent, - type: AuthType.SigV4, - }); - await configureLegacyClient( { ...dataSourceClientParams, authRegistry: authenticationMethodRegistery }, callApiParams, @@ -653,10 +655,7 @@ describe('configureLegacyClient', () => { beforeEach(() => { const authMethodWithClientPool: AuthenticationMethod = { name: 'clientPoolTest', - authType: AuthType.SigV4, credentialProvider: jest.fn(), - clientPoolSetup, - legacyClientPoolSetup: opensearchClientPoolSetup, }; authenticationMethodRegistery.getAuthenticationMethod .mockReset() @@ -674,16 +673,12 @@ describe('configureLegacyClient', () => { }, references: [], }); - authRegistryCredentialProviderMock.mockReturnValue({ - credential: sigV4AuthContent, - type: AuthType.SigV4, - }); }); - test('Auth Method from Registry: If endpoint is same for multiple requests client pool size should be 1', async () => { + test(' If endpoint is same for multiple requests client pool size should be 1', async () => { await configureLegacyClient( { ...dataSourceClientParams, authRegistry: authenticationMethodRegistery }, callApiParams, - clientPoolSetup, + opensearchClientPoolSetup, config, logger ); @@ -691,7 +686,7 @@ describe('configureLegacyClient', () => { await configureLegacyClient( { ...dataSourceClientParams, authRegistry: authenticationMethodRegistery }, callApiParams, - clientPoolSetup, + opensearchClientPoolSetup, config, logger ); @@ -699,11 +694,11 @@ describe('configureLegacyClient', () => { expect(ClientMock).toHaveBeenCalledTimes(1); }); - test('Auth Method from Registry: If endpoint is different for two requests client pool size should be 2', async () => { + test('If endpoint is different for two requests client pool size should be 2', async () => { await configureLegacyClient( { ...dataSourceClientParams, authRegistry: authenticationMethodRegistery }, callApiParams, - clientPoolSetup, + opensearchClientPoolSetup, config, logger ); @@ -725,11 +720,16 @@ describe('configureLegacyClient', () => { }, references: [], }); + authRegistryCredentialProviderMock.mockReturnValue({ + ...clientParameters, + endpoint: 'http://test.com', + cacheKeySuffix: 'test', + }); await configureLegacyClient( { ...dataSourceClientParams, authRegistry: authenticationMethodRegistery }, callApiParams, - clientPoolSetup, + opensearchClientPoolSetup, config, logger ); diff --git a/src/plugins/data_source/server/legacy/configure_legacy_client.ts b/src/plugins/data_source/server/legacy/configure_legacy_client.ts index 8cd46be10e21..b12c202d91a3 100644 --- a/src/plugins/data_source/server/legacy/configure_legacy_client.ts +++ b/src/plugins/data_source/server/legacy/configure_legacy_client.ts @@ -24,7 +24,7 @@ import { } from '../../common/data_sources'; import { DataSourcePluginConfigType } from '../../config'; import { CryptographyServiceSetup } from '../cryptography_service'; -import { AuthenticationMethod, DataSourceClientParams, LegacyClientCallAPIParams } from '../types'; +import { DataSourceClientParams, LegacyClientCallAPIParams, ClientParameters } from '../types'; import { OpenSearchClientPoolSetup } from '../client'; import { parseClientOptions } from './client_config'; import { createDataSourceError } from '../lib/error'; @@ -34,6 +34,7 @@ import { getCredential, getDataSource, getAuthenticationMethod, + generateCacheKey, } from '../client/configure_client_utils'; import { authRegistryCredentialProvider } from '../util/credential_provider'; @@ -53,16 +54,20 @@ export const configureLegacyClient = async ( ) => { try { const dataSourceAttr = await getDataSource(dataSourceId!, savedObjects); + let clientParams; - let clientPool = openSearchClientPoolSetup; const authenticationMethod = getAuthenticationMethod(dataSourceAttr, authRegistry); if (authenticationMethod !== undefined) { - clientPool = authenticationMethod.legacyClientPoolSetup; + clientParams = await authRegistryCredentialProvider(authenticationMethod, { + dataSourceAttr, + request, + cryptography, + }); } const rootClient = getRootClient( dataSourceAttr, - clientPool.getClientFromPool, - request + openSearchClientPoolSetup.getClientFromPool, + clientParams ) as LegacyClient; const registeredSchema = (await customApiSchemaRegistryPromise).getAll(); @@ -71,13 +76,13 @@ export const configureLegacyClient = async ( dataSourceAttr, cryptography, callApiParams, - clientPool, + openSearchClientPoolSetup.addClientToPool, config, registeredSchema, rootClient, dataSourceId, request, - authenticationMethod + clientParams ); } catch (error: any) { logger.debug( @@ -104,41 +109,39 @@ const getQueryClient = async ( dataSourceAttr: DataSourceAttributes, cryptography: CryptographyServiceSetup, { endpoint, clientParams, options }: LegacyClientCallAPIParams, - clientPool: OpenSearchClientPoolSetup, + addClientToPool: (endpoint: string, authType: AuthType, client: Client | LegacyClient) => void, config: DataSourcePluginConfigType, registeredSchema: any[], rootClient?: LegacyClient, dataSourceId?: string, request?: OpenSearchDashboardsRequest, - authenticationMethod?: AuthenticationMethod + authClientParams?: ClientParameters ) => { let credential; + let cacheKeySuffix; let { auth: { type }, + endpoint: nodeUrl, } = dataSourceAttr; - const { endpoint: nodeUrl } = dataSourceAttr; const clientOptions = parseClientOptions(config, nodeUrl, registeredSchema); - if (authenticationMethod !== undefined) { - const credentialProvider = await authRegistryCredentialProvider(authenticationMethod, { - dataSourceAttr, - request, - cryptography, - }); - credential = credentialProvider.credential; - type = credentialProvider.type; + if (authClientParams !== undefined) { + credential = authClientParams.credentials; + type = authClientParams.authType; + cacheKeySuffix = authClientParams.cacheKeySuffix; + nodeUrl = authClientParams.endpoint; if (credential.service === undefined) { credential = { ...credential, service: dataSourceAttr.auth.credentials?.service }; } } - const cacheKey = nodeUrl; + const cacheKey = generateCacheKey(nodeUrl, cacheKeySuffix); switch (type) { case AuthType.NoAuth: if (!rootClient) rootClient = new LegacyClient(clientOptions); - clientPool.addClientToPool(cacheKey, type, rootClient, request); + addClientToPool(cacheKey, type, rootClient); return await (callAPI.bind(null, rootClient) as LegacyAPICaller)( endpoint, @@ -152,7 +155,7 @@ const getQueryClient = async ( (await getCredential(dataSourceAttr, cryptography)); if (!rootClient) rootClient = new LegacyClient(clientOptions); - clientPool.addClientToPool(cacheKey, type, rootClient, request); + addClientToPool(cacheKey, type, rootClient); return getBasicAuthClient(rootClient, { endpoint, clientParams, options }, credential); @@ -163,7 +166,7 @@ const getQueryClient = async ( if (!rootClient) { rootClient = getAWSClient(credential, clientOptions); } - clientPool.addClientToPool(cacheKey, type, rootClient, request); + addClientToPool(cacheKey, type, rootClient); return await getAWSChildClient(rootClient, { endpoint, clientParams, options }, credential); diff --git a/src/plugins/data_source/server/types.ts b/src/plugins/data_source/server/types.ts index 2b14c0db1c44..847e2f72ff68 100644 --- a/src/plugins/data_source/server/types.ts +++ b/src/plugins/data_source/server/types.ts @@ -20,7 +20,6 @@ import { CryptographyServiceSetup } from './cryptography_service'; import { DataSourceError } from './lib/error'; import { IAuthenticationMethodRegistery } from './auth_registry'; import { CustomApiSchemaRegistry } from './schema_registry'; -import { OpenSearchClientPoolSetup } from './client'; export interface LegacyClientCallAPIParams { endpoint: string; @@ -52,14 +51,18 @@ export interface DataSourceCredentialsProviderOptions { export type DataSourceCredentialsProvider = ( options: DataSourceCredentialsProviderOptions -) => Promise; +) => Promise; + +export interface ClientParameters { + authType: AuthType; + endpoint: string; + cacheKeySuffix: string; + credentials: UsernamePasswordTypedContent | SigV4Content; +} export interface AuthenticationMethod { name: string; - authType: AuthType; credentialProvider: DataSourceCredentialsProvider; - clientPoolSetup: OpenSearchClientPoolSetup; - legacyClientPoolSetup: OpenSearchClientPoolSetup; } export interface DataSourcePluginRequestContext { diff --git a/src/plugins/data_source/server/util/credential_provider.ts b/src/plugins/data_source/server/util/credential_provider.ts index d737c932fd95..df3eeb60e5df 100644 --- a/src/plugins/data_source/server/util/credential_provider.ts +++ b/src/plugins/data_source/server/util/credential_provider.ts @@ -3,12 +3,16 @@ * SPDX-License-Identifier: Apache-2.0 */ -import { DataSourceCredentialsProviderOptions, AuthenticationMethod } from '../types'; +import { + DataSourceCredentialsProviderOptions, + AuthenticationMethod, + ClientParameters, +} from '../types'; export const authRegistryCredentialProvider = async ( authenticationMethod: AuthenticationMethod, options: DataSourceCredentialsProviderOptions -) => ({ - credential: await authenticationMethod.credentialProvider(options), - type: authenticationMethod.authType, -}); +): Promise => { + const clientParameters = await authenticationMethod.credentialProvider(options); + return clientParameters as ClientParameters; +};