Skip to content

Commit

Permalink
[ES body removal] @elastic/fleet
Browse files Browse the repository at this point in the history
  • Loading branch information
afharo committed Dec 19, 2024
1 parent 9b0879c commit c810264
Show file tree
Hide file tree
Showing 19 changed files with 56 additions and 62 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
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
4 changes: 2 additions & 2 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
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
12 changes: 6 additions & 6 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);

// 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 @@ -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);
});
});
});
45 changes: 21 additions & 24 deletions x-pack/plugins/fleet/server/services/agents/unenroll.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 { AGENT_ACTIONS_INDEX, AGENT_ACTIONS_RESULTS_INDEX } from '../../../common';

Expand Down Expand Up @@ -48,9 +48,7 @@ describe('unenroll', () => {
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).toHaveProperty(
'doc.unenrollment_started_at'
);
expect(calledWith[0] as estypes.UpdateRequest).toHaveProperty('doc.unenrollment_started_at');
});

it('cannot unenroll from hosted agent policy by default', async () => {
Expand Down Expand Up @@ -78,9 +76,7 @@ describe('unenroll', () => {
expect(esClient.update).toBeCalledTimes(1);
const calledWith = esClient.update.mock.calls[0];
expect(calledWith[0]?.id).toBe(agentInHostedDoc._id);
expect((calledWith[0] as estypes.UpdateRequest)?.body).toHaveProperty(
'doc.unenrollment_started_at'
);
expect(calledWith[0] as estypes.UpdateRequest).toHaveProperty('doc.unenrollment_started_at');
});

it('can unenroll from hosted agent policy with force=true and revoke=true', async () => {
Expand All @@ -90,7 +86,7 @@ describe('unenroll', () => {
expect(esClient.update).toBeCalledTimes(1);
const calledWith = esClient.update.mock.calls[0];
expect(calledWith[0]?.id).toBe(agentInHostedDoc._id);
expect((calledWith[0] as estypes.UpdateRequest)?.body).toHaveProperty('doc.unenrolled_at');
expect(calledWith[0] as estypes.UpdateRequest).toHaveProperty('doc.unenrolled_at');
});
});

Expand All @@ -102,10 +98,10 @@ describe('unenroll', () => {

// calls ES update with correct values
const calledWith = esClient.bulk.mock.calls[0][0];
const ids = (calledWith as estypes.BulkRequest)?.body
const ids = (calledWith as estypes.BulkRequest)?.operations
?.filter((i: any) => i.update !== undefined)
.map((i: any) => i.update._id);
const docs = (calledWith as estypes.BulkRequest)?.body
const docs = (calledWith as estypes.BulkRequest)?.operations
?.filter((i: any) => i.doc)
.map((i: any) => i.doc);
expect(ids).toEqual(idsToUnenroll);
Expand All @@ -123,10 +119,10 @@ describe('unenroll', () => {
// calls ES update with correct values
const onlyRegular = [agentInRegularDoc._id, agentInRegularDoc2._id];
const calledWith = esClient.bulk.mock.calls[0][0];
const ids = (calledWith as estypes.BulkRequest)?.body
const ids = (calledWith as estypes.BulkRequest)?.operations
?.filter((i: any) => i.update !== undefined)
.map((i: any) => i.update._id);
const docs = (calledWith as estypes.BulkRequest)?.body
const docs = (calledWith as estypes.BulkRequest)?.operations
?.filter((i: any) => i.doc)
.map((i: any) => i.doc);
expect(ids).toEqual(onlyRegular);
Expand All @@ -137,15 +133,15 @@ describe('unenroll', () => {
// 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(2);
expect(calledWithActionResults.operations?.length).toBe(2);
const expectedObject = expect.objectContaining({
'@timestamp': expect.anything(),
action_id: expect.anything(),
agent_id: 'agent-in-hosted-policy',
error:
'Cannot unenroll agent-in-hosted-policy from a 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('force unenroll updates in progress unenroll actions', async () => {
Expand Down Expand Up @@ -186,7 +182,8 @@ describe('unenroll', () => {
});

expect(esClient.bulk.mock.calls.length).toEqual(3);
const bulkBody = (esClient.bulk.mock.calls[2][0] as estypes.BulkRequest)?.body?.[1] as any;
const bulkBody = (esClient.bulk.mock.calls[2][0] as estypes.BulkRequest)
?.operations?.[1] as any;
expect(bulkBody.agent_id).toEqual(agentInRegularDoc._id);
expect(bulkBody.action_id).toEqual('other-action');
});
Expand Down Expand Up @@ -248,10 +245,10 @@ describe('unenroll', () => {
// calls ES update with correct values
const onlyRegular = [agentInRegularDoc._id, agentInRegularDoc2._id];
const calledWith = esClient.bulk.mock.calls[0][0];
const ids = (calledWith as estypes.BulkRequest)?.body
const ids = (calledWith as estypes.BulkRequest)?.operations
?.filter((i: any) => i.update !== undefined)
.map((i: any) => i.update._id);
const docs = (calledWith as estypes.BulkRequest)?.body
const docs = (calledWith as estypes.BulkRequest)?.operations
?.filter((i: any) => i.doc)
.map((i: any) => i.doc);
expect(ids).toEqual(onlyRegular);
Expand All @@ -260,13 +257,13 @@ describe('unenroll', () => {
}

const errorResults = esClient.bulk.mock.calls[2][0];
const errorIds = (errorResults as estypes.BulkRequest)?.body
const errorIds = (errorResults as estypes.BulkRequest)?.operations
?.filter((i: any) => i.agent_id)
.map((i: any) => i.agent_id);
expect(errorIds).toEqual([agentInHostedDoc._id]);

const actionResults = esClient.bulk.mock.calls[1][0];
const resultIds = (actionResults as estypes.BulkRequest)?.body
const resultIds = (actionResults as estypes.BulkRequest)?.operations
?.filter((i: any) => i.agent_id)
.map((i: any) => i.agent_id);
expect(resultIds).toEqual(onlyRegular);
Expand All @@ -283,10 +280,10 @@ describe('unenroll', () => {

// calls ES update with correct values
const calledWith = esClient.bulk.mock.calls[0][0];
const ids = (calledWith as estypes.BulkRequest)?.body
const ids = (calledWith as estypes.BulkRequest)?.operations
?.filter((i: any) => i.update !== undefined)
.map((i: any) => i.update._id);
const docs = (calledWith as estypes.BulkRequest)?.body
const docs = (calledWith as estypes.BulkRequest)?.operations
?.filter((i: any) => i.doc)
.map((i: any) => i.doc);
expect(ids).toEqual(idsToUnenroll);
Expand All @@ -311,10 +308,10 @@ describe('unenroll', () => {

// calls ES update with correct values
const calledWith = esClient.bulk.mock.calls[0][0];
const ids = (calledWith as estypes.BulkRequest)?.body
const ids = (calledWith as estypes.BulkRequest)?.operations
?.filter((i: any) => i.update !== undefined)
.map((i: any) => i.update._id);
const docs = (calledWith as estypes.BulkRequest)?.body
const docs = (calledWith as estypes.BulkRequest)?.operations
?.filter((i: any) => i.doc)
.map((i: any) => i.doc);
expect(ids).toEqual(idsToUnenroll);
Expand All @@ -323,7 +320,7 @@ describe('unenroll', () => {
}

const actionResults = esClient.bulk.mock.calls[1][0];
const resultIds = (actionResults as estypes.BulkRequest)?.body
const resultIds = (actionResults as estypes.BulkRequest)?.operations
?.filter((i: any) => i.agent_id)
.map((i: any) => i.agent_id);
expect(resultIds).toEqual(idsToUnenroll);
Expand Down
18 changes: 9 additions & 9 deletions x-pack/plugins/fleet/server/services/agents/upgrade.test.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 { appContextService } from '../app_context';
import type { Agent } from '../../types';
Expand Down Expand Up @@ -58,10 +58,10 @@ describe('sendUpgradeAgentsActions (plural)', () => {

// calls ES update with correct values
const calledWith = esClient.bulk.mock.calls[0][0];
const ids = (calledWith as estypes.BulkRequest)?.body
const ids = (calledWith as estypes.BulkRequest)?.operations
?.filter((i: any) => i.update !== undefined)
.map((i: any) => i.update._id);
const docs = (calledWith as estypes.BulkRequest)?.body
const docs = (calledWith as estypes.BulkRequest)?.operations
?.filter((i: any) => i.doc)
.map((i: any) => i.doc);

Expand All @@ -80,10 +80,10 @@ describe('sendUpgradeAgentsActions (plural)', () => {
// calls ES update with correct values
const onlyRegular = [agentInRegularDoc._id, agentInRegularDoc2._id];
const calledWith = esClient.bulk.mock.calls[0][0];
const ids = (calledWith as estypes.BulkRequest)?.body
const ids = (calledWith as estypes.BulkRequest)?.operations
?.filter((i: any) => i.update !== undefined)
.map((i: any) => i.update._id);
const docs = (calledWith as estypes.BulkRequest)?.body
const docs = (calledWith as estypes.BulkRequest)?.operations
?.filter((i: any) => i.doc)
.map((i: any) => i.doc);
expect(ids).toEqual(onlyRegular);
Expand All @@ -95,15 +95,15 @@ describe('sendUpgradeAgentsActions (plural)', () => {
// 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(2);
expect(calledWithActionResults.operations?.length).toBe(2);
const expectedObject = expect.objectContaining({
'@timestamp': expect.anything(),
action_id: expect.anything(),
agent_id: 'agent-in-hosted-policy',
error:
'Cannot upgrade agent in 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('can upgrade from hosted agent policy with force=true', async () => {
Expand All @@ -118,10 +118,10 @@ describe('sendUpgradeAgentsActions (plural)', () => {

// calls ES update with correct values
const calledWith = esClient.bulk.mock.calls[0][0];
const ids = (calledWith as estypes.BulkRequest)?.body
const ids = (calledWith as estypes.BulkRequest)?.operations
?.filter((i: any) => i.update !== undefined)
.map((i: any) => i.update._id);
const docs = (calledWith as estypes.BulkRequest)?.body
const docs = (calledWith as estypes.BulkRequest)?.operations
?.filter((i: any) => i.doc)
.map((i: any) => i.doc);
expect(ids).toEqual(idsToAction);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import type { ElasticsearchClient } from '@kbn/core-elasticsearch-server';

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

import { appContextService } from '../../..';

Expand Down
Loading

0 comments on commit c810264

Please sign in to comment.