Skip to content

Commit

Permalink
[eem] use api key clients to check enablement state (#189502)
Browse files Browse the repository at this point in the history
  • Loading branch information
klacabane authored Jul 30, 2024
1 parent d88a1b4 commit fafdd16
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,21 @@
* 2.0.
*/

import moment from 'moment';
import { ElasticsearchClient } from '@kbn/core-elasticsearch-server';
import { SavedObjectsClientContract } from '@kbn/core-saved-objects-api-server';
import { getFakeKibanaRequest } from '@kbn/security-plugin/server/authentication/api_keys/fake_kibana_request';
import { EntityManagerServerSetup } from '../types';
import { EntityDiscoveryAPIKey } from './auth/api_key/api_key';

export function toArray<T>(maybeArray: T | T[] | undefined): T[] {
if (!maybeArray) {
return [];
}
if (Array.isArray(maybeArray)) {
return maybeArray;
}
return [maybeArray];
}

export const isValidRange = (from: string, to: string): boolean => {
if (moment(from).isAfter(to)) {
return false;
}
return true;
export const getClientsFromAPIKey = ({
apiKey,
server,
}: {
apiKey: EntityDiscoveryAPIKey;
server: EntityManagerServerSetup;
}): { esClient: ElasticsearchClient; soClient: SavedObjectsClientContract } => {
const fakeRequest = getFakeKibanaRequest({ id: apiKey.id, api_key: apiKey.apiKey });
const esClient = server.core.elasticsearch.client.asScoped(fakeRequest).asCurrentUser;
const soClient = server.core.savedObjects.getScopedClient(fakeRequest);
return { esClient, soClient };
};

export function isStringOrNonEmptyArray(
value: string | string[] | undefined
): value is string | string[] {
if (typeof value === 'undefined') {
return false;
}
if (Array.isArray(value) && value.length === 0) {
return false;
}
return true;
}

export function extractFieldValue<T>(maybeArray: T | T[] | undefined): T {
return toArray(maybeArray)[0];
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
} from '../../../common/errors';
import { findEntityDefinitions } from '../../lib/entities/find_entity_definition';
import { builtInDefinitions } from '../../lib/entities/built_in';
import { getClientsFromAPIKey } from '../../lib/utils';

export function checkEntityDiscoveryEnabledRoute<T extends RequestHandlerContext>({
router,
Expand Down Expand Up @@ -43,8 +44,7 @@ export function checkEntityDiscoveryEnabledRoute<T extends RequestHandlerContext
return res.ok({ body: { enabled: false, reason: ERROR_API_KEY_NOT_VALID } });
}

const esClient = (await context.core).elasticsearch.client.asCurrentUser;
const soClient = (await context.core).savedObjects.client;
const { esClient, soClient } = getClientsFromAPIKey({ apiKey, server });

const entityDiscoveryState = await Promise.all(
builtInDefinitions.map(async (builtInDefinition) => {
Expand Down

0 comments on commit fafdd16

Please sign in to comment.