Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fleet] Migrate ES client #92805

Merged
merged 42 commits into from
Mar 8, 2021
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
2d00471
Migrate `deletePipeline` to new ESclient
Feb 25, 2021
0b52acc
Migrate `installILMPolicy` to new client
Feb 25, 2021
b6af091
Remove dead function
Feb 25, 2021
c3add82
Migrate `deleteIlms` to new client
Feb 25, 2021
75be7c2
Migrate `deleteTemplate` to new client
Feb 25, 2021
f8ca0f3
Migrate `deleteTransports` to new client
Feb 25, 2021
5e553a0
Remove old client from `deleteAssets`
Feb 25, 2021
2c17bf8
Remove old client from `removeInstallation`
Feb 25, 2021
2e02613
Migrate `updateCurrentWriteIndices` to new client
Feb 25, 2021
1c1a9d5
Remove old client from `installTransform`
Feb 25, 2021
cf61b1f
Migrate `installTemplates` to new client
Feb 25, 2021
63a9634
Migrate `installPipelines` to new client
Feb 25, 2021
dbf08af
Migrate `installIlmForDataStream` to new client
Feb 25, 2021
82b5955
Remove old client from `installPackage`
Feb 25, 2021
56b4ad1
Remove old client from `bulkInstallPackages`
Feb 25, 2021
d842493
Remove legacy client from `ensurePackagesCompletedInstall`
Feb 25, 2021
95db78c
Remove legacy client from `addPackageToAgentPolicy`
Feb 25, 2021
a31a6d7
Migrate `setupIngestManager` to new client
Feb 25, 2021
59536b5
Fix types inside `createSetupSideEffects`
Feb 25, 2021
82270f7
Fix failing jest tests
Mar 1, 2021
a797237
Properly handle `resource_already_exists_exception` errors
Mar 1, 2021
8401da6
Cleanup test file
Mar 1, 2021
3ecbde6
Remove dead function
Mar 2, 2021
14f1965
Migrate `getListHandler` to new client
Mar 2, 2021
8ddaf75
Use `putIndexTemplate` method
Mar 2, 2021
b304493
Use `ingest.putPipeline` method
Mar 2, 2021
c075e85
Use `cluster.putComponentTemplate`
Mar 2, 2021
8cbb052
Cleanup references
Mar 2, 2021
d27cecc
Merge branch 'master' into 74111-migrate-es-client
kibanamachine Mar 2, 2021
09d5836
Handle v1 templates
Mar 2, 2021
32f4ca8
Use specific index template API methods
Mar 2, 2021
93a7a28
Use specific data stream API methods
Mar 2, 2021
8e850a3
Use specific security API methods
Mar 2, 2021
9b34fc7
Use specific transform API methods
Mar 2, 2021
fae1df1
Fix tests
Mar 2, 2021
b5f73c3
Fix installTemplat tests
Mar 3, 2021
0198e02
Merge branch 'master' into 74111-migrate-es-client
Mar 3, 2021
f6f094a
Merge branch 'master' into 74111-migrate-es-client
kibanamachine Mar 3, 2021
fabd93e
Run `node scripts/build_api_docs` locally to try to fix the build
Mar 3, 2021
d5f4f02
Merge branch 'master' into 74111-migrate-es-client
kibanamachine Mar 3, 2021
9df6b37
Merge branch 'master' into 74111-migrate-es-client
Mar 8, 2021
c6c3c0a
Merge branch 'master' into 74111-migrate-es-client
Mar 8, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions x-pack/plugins/fleet/server/routes/epm/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ export const installPackageFromRegistryHandler: RequestHandler<
> = async (context, request, response) => {
const savedObjectsClient = context.core.savedObjects.client;
const callCluster = context.core.elasticsearch.legacy.client.callAsCurrentUser;
const esClient = context.core.elasticsearch.client.asCurrentUser;
const { pkgkey } = request.params;
const { pkgName, pkgVersion } = splitPkgKey(pkgkey);
const installedPkg = await getInstallationObject({ savedObjectsClient, pkgName });
Expand All @@ -234,6 +235,7 @@ export const installPackageFromRegistryHandler: RequestHandler<
savedObjectsClient,
pkgkey,
callCluster,
esClient,
force: request.body?.force,
});
const body: InstallPackageResponse = {
Expand All @@ -249,6 +251,7 @@ export const installPackageFromRegistryHandler: RequestHandler<
pkgVersion,
installedPkg,
callCluster,
esClient,
});

return defaultResult;
Expand Down Expand Up @@ -277,9 +280,11 @@ export const bulkInstallPackagesFromRegistryHandler: RequestHandler<
> = async (context, request, response) => {
const savedObjectsClient = context.core.savedObjects.client;
const callCluster = context.core.elasticsearch.legacy.client.callAsCurrentUser;
const esClient = context.core.elasticsearch.client.asCurrentUser;
const bulkInstalledResponses = await bulkInstallPackages({
savedObjectsClient,
callCluster,
esClient,
packagesToUpgrade: request.body.packages,
});
const payload = bulkInstalledResponses.map(bulkInstallServiceResponseToHttpEntry);
Expand All @@ -302,13 +307,15 @@ export const installPackageByUploadHandler: RequestHandler<
}
const savedObjectsClient = context.core.savedObjects.client;
const callCluster = context.core.elasticsearch.legacy.client.callAsCurrentUser;
const esClient = context.core.elasticsearch.client.asCurrentUser;
const contentType = request.headers['content-type'] as string; // from types it could also be string[] or undefined but this is checked later
const archiveBuffer = Buffer.from(request.body);
try {
const res = await installPackage({
installSource: 'upload',
savedObjectsClient,
callCluster,
esClient,
archiveBuffer,
contentType,
});
Expand All @@ -327,8 +334,8 @@ export const deletePackageHandler: RequestHandler<
try {
const { pkgkey } = request.params;
const savedObjectsClient = context.core.savedObjects.client;
const callCluster = context.core.elasticsearch.legacy.client.callAsCurrentUser;
const res = await removeInstallation({ savedObjectsClient, pkgkey, callCluster });
const esClient = context.core.elasticsearch.client.asCurrentUser;
const res = await removeInstallation({ savedObjectsClient, pkgkey, esClient });
const body: DeletePackageResponse = {
response: res,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { SavedObjectsClientContract } from 'kibana/server';
import { ElasticsearchClient, SavedObjectsClientContract } from 'kibana/server';
import {
ElasticsearchAssetType,
EsAssetReference,
Expand All @@ -32,6 +32,7 @@ export const installIlmForDataStream = async (
registryPackage: InstallablePackage,
paths: string[],
callCluster: CallESAsCurrentUser,
esClient: ElasticsearchClient,
savedObjectsClient: SavedObjectsClientContract
) => {
const installation = await getInstallation({ savedObjectsClient, pkgName: registryPackage.name });
Expand All @@ -44,7 +45,7 @@ export const installIlmForDataStream = async (

// delete all previous ilm
await deleteIlms(
callCluster,
esClient,
previousInstalledIlmEsAssets.map((asset) => asset.id)
);
// install the latest dataset
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,22 @@
* 2.0.
*/

import { SavedObjectsClientContract } from 'kibana/server';
import { CallESAsCurrentUser, ElasticsearchAssetType, EsAssetReference } from '../../../../types';
import { ElasticsearchClient, SavedObjectsClientContract } from 'kibana/server';
import { ElasticsearchAssetType, EsAssetReference } from '../../../../types';
import { PACKAGES_SAVED_OBJECT_TYPE } from '../../../../../common/constants';

export const deleteIlms = async (callCluster: CallESAsCurrentUser, ilmPolicyIds: string[]) => {
export const deleteIlms = async (esClient: ElasticsearchClient, ilmPolicyIds: string[]) => {
await Promise.all(
ilmPolicyIds.map(async (ilmPolicyId) => {
await callCluster('transport.request', {
method: 'DELETE',
path: `_ilm/policy/${ilmPolicyId}`,
ignore: [404, 400],
});
await esClient.transport.request(
{
method: 'DELETE',
path: `_ilm/policy/${ilmPolicyId}`,
},
{
ignore: [404, 400],
}
);
})
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
* 2.0.
*/

import { CallESAsCurrentUser, ElasticsearchAssetType } from '../../../../types';
import { ElasticsearchClient } from 'kibana/server';
import { ElasticsearchAssetType } from '../../../../types';
import { getAsset, getPathParts } from '../../archive';

export async function installILMPolicy(paths: string[], callCluster: CallESAsCurrentUser) {
export async function installILMPolicy(paths: string[], esClient: ElasticsearchClient) {
const ilmPaths = paths.filter((path) => isILMPolicy(path));
if (!ilmPaths.length) return;
await Promise.all(
Expand All @@ -17,7 +18,7 @@ export async function installILMPolicy(paths: string[], callCluster: CallESAsCur
const { file } = getPathParts(path);
const name = file.substr(0, file.lastIndexOf('.'));
try {
await callCluster('transport.request', {
await esClient.transport.request({
method: 'PUT',
path: '/_ilm/policy/' + name,
body,
Expand All @@ -28,19 +29,8 @@ export async function installILMPolicy(paths: string[], callCluster: CallESAsCur
})
);
}

const isILMPolicy = (path: string) => {
const pathParts = getPathParts(path);
return pathParts.type === ElasticsearchAssetType.ilmPolicy;
};
export async function policyExists(
name: string,
callCluster: CallESAsCurrentUser
): Promise<boolean> {
const response = await callCluster('transport.request', {
method: 'GET',
path: '/_ilm/policy/?filter_path=' + name,
});

// If the response contains a key, it means the policy exists
return Object.keys(response).length > 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
* 2.0.
*/

import { SavedObjectsClientContract } from 'src/core/server';
import { ElasticsearchClient, SavedObjectsClientContract } from 'src/core/server';
import { appContextService } from '../../../';
import { CallESAsCurrentUser, ElasticsearchAssetType } from '../../../../types';
import { ElasticsearchAssetType } from '../../../../types';
import { IngestManagerError } from '../../../../errors';
import { getInstallation } from '../../packages/get';
import { PACKAGES_SAVED_OBJECT_TYPE, EsAssetReference } from '../../../../../common';

export const deletePreviousPipelines = async (
callCluster: CallESAsCurrentUser,
esClient: ElasticsearchClient,
savedObjectsClient: SavedObjectsClientContract,
pkgName: string,
previousPkgVersion: string
Expand All @@ -27,7 +27,7 @@ export const deletePreviousPipelines = async (
type === ElasticsearchAssetType.ingestPipeline && id.includes(previousPkgVersion)
);
const deletePipelinePromises = installedPipelines.map(({ type, id }) => {
return deletePipeline(callCluster, id);
return deletePipeline(esClient, id);
});
try {
await Promise.all(deletePipelinePromises);
Expand Down Expand Up @@ -56,11 +56,11 @@ export const deletePipelineRefs = async (
installed_es: filteredAssets,
});
};
export async function deletePipeline(callCluster: CallESAsCurrentUser, id: string): Promise<void> {
export async function deletePipeline(esClient: ElasticsearchClient, id: string): Promise<void> {
// '*' shouldn't ever appear here, but it still would delete all ingest pipelines
if (id && id !== '*') {
try {
await callCluster('ingest.deletePipeline', { id });
await esClient.ingest.deletePipeline({ id });
} catch (err) {
// Only throw if error is not a 404 error. Sometimes the pipeline is already deleted, but we have
// duplicate references to them, see https://github.com/elastic/kibana/issues/91192
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@ import { elasticsearchServiceMock } from 'src/core/server/mocks';
import { installTemplate } from './install';

test('tests installPackage to use correct priority and index_patterns for data stream with dataset_is_prefix not set', async () => {
const callCluster = elasticsearchServiceMock.createLegacyScopedClusterClient().callAsCurrentUser;
callCluster.mockImplementation(async (_, params) => {
const esClient = elasticsearchServiceMock.createClusterClient().asInternalUser;
esClient.transport.request.mockImplementation((params) => {
if (
params &&
params.method === 'GET' &&
params.path === '/_index_template/metrics-package.dataset'
) {
return { index_templates: [] };
return elasticsearchServiceMock.createSuccessTransportRequestPromise({ index_templates: [] });
}
return elasticsearchServiceMock.createSuccessTransportRequestPromise({});
});

const fields: Field[] = [];
Expand All @@ -40,7 +41,7 @@ test('tests installPackage to use correct priority and index_patterns for data s
const templateIndexPatternDatasetIsPrefixUnset = 'metrics-package.dataset-*';
const templatePriorityDatasetIsPrefixUnset = 200;
await installTemplate({
callCluster,
esClient,
fields,
dataStream: dataStreamDatasetIsPrefixUnset,
packageVersion: pkg.version,
Expand All @@ -54,15 +55,16 @@ test('tests installPackage to use correct priority and index_patterns for data s
});

test('tests installPackage to use correct priority and index_patterns for data stream with dataset_is_prefix set to false', async () => {
const callCluster = elasticsearchServiceMock.createLegacyScopedClusterClient().callAsCurrentUser;
callCluster.mockImplementation(async (_, params) => {
const esClient = elasticsearchServiceMock.createClusterClient().asInternalUser;
esClient.transport.request.mockImplementation((params) => {
if (
params &&
params.method === 'GET' &&
params.path === '/_index_template/metrics-package.dataset'
) {
return { index_templates: [] };
return elasticsearchServiceMock.createSuccessTransportRequestPromise({ index_templates: [] });
}
return elasticsearchServiceMock.createSuccessTransportRequestPromise({});
});

const fields: Field[] = [];
Expand All @@ -83,7 +85,7 @@ test('tests installPackage to use correct priority and index_patterns for data s
const templateIndexPatternDatasetIsPrefixFalse = 'metrics-package.dataset-*';
const templatePriorityDatasetIsPrefixFalse = 200;
await installTemplate({
callCluster,
esClient,
fields,
dataStream: dataStreamDatasetIsPrefixFalse,
packageVersion: pkg.version,
Expand All @@ -97,15 +99,16 @@ test('tests installPackage to use correct priority and index_patterns for data s
});

test('tests installPackage to use correct priority and index_patterns for data stream with dataset_is_prefix set to true', async () => {
const callCluster = elasticsearchServiceMock.createLegacyScopedClusterClient().callAsCurrentUser;
callCluster.mockImplementation(async (_, params) => {
const esClient = elasticsearchServiceMock.createClusterClient().asInternalUser;
esClient.transport.request.mockImplementation((params) => {
if (
params &&
params.method === 'GET' &&
params.path === '/_index_template/metrics-package.dataset'
) {
return { index_templates: [] };
return elasticsearchServiceMock.createSuccessTransportRequestPromise({ index_templates: [] });
}
return elasticsearchServiceMock.createSuccessTransportRequestPromise({});
});

const fields: Field[] = [];
Expand All @@ -126,7 +129,7 @@ test('tests installPackage to use correct priority and index_patterns for data s
const templateIndexPatternDatasetIsPrefixTrue = 'metrics-package.dataset.*-*';
const templatePriorityDatasetIsPrefixTrue = 150;
await installTemplate({
callCluster,
esClient,
fields,
dataStream: dataStreamDatasetIsPrefixTrue,
packageVersion: pkg.version,
Expand All @@ -140,14 +143,14 @@ test('tests installPackage to use correct priority and index_patterns for data s
});

test('tests installPackage remove the aliases property if the property existed', async () => {
const callCluster = elasticsearchServiceMock.createLegacyScopedClusterClient().callAsCurrentUser;
callCluster.mockImplementation(async (_, params) => {
const esClient = elasticsearchServiceMock.createClusterClient().asInternalUser;
esClient.transport.request.mockImplementation((params) => {
if (
params &&
params.method === 'GET' &&
params.path === '/_index_template/metrics-package.dataset'
) {
return {
return elasticsearchServiceMock.createSuccessTransportRequestPromise({
index_templates: [
{
name: 'metrics-package.dataset',
Expand All @@ -157,8 +160,9 @@ test('tests installPackage remove the aliases property if the property existed',
},
},
],
};
});
}
return elasticsearchServiceMock.createSuccessTransportRequestPromise({});
});

const fields: Field[] = [];
Expand All @@ -178,7 +182,7 @@ test('tests installPackage remove the aliases property if the property existed',
const templateIndexPatternDatasetIsPrefixUnset = 'metrics-package.dataset-*';
const templatePriorityDatasetIsPrefixUnset = 200;
await installTemplate({
callCluster,
esClient,
fields,
dataStream: dataStreamDatasetIsPrefixUnset,
packageVersion: pkg.version,
Expand Down
Loading