Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MDS] Fix the dsm plugin setup when mds feature flag is disabled #7163

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/7163.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
fix:
- [MDS] Fix the dsm plugin setup when mds feature flag is disabled ([#7163](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/7163))
104 changes: 103 additions & 1 deletion src/plugins/data_source_management/public/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,43 @@
import { coreMock } from '../../../core/public/mocks';
import { DataSourceManagementPluginStart } from './plugin';
import { testDataSourceManagementPlugin, createAuthenticationMethod } from './mocks';
import {
DataSourceManagementPlugin,
DataSourceManagementSetupDependencies,
DSM_APP_ID,
} from './plugin';
import { PLUGIN_NAME } from '../common';

describe('#dataSourceManagement', () => {
let coreSetup: any;
let coreStart: any;
let mockDataSourceManagementPluginStart: MockedKeys<DataSourceManagementPluginStart>;

const managementMock = {
sections: {
section: {
opensearchDashboards: {
registerApp: jest.fn(),
},
},
},
};

const indexPatternManagementMock = {
columns: {
register: jest.fn(),
},
};

beforeEach(() => {
coreSetup = coreMock.createSetup({ pluginStartContract: mockDataSourceManagementPluginStart });
coreSetup = {
...coreMock.createSetup({ pluginStartContract: mockDataSourceManagementPluginStart }),
management: managementMock,
indexPatternManagement: indexPatternManagementMock,
};
coreStart = coreMock.createStart();
});

it('can register custom authentication method', () => {
const { setup, doStart } = testDataSourceManagementPlugin(coreSetup, coreStart);
const typeA = createAuthenticationMethod({ name: 'typeA' });
Expand All @@ -26,4 +54,78 @@ describe('#dataSourceManagement', () => {
getDataSourceMenu: expect.any(Function),
});
});

it('should not register any authentication method if feature flag is disabled', () => {
const plugin = new DataSourceManagementPlugin();
const setupDeps: DataSourceManagementSetupDependencies = {
management: coreSetup.management,
indexPatternManagement: coreSetup.indexPatternManagement,
dataSource: undefined, // Feature flag disabled
};

const setup = plugin.setup(coreSetup, setupDeps);
expect(setup).toBeUndefined();
});

it('should return setup object with methods when feature flag is enabled', () => {
const plugin = new DataSourceManagementPlugin();
const setupDeps: DataSourceManagementSetupDependencies = {
management: coreSetup.management,
indexPatternManagement: coreSetup.indexPatternManagement,
dataSource: {
awsSigV4AuthEnabled: true,
noAuthenticationTypeEnabled: true,
usernamePasswordAuthEnabled: true,
hideLocalCluster: false,
} as any,
};

const setup = plugin.setup(coreSetup, setupDeps);
expect(setup).toBeDefined();
expect(setup?.registerAuthenticationMethod).toBeInstanceOf(Function);
expect(setup?.dataSourceSelection).toBeInstanceOf(Object);
expect(setup?.ui.DataSourceSelector).toBeInstanceOf(Function);
expect(setup?.ui.getDataSourceMenu).toBeInstanceOf(Function);
});

it('should throw error if registering authentication method after startup', () => {
const plugin = new DataSourceManagementPlugin();
const setupDeps: DataSourceManagementSetupDependencies = {
management: coreSetup.management,
indexPatternManagement: coreSetup.indexPatternManagement,
dataSource: {
awsSigV4AuthEnabled: true,
noAuthenticationTypeEnabled: true,
usernamePasswordAuthEnabled: true,
hideLocalCluster: false,
} as any,
};

const setup = plugin.setup(coreSetup, setupDeps);
plugin.start(coreStart);
expect(() => {
setup?.registerAuthenticationMethod(createAuthenticationMethod({ name: 'typeB' }));
}).toThrow('cannot call `registerAuthenticationMethod` after data source management startup.');
});

it('should register application in the management section', () => {
const plugin = new DataSourceManagementPlugin();
const setupDeps: DataSourceManagementSetupDependencies = {
management: coreSetup.management,
indexPatternManagement: coreSetup.indexPatternManagement,
dataSource: undefined, // Feature flag disabled
};

plugin.setup(coreSetup, setupDeps);
expect(
setupDeps.management.sections.section.opensearchDashboards.registerApp
).toHaveBeenCalledWith(
expect.objectContaining({
id: DSM_APP_ID,
title: PLUGIN_NAME,
order: 1,
mount: expect.any(Function),
})
);
});
});
35 changes: 19 additions & 16 deletions src/plugins/data_source_management/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@
},
});

// when the feature flag is disabled, we don't need to register any of the mds components
if (!featureFlagStatus) {
return undefined;

Check warning on line 107 in src/plugins/data_source_management/public/plugin.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data_source_management/public/plugin.ts#L107

Added line #L107 was not covered by tests
}

const registerAuthenticationMethod = (authMethod: AuthenticationMethod) => {
if (this.started) {
throw new Error(
Expand All @@ -111,29 +116,27 @@
this.authMethodsRegistry.registerAuthenticationMethod(authMethod);
};

if (dataSource) {
if (dataSource.noAuthenticationTypeEnabled) {
registerAuthenticationMethod(noAuthCredentialAuthMethod);
}
if (dataSource.usernamePasswordAuthEnabled) {
registerAuthenticationMethod(usernamePasswordAuthMethod);
}
if (dataSource.awsSigV4AuthEnabled) {
registerAuthenticationMethod(sigV4AuthMethod);
}

setHideLocalCluster({ enabled: dataSource.hideLocalCluster });
setUiSettings(uiSettings);
// This instance will be got in each data source selector component.
setDataSourceSelection(this.dataSourceSelection);
if (dataSource.noAuthenticationTypeEnabled) {
registerAuthenticationMethod(noAuthCredentialAuthMethod);

Check warning on line 120 in src/plugins/data_source_management/public/plugin.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data_source_management/public/plugin.ts#L120

Added line #L120 was not covered by tests
}
if (dataSource.usernamePasswordAuthEnabled) {
registerAuthenticationMethod(usernamePasswordAuthMethod);

Check warning on line 123 in src/plugins/data_source_management/public/plugin.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data_source_management/public/plugin.ts#L123

Added line #L123 was not covered by tests
}
if (dataSource.awsSigV4AuthEnabled) {
registerAuthenticationMethod(sigV4AuthMethod);

Check warning on line 126 in src/plugins/data_source_management/public/plugin.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data_source_management/public/plugin.ts#L126

Added line #L126 was not covered by tests
}

setHideLocalCluster({ enabled: dataSource.hideLocalCluster });
setUiSettings(uiSettings);

Check warning on line 130 in src/plugins/data_source_management/public/plugin.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data_source_management/public/plugin.ts#L129-L130

Added lines #L129 - L130 were not covered by tests
// This instance will be got in each data source selector component.
setDataSourceSelection(this.dataSourceSelection);

Check warning on line 132 in src/plugins/data_source_management/public/plugin.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data_source_management/public/plugin.ts#L132

Added line #L132 was not covered by tests

return {
registerAuthenticationMethod,
// Other plugins can get this instance from setupDeps and use to get selected data sources.
dataSourceSelection: this.dataSourceSelection,
ui: {
DataSourceSelector: dataSource ? createDataSourceSelector(uiSettings, dataSource) : null,
DataSourceSelector: createDataSourceSelector(uiSettings, dataSource),
getDataSourceMenu: <T>() => createDataSourceMenu<T>(),
},
getDefaultDataSourceId,
Expand Down
Loading