Skip to content

Commit

Permalink
Merge branch 'main' into entity-risk-scoring-new-doc-link
Browse files Browse the repository at this point in the history
  • Loading branch information
kibanamachine authored Dec 6, 2023
2 parents 56e545e + 9b822b9 commit 2be2d0d
Show file tree
Hide file tree
Showing 8 changed files with 251 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

export const API_BASE_PATH = '/api/index_management';
export const INTERNAL_API_BASE_PATH = '/internal/index_management';

export const INDEX_PATTERNS = ['test*'];

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* 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.
*/

import expect from 'expect';
import { FtrProviderContext } from '../../../ftr_provider_context';

import { enrichPoliciesApi } from './lib/enrich_policies.api';
import { enrichPoliciesHelpers } from './lib/enrich_policies.helpers';

export default function ({ getService }: FtrProviderContext) {
const log = getService('log');

const { createIndex, deleteIndex, createEnrichPolicy } = enrichPoliciesHelpers(getService);

const { getAllEnrichPolicies, removeEnrichPolicy, executeEnrichPolicy } =
enrichPoliciesApi(getService);

describe('Enrich policies', function () {
const INDEX_NAME = `index-${Math.random()}`;
const POLICY_NAME = `policy-${Math.random()}`;

before(async () => {
try {
await createIndex(INDEX_NAME);
await createEnrichPolicy(POLICY_NAME, INDEX_NAME);
} catch (err) {
log.debug('[Setup error] Error creating test index and policy');
throw err;
}
});

after(async () => {
try {
await deleteIndex(INDEX_NAME);
} catch (err) {
log.debug('[Cleanup error] Error deleting test index');
throw err;
}
});

it('should list all policies', async () => {
const { body } = await getAllEnrichPolicies().expect(200);

expect(body).toStrictEqual([
{
enrichFields: ['firstName'],
matchField: 'email',
name: POLICY_NAME,
sourceIndices: [INDEX_NAME],
type: 'match',
},
]);
});

it('should be able to execute a policy', async () => {
await executeEnrichPolicy(POLICY_NAME).expect(200);

// Wait for a little bit for the policy to be executed, so that it can
// be deleted in the next test.
await new Promise((resolve) => setTimeout(resolve, 2000));
});

it('should be able to delete a policy', async () => {
const { body } = await removeEnrichPolicy(POLICY_NAME).expect(200);

expect(body).toStrictEqual({ acknowledged: true });
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default function ({ loadTestFile }: FtrProviderContext) {
loadTestFile(require.resolve('./component_templates'));
loadTestFile(require.resolve('./cluster_nodes'));
loadTestFile(require.resolve('./index_details'));
loadTestFile(require.resolve('./enrich_policies'));
loadTestFile(require.resolve('./create_enrich_policy'));
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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.
*/

import { INTERNAL_API_BASE_PATH } from '../constants';
import { FtrProviderContext } from '../../../../ftr_provider_context';

export function enrichPoliciesApi(getService: FtrProviderContext['getService']) {
const supertest = getService('supertest');

const getAllEnrichPolicies = () =>
supertest
.get(`${INTERNAL_API_BASE_PATH}/enrich_policies`)
.set('kbn-xsrf', 'xxx')
.set('x-elastic-internal-origin', 'xxx');

const executeEnrichPolicy = (name: string) =>
supertest
.put(`${INTERNAL_API_BASE_PATH}/enrich_policies/${name}`)
.set('kbn-xsrf', 'xxx')
.set('x-elastic-internal-origin', 'xxx');

const removeEnrichPolicy = (name: string) =>
supertest
.delete(`${INTERNAL_API_BASE_PATH}/enrich_policies/${name}`)
.set('kbn-xsrf', 'xxx')
.set('x-elastic-internal-origin', 'xxx');

return {
getAllEnrichPolicies,
removeEnrichPolicy,
executeEnrichPolicy,
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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.
*/

import { FtrProviderContext } from '../../../../ftr_provider_context';

export function enrichPoliciesHelpers(getService: FtrProviderContext['getService']) {
const es = getService('es');

const createEnrichPolicy = async (policyName: string, indexName: string) => {
await es.enrich.putPolicy({
name: policyName,
match: {
match_field: 'email',
enrich_fields: ['firstName'],
indices: [indexName],
},
});
};

const createIndex = async (indexName: string) => {
await es.indices.create({
index: indexName,
body: {
mappings: {
properties: {
email: {
type: 'text',
},
firstName: {
type: 'text',
},
},
},
},
});
};

const deleteIndex = async (indexName: string) => {
await es.indices.delete({ index: indexName });
};

return {
createEnrichPolicy,
createIndex,
deleteIndex,
};
}
6 changes: 6 additions & 0 deletions x-pack/test/api_integration/services/index_management.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import { componentTemplateHelpers } from '../apis/management/index_management/li
import { settingsApi } from '../apis/management/index_management/lib/settings.api';
import { clusterNodesApi } from '../apis/management/index_management/lib/cluster_nodes.api';
import { datastreamsHelpers } from '../apis/management/index_management/lib/datastreams.helpers';
import { enrichPoliciesApi } from '../apis/management/index_management/lib/enrich_policies.api';
import { enrichPoliciesHelpers } from '../apis/management/index_management/lib/enrich_policies.helpers';

export function IndexManagementProvider({ getService }: FtrProviderContext) {
return {
Expand Down Expand Up @@ -43,5 +45,9 @@ export function IndexManagementProvider({ getService }: FtrProviderContext) {
settings: {
api: settingsApi(getService),
},
enrichPolicies: {
api: enrichPoliciesApi(getService),
helpers: enrichPoliciesHelpers(getService),
},
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* 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.
*/

import expect from 'expect';
import { FtrProviderContext } from '../../../ftr_provider_context';

export default function ({ getService }: FtrProviderContext) {
const log = getService('log');
const indexManagementService = getService('indexManagement');

describe('Enrich policies', function () {
const INDEX_NAME = `index-${Math.random()}`;
const POLICY_NAME = `policy-${Math.random()}`;

let createIndex: typeof indexManagementService['enrichPolicies']['helpers']['createIndex'];
let deleteIndex: typeof indexManagementService['enrichPolicies']['helpers']['deleteIndex'];
let createEnrichPolicy: typeof indexManagementService['enrichPolicies']['helpers']['createEnrichPolicy'];

let getAllEnrichPolicies: typeof indexManagementService['enrichPolicies']['api']['getAllEnrichPolicies'];
let removeEnrichPolicy: typeof indexManagementService['enrichPolicies']['api']['removeEnrichPolicy'];
let executeEnrichPolicy: typeof indexManagementService['enrichPolicies']['api']['executeEnrichPolicy'];

before(async () => {
({
enrichPolicies: {
helpers: { createIndex, deleteIndex, createEnrichPolicy },
api: { getAllEnrichPolicies, removeEnrichPolicy, executeEnrichPolicy },
},
} = indexManagementService);

try {
await createIndex(INDEX_NAME);
await createEnrichPolicy(POLICY_NAME, INDEX_NAME);
} catch (err) {
log.debug('[Setup error] Error creating test index and policy');
throw err;
}
});

after(async () => {
try {
await deleteIndex(INDEX_NAME);
} catch (err) {
log.debug('[Cleanup error] Error deleting test index');
throw err;
}
});

it('should list all policies', async () => {
const { body } = await getAllEnrichPolicies().expect(200);

expect(body).toEqual([
{
enrichFields: ['firstName'],
matchField: 'email',
name: POLICY_NAME,
sourceIndices: [INDEX_NAME],
type: 'match',
},
]);
});

it('should be able to execute a policy', async () => {
await executeEnrichPolicy(POLICY_NAME).expect(200);

// Wait for a little bit for the policy to be executed, so that it can
// be deleted in the next test.
await new Promise((resolve) => setTimeout(resolve, 2000));
});

it('should be able to delete a policy', async () => {
const { body } = await removeEnrichPolicy(POLICY_NAME).expect(200);

expect(body).toEqual({ acknowledged: true });
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default function ({ loadTestFile }: FtrProviderContext) {

loadTestFile(require.resolve('./index_templates'));
loadTestFile(require.resolve('./indices'));
loadTestFile(require.resolve('./enrich_policies'));
loadTestFile(require.resolve('./create_enrich_policies'));
loadTestFile(require.resolve('./index_component_templates'));
loadTestFile(require.resolve('./cluster_nodes'));
Expand Down

0 comments on commit 2be2d0d

Please sign in to comment.