Skip to content

Commit

Permalink
[Fleet] Rename CONFIG_CHANGE action to POLICY_CHANGE (#78221)
Browse files Browse the repository at this point in the history
* Initial pass at config->policy change action

* Make policy change action BWC with agent <= 7.9

* Fix test

* Add agent action SO migration for policy change actions

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
  • Loading branch information
jen-huang and elasticmachine committed Sep 29, 2020
1 parent e873848 commit b412b68
Show file tree
Hide file tree
Showing 17 changed files with 255 additions and 40 deletions.
4 changes: 2 additions & 2 deletions x-pack/plugins/ingest_manager/common/openapi/spec_oas3.json
Original file line number Diff line number Diff line change
Expand Up @@ -2757,7 +2757,7 @@
"data": "{\"config\":{\"id\":\"ae556400-5e39-11ea-8b49-f9747e466f7b\",\"outputs\":{\"default\":{\"type\":\"elasticsearch\",\"hosts\":[\"http://localhost:9200\"],\"api_key\":\"\",\"api_token\":\"6ckkp3ABz7e_XRqr3LM8:gQuDfUNSRgmY0iziYqP9Hw\"}},\"packagePolicies\":[]}}",
"created_at": "2020-03-04T20:02:56.149Z",
"id": "6a95c00a-d76d-4931-97c3-0bf935272d7d",
"type": "CONFIG_CHANGE"
"type": "POLICY_CHANGE"
}
],
"access_api_key_id": "6Mkkp3ABz7e_XRqrzLNJ",
Expand Down Expand Up @@ -2920,7 +2920,7 @@
"actions": [
{
"agent_id": "a6f14bd2-1a2a-481c-9212-9494d064ffdf",
"type": "CONFIG_CHANGE",
"type": "POLICY_CHANGE",
"data": {
"config": {
"id": "2fe89350-a5e0-11ea-a587-5f886c8a849f",
Expand Down
17 changes: 14 additions & 3 deletions x-pack/plugins/ingest_manager/common/types/models/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { FullAgentPolicy } from './agent_policy';
import { AGENT_TYPE_EPHEMERAL, AGENT_TYPE_PERMANENT, AGENT_TYPE_TEMPORARY } from '../../constants';

export type AgentType =
Expand All @@ -22,7 +22,7 @@ export type AgentStatus =
| 'upgrading'
| 'degraded';

export type AgentActionType = 'CONFIG_CHANGE' | 'UNENROLL' | 'UPGRADE';
export type AgentActionType = 'POLICY_CHANGE' | 'UNENROLL' | 'UPGRADE';
export interface NewAgentAction {
type: AgentActionType;
data?: any;
Expand All @@ -42,13 +42,24 @@ export interface AgentAction extends NewAgentAction {
export interface AgentPolicyAction extends NewAgentAction {
id: string;
type: AgentActionType;
data?: any;
data: {
policy: FullAgentPolicy;
};
policy_id: string;
policy_revision: number;
created_at: string;
ack_data?: any;
}

// Make policy change action renaming BWC with agent version <= 7.9
// eslint-disable-next-line @typescript-eslint/naming-convention
export type AgentPolicyActionV7_9 = Omit<AgentPolicyAction, 'type' | 'data'> & {
type: 'CONFIG_CHANGE';
data: {
config: FullAgentPolicy;
};
};

interface CommonAgentActionSOAttributes {
type: AgentActionType;
sent_at?: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ describe('test acks handlers', () => {
const ackService: AcksService = {
acknowledgeAgentActions: jest.fn().mockReturnValueOnce([
{
type: 'CONFIG_CHANGE',
type: 'POLICY_CHANGE',
id: 'action1',
},
]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('test actions handlers schema', () => {
it('validate that new agent actions schema is valid', async () => {
expect(
NewAgentActionSchema.validate({
type: 'CONFIG_CHANGE',
type: 'POLICY_CHANGE',
data: 'data',
sent_at: '2020-03-14T19:45:02.620Z',
})
Expand Down Expand Up @@ -53,7 +53,7 @@ describe('test actions handlers', () => {
const postNewAgentActionRequest: PostNewAgentActionRequest = {
body: {
action: {
type: 'CONFIG_CHANGE',
type: 'POLICY_CHANGE',
data: 'data',
sent_at: '2020-03-14T19:45:02.620Z',
},
Expand All @@ -66,7 +66,7 @@ describe('test actions handlers', () => {
const mockRequest = httpServerMock.createKibanaRequest(postNewAgentActionRequest);

const agentAction = ({
type: 'CONFIG_CHANGE',
type: 'POLICY_CHANGE',
id: 'action1',
sent_at: '2020-03-14T19:45:02.620Z',
timestamp: '2019-01-04T14:32:03.36764-05:00',
Expand Down
4 changes: 4 additions & 0 deletions x-pack/plugins/ingest_manager/server/saved_objects/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
migrateEnrollmentApiKeysToV7100,
migratePackagePolicyToV7100,
migrateSettingsToV7100,
migrateAgentActionToV7100,
} from './migrations/to_v7_10_0';

/*
Expand Down Expand Up @@ -109,6 +110,9 @@ const savedObjectTypes: { [key: string]: SavedObjectsType } = {
created_at: { type: 'date' },
},
},
migrations: {
'7.10.0': migrateAgentActionToV7100,
},
},
[AGENT_EVENT_SAVED_OBJECT_TYPE]: {
name: AGENT_EVENT_SAVED_OBJECT_TYPE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
PackagePolicy,
EnrollmentAPIKey,
Settings,
AgentAction,
} from '../../types';

export const migrateAgentToV7100: SavedObjectMigrationFn<
Expand Down Expand Up @@ -92,3 +93,18 @@ export const migrateSettingsToV7100: SavedObjectMigrationFn<

return settingsDoc;
};

export const migrateAgentActionToV7100: SavedObjectMigrationFn<AgentAction, AgentAction> = (
agentActionDoc
) => {
// @ts-expect-error
if (agentActionDoc.attributes.type === 'CONFIG_CHANGE') {
agentActionDoc.attributes.type = 'POLICY_CHANGE';
if (agentActionDoc.attributes.data?.config) {
agentActionDoc.attributes.data.policy = agentActionDoc.attributes.data.config;
delete agentActionDoc.attributes.data.config;
}
}

return agentActionDoc;
};
4 changes: 2 additions & 2 deletions x-pack/plugins/ingest_manager/server/services/agent_policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,8 +399,8 @@ class AgentPolicyService {
}, []);

await createAgentPolicyAction(soClient, {
type: 'CONFIG_CHANGE',
data: { config: policy } as any,
type: 'POLICY_CHANGE',
data: { policy },
ack_data: { packages },
created_at: new Date().toISOString(),
policy_id: policy.id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('test agent acks services', () => {
references: [],
type: AGENT_ACTION_SAVED_OBJECT_TYPE,
attributes: {
type: 'CONFIG_CHANGE',
type: 'POLICY_CHANGE',
agent_id: 'id',
sent_at: '2020-03-14T19:45:02.620Z',
timestamp: '2019-01-04T14:32:03.36764-05:00',
Expand Down Expand Up @@ -61,7 +61,7 @@ describe('test agent acks services', () => {
const mockSavedObjectsClient = savedObjectsClientMock.create();

const actionAttributes = {
type: 'CONFIG_CHANGE',
type: 'POLICY_CHANGE',
policy_id: 'policy1',
policy_revision: 4,
sent_at: '2020-03-14T19:45:02.620Z',
Expand Down Expand Up @@ -120,7 +120,7 @@ describe('test agent acks services', () => {
const mockSavedObjectsClient = savedObjectsClientMock.create();

const actionAttributes = {
type: 'CONFIG_CHANGE',
type: 'POLICY_CHANGE',
policy_id: 'policy1',
policy_revision: 4,
sent_at: '2020-03-14T19:45:02.620Z',
Expand Down Expand Up @@ -180,7 +180,7 @@ describe('test agent acks services', () => {
const mockSavedObjectsClient = savedObjectsClientMock.create();

const actionAttributes = {
type: 'CONFIG_CHANGE',
type: 'POLICY_CHANGE',
policy_id: 'policy1',
policy_revision: 4,
sent_at: '2020-03-14T19:45:02.620Z',
Expand Down Expand Up @@ -235,7 +235,7 @@ describe('test agent acks services', () => {
references: [],
type: AGENT_ACTION_SAVED_OBJECT_TYPE,
attributes: {
type: 'CONFIG_CHANGE',
type: 'POLICY_CHANGE',
sent_at: '2020-03-14T19:45:02.620Z',
timestamp: '2019-01-04T14:32:03.36764-05:00',
created_at: '2020-03-14T19:45:02.620Z',
Expand Down Expand Up @@ -319,7 +319,7 @@ describe('test agent acks services', () => {
references: [],
type: AGENT_ACTION_SAVED_OBJECT_TYPE,
attributes: {
type: 'CONFIG_CHANGE',
type: 'POLICY_CHANGE',
agent_id: 'id',
sent_at: '2020-03-14T19:45:02.620Z',
timestamp: '2019-01-04T14:32:03.36764-05:00',
Expand Down
13 changes: 8 additions & 5 deletions x-pack/plugins/ingest_manager/server/services/agents/acks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
Agent,
AgentAction,
AgentPolicyAction,
AgentPolicyActionV7_9,
AgentEvent,
AgentEventSOAttributes,
AgentSOAttributes,
Expand Down Expand Up @@ -132,18 +133,20 @@ async function fetchActionsUsingCache(
return [...freshActions, ...actions];
}

function isAgentPolicyAction(action: AgentAction | AgentPolicyAction): action is AgentPolicyAction {
function isAgentPolicyAction(
action: AgentAction | AgentPolicyAction | AgentPolicyActionV7_9
): action is AgentPolicyAction | AgentPolicyActionV7_9 {
return (action as AgentPolicyAction).policy_id !== undefined;
}

function getLatestConfigChangePolicyActionIfUpdated(
agent: Agent,
actions: Array<AgentAction | AgentPolicyAction>
): AgentPolicyAction | null {
return actions.reduce<null | AgentPolicyAction>((acc, action) => {
actions: Array<AgentAction | AgentPolicyAction | AgentPolicyActionV7_9>
): AgentPolicyAction | AgentPolicyActionV7_9 | null {
return actions.reduce<null | AgentPolicyAction | AgentPolicyActionV7_9>((acc, action) => {
if (
!isAgentPolicyAction(action) ||
action.type !== 'CONFIG_CHANGE' ||
(action.type !== 'POLICY_CHANGE' && action.type !== 'CONFIG_CHANGE') ||
action.policy_id !== agent.policy_id ||
(action?.policy_revision ?? 0) < (agent.policy_revision || 0)
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('test agent actions services', () => {

const newAgentAction: Omit<AgentAction, 'id'> = {
agent_id: 'agentid',
type: 'CONFIG_CHANGE',
type: 'POLICY_CHANGE',
data: { content: 'data' },
sent_at: '2020-03-14T19:45:02.620Z',
created_at: '2020-03-14T19:45:02.620Z',
Expand All @@ -24,7 +24,7 @@ describe('test agent actions services', () => {
Promise.resolve({
attributes: {
agent_id: 'agentid',
type: 'CONFIG_CHANGE',
type: 'POLICY_CHANGE',
data: JSON.stringify({ content: 'data' }),
sent_at: '2020-03-14T19:45:02.620Z',
created_at: '2020-03-14T19:45:02.620Z',
Expand Down
Loading

0 comments on commit b412b68

Please sign in to comment.