Skip to content

Commit

Permalink
make the name of the index configurable
Browse files Browse the repository at this point in the history
Signed-off-by: Tianle Huang <[email protected]>
  • Loading branch information
tianleh committed Jan 8, 2024
1 parent 178fe61 commit 1b38a93
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 11 deletions.
4 changes: 4 additions & 0 deletions config/opensearch_dashboards.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
# dashboards. OpenSearch Dashboards creates a new index if the index doesn't already exist.
#opensearchDashboards.index: ".opensearch_dashboards"

# OpenSearch Dashboards uses an index in OpenSearch to store dynamic configurations.
# This shall be a different index from opensearchDashboards.index.
# opensearchDashboards.dynamic_config_index: ".opensearch_dashboards_config"

# The default application to load.
#opensearchDashboards.defaultAppId: "home"

Expand Down
1 change: 1 addition & 0 deletions src/core/server/opensearch_dashboards_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export const config = {
schema: schema.object({
enabled: schema.boolean({ defaultValue: true }),
index: schema.string({ defaultValue: '.kibana' }),
dynamic_config_index: schema.string({ defaultValue: '.opensearch_dashboards_config' }),
autocompleteTerminateAfter: schema.duration({ defaultValue: 100000 }),
autocompleteTimeout: schema.duration({ defaultValue: 1000 }),
branding: schema.object({
Expand Down
1 change: 1 addition & 0 deletions src/core/server/plugins/plugin_context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ describe('createPluginInitializerContext', () => {
expect(configObject).toStrictEqual({
opensearchDashboards: {
index: '.kibana',
dynamic_config_index: '.opensearch_dashboards_config',
autocompleteTerminateAfter: duration(100000),
autocompleteTimeout: duration(1000),
},
Expand Down
7 changes: 6 additions & 1 deletion src/core/server/plugins/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,12 @@ export interface Plugin<

export const SharedGlobalConfigKeys = {
// We can add more if really needed
opensearchDashboards: ['index', 'autocompleteTerminateAfter', 'autocompleteTimeout'] as const,
opensearchDashboards: [
'index',
'dynamic_config_index',
'autocompleteTerminateAfter',
'autocompleteTimeout',
] as const,
opensearch: ['shardTimeout', 'requestTimeout', 'pingTimeout'] as const,
path: ['data'] as const,
savedObjects: ['maxImportPayloadBytes'] as const,
Expand Down
1 change: 1 addition & 0 deletions src/legacy/server/config/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ export default () =>
opensearchDashboards: Joi.object({
enabled: Joi.boolean().default(true),
index: Joi.string().default('.kibana'),
dynamic_config_index: Joi.string().default('.opensearch_dashboards_config'),
autocompleteTerminateAfter: Joi.number().integer().min(1).default(100000),
// TODO Also allow units here like in opensearch config once this is moved to the new platform
autocompleteTimeout: Joi.number().integer().min(1).default(1000),
Expand Down
32 changes: 27 additions & 5 deletions src/plugins/csp_configuration_provider/server/csp_handlers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ const forgeRequest = ({
});
};

const DEFAULT_DYNAMIC_CONFIG_INDEX = '.opensearch_dashboards_config';

describe('CSP handlers', () => {
let toolkit: ReturnType<typeof httpServerMock.createToolkit>;

Expand All @@ -49,7 +51,11 @@ describe('CSP handlers', () => {

const getCspClient = jest.fn().mockReturnValue(cspClient);

const handler = createCspRulesPreResponseHandler(coreSetup, getCspClient);
const handler = createCspRulesPreResponseHandler(
coreSetup,
DEFAULT_DYNAMIC_CONFIG_INDEX,
getCspClient
);
const request = forgeRequest({ method: 'get', headers: { 'sec-fetch-dest': 'document' } });

toolkit.next.mockReturnValue('next' as any);
Expand Down Expand Up @@ -81,7 +87,11 @@ describe('CSP handlers', () => {

const getCspClient = jest.fn().mockReturnValue(cspClient);

const handler = createCspRulesPreResponseHandler(coreSetup, getCspClient);
const handler = createCspRulesPreResponseHandler(
coreSetup,
DEFAULT_DYNAMIC_CONFIG_INDEX,
getCspClient
);
const request = forgeRequest({ method: 'get', headers: { 'sec-fetch-dest': 'document' } });

toolkit.next.mockReturnValue('next' as any);
Expand All @@ -107,7 +117,11 @@ describe('CSP handlers', () => {

const getCspClient = jest.fn().mockReturnValue(cspClient);

const handler = createCspRulesPreResponseHandler(coreSetup, getCspClient);
const handler = createCspRulesPreResponseHandler(
coreSetup,
DEFAULT_DYNAMIC_CONFIG_INDEX,
getCspClient
);
const request = forgeRequest({ method: 'get', headers: { 'sec-fetch-dest': 'document' } });

toolkit.next.mockReturnValue('next' as any);
Expand All @@ -133,7 +147,11 @@ describe('CSP handlers', () => {

const getCspClient = jest.fn().mockReturnValue(cspClient);

const handler = createCspRulesPreResponseHandler(coreSetup, getCspClient);
const handler = createCspRulesPreResponseHandler(
coreSetup,
DEFAULT_DYNAMIC_CONFIG_INDEX,
getCspClient
);

const cssSecFetchDest = 'css';
const request = forgeRequest({ method: 'get', headers: { 'sec-fetch-dest': cssSecFetchDest } });
Expand Down Expand Up @@ -161,7 +179,11 @@ describe('CSP handlers', () => {

const getCspClient = jest.fn().mockReturnValue(cspClient);

const handler = createCspRulesPreResponseHandler(coreSetup, getCspClient);
const handler = createCspRulesPreResponseHandler(
coreSetup,
DEFAULT_DYNAMIC_CONFIG_INDEX,
getCspClient
);

const request = forgeRequest({ method: 'get', headers: {} });

Expand Down
6 changes: 3 additions & 3 deletions src/plugins/csp_configuration_provider/server/csp_handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
import { CoreSetup, OnPreResponseHandler, OpenSearchClient } from '../../../core/server';
import { CspClient } from './types';

const OPENSEARCH_DASHBOARDS_CONFIG_INDEX_NAME = '.opensearch_dashboards_config';
const OPENSEARCH_DASHBOARDS_CONFIG_DOCUMENT_NAME = 'csp.rules';

export function createCspRulesPreResponseHandler(
core: CoreSetup,
dynamicConfigIndex: string,
getCspClient: (inputOpenSearchClient: OpenSearchClient) => CspClient
): OnPreResponseHandler {
return async (request, response, toolkit) => {
Expand All @@ -26,14 +26,14 @@ export function createCspRulesPreResponseHandler(

const myClient = getCspClient(coreStart.opensearch.client.asInternalUser);

const existsValue = await myClient.exists(OPENSEARCH_DASHBOARDS_CONFIG_INDEX_NAME);
const existsValue = await myClient.exists(dynamicConfigIndex);

if (!existsValue) {
return toolkit.next({});
}

const cspRules = await myClient.get(
OPENSEARCH_DASHBOARDS_CONFIG_INDEX_NAME,
dynamicConfigIndex,
OPENSEARCH_DASHBOARDS_CONFIG_DOCUMENT_NAME
);

Expand Down
15 changes: 13 additions & 2 deletions src/plugins/csp_configuration_provider/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { Observable } from 'rxjs';
import { first } from 'rxjs/operators';
import {
CoreSetup,
CoreStart,
Logger,
OpenSearchClient,
Plugin,
PluginInitializerContext,
SharedGlobalConfig,
} from '../../../core/server';

import { createCspRulesPreResponseHandler } from './csp_handlers';
Expand All @@ -24,10 +27,12 @@ import {
export class CspConfigurationProviderPlugin
implements Plugin<CspConfigurationProviderPluginSetup, CspConfigurationProviderPluginStart> {
private readonly logger: Logger;
private readonly config$: Observable<SharedGlobalConfig>;
private cspClient: CspClient | undefined;

constructor(initializerContext: PluginInitializerContext) {
this.logger = initializerContext.logger.get();
this.config$ = initializerContext.config.legacy.globalConfig$;
}

private setCspClient(inputCspClient: CspClient) {
Expand All @@ -44,15 +49,21 @@ export class CspConfigurationProviderPlugin
return this.cspClient;
}

public setup(core: CoreSetup) {
public async setup(core: CoreSetup) {
this.logger.debug('CspConfigurationProvider: Setup');
const router = core.http.createRouter();

// Register server side APIs
defineRoutes(router);

const config = await this.config$.pipe(first()).toPromise();

core.http.registerOnPreResponse(
createCspRulesPreResponseHandler(core, this.getCspClient.bind(this))
createCspRulesPreResponseHandler(
core,
config.opensearchDashboards.dynamic_config_index,
this.getCspClient.bind(this)
)
);

return {
Expand Down

0 comments on commit 1b38a93

Please sign in to comment.