Skip to content

Commit

Permalink
update types
Browse files Browse the repository at this point in the history
Signed-off-by: Tianle Huang <tianleh@amazon.com>
  • Loading branch information
tianleh committed Apr 10, 2024
1 parent 667e834 commit c00bb41
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 19 deletions.
18 changes: 8 additions & 10 deletions src/plugins/application_config/server/opensearch_config_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ export class OpenSearchConfigurationClient implements ConfigurationClient {
private client: IScopedClusterClient;
private configurationIndexName: string;
private readonly logger: Logger;
private cache: LRUCache<string, string>;
private cache: LRUCache<string, string | undefined>;

constructor(
scopedClusterClient: IScopedClusterClient,
configurationIndexName: string,
logger: Logger,
cache: LRUCache<string, string>
cache: LRUCache<string, string | undefined>
) {
this.client = scopedClusterClient;
this.configurationIndexName = configurationIndexName;
Expand All @@ -29,21 +29,17 @@ export class OpenSearchConfigurationClient implements ConfigurationClient {
async getEntityConfig(entity: string) {
const entityValidated = validate(entity, this.logger);

const cachedValue = this.cache.get(entityValidated);

if (this.cache.has(entityValidated)) {
this.logger.info(`found value ${cachedValue} from cache`);
return cachedValue;
this.logger.info(`Key ${entityValidated} is found from cache.`);
return this.cache.get(entityValidated);
}

this.logger.info('No value found in cache');

try {
const data = await this.client.asInternalUser.get({
index: this.configurationIndexName,
id: entityValidated,
});
const value = data?.body?._source?.value || '';
const value = data?.body?._source?.value;

this.cache.set(entityValidated, value);

Expand All @@ -53,7 +49,7 @@ export class OpenSearchConfigurationClient implements ConfigurationClient {

this.logger.error(errorMessage);

this.cache.set(entityValidated, '');
this.cache.set(entityValidated, undefined);
throw e;
}
}
Expand Down Expand Up @@ -98,11 +94,13 @@ export class OpenSearchConfigurationClient implements ConfigurationClient {
} catch (e) {
if (e?.body?.error?.type === 'index_not_found_exception') {
this.logger.info('Attemp to delete a not found index.');
this.cache.del(entityValidated);
return entityValidated;
}

if (e?.body?.result === 'not_found') {
this.logger.info('Attemp to delete a not found document.');
this.cache.del(entityValidated);
return entityValidated;
}

Expand Down
7 changes: 3 additions & 4 deletions src/plugins/application_config/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
CoreStart,
Plugin,
Logger,
IScopedClusterClient,
SharedGlobalConfig,
OpenSearchDashboardsRequest,
IClusterClient,
Expand All @@ -36,7 +35,7 @@ export class ApplicationConfigPlugin
private configurationIndexName: string;
private clusterClient: IClusterClient;

private cache: LRUCache<string, string>;
private cache: LRUCache<string, string | undefined>;

constructor(initializerContext: PluginInitializerContext) {
this.logger = initializerContext.logger.get();
Expand All @@ -45,8 +44,8 @@ export class ApplicationConfigPlugin
this.clusterClient = null;

this.cache = new LRUCache({
max: 30,
maxAge: 100 * 1000,
max: 100, // at most 100 entries
maxAge: 10 * 60 * 1000, // 10 mins
});
}

Expand Down
4 changes: 2 additions & 2 deletions src/plugins/application_config/server/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { IScopedClusterClient, Headers } from 'src/core/server';
import { Headers, OpenSearchDashboardsRequest } from 'src/core/server';

export interface ApplicationConfigPluginSetup {
getConfigurationClient: (inputOpenSearchClient: IScopedClusterClient) => ConfigurationClient;
getConfigurationClient: (request: OpenSearchDashboardsRequest) => ConfigurationClient;
registerConfigurationClient: (inputConfigurationClient: ConfigurationClient) => void;
}
// eslint-disable-next-line @typescript-eslint/no-empty-interface
Expand Down
4 changes: 1 addition & 3 deletions src/plugins/csp_handler/server/csp_handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const CSP_RULES_CONFIG_KEY = 'csp.rules';
export function createCspRulesPreResponseHandler(
core: CoreSetup,
cspHeader: string,
getConfigurationClient: (scopedClusterClient: IScopedClusterClient) => ConfigurationClient,
getConfigurationClient: (request: OpenSearchDashboardsRequest) => ConfigurationClient,
logger: Logger
): OnPreResponseHandler {
return async (
Expand All @@ -47,8 +47,6 @@ export function createCspRulesPreResponseHandler(
return toolkit.next({});
}

const [coreStart] = await core.getStartServices();

const client = getConfigurationClient(request);

const cspRules = await client.getEntityConfig(CSP_RULES_CONFIG_KEY, {
Expand Down

0 comments on commit c00bb41

Please sign in to comment.