Skip to content

Commit

Permalink
[Ingest Manager] Ensure we trigger agent policy updated event when we…
Browse files Browse the repository at this point in the history
… bump revision. (elastic#78836)
  • Loading branch information
nchaulet committed Oct 1, 2020
1 parent ecc46d9 commit fc8b872
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
18 changes: 15 additions & 3 deletions x-pack/plugins/ingest_manager/server/services/agent_policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const SAVED_OBJECT_TYPE = AGENT_POLICY_SAVED_OBJECT_TYPE;
class AgentPolicyService {
private triggerAgentPolicyUpdatedEvent = async (
soClient: SavedObjectsClientContract,
action: string,
action: 'created' | 'updated' | 'deleted',
agentPolicyId: string
) => {
return agentPolicyUpdateEventHandler(soClient, action, agentPolicyId);
Expand Down Expand Up @@ -258,7 +258,11 @@ class AgentPolicyService {
id: string,
options?: { user?: AuthenticatedUser }
): Promise<AgentPolicy> {
return this._update(soClient, id, {}, options?.user);
const res = await this._update(soClient, id, {}, options?.user);

await this.triggerAgentPolicyUpdatedEvent(soClient, 'updated', id);

return res;
}
public async bumpAllAgentPolicies(
soClient: SavedObjectsClientContract,
Expand All @@ -277,7 +281,15 @@ class AgentPolicyService {
};
return policy;
});
return soClient.bulkUpdate<AgentPolicySOAttributes>(bumpedPolicies);
const res = await soClient.bulkUpdate<AgentPolicySOAttributes>(bumpedPolicies);

await Promise.all(
currentPolicies.saved_objects.map((policy) =>
this.triggerAgentPolicyUpdatedEvent(soClient, 'updated', policy.id)
)
);

return res;
}

public async assignPackagePolicies(
Expand Down
40 changes: 40 additions & 0 deletions x-pack/test/ingest_manager_api_integration/apis/settings/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@
*/

import expect from '@kbn/expect';
import { Client } from 'elasticsearch';
import { FtrProviderContext } from '../../../api_integration/ftr_provider_context';
import { skipIfNoDockerRegistry } from '../../helpers';
import { setupIngest } from '../fleet/agents/services';

export default function (providerContext: FtrProviderContext) {
const { getService } = providerContext;
const supertest = getService('supertest');
const kibanaServer = getService('kibanaServer');
const esClient: Client = getService('legacyEs');

describe('Settings - update', async function () {
skipIfNoDockerRegistry(providerContext);
setupIngest(providerContext);

it("should bump all agent policy's revision", async function () {
const { body: testPolicy1PostRes } = await supertest
Expand Down Expand Up @@ -49,5 +53,41 @@ export default function (providerContext: FtrProviderContext) {
expect(getTestPolicy1Res.attributes.revision).equal(2);
expect(getTestPolicy2Res.attributes.revision).equal(2);
});

it('should create agent actions', async function () {
const { body: testPolicyRes } = await supertest
.post(`/api/ingest_manager/agent_policies`)
.set('kbn-xsrf', 'xxxx')
.send({
name: 'test',
description: '',
namespace: 'default',
});

await supertest
.put(`/api/ingest_manager/settings`)
.set('kbn-xsrf', 'xxxx')
.send({ kibana_urls: ['http://localhost:1232/abc', 'http://localhost:1232/abc'] });

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

expect(res.hits.hits.length).equal(2);
});
});
}

0 comments on commit fc8b872

Please sign in to comment.