Skip to content

Commit 61951a5

Browse files
authored
[Ingest Manager] Shared Fleet agent policy action (#76013)
1 parent 49a0035 commit 61951a5

File tree

16 files changed

+397
-355
lines changed

16 files changed

+397
-355
lines changed

x-pack/plugins/ingest_manager/common/types/models/agent.ts

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,53 @@ export type AgentStatus =
2121
| 'unenrolling'
2222
| 'degraded';
2323

24-
export type AgentActionType = 'CONFIG_CHANGE' | 'DATA_DUMP' | 'RESUME' | 'PAUSE' | 'UNENROLL';
24+
export type AgentActionType = 'CONFIG_CHANGE' | 'UNENROLL';
25+
2526
export interface NewAgentAction {
2627
type: AgentActionType;
2728
data?: any;
2829
sent_at?: string;
2930
}
3031

3132
export interface AgentAction extends NewAgentAction {
33+
type: AgentActionType;
34+
data?: any;
35+
sent_at?: string;
3236
id: string;
3337
agent_id: string;
3438
created_at: string;
39+
ack_data?: any;
40+
}
41+
42+
export interface AgentPolicyAction extends NewAgentAction {
43+
id: string;
44+
type: AgentActionType;
45+
data?: any;
46+
policy_id: string;
47+
policy_revision: number;
48+
created_at: string;
49+
ack_data?: any;
3550
}
3651

37-
export interface AgentActionSOAttributes {
52+
interface CommonAgentActionSOAttributes {
3853
type: AgentActionType;
3954
sent_at?: string;
4055
timestamp?: string;
4156
created_at: string;
42-
agent_id: string;
4357
data?: string;
58+
ack_data?: string;
4459
}
4560

61+
export type AgentActionSOAttributes = CommonAgentActionSOAttributes & {
62+
agent_id: string;
63+
};
64+
export type AgentPolicyActionSOAttributes = CommonAgentActionSOAttributes & {
65+
policy_id: string;
66+
policy_revision: number;
67+
};
68+
69+
export type BaseAgentActionSOAttributes = AgentActionSOAttributes | AgentPolicyActionSOAttributes;
70+
4671
export interface NewAgentEvent {
4772
type: 'STATE' | 'ERROR' | 'ACTION_RESULT' | 'ACTION';
4873
subtype: // State

x-pack/plugins/ingest_manager/common/types/rest_spec/agent.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
import {
88
Agent,
99
AgentAction,
10+
NewAgentAction,
1011
NewAgentEvent,
1112
AgentEvent,
1213
AgentStatus,
1314
AgentType,
14-
NewAgentAction,
1515
} from '../models';
1616

1717
export interface GetAgentsRequest {

x-pack/plugins/ingest_manager/server/routes/agent/actions_handlers.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import { RequestHandler } from 'kibana/server';
1010
import { TypeOf } from '@kbn/config-schema';
1111
import { PostNewAgentActionRequestSchema } from '../../types/rest_spec';
1212
import { ActionsService } from '../../services/agents';
13-
import { NewAgentAction } from '../../../common/types/models';
1413
import { PostNewAgentActionResponse } from '../../../common/types/rest_spec';
1514

1615
export const postNewAgentActionHandlerBuilder = function (
@@ -26,7 +25,7 @@ export const postNewAgentActionHandlerBuilder = function (
2625

2726
const agent = await actionsService.getAgent(soClient, request.params.agentId);
2827

29-
const newAgentAction = request.body.action as NewAgentAction;
28+
const newAgentAction = request.body.action;
3029

3130
const savedAgentAction = await actionsService.createAgentAction(soClient, {
3231
created_at: new Date().toISOString(),

x-pack/plugins/ingest_manager/server/saved_objects/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,11 @@ const savedObjectTypes: { [key: string]: SavedObjectsType } = {
9898
mappings: {
9999
properties: {
100100
agent_id: { type: 'keyword' },
101+
policy_id: { type: 'keyword' },
102+
policy_revision: { type: 'integer' },
101103
type: { type: 'keyword' },
102104
data: { type: 'binary' },
105+
ack_data: { type: 'text' },
103106
sent_at: { type: 'date' },
104107
created_at: { type: 'date' },
105108
},

x-pack/plugins/ingest_manager/server/services/agent_policy.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {
2121
ListWithKuery,
2222
} from '../types';
2323
import { DeleteAgentPolicyResponse, storedPackagePoliciesToAgentInputs } from '../../common';
24-
import { listAgents } from './agents';
24+
import { createAgentPolicyAction, listAgents } from './agents';
2525
import { packagePolicyService } from './package_policy';
2626
import { outputService } from './output';
2727
import { agentPolicyUpdateEventHandler } from './agent_policy_update';
@@ -67,6 +67,10 @@ class AgentPolicyService {
6767
updated_by: user ? user.username : 'system',
6868
});
6969

70+
if (options.bumpRevision) {
71+
await this.triggerAgentPolicyUpdatedEvent(soClient, 'updated', id);
72+
}
73+
7074
return (await this.get(soClient, id)) as AgentPolicy;
7175
}
7276

@@ -383,6 +387,32 @@ class AgentPolicyService {
383387
};
384388
}
385389

390+
public async createFleetPolicyChangeAction(
391+
soClient: SavedObjectsClientContract,
392+
agentPolicyId: string
393+
) {
394+
const policy = await agentPolicyService.getFullAgentPolicy(soClient, agentPolicyId);
395+
if (!policy || !policy.revision) {
396+
return;
397+
}
398+
const packages = policy.inputs.reduce<string[]>((acc, input) => {
399+
const packageName = input.meta?.package?.name;
400+
if (packageName && acc.indexOf(packageName) < 0) {
401+
acc.push(packageName);
402+
}
403+
return acc;
404+
}, []);
405+
406+
await createAgentPolicyAction(soClient, {
407+
type: 'CONFIG_CHANGE',
408+
data: { config: policy } as any,
409+
ack_data: { packages },
410+
created_at: new Date().toISOString(),
411+
policy_id: policy.id,
412+
policy_revision: policy.revision,
413+
});
414+
}
415+
386416
public async getFullAgentPolicy(
387417
soClient: SavedObjectsClientContract,
388418
id: string,

x-pack/plugins/ingest_manager/server/services/agent_policy_update.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,29 @@ import { SavedObjectsClientContract } from 'src/core/server';
88
import { generateEnrollmentAPIKey, deleteEnrollmentApiKeyForAgentPolicyId } from './api_keys';
99
import { unenrollForAgentPolicyId } from './agents';
1010
import { outputService } from './output';
11+
import { agentPolicyService } from './agent_policy';
1112

1213
export async function agentPolicyUpdateEventHandler(
1314
soClient: SavedObjectsClientContract,
1415
action: string,
1516
agentPolicyId: string
1617
) {
1718
const adminUser = await outputService.getAdminUser(soClient);
18-
// If no admin user fleet is not enabled just skip this hook
19-
if (!adminUser) {
19+
const outputId = await outputService.getDefaultOutputId(soClient);
20+
// If no admin user and no default output fleet is not enabled just skip this hook
21+
if (!adminUser || !outputId) {
2022
return;
2123
}
2224

2325
if (action === 'created') {
2426
await generateEnrollmentAPIKey(soClient, {
2527
agentPolicyId,
2628
});
29+
await agentPolicyService.createFleetPolicyChangeAction(soClient, agentPolicyId);
30+
}
31+
32+
if (action === 'updated') {
33+
await agentPolicyService.createFleetPolicyChangeAction(soClient, agentPolicyId);
2734
}
2835

2936
if (action === 'deleted') {

0 commit comments

Comments
 (0)