Skip to content

Commit

Permalink
Create config change action during setup
Browse files Browse the repository at this point in the history
  • Loading branch information
nchaulet committed Aug 31, 2020
1 parent ec3cd82 commit cd86268
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 30 deletions.
28 changes: 27 additions & 1 deletion x-pack/plugins/ingest_manager/server/services/agent_policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
ListWithKuery,
} from '../types';
import { DeleteAgentPolicyResponse, storedPackagePoliciesToAgentInputs } from '../../common';
import { listAgents } from './agents';
import { createAgentPolicyAction, listAgents } from './agents';
import { packagePolicyService } from './package_policy';
import { outputService } from './output';
import { agentPolicyUpdateEventHandler } from './agent_policy_update';
Expand Down Expand Up @@ -368,6 +368,32 @@ class AgentPolicyService {
};
}

public async createFleetPolicyChangeAction(
soClient: SavedObjectsClientContract,
agentPolicyId: string
) {
const policy = await agentPolicyService.getFullAgentPolicy(soClient, agentPolicyId);
if (!policy || !policy.revision) {
return;
}
const packages = policy.inputs.reduce<string[]>((acc, input) => {
const packageName = input.meta?.package?.name;
if (packageName && acc.indexOf(packageName) < 0) {
return [packageName, ...acc];
}
return acc;
}, []);

await createAgentPolicyAction(soClient, {
type: 'CONFIG_CHANGE',
data: { config: policy } as any,
ack_data: { packages },
created_at: new Date().toISOString(),
policy_id: policy.id,
policy_revision: policy.revision,
});
}

public async getFullAgentPolicy(
soClient: SavedObjectsClientContract,
id: string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import { SavedObjectsClientContract } from 'src/core/server';
import { generateEnrollmentAPIKey, deleteEnrollmentApiKeyForAgentPolicyId } from './api_keys';
import { unenrollForAgentPolicyId, createAgentPolicyAction } from './agents';
import { unenrollForAgentPolicyId } from './agents';
import { outputService } from './output';
import { agentPolicyService } from './agent_policy';

Expand All @@ -25,29 +25,11 @@ export async function agentPolicyUpdateEventHandler(
await generateEnrollmentAPIKey(soClient, {
agentPolicyId,
});
await agentPolicyService.createFleetPolicyChangeAction(soClient, agentPolicyId);
}

if (action === 'updated') {
const policy = await agentPolicyService.getFullAgentPolicy(soClient, agentPolicyId);
if (!policy || !policy.revision) {
return;
}
const packages = policy.inputs.reduce<string[]>((acc, input) => {
const packageName = input.meta?.package?.name;
if (packageName && acc.indexOf(packageName) < 0) {
return [packageName, ...acc];
}
return acc;
}, []);

await createAgentPolicyAction(soClient, {
type: 'CONFIG_CHANGE',
data: { config: policy } as any,
ack_data: { packages },
created_at: new Date().toISOString(),
policy_id: policy.id,
policy_revision: policy.revision,
});
await agentPolicyService.createFleetPolicyChangeAction(soClient, agentPolicyId);
}

if (action === 'deleted') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ describe('test agent acks services', () => {
Promise.resolve({
saved_objects: [
{
id: 'action1',
id: 'action2',
references: [],
type: AGENT_ACTION_SAVED_OBJECT_TYPE,
attributes: actionAttributes,
Expand All @@ -95,7 +95,7 @@ describe('test agent acks services', () => {
type: 'ACTION_RESULT',
subtype: 'CONFIG',
timestamp: '2019-01-04T14:32:03.36764-05:00',
action_id: 'action1',
action_id: 'action2',
agent_id: 'id',
} as AgentEvent,
]
Expand Down Expand Up @@ -123,7 +123,7 @@ describe('test agent acks services', () => {
Promise.resolve({
saved_objects: [
{
id: 'action1',
id: 'action3',
references: [],
type: AGENT_ACTION_SAVED_OBJECT_TYPE,
attributes: {
Expand Down Expand Up @@ -152,7 +152,7 @@ describe('test agent acks services', () => {
type: 'ACTION_RESULT',
subtype: 'CONFIG',
timestamp: '2019-01-04T14:32:03.36764-05:00',
action_id: 'action1',
action_id: 'action3',
agent_id: 'id',
} as AgentEvent,
]
Expand All @@ -167,7 +167,7 @@ describe('test agent acks services', () => {
Promise.resolve({
saved_objects: [
{
id: 'action1',
id: 'action4',
error: {
message: 'Not found',
statusCode: 404,
Expand All @@ -189,7 +189,7 @@ describe('test agent acks services', () => {
type: 'ACTION_RESULT',
subtype: 'CONFIG',
timestamp: '2019-01-04T14:32:03.36764-05:00',
action_id: 'action2',
action_id: 'action4',
agent_id: 'id',
} as unknown) as AgentEvent,
]
Expand All @@ -207,7 +207,7 @@ describe('test agent acks services', () => {
Promise.resolve({
saved_objects: [
{
id: 'action1',
id: 'action5',
references: [],
type: AGENT_ACTION_SAVED_OBJECT_TYPE,
attributes: {
Expand All @@ -234,7 +234,7 @@ describe('test agent acks services', () => {
type: 'ACTION',
subtype: 'FAILED',
timestamp: '2019-01-04T14:32:03.36764-05:00',
action_id: 'action1',
action_id: 'action5',
agent_id: 'id',
} as unknown) as AgentEvent,
]
Expand Down
6 changes: 6 additions & 0 deletions x-pack/plugins/ingest_manager/server/services/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,12 @@ export async function setupFleet(
});
})
);

await Promise.all(
agentPolicies.map((agentPolicy) =>
agentPolicyService.createFleetPolicyChangeAction(soClient, agentPolicy.id)
)
);
}

function generateRandomPassword() {
Expand Down

0 comments on commit cd86268

Please sign in to comment.