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

[7.x] [Fleet] Fix POLICY_CHANGE action creation for new policy (#81236) #81272

Merged
merged 2 commits into from
Oct 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});
});
}