Skip to content

Commit

Permalink
[8.x] [ES body removal] @elastic/fleet (elastic#204867) (elastic#…
Browse files Browse the repository at this point in the history
…205009)

# Backport

This will backport the following commits from `main` to `8.x`:
- [[ES `body` removal] `@elastic/fleet`
(elastic#204867)](elastic#204867)

<!--- Backport version: 8.9.8 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Alejandro Fernández
Haro","email":"alejandro.haro@elastic.co"},"sourceCommit":{"committedDate":"2024-12-19T20:09:27Z","message":"[ES
`body` removal] `@elastic/fleet` (elastic#204867)\n\n## Summary\n\nAttempt to
remove the deprecated `body` in the ES
client.","sha":"d7ef161d8d8ddcfe652bb607a0c01002e985eae6","branchLabelMapping":{"^v9.0.0$":"main","^v8.18.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","Team:Fleet","v9.0.0","backport:prev-minor"],"number":204867,"url":"https://github.com/elastic/kibana/pull/204867","mergeCommit":{"message":"[ES
`body` removal] `@elastic/fleet` (elastic#204867)\n\n## Summary\n\nAttempt to
remove the deprecated `body` in the ES
client.","sha":"d7ef161d8d8ddcfe652bb607a0c01002e985eae6"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","labelRegex":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/204867","number":204867,"mergeCommit":{"message":"[ES
`body` removal] `@elastic/fleet` (elastic#204867)\n\n## Summary\n\nAttempt to
remove the deprecated `body` in the ES
client.","sha":"d7ef161d8d8ddcfe652bb607a0c01002e985eae6"}}]}]
BACKPORT-->
  • Loading branch information
afharo authored Dec 19, 2024
1 parent dec83ab commit 6038efa
Show file tree
Hide file tree
Showing 24 changed files with 98 additions and 110 deletions.
2 changes: 1 addition & 1 deletion x-pack/plugins/fleet/common/types/models/agent_policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import type { SecurityRoleDescriptor } from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import type { SecurityRoleDescriptor } from '@elastic/elasticsearch/lib/api/types';

import type { agentPolicyStatuses } from '../../constants';
import type { MonitoringType, PolicySecretReference, ValueOf } from '..';
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/fleet/common/types/models/epm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import type * as estypes from '@elastic/elasticsearch/lib/api/types';

import type {
ASSETS_SAVED_OBJECT_TYPE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type { Dictionary } from 'lodash';
import { keyBy, keys, merge } from 'lodash';
import type { RequestHandler } from '@kbn/core/server';
import pMap from 'p-map';
import type { IndicesDataStreamsStatsDataStreamsStatsItem } from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import type { IndicesDataStreamsStatsDataStreamsStatsItem } from '@elastic/elasticsearch/lib/api/types';
import { ByteSizeValue } from '@kbn/config-schema';

import type { DataStream } from '../../types';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import type {
SecurityIndicesPrivileges,
SecurityRoleDescriptor,
} from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
} from '@elastic/elasticsearch/lib/api/types';

import {
FLEET_APM_PACKAGE,
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/fleet/server/services/agent_policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import type {
} from '@kbn/core/server';
import { SavedObjectsUtils } from '@kbn/core/server';

import type { BulkResponseItem } from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import type { BulkResponseItem } from '@elastic/elasticsearch/lib/api/types';

import { DEFAULT_SPACE_ID } from '@kbn/spaces-plugin/common/constants';

Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/fleet/server/services/agents/action.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export function createClientMock() {

esClientMock.mget.mockResponseImplementation((params) => {
// @ts-expect-error
const docs = params?.body.docs.map(({ _id }) => {
const docs = params?.docs.map(({ _id }) => {
let result;
switch (_id) {
case agentInHostedDoc._id:
Expand Down
12 changes: 6 additions & 6 deletions x-pack/plugins/fleet/server/services/agents/actions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ describe('Agent actions', () => {

expect(esClient.create).toBeCalledWith(
expect.objectContaining({
body: expect.objectContaining({
document: expect.objectContaining({
signed: {
data: expect.any(String),
signature: expect.any(String),
Expand Down Expand Up @@ -180,7 +180,7 @@ describe('Agent actions', () => {

expect(esClient.create).toBeCalledWith(
expect.objectContaining({
body: expect.not.objectContaining({
document: expect.not.objectContaining({
signed: expect.any(Object),
}),
})
Expand Down Expand Up @@ -235,7 +235,7 @@ describe('Agent actions', () => {
await bulkCreateAgentActions(esClient, newActions);
expect(esClient.bulk).toHaveBeenCalledWith(
expect.objectContaining({
body: expect.arrayContaining([
operations: expect.arrayContaining([
expect.arrayContaining([
expect.objectContaining({
signed: {
Expand Down Expand Up @@ -274,7 +274,7 @@ describe('Agent actions', () => {
await bulkCreateAgentActions(esClient, newActions);
expect(esClient.bulk).toHaveBeenCalledWith(
expect.objectContaining({
body: expect.arrayContaining([
operations: expect.arrayContaining([
expect.arrayContaining([
expect.not.objectContaining({
signed: {
Expand Down Expand Up @@ -350,7 +350,7 @@ describe('Agent actions', () => {
expect(esClient.create).toBeCalledTimes(2);
expect(esClient.create).toBeCalledWith(
expect.objectContaining({
body: expect.objectContaining({
document: expect.objectContaining({
type: 'CANCEL',
data: { target_id: 'action1' },
agents: ['agent1', 'agent2'],
Expand All @@ -359,7 +359,7 @@ describe('Agent actions', () => {
);
expect(esClient.create).toBeCalledWith(
expect.objectContaining({
body: expect.objectContaining({
document: expect.objectContaining({
type: 'CANCEL',
data: { target_id: 'action1' },
agents: ['agent3', 'agent4'],
Expand Down
6 changes: 3 additions & 3 deletions x-pack/plugins/fleet/server/services/agents/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export async function createAgentAction(
await esClient.create({
index: AGENT_ACTIONS_INDEX,
id: uuidv4(),
body,
document: body,
refresh: 'wait_for',
});

Expand Down Expand Up @@ -112,7 +112,7 @@ export async function bulkCreateAgentActions(
const messageSigningService = appContextService.getMessageSigningService();
await esClient.bulk({
index: AGENT_ACTIONS_INDEX,
body: await Promise.all(
operations: await Promise.all(
actions.flatMap(async (action) => {
const body: FleetServerAgentAction = {
'@timestamp': new Date().toISOString(),
Expand Down Expand Up @@ -221,7 +221,7 @@ export async function bulkCreateAgentActionResults(

await esClient.bulk({
index: AGENT_ACTIONS_RESULTS_INDEX,
body: bulkBody,
operations: bulkBody,
refresh: 'wait_for',
});
}
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/fleet/server/services/agents/agent_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import type {
SavedObjectsClientContract,
} from '@kbn/core/server';

import type { AggregationsAggregationContainer } from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import type { AggregationsAggregationContainer } from '@elastic/elasticsearch/lib/api/types';

import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import type * as estypes from '@elastic/elasticsearch/lib/api/types';

import type { SortResults } from '@elastic/elasticsearch/lib/api/types';

Expand Down
6 changes: 2 additions & 4 deletions x-pack/plugins/fleet/server/services/agents/crud.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,7 @@ describe('Agents CRUD test', () => {
expect(searchMock).toHaveBeenCalledWith(
expect.objectContaining({
aggs: { tags: { terms: { field: 'tags', size: 10000 } } },
body: {
query: expect.any(Object),
},
query: expect.any(Object),
index: '.fleet-agents',
size: 0,
fields: ['status'],
Expand Down Expand Up @@ -164,7 +162,7 @@ describe('Agents CRUD test', () => {
})
);

expect(searchMock.mock.calls.at(-1)[0].body.query).toEqual(
expect(searchMock.mock.calls.at(-1)[0].query).toEqual(
toElasticsearchQuery(
_joinFilters(['fleet-agents.policy_id: 123', 'NOT status:unenrolled'])!
)
Expand Down
36 changes: 16 additions & 20 deletions x-pack/plugins/fleet/server/services/agents/crud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
*/

import { groupBy } from 'lodash';
import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import type * as estypes from '@elastic/elasticsearch/lib/api/types';
import type { SortResults } from '@elastic/elasticsearch/lib/api/types';
import type { SavedObjectsClientContract, ElasticsearchClient } from '@kbn/core/server';
import type { KueryNode } from '@kbn/es-query';
import { fromKueryExpression, toElasticsearchQuery } from '@kbn/es-query';
import { DEFAULT_SPACE_ID } from '@kbn/spaces-plugin/common';
import type { AggregationsAggregationContainer } from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import type { AggregationsAggregationContainer } from '@elastic/elasticsearch/lib/api/types';

import type { AgentSOAttributes, Agent, ListWithKuery } from '../../types';
import { appContextService, agentPolicyService } from '..';
Expand Down Expand Up @@ -169,13 +169,13 @@ export async function getAgentTags(
}

const kueryNode = _joinFilters(filters);
const body = kueryNode ? { query: toElasticsearchQuery(kueryNode) } : {};
const query = kueryNode ? { query: toElasticsearchQuery(kueryNode) } : {};
const runtimeFields = await buildAgentStatusRuntimeField(soClient);
try {
const result = await esClient.search<{}, { tags: { buckets: Array<{ key: string }> } }>({
index: AGENTS_INDEX,
size: 0,
body,
...query,
fields: Object.keys(runtimeFields),
runtime_mappings: runtimeFields,
aggs: {
Expand Down Expand Up @@ -546,17 +546,15 @@ export async function getAgentVersionsForAgentPolicyIds(
FleetServerAgent,
Record<'agent_versions', { buckets: Array<{ key: string; doc_count: number }> }>
>({
body: {
query: {
bool: {
filter: [
{
terms: {
policy_id: agentPolicyIds,
},
query: {
bool: {
filter: [
{
terms: {
policy_id: agentPolicyIds,
},
],
},
},
],
},
},
index: AGENTS_INDEX,
Expand Down Expand Up @@ -628,7 +626,7 @@ export async function updateAgent(
await esClient.update({
id: agentId,
index: AGENTS_INDEX,
body: { doc: agentSOAttributesToFleetServerAgentDoc(data) },
doc: agentSOAttributesToFleetServerAgentDoc(data),
refresh: 'wait_for',
});
}
Expand All @@ -645,7 +643,7 @@ export async function bulkUpdateAgents(
return;
}

const body = updateData.flatMap(({ agentId, data }) => [
const operations = updateData.flatMap(({ agentId, data }) => [
{
update: {
_id: agentId,
Expand All @@ -658,7 +656,7 @@ export async function bulkUpdateAgents(
]);

const res = await esClient.bulk({
body,
operations,
index: AGENTS_INDEX,
refresh: 'wait_for',
});
Expand All @@ -676,9 +674,7 @@ export async function deleteAgent(esClient: ElasticsearchClient, agentId: string
await esClient.update({
id: agentId,
index: AGENTS_INDEX,
body: {
doc: { active: false },
},
doc: { active: false },
});
} catch (err) {
if (isESClientError(err) && err.meta.statusCode === 404) {
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/fleet/server/services/agents/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import type * as estypes from '@elastic/elasticsearch/lib/api/types';
import type { SortResults } from '@elastic/elasticsearch/lib/api/types';
import type { SearchHit } from '@kbn/es-types';

Expand Down
14 changes: 7 additions & 7 deletions x-pack/plugins/fleet/server/services/agents/reassign.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import type * as estypes from '@elastic/elasticsearch/lib/api/types';

import { HostedAgentPolicyRestrictionRelatedError } from '../../errors';

Expand Down Expand Up @@ -40,7 +40,7 @@ describe('reassignAgent', () => {
expect(esClient.update).toBeCalledTimes(1);
const calledWith = esClient.update.mock.calls[0];
expect(calledWith[0]?.id).toBe(agentInRegularDoc._id);
expect((calledWith[0] as estypes.UpdateRequest)?.body?.doc).toHaveProperty(
expect((calledWith[0] as estypes.UpdateRequest)?.doc).toHaveProperty(
'policy_id',
regularAgentPolicySO.id
);
Expand Down Expand Up @@ -101,22 +101,22 @@ describe('reassignAgent', () => {
// calls ES update with correct values
const calledWith = esClient.bulk.mock.calls[0][0];
// only 1 are regular and bulk write two line per update
expect((calledWith as estypes.BulkRequest).body?.length).toBe(2);
expect((calledWith as estypes.BulkRequest).operations?.length).toBe(2);
// @ts-expect-error
expect(calledWith.body[0].update._id).toEqual(agentInRegularDoc._id);
expect(calledWith.operations[0].update._id).toEqual(agentInRegularDoc._id);

// hosted policy is updated in action results with error
const calledWithActionResults = esClient.bulk.mock.calls[1][0] as estypes.BulkRequest;
// bulk write two line per create
expect(calledWithActionResults.body?.length).toBe(4);
expect(calledWithActionResults.operations?.length).toBe(4);
const expectedObject = expect.objectContaining({
'@timestamp': expect.anything(),
action_id: expect.anything(),
agent_id: 'agent-in-hosted-policy',
error:
'Cannot reassign an agent from hosted agent policy hosted-agent-policy in Fleet because the agent policy is managed by an external orchestration solution, such as Elastic Cloud, Kubernetes, etc. Please make changes using your orchestration solution.',
});
expect(calledWithActionResults.body?.[1] as any).toEqual(expectedObject);
expect(calledWithActionResults.operations?.[1]).toEqual(expectedObject);
});

it('should report errors from ES agent update call', async () => {
Expand Down Expand Up @@ -147,7 +147,7 @@ describe('reassignAgent', () => {
agent_id: agentInRegularDoc._id,
error: 'version conflict',
});
expect(calledWithActionResults.body?.[1] as any).toEqual(expectedObject);
expect(calledWithActionResults.operations?.[1]).toEqual(expectedObject);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import type * as estypes from '@elastic/elasticsearch/lib/api/types';

import { appContextService } from '../app_context';
import { createAppContextStartContractMock } from '../../mocks';
Expand Down Expand Up @@ -33,7 +33,7 @@ describe('requestDiagnostics', () => {

expect(esClient.create).toHaveBeenCalledWith(
expect.objectContaining({
body: expect.objectContaining({
document: expect.objectContaining({
agents: ['agent-in-regular-policy'],
type: 'REQUEST_DIAGNOSTICS',
expiration: expect.anything(),
Expand All @@ -53,7 +53,7 @@ describe('requestDiagnostics', () => {

expect(esClient.create).toHaveBeenCalledWith(
expect.objectContaining({
body: expect.objectContaining({
document: expect.objectContaining({
agents: ['agent-in-regular-policy-newer', 'agent-in-regular-policy-newer2'],
type: 'REQUEST_DIAGNOSTICS',
expiration: expect.anything(),
Expand All @@ -70,7 +70,7 @@ describe('requestDiagnostics', () => {

expect(esClient.create).toHaveBeenCalledWith(
expect.objectContaining({
body: expect.objectContaining({
document: expect.objectContaining({
agents: ['agent-in-regular-policy-newer', 'agent-in-regular-policy'],
type: 'REQUEST_DIAGNOSTICS',
expiration: expect.anything(),
Expand All @@ -80,14 +80,14 @@ describe('requestDiagnostics', () => {
);
const calledWithActionResults = esClient.bulk.mock.calls[0][0] as estypes.BulkRequest;
// bulk write two line per create
expect(calledWithActionResults.body?.length).toBe(2);
expect(calledWithActionResults.operations?.length).toBe(2);
const expectedObject = expect.objectContaining({
'@timestamp': expect.anything(),
action_id: expect.anything(),
agent_id: 'agent-in-regular-policy',
error: 'Agent agent-in-regular-policy does not support request diagnostics action.',
});
expect(calledWithActionResults.body?.[1] as any).toEqual(expectedObject);
expect(calledWithActionResults.operations?.[1]).toEqual(expectedObject);
});
});
});
Loading

0 comments on commit 6038efa

Please sign in to comment.