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] Rename CONFIG_CHANGE action to POLICY_CHANGE #78221

Merged
merged 9 commits into from
Sep 29, 2020
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 @@ -21,7 +21,7 @@ export type AgentStatus =
| 'unenrolling'
| 'degraded';

export type AgentActionType = 'CONFIG_CHANGE' | 'UNENROLL';
export type AgentActionType = 'POLICY_CHANGE' | 'UNENROLL';

export interface NewAgentAction {
type: AgentActionType;
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: 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 @@ -404,8 +404,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 @@ -126,18 +127,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
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { savedObjectsClientMock } from 'src/core/server/mocks';
import { createAgentActionFromPolicyAction } from './state_new_actions';
import { OutputType, Agent, AgentPolicyAction } from '../../../types';

jest.mock('../../app_context', () => ({
appContextService: {
getEncryptedSavedObjects: () => ({
getDecryptedAsInternalUser: () => ({
attributes: {
default_api_key: 'MOCK_API_KEY',
},
}),
}),
},
}));

describe('test agent checkin new action services', () => {
describe('createAgentActionFromPolicyAction()', () => {
const mockSavedObjectsClient = savedObjectsClientMock.create();
const mockAgent: Agent = {
id: 'agent1',
active: true,
type: 'PERMANENT',
local_metadata: { elastic: { agent: { version: '7.10.0' } } },
user_provided_metadata: {},
current_error_events: [],
packages: [],
enrolled_at: '2020-03-14T19:45:02.620Z',
};
const mockPolicyAction: AgentPolicyAction = {
id: 'action1',
type: 'POLICY_CHANGE',
policy_id: 'policy1',
policy_revision: 1,
sent_at: '2020-03-14T19:45:02.620Z',
created_at: '2020-03-14T19:45:02.620Z',
data: {
policy: {
id: 'policy1',
outputs: {
default: {
type: OutputType.Elasticsearch,
hosts: [],
ca_sha256: undefined,
api_key: undefined,
},
},
inputs: [],
},
},
};

it('should return POLICY_CHANGE and data.policy for agent version >= 7.10', async () => {
const expectedResult = [
{
agent_id: 'agent1',
created_at: '2020-03-14T19:45:02.620Z',
data: {
policy: {
id: 'policy1',
inputs: [],
outputs: { default: { api_key: 'MOCK_API_KEY', hosts: [], type: 'elasticsearch' } },
},
},
id: 'action1',
sent_at: '2020-03-14T19:45:02.620Z',
type: 'POLICY_CHANGE',
},
];

expect(
await createAgentActionFromPolicyAction(mockSavedObjectsClient, mockAgent, mockPolicyAction)
).toEqual(expectedResult);

expect(
await createAgentActionFromPolicyAction(
mockSavedObjectsClient,
{ ...mockAgent, local_metadata: { elastic: { agent: { version: '7.10.1-SNAPSHOT' } } } },
mockPolicyAction
)
).toEqual(expectedResult);

expect(
await createAgentActionFromPolicyAction(
mockSavedObjectsClient,
{ ...mockAgent, local_metadata: { elastic: { agent: { version: '8.0.0' } } } },
mockPolicyAction
)
).toEqual(expectedResult);

expect(
await createAgentActionFromPolicyAction(
mockSavedObjectsClient,
{ ...mockAgent, local_metadata: { elastic: { agent: { version: '8.0.0-SNAPSHOT' } } } },
mockPolicyAction
)
).toEqual(expectedResult);
});

it('should return CONNFIG_CHANGE and data.config for agent version <= 7.9', async () => {
const expectedResult = [
{
agent_id: 'agent1',
created_at: '2020-03-14T19:45:02.620Z',
data: {
config: {
id: 'policy1',
inputs: [],
outputs: { default: { api_key: 'MOCK_API_KEY', hosts: [], type: 'elasticsearch' } },
},
},
id: 'action1',
sent_at: '2020-03-14T19:45:02.620Z',
type: 'CONFIG_CHANGE',
},
];

expect(
await createAgentActionFromPolicyAction(
mockSavedObjectsClient,
{ ...mockAgent, local_metadata: { elastic: { agent: { version: '7.9.0' } } } },
mockPolicyAction
)
).toEqual(expectedResult);

expect(
await createAgentActionFromPolicyAction(
mockSavedObjectsClient,
{ ...mockAgent, local_metadata: { elastic: { agent: { version: '7.9.1-SNAPSHOT' } } } },
mockPolicyAction
)
).toEqual(expectedResult);

expect(
await createAgentActionFromPolicyAction(
mockSavedObjectsClient,
{ ...mockAgent, local_metadata: { elastic: { agent: { version: '7.9.3' } } } },
mockPolicyAction
)
).toEqual(expectedResult);

expect(
await createAgentActionFromPolicyAction(
mockSavedObjectsClient,
{ ...mockAgent, local_metadata: { elastic: { agent: { version: '7.8.2' } } } },
mockPolicyAction
)
).toEqual(expectedResult);
});
});
});
Loading