Skip to content

Commit

Permalink
Polishing for kibana merge(elastic#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
CohenIdo authored and orouz committed Jan 13, 2022
1 parent d55c256 commit 5c6257c
Show file tree
Hide file tree
Showing 13 changed files with 273 additions and 240 deletions.
4 changes: 2 additions & 2 deletions x-pack/plugins/cloud_security_posture/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
*/
export const CSP_KUBEBEAT_INDEX_NAME = 'logs-k8s_cis*';
export const CSP_FINDINGS_INDEX_NAME = 'findings';
export const STATS_ROUTH_PATH = '/api/csp/stats';
export const FINDINGS_ROUTH_PATH = '/api/csp/finding';
export const STATS_ROUTE_PATH = '/api/csp/stats';
export const FINDINGS_ROUTE_PATH = '/api/csp/finding';
export const AGENT_LOGS_INDEX = 'logs-k8s_cis*';
export const RULE_PASSED = `passed`;
export const RULE_FAILED = `failed`;
9 changes: 9 additions & 0 deletions x-pack/plugins/cloud_security_posture/server/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

export const RULE_PASSED = `passed`;
export const RULE_FAILED = `failed`;
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@
import { elasticsearchClientMock } from 'src/core/server/elasticsearch/client/mocks';
import { createFindingsIndexTemplate } from './create_index_template';
const mockEsClient = elasticsearchClientMock.createClusterClient().asScoped().asInternalUser;

afterEach(() => {
mockEsClient.indices.putIndexTemplate.mockClear();
mockEsClient.indices.existsIndexTemplate.mockClear();
});
describe('create index template for findings', () => {
it('expect to find existing template', async () => {
mockEsClient.indices.existsIndexTemplate.mockResolvedValueOnce(
elasticsearchClientMock.createSuccessTransportRequestPromise(true)
);
const response = await createFindingsIndexTemplate(mockEsClient);
expect(mockEsClient.indices.putIndexTemplate.mock.calls.length).toEqual(0);
expect(response).toEqual(true);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,27 @@
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { MappingProperty } from '@elastic/elasticsearch/lib/api/types';
import type { MappingTypeMapping } from '@elastic/elasticsearch/lib/api/types';
import type { ElasticsearchClient } from 'src/core/server';
import { CSP_KUBEBEAT_INDEX_NAME, CSP_FINDINGS_INDEX_NAME } from '../..//common/constants';
import findingsIndexMapping from './findings_mapping.json';
import { CSP_KUBEBEAT_INDEX_NAME, CSP_FINDINGS_INDEX_NAME } from '../../common/constants';
import { mapping as findingsIndexMapping } from './findings_mapping';

export type Status = boolean;

const doesIndexTemplateExist = async (esClient: ElasticsearchClient, templateName: string) => {
try {
const isExisting = await esClient.indices.existsIndexTemplate({ name: templateName });
return isExisting.body;
const response = await esClient.indices.existsIndexTemplate({ name: templateName });
return response.body;
} catch (err) {
return false;
}
};

export const createIndexTemplate = async (
const createIndexTemplate = async (
esClient: ElasticsearchClient,
indexName: string,
indexPattern: string,
properties: Record<string, MappingProperty>
properties: MappingTypeMapping
): Promise<Status> => {
try {
const response = await esClient.indices.putIndexTemplate({
Expand All @@ -51,14 +51,14 @@ export const createFindingsIndexTemplate = async (
esClient: ElasticsearchClient
): Promise<Status> => {
try {
const isExisting = await doesIndexTemplateExist(esClient, CSP_FINDINGS_INDEX_NAME);
if (isExisting) return true;
const indexTemplateExists = await doesIndexTemplateExist(esClient, CSP_FINDINGS_INDEX_NAME);
if (indexTemplateExists) return true;
return await createIndexTemplate(
esClient,
CSP_FINDINGS_INDEX_NAME,
CSP_KUBEBEAT_INDEX_NAME,
// TODO: check why this cast is required
findingsIndexMapping as Record<string, MappingProperty>
findingsIndexMapping
);
} catch (err) {
// TODO: add logger
Expand Down

This file was deleted.

Loading

0 comments on commit 5c6257c

Please sign in to comment.