Skip to content

Commit f87fb1f

Browse files
committed
[Fleet] Fix POLICY_CHANGE action creation for new policy (#81236)
1 parent 81e194b commit f87fb1f

3 files changed

Lines changed: 30 additions & 1 deletion

File tree

x-pack/plugins/ingest_manager/server/routes/agent_policy/handlers.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ export const createAgentPolicyHandler: RequestHandler<
132132
});
133133
}
134134

135+
await agentPolicyService.createFleetPolicyChangeAction(soClient, agentPolicy.id);
136+
135137
const body: CreateAgentPolicyResponse = {
136138
item: agentPolicy,
137139
};
@@ -185,6 +187,7 @@ export const copyAgentPolicyHandler: RequestHandler<
185187
user: user || undefined,
186188
}
187189
);
190+
188191
const body: CopyAgentPolicyResponse = { item: agentPolicy };
189192
return response.ok({
190193
body,

x-pack/plugins/ingest_manager/server/services/agent_policy.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import { agentPolicyUpdateEventHandler } from './agent_policy_update';
3434
import { getSettings } from './settings';
3535
import { normalizeKuery, escapeSearchQueryPhrase } from './saved_object';
3636
import { getFullAgentPolicyKibanaConfig } from '../../common/services/full_agent_policy_kibana_config';
37+
import { isAgentsSetup } from './agents/setup';
3738

3839
const SAVED_OBJECT_TYPE = AGENT_POLICY_SAVED_OBJECT_TYPE;
3940

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

291+
await this.createFleetPolicyChangeAction(soClient, newAgentPolicy.id);
292+
290293
return updatedAgentPolicy;
291294
}
292295

@@ -433,6 +436,11 @@ class AgentPolicyService {
433436
soClient: SavedObjectsClientContract,
434437
agentPolicyId: string
435438
) {
439+
// If Agents is not setup skip the creation of POLICY_CHANGE agent actions
440+
// the action will be created during the fleet setup
441+
if (!(await isAgentsSetup(soClient))) {
442+
return;
443+
}
436444
const policy = await agentPolicyService.getFullAgentPolicy(soClient, agentPolicyId);
437445
if (!policy || !policy.revision) {
438446
return;

x-pack/test/ingest_manager_api_integration/apis/settings/update.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,24 @@ export default function (providerContext: FtrProviderContext) {
8181
});
8282
createdAgentPolicyIds.push(testPolicyRes.item.id);
8383

84+
const beforeRes = await esClient.search({
85+
index: '.kibana',
86+
body: {
87+
query: {
88+
bool: {
89+
must: [
90+
{
91+
terms: {
92+
type: ['fleet-agent-actions'],
93+
},
94+
},
95+
{ match: { 'fleet-agent-actions.policy_id': testPolicyRes.item.id } },
96+
],
97+
},
98+
},
99+
},
100+
});
101+
84102
await supertest
85103
.put(`/api/fleet/settings`)
86104
.set('kbn-xsrf', 'xxxx')
@@ -105,7 +123,7 @@ export default function (providerContext: FtrProviderContext) {
105123
},
106124
});
107125

108-
expect(res.hits.hits.length).equal(2);
126+
expect(res.hits.hits.length).equal(beforeRes.hits.hits.length + 1);
109127
});
110128
});
111129
}

0 commit comments

Comments
 (0)