Skip to content

Commit

Permalink
[Fleet] Fix POLICY_CHANGE action creation for new policy (elastic#81236)
Browse files Browse the repository at this point in the history
  • Loading branch information
nchaulet committed Oct 21, 2020
1 parent 1c351b0 commit 53ed8c0
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ export const createAgentPolicyHandler: RequestHandler<
});
}

await agentPolicyService.createFleetPolicyChangeAction(soClient, agentPolicy.id);

const body: CreateAgentPolicyResponse = {
item: agentPolicy,
};
Expand Down Expand Up @@ -185,6 +187,7 @@ export const copyAgentPolicyHandler: RequestHandler<
user: user || undefined,
}
);

const body: CopyAgentPolicyResponse = { item: agentPolicy };
return response.ok({
body,
Expand Down
8 changes: 8 additions & 0 deletions x-pack/plugins/ingest_manager/server/services/agent_policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { agentPolicyUpdateEventHandler } from './agent_policy_update';
import { getSettings } from './settings';
import { normalizeKuery, escapeSearchQueryPhrase } from './saved_object';
import { getFullAgentPolicyKibanaConfig } from '../../common/services/full_agent_policy_kibana_config';
import { isAgentsSetup } from './agents/setup';

const SAVED_OBJECT_TYPE = AGENT_POLICY_SAVED_OBJECT_TYPE;

Expand Down Expand Up @@ -287,6 +288,8 @@ class AgentPolicyService {
throw new Error('Copied agent policy not found');
}

await this.createFleetPolicyChangeAction(soClient, newAgentPolicy.id);

return updatedAgentPolicy;
}

Expand Down Expand Up @@ -433,6 +436,11 @@ class AgentPolicyService {
soClient: SavedObjectsClientContract,
agentPolicyId: string
) {
// If Agents is not setup skip the creation of POLICY_CHANGE agent actions
// the action will be created during the fleet setup
if (!(await isAgentsSetup(soClient))) {
return;
}
const policy = await agentPolicyService.getFullAgentPolicy(soClient, agentPolicyId);
if (!policy || !policy.revision) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,24 @@ export default function (providerContext: FtrProviderContext) {
});
createdAgentPolicyIds.push(testPolicyRes.item.id);

const beforeRes = await esClient.search({
index: '.kibana',
body: {
query: {
bool: {
must: [
{
terms: {
type: ['fleet-agent-actions'],
},
},
{ match: { 'fleet-agent-actions.policy_id': testPolicyRes.item.id } },
],
},
},
},
});

await supertest
.put(`/api/fleet/settings`)
.set('kbn-xsrf', 'xxxx')
Expand All @@ -105,7 +123,7 @@ export default function (providerContext: FtrProviderContext) {
},
});

expect(res.hits.hits.length).equal(2);
expect(res.hits.hits.length).equal(beforeRes.hits.hits.length + 1);
});
});
}

0 comments on commit 53ed8c0

Please sign in to comment.