From 717bd13875182a58b03df306eabf3b76f1230ac7 Mon Sep 17 00:00:00 2001 From: Renan Mendes Carvalho Date: Wed, 7 Dec 2022 09:31:15 +0100 Subject: [PATCH] plugin-search-backend-module-elasticsearch: Opensearch provider This patch continues the work started by jinnjwu on https://github.com/backstage/backstage/pull/14988 It creates a new provider, opensearch, allowing the usage of it outside of managed aws solutions. Signed-off-by: Renan Mendes Carvalho --- .../api-report.md | 2 +- .../config.d.ts | 5 +++-- .../src/engines/ElasticSearchClientOptions.ts | 4 ++-- .../ElasticSearchClientWrapper.test.ts | 19 ++++++++++++++++++ .../src/engines/ElasticSearchSearchEngine.ts | 20 +++++++++++++++++++ 5 files changed, 45 insertions(+), 5 deletions(-) diff --git a/plugins/search-backend-module-elasticsearch/api-report.md b/plugins/search-backend-module-elasticsearch/api-report.md index be54fe9d4a..9e3ca40ec7 100644 --- a/plugins/search-backend-module-elasticsearch/api-report.md +++ b/plugins/search-backend-module-elasticsearch/api-report.md @@ -421,7 +421,7 @@ export interface OpenSearchElasticSearchClientOptions // (undocumented) nodes?: string | string[] | OpenSearchNodeOptions | OpenSearchNodeOptions[]; // (undocumented) - provider?: 'aws'; + provider?: 'aws' | 'opensearch'; } // @public (undocumented) diff --git a/plugins/search-backend-module-elasticsearch/config.d.ts b/plugins/search-backend-module-elasticsearch/config.d.ts index 2428d44b84..16190e7ed0 100644 --- a/plugins/search-backend-module-elasticsearch/config.d.ts +++ b/plugins/search-backend-module-elasticsearch/config.d.ts @@ -138,10 +138,11 @@ export interface Config { bearer: string; };*/ } + /** * AWS = In house hosting Open Search */ - | { + | { provider: 'opensearch'; /** * Node configuration. @@ -166,7 +167,7 @@ export interface Config { | { apiKey: string; }; - } + } ); }; }; diff --git a/plugins/search-backend-module-elasticsearch/src/engines/ElasticSearchClientOptions.ts b/plugins/search-backend-module-elasticsearch/src/engines/ElasticSearchClientOptions.ts index 74523020db..8cb2f67fce 100644 --- a/plugins/search-backend-module-elasticsearch/src/engines/ElasticSearchClientOptions.ts +++ b/plugins/search-backend-module-elasticsearch/src/engines/ElasticSearchClientOptions.ts @@ -25,7 +25,7 @@ import type { ConnectionOptions as TLSConnectionOptions } from 'tls'; export const isOpenSearchCompatible = ( opts: ElasticSearchClientOptions, ): opts is OpenSearchElasticSearchClientOptions => { - return opts?.provider === 'aws'; + return ['aws', 'opensearch'].includes(opts?.provider ?? ''); }; /** @@ -50,7 +50,7 @@ export type ElasticSearchClientOptions = */ export interface OpenSearchElasticSearchClientOptions extends BaseElasticSearchClientOptions { - provider?: 'aws'; + provider?: 'aws' | 'opensearch'; auth?: OpenSearchAuth; connection?: OpenSearchConnectionConstructor; node?: string | string[] | OpenSearchNodeOptions | OpenSearchNodeOptions[]; diff --git a/plugins/search-backend-module-elasticsearch/src/engines/ElasticSearchClientWrapper.test.ts b/plugins/search-backend-module-elasticsearch/src/engines/ElasticSearchClientWrapper.test.ts index cbed4f4442..2677eb1420 100644 --- a/plugins/search-backend-module-elasticsearch/src/engines/ElasticSearchClientWrapper.test.ts +++ b/plugins/search-backend-module-elasticsearch/src/engines/ElasticSearchClientWrapper.test.ts @@ -336,5 +336,24 @@ describe('ElasticSearchClientWrapper', () => { body: { actions: input.actions }, }); }); + + it('accepts provider "opensearch"', async () => { + osOptions = { + provider: 'opensearch', + node: 'https://my-opensearch-instance.address.com', + // todo(backstage/techdocs-core): Remove the following ts-ignore when + // @short.io/opensearch-mock is updated to work w/opensearch >= 2.0.0 + // @ts-ignore + connection: mock.getConnection(), + }; + + const wrapper = ElasticSearchClientWrapper.fromClientOptions(osOptions); + expect(OpenSearchClient).toHaveBeenCalledWith(osOptions); + + const searchInput = { index: 'xyz', body: { eg: 'etc' } }; + const result = (await wrapper.search(searchInput)) as any; + expect(result.client).toBe('os'); + expect(result.args).toStrictEqual(searchInput); + }); }); }); diff --git a/plugins/search-backend-module-elasticsearch/src/engines/ElasticSearchSearchEngine.ts b/plugins/search-backend-module-elasticsearch/src/engines/ElasticSearchSearchEngine.ts index f3db46956d..bd7224d850 100644 --- a/plugins/search-backend-module-elasticsearch/src/engines/ElasticSearchSearchEngine.ts +++ b/plugins/search-backend-module-elasticsearch/src/engines/ElasticSearchSearchEngine.ts @@ -150,6 +150,8 @@ export class ElasticSearchSearchEngine implements SearchEngine { logger.info('Initializing Elastic.co ElasticSearch search engine.'); } else if (options.provider === 'aws') { logger.info('Initializing AWS OpenSearch search engine.'); + } else if (options.provider === 'opensearch') { + logger.info('Initializing OpenSearch search engine.'); } else { logger.info('Initializing ElasticSearch search engine.'); } @@ -452,6 +454,24 @@ export async function createElasticSearchClientOptions( : {}), }; } + if (config.getOptionalString('provider') === 'opensearch') { + const authConfig = config.getConfig('auth'); + return { + provider: 'opensearch', + auth: { + username: authConfig.getString('username'), + password: authConfig.getString('password'), + }, + ...(sslConfig + ? { + ssl: { + rejectUnauthorized: + sslConfig?.getOptionalBoolean('rejectUnauthorized'), + }, + } + : {}), + }; + } const authConfig = config.getOptionalConfig('auth'); const auth = authConfig &&