Skip to content

Commit 32e3308

Browse files
authored
[Ingest Manager] Ensure we trigger agent policy updated event when we bump revision. (#78836) (#79170)
1 parent 48994b9 commit 32e3308

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const SAVED_OBJECT_TYPE = AGENT_POLICY_SAVED_OBJECT_TYPE;
3333
class AgentPolicyService {
3434
private triggerAgentPolicyUpdatedEvent = async (
3535
soClient: SavedObjectsClientContract,
36-
action: string,
36+
action: 'created' | 'updated' | 'deleted',
3737
agentPolicyId: string
3838
) => {
3939
return agentPolicyUpdateEventHandler(soClient, action, agentPolicyId);
@@ -258,7 +258,11 @@ class AgentPolicyService {
258258
id: string,
259259
options?: { user?: AuthenticatedUser }
260260
): Promise<AgentPolicy> {
261-
return this._update(soClient, id, {}, options?.user);
261+
const res = await this._update(soClient, id, {}, options?.user);
262+
263+
await this.triggerAgentPolicyUpdatedEvent(soClient, 'updated', id);
264+
265+
return res;
262266
}
263267
public async bumpAllAgentPolicies(
264268
soClient: SavedObjectsClientContract,
@@ -277,7 +281,15 @@ class AgentPolicyService {
277281
};
278282
return policy;
279283
});
280-
return soClient.bulkUpdate<AgentPolicySOAttributes>(bumpedPolicies);
284+
const res = await soClient.bulkUpdate<AgentPolicySOAttributes>(bumpedPolicies);
285+
286+
await Promise.all(
287+
currentPolicies.saved_objects.map((policy) =>
288+
this.triggerAgentPolicyUpdatedEvent(soClient, 'updated', policy.id)
289+
)
290+
);
291+
292+
return res;
281293
}
282294

283295
public async assignPackagePolicies(

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,20 @@
55
*/
66

77
import expect from '@kbn/expect';
8+
import { Client } from 'elasticsearch';
89
import { FtrProviderContext } from '../../../api_integration/ftr_provider_context';
910
import { skipIfNoDockerRegistry } from '../../helpers';
11+
import { setupIngest } from '../fleet/agents/services';
1012

1113
export default function (providerContext: FtrProviderContext) {
1214
const { getService } = providerContext;
1315
const supertest = getService('supertest');
1416
const kibanaServer = getService('kibanaServer');
17+
const esClient: Client = getService('legacyEs');
1518

1619
describe('Settings - update', async function () {
1720
skipIfNoDockerRegistry(providerContext);
21+
setupIngest(providerContext);
1822

1923
it("should bump all agent policy's revision", async function () {
2024
const { body: testPolicy1PostRes } = await supertest
@@ -49,5 +53,41 @@ export default function (providerContext: FtrProviderContext) {
4953
expect(getTestPolicy1Res.attributes.revision).equal(2);
5054
expect(getTestPolicy2Res.attributes.revision).equal(2);
5155
});
56+
57+
it('should create agent actions', async function () {
58+
const { body: testPolicyRes } = await supertest
59+
.post(`/api/ingest_manager/agent_policies`)
60+
.set('kbn-xsrf', 'xxxx')
61+
.send({
62+
name: 'test',
63+
description: '',
64+
namespace: 'default',
65+
});
66+
67+
await supertest
68+
.put(`/api/ingest_manager/settings`)
69+
.set('kbn-xsrf', 'xxxx')
70+
.send({ kibana_urls: ['http://localhost:1232/abc', 'http://localhost:1232/abc'] });
71+
72+
const res = await esClient.search({
73+
index: '.kibana',
74+
body: {
75+
query: {
76+
bool: {
77+
must: [
78+
{
79+
terms: {
80+
type: ['fleet-agent-actions'],
81+
},
82+
},
83+
{ match: { 'fleet-agent-actions.policy_id': testPolicyRes.item.id } },
84+
],
85+
},
86+
},
87+
},
88+
});
89+
90+
expect(res.hits.hits.length).equal(2);
91+
});
5292
});
5393
}

0 commit comments

Comments
 (0)