Skip to content

Commit

Permalink
Add UT for configure client with Auth registry
Browse files Browse the repository at this point in the history
Signed-off-by: Bandini Bhopi <bandinib@amazon.com>
  • Loading branch information
bandinib-amzn committed Jan 23, 2024
1 parent df24345 commit 6d36d6c
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { IAuthenticationMethodRegistery } from './authentication_methods_registry';

const create = () =>
(({
getAllAuthenticationMethods: jest.fn(),
getAuthenticationMethod: jest.fn(),
} as unknown) as jest.Mocked<IAuthenticationMethodRegistery>);

export const authenticationMethodRegisteryMock = { create };
2 changes: 2 additions & 0 deletions src/core/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ import {
DataSourceServiceStart,
AuthenticationMethod,
IAuthenticationMethodRegistery,
AuthAttributes,
} from './data_source';

// Because of #79265 we need to explicity import, then export these types for
Expand Down Expand Up @@ -511,6 +512,7 @@ export {
CrossCompatibilityServiceStart,
AuthenticationMethod,
IAuthenticationMethodRegistery,
AuthAttributes,
};

/**
Expand Down
1 change: 1 addition & 0 deletions src/core/server/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export { contextServiceMock } from './context/context_service.mock';
export { capabilitiesServiceMock } from './capabilities/capabilities_service.mock';
export { coreUsageDataServiceMock } from './core_usage_data/core_usage_data_service.mock';
export { crossCompatibilityServiceMock } from './cross_compatibility/cross_compatibility.mock';
export { authenticationMethodRegisteryMock } from './data_source/authentication_methods_registry.mock';

export function pluginInitializerContextConfigMock<T>(config: T) {
const globalConfig: SharedGlobalConfig = {
Expand Down
64 changes: 62 additions & 2 deletions src/plugins/data_source/server/client/configure_client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,16 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { SavedObjectsClientContract } from '../../../../core/server';
import { loggingSystemMock, savedObjectsClientMock } from '../../../../core/server/mocks';
import {
SavedObjectsClientContract,
IAuthenticationMethodRegistery,
AuthenticationMethod,
} from '../../../../core/server';
import {
loggingSystemMock,
savedObjectsClientMock,
authenticationMethodRegisteryMock,
} from '../../../../core/server/mocks';
import { DATA_SOURCE_SAVED_OBJECT_TYPE } from '../../common';
import {
DataSourceAttributes,
Expand Down Expand Up @@ -38,12 +46,14 @@ describe('configureClient', () => {
let dataSourceClientParams: DataSourceClientParams;
let usernamePasswordAuthContent: UsernamePasswordTypedContent;
let sigV4AuthContent: SigV4Content;
let authenticationMethodRegistery: jest.Mocked<IAuthenticationMethodRegistery>;

beforeEach(() => {
dsClient = opensearchClientMock.createInternalClient();
logger = loggingSystemMock.createLogger();
savedObjectsMock = savedObjectsClientMock.create();
cryptographyMock = cryptographyServiceSetupMock.create();
authenticationMethodRegistery = authenticationMethodRegisteryMock.create();

config = {
enabled: true,
Expand Down Expand Up @@ -238,4 +248,54 @@ describe('configureClient', () => {
expect(savedObjectsMock.get).toHaveBeenCalledTimes(1);
expect(decodeAndDecryptSpy).toHaveBeenCalledTimes(1);
});

test('should throw error when no credentials present for auth method other than NO_AUTH', async () => {
savedObjectsMock.get.mockReset().mockResolvedValueOnce({
id: DATA_SOURCE_ID,
type: DATA_SOURCE_SAVED_OBJECT_TYPE,
attributes: {
...dataSourceAttr,
auth: {
type: 'token_exchange',
},
},
references: [],
});
await expect(
configureClient(dataSourceClientParams, clientPoolSetup, config, logger)
).rejects.toThrowError();
});

test('configureClient should retunrn client from authentication registery if method present in registry', async () => {
const tokenExchangeContent = {
region: 'us-east-1',
roleARN: 'test-role',
};
savedObjectsMock.get.mockReset().mockResolvedValueOnce({
id: DATA_SOURCE_ID,
type: DATA_SOURCE_SAVED_OBJECT_TYPE,
attributes: {
...dataSourceAttr,
auth: {
type: 'token_exchange',
credentials: tokenExchangeContent,
},
},
references: [],
});
const authMethod: AuthenticationMethod = { authType: 'token_exchange', getClient: ClientMock };
authenticationMethodRegistery.getAuthenticationMethod.mockImplementation(() => authMethod);

const client = await configureClient(
dataSourceClientParams,
clientPoolSetup,
config,
logger,
authenticationMethodRegistery
);
expect(authenticationMethodRegistery.getAuthenticationMethod).toHaveBeenCalledTimes(1);
expect(ClientMock).toHaveBeenCalledTimes(1);
expect(savedObjectsMock.get).toHaveBeenCalledTimes(1);
// expect(client).toBe(dsClient.child.mock.results[0].value);
});
});
4 changes: 2 additions & 2 deletions src/plugins/data_source/server/client/configure_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const configureClient = async (
openSearchClientPoolSetup: OpenSearchClientPoolSetup,
config: DataSourcePluginConfigType,
logger: Logger,
registeredAuthRegistry: IAuthenticationMethodRegistery | undefined
registeredAuthRegistry?: IAuthenticationMethodRegistery | undefined
): Promise<Client> => {
let dataSource;
let requireDecryption = true;
Expand Down Expand Up @@ -126,7 +126,7 @@ const getQueryClient = async (
const clientOptions = parseClientOptions(config, endpoint);
const cacheKey = generateCacheKey(dataSourceAttr, dataSourceId);

if (!dataSourceAttr.auth.credentials) {
if (type !== AuthType.NoAuth && !dataSourceAttr.auth.credentials) {
throw Error(`Credentials not found.`);
}
if (registeredAuthRegistry !== undefined) {
Expand Down

0 comments on commit 6d36d6c

Please sign in to comment.