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] [Ingest Manager] Ensure we trigger agent policy updated event when we bump revision. (#78836) #79170

Merged
merged 1 commit into from
Oct 1, 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
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);
});
});
}