Skip to content

Commit

Permalink
Adds auth registry to store auth details passed by plugin and adds ap…
Browse files Browse the repository at this point in the history
…i in datasource plugin start

Signed-off-by: Bandini Bhopi <bandinib@amazon.com>
  • Loading branch information
bandinib-amzn committed Jan 27, 2024
1 parent 3b96379 commit fd91205
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

// eslint-disable-next-line @osd/eslint/no-restricted-paths
import { DataSourceCredentialsProvider } from '../../server/types';

export type IAuthenticationMethodRegistery = Omit<
AuthenticationMethodRegistery,
'registerAuthenticationMethod'
>;

export class AuthenticationMethodRegistery {
private readonly authMethods = new Map<string, DataSourceCredentialsProvider>();

/**
* Register a authMethods with function to return credentials inside the registry.
* Authentication Method can only be registered once. subsequent calls with the same method name will throw an error.
*/
public registerAuthenticationMethod(
type: string,
credentialProvider: DataSourceCredentialsProvider
) {
if (this.authMethods.has(type)) {
throw new Error(`Authentication method '${type}' is already registered`);
}
this.authMethods.set(type, credentialProvider);
}

public getAllAuthenticationMethods() {
return [...this.authMethods.values()];
}

public getAuthenticationMethod(authType: string) {
return this.authMethods.get(authType);
}
}
9 changes: 9 additions & 0 deletions src/plugins/data_source/common/auth_registry/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

export {
IAuthenticationMethodRegistery,
AuthenticationMethodRegistery,
} from './authentication_methods_registry';
2 changes: 1 addition & 1 deletion src/plugins/data_source/server/client/configure_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ const getQueryClient = async (
dataSourceAttr: DataSourceAttributes,
addClientToPool: (endpoint: string, authType: AuthType, client: Client | LegacyClient) => void,
config: DataSourcePluginConfigType,
request: OpenSearchDashboardsRequest,
request?: OpenSearchDashboardsRequest,
cryptography?: CryptographyServiceSetup,
rootClient?: Client,
dataSourceId?: string,
Expand Down
40 changes: 31 additions & 9 deletions src/plugins/data_source/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,18 @@ import { DATA_SOURCE_SAVED_OBJECT_TYPE } from '../common';
import { ensureRawRequest } from '../../../../src/core/server/http/router';
import { createDataSourceError } from './lib/error';
import { registerTestConnectionRoute } from './routes/test_connection';
import {
AuthenticationMethodRegistery,
IAuthenticationMethodRegistery,
} from '../common/auth_registry';

export class DataSourcePlugin implements Plugin<DataSourcePluginSetup, DataSourcePluginStart> {
private readonly logger: Logger;
private readonly cryptographyService: CryptographyService;
private readonly dataSourceService: DataSourceService;
private readonly config$: Observable<DataSourcePluginConfigType>;
private started = false;
private authMethodsRegistry = new AuthenticationMethodRegistery();

constructor(private initializerContext: PluginInitializerContext<DataSourcePluginConfigType>) {
this.logger = this.initializerContext.logger.get();
Expand All @@ -48,7 +54,7 @@ export class DataSourcePlugin implements Plugin<DataSourcePluginSetup, DataSourc
this.config$ = this.initializerContext.config.create<DataSourcePluginConfigType>();
}

public async setup(core: CoreSetup) {
public async setup(core: CoreSetup<DataSourcePluginStart>) {
this.logger.debug('dataSource: Setup');

// Register data source saved object type
Expand Down Expand Up @@ -99,14 +105,21 @@ export class DataSourcePlugin implements Plugin<DataSourcePluginSetup, DataSourc
const auditTrailPromise = core.getStartServices().then(([coreStart]) => coreStart.auditTrail);

const dataSourceService: DataSourceServiceSetup = await this.dataSourceService.setup(config);

const authRegistryPromise = core.getStartServices().then(([, , selfStart]) => {
const dataSourcePluginStart = selfStart as DataSourcePluginStart;
return dataSourcePluginStart.getAuthenticationMethodRegistery();
});

// Register data source plugin context to route handler context
core.http.registerRouteHandlerContext(
'dataSource',
this.createDataSourceRouteHandlerContext(
dataSourceService,
cryptographyServiceSetup,
this.logger,
auditTrailPromise
auditTrailPromise,
authRegistryPromise
)
);

Expand All @@ -118,10 +131,10 @@ export class DataSourcePlugin implements Plugin<DataSourcePluginSetup, DataSourc
credentialProvider: DataSourceCredentialsProvider
) => {
this.logger.info(`Registered Credential Provider for authType = ${authType}`);
/*
Add in auth registry
this.authRegistery.registerAuth(authType, credentialProvider);
*/
if (this.started) {
throw new Error('cannot call `registerCredentialProvider` after service startup.');
}
this.authMethodsRegistry.registerAuthenticationMethod(authType, credentialProvider);
};

return {
Expand All @@ -132,8 +145,10 @@ export class DataSourcePlugin implements Plugin<DataSourcePluginSetup, DataSourc

public start(core: CoreStart) {
this.logger.debug('dataSource: Started');

return {};
this.started = true;
return {
getAuthenticationMethodRegistery: () => this.authMethodsRegistry,
};
}

public stop() {
Expand All @@ -144,9 +159,16 @@ export class DataSourcePlugin implements Plugin<DataSourcePluginSetup, DataSourc
dataSourceService: DataSourceServiceSetup,
cryptography: CryptographyServiceSetup,
logger: Logger,
auditTrailPromise: Promise<AuditorFactory>
auditTrailPromise: Promise<AuditorFactory>,
authRegistry: Promise<IAuthenticationMethodRegistery>
): IContextProvider<RequestHandler<unknown, unknown, unknown>, 'dataSource'> => {
return (context, req) => {
authRegistry.then((auth) => {
if (auth !== undefined)
logger.info(
`Total item found in auth registry is ${auth.getAllAuthenticationMethods().length}`
);
});
return {
opensearch: {
getClient: (dataSourceId: string) => {
Expand Down
15 changes: 11 additions & 4 deletions src/plugins/data_source/server/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@ import {
SavedObjectsClientContract,
OpenSearchDashboardsRequest,
} from 'src/core/server';
import { DataSourceAttributes } from '../common/data_sources';
import {
DataSourceAttributes,
SigV4Content,
UsernamePasswordTypedContent,
} from '../common/data_sources';

import { CryptographyServiceSetup } from './cryptography_service';
import { DataSourceError } from './lib/error';
import { IAuthenticationMethodRegistery } from '../common/auth_registry';

export interface LegacyClientCallAPIParams {
endpoint: string;
Expand All @@ -28,7 +33,7 @@ export interface DataSourceClientParams {
dataSourceId?: string;
// required when creating test client
testClientDataSourceAttr?: DataSourceAttributes;
request: OpenSearchDashboardsRequest;
request?: OpenSearchDashboardsRequest;
}

export interface DataSourceCredentialsProviderOptions {
Expand Down Expand Up @@ -71,5 +76,7 @@ export interface DataSourcePluginSetup {
credentialProvider: DataSourceCredentialsProvider
) => void;
}
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface DataSourcePluginStart {}

export interface DataSourcePluginStart {
getAuthenticationMethodRegistery: () => IAuthenticationMethodRegistery;
}

0 comments on commit fd91205

Please sign in to comment.