From 5b62400d9bcd5f2d378417bbf281f332d38831c2 Mon Sep 17 00:00:00 2001 From: Louis Chu Date: Thu, 1 Sep 2022 09:32:00 -0700 Subject: [PATCH] Merge credential object into data source Signed-off-by: Louis Chu --- .../server/client/configure_client.test.ts | 48 +++++++------------ .../server/client/configure_client.ts | 31 ++++-------- 2 files changed, 24 insertions(+), 55 deletions(-) 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 11523f649685..63cc18d7a3cb 100644 --- a/src/plugins/data_source/server/client/configure_client.test.ts +++ b/src/plugins/data_source/server/client/configure_client.test.ts @@ -5,12 +5,8 @@ import { SavedObjectsClientContract } from '../../../../core/server'; import { loggingSystemMock, savedObjectsClientMock } from '../../../../core/server/mocks'; -import { DATA_SOURCE_SAVED_OBJECT_TYPE, CREDENTIAL_SAVED_OBJECT_TYPE } from '../../common'; -import { - CredentialMaterialsType, - CredentialSavedObjectAttributes, -} from '../../common/credentials/types'; -import { DataSourceAttributes } from '../../common/data_sources'; +import { DATA_SOURCE_SAVED_OBJECT_TYPE } from '../../common'; +import { DataSourceAttributes, CredentialsType } from '../../common/data_sources/types'; import { DataSourcePluginConfigType } from '../../config'; import { ClientMock, parseClientOptionsMock } from './configure_client.test.mocks'; import { OpenSearchClientPoolSetup } from './client_pool'; @@ -21,7 +17,6 @@ import { opensearchClientMock } from '../../../../core/server/opensearch/client/ import { CryptographyClient } from '../cryptography'; const DATA_SOURCE_ID = 'a54b76ec86771ee865a0f74a305dfff8'; -const CREDENETIAL_ID = 'a54dsaadasfasfwe22d23d23d2453df3'; const cryptoClient = new CryptographyClient('test', 'test', new Array(32).fill(0)); // TODO: improve UT @@ -55,6 +50,13 @@ describe('configureClient', () => { title: 'title', endpoint: 'http://localhost', noAuth: false, + credentials: { + type: CredentialsType.UsernamePasswordType, + credentialsContent: { + username: 'username', + password: 'password', + }, + }, } as DataSourceAttributes; clientPoolSetup = { @@ -62,30 +64,12 @@ describe('configureClient', () => { addClientToPool: jest.fn(), }; - const crendentialAttr = { - title: 'cred', - credentialMaterials: { - credentialMaterialsType: CredentialMaterialsType.UsernamePasswordType, - credentialMaterialsContent: { - username: 'username', - password: 'password', - }, - }, - } as CredentialSavedObjectAttributes; - - savedObjectsMock.get - .mockResolvedValueOnce({ - id: DATA_SOURCE_ID, - type: DATA_SOURCE_SAVED_OBJECT_TYPE, - attributes: dataSourceAttr, - references: [{ name: 'user', type: CREDENTIAL_SAVED_OBJECT_TYPE, id: CREDENETIAL_ID }], - }) - .mockResolvedValueOnce({ - id: CREDENETIAL_ID, - type: CREDENTIAL_SAVED_OBJECT_TYPE, - attributes: crendentialAttr, - references: [], - }); + savedObjectsMock.get.mockResolvedValueOnce({ + id: DATA_SOURCE_ID, + type: DATA_SOURCE_SAVED_OBJECT_TYPE, + attributes: dataSourceAttr, + references: [], + }); ClientMock.mockImplementation(() => { return dsClient; @@ -135,7 +119,7 @@ describe('configureClient', () => { ); expect(ClientMock).toHaveBeenCalledTimes(1); - expect(savedObjectsMock.get).toHaveBeenCalledTimes(2); + expect(savedObjectsMock.get).toHaveBeenCalledTimes(1); expect(spy).toHaveBeenCalledTimes(1); expect(client).toBe(dsClient.child.mock.results[0].value); }); diff --git a/src/plugins/data_source/server/client/configure_client.ts b/src/plugins/data_source/server/client/configure_client.ts index e8af67809981..e24cfe7b9930 100644 --- a/src/plugins/data_source/server/client/configure_client.ts +++ b/src/plugins/data_source/server/client/configure_client.ts @@ -10,12 +10,9 @@ import { SavedObjectsClientContract, SavedObjectsErrorHelpers, } from '../../../../../src/core/server'; -import { DATA_SOURCE_SAVED_OBJECT_TYPE, CREDENTIAL_SAVED_OBJECT_TYPE } from '../../common'; -import { - CredentialSavedObjectAttributes, - UsernamePasswordTypedContent, -} from '../../common/credentials/types'; -import { DataSourceAttributes } from '../../common/data_sources'; +import { DATA_SOURCE_SAVED_OBJECT_TYPE } from '../../common'; + +import { DataSourceAttributes, UsernamePasswordTypedContent } from '../../common/data_sources'; import { DataSourcePluginConfigType } from '../../config'; import { CryptographyClient } from '../cryptography'; import { parseClientOptions } from './client_config'; @@ -32,7 +29,7 @@ export const configureClient = async ( const dataSource = await getDataSource(dataSourceId, savedObjects); const rootClient = getRootClient(dataSource.attributes, config, openSearchClientPoolSetup); - return getQueryClient(rootClient, dataSource, savedObjects, cryptographyClient); + return getQueryClient(rootClient, dataSource, cryptographyClient); }; export const getDataSource = async ( @@ -52,19 +49,11 @@ export const getDataSource = async ( }; export const getCredential = async ( - credentialId: string, - savedObjects: SavedObjectsClientContract, + dataSource: SavedObject, cryptographyClient: CryptographyClient ): Promise => { try { - const credentialSavedObject = await savedObjects.get( - CREDENTIAL_SAVED_OBJECT_TYPE, - credentialId - ); - const { - username, - password, - } = credentialSavedObject.attributes.credentialMaterials.credentialMaterialsContent; + const { username, password } = dataSource.attributes.credentials!.credentialsContent; const decodedPassword = await cryptographyClient.decodeAndDecrypt(password); const credential = { username, @@ -89,17 +78,13 @@ export const getCredential = async ( const getQueryClient = async ( rootClient: Client, dataSource: SavedObject, - savedObjects: SavedObjectsClientContract, cryptographyClient: CryptographyClient ): Promise => { if (dataSource.attributes.noAuth) { return rootClient.child(); } else { - const credential = await getCredential( - dataSource.references[0].id, - savedObjects, - cryptographyClient - ); + const credential = await getCredential(dataSource, cryptographyClient); + return getBasicAuthClient(rootClient, credential); } };