Skip to content

Commit

Permalink
[Fleet] Fix increment package policy name (elastic#121101)
Browse files Browse the repository at this point in the history
  • Loading branch information
nchaulet authored and TinLe committed Dec 22, 2021
1 parent 108083f commit 015de2c
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
3 changes: 2 additions & 1 deletion x-pack/plugins/fleet/server/services/package_policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
doesAgentPolicyAlreadyIncludePackage,
validatePackagePolicy,
validationHasErrors,
SO_SEARCH_LIMIT,
} from '../../common';
import type {
DeletePackagePoliciesResponse,
Expand Down Expand Up @@ -1389,7 +1390,7 @@ export async function incrementPackageName(
) {
// Fetch all packagePolicies having the package name
const packagePolicyData = await packagePolicyService.list(soClient, {
perPage: 1,
perPage: SO_SEARCH_LIMIT,
kuery: `${PACKAGE_POLICY_SAVED_OBJECT_TYPE}.package.name: "${packageName}"`,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,38 @@
*/

import expect from '@kbn/expect';
import { skipIfNoDockerRegistry } from '../../helpers';
import { setupFleetAndAgents } from '../agents/services';
import { FtrProviderContext } from '../../../api_integration/ftr_provider_context';
import { PACKAGE_POLICY_SAVED_OBJECT_TYPE } from '../../../../plugins/fleet/common';

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

describe('fleet_agent_policies', () => {
skipIfNoDockerRegistry(providerContext);
describe('POST /api/fleet/agent_policies', () => {
before(async () => {
await esArchiver.load('x-pack/test/functional/es_archives/fleet/empty_fleet_server');
await esArchiver.load('x-pack/test/functional/es_archives/empty_kibana');
});
setupFleetAndAgents(providerContext);
const packagePoliciesToDeleteIds: string[] = [];
after(async () => {
if (packagePoliciesToDeleteIds.length > 0) {
await kibanaServer.savedObjects.bulkDelete({
objects: packagePoliciesToDeleteIds.map((id) => ({
id,
type: PACKAGE_POLICY_SAVED_OBJECT_TYPE,
})),
});
}

await esArchiver.unload('x-pack/test/functional/es_archives/fleet/empty_fleet_server');
await esArchiver.unload('x-pack/test/functional/es_archives/empty_kibana');
});
it('should work with valid minimum required values', async () => {
const {
Expand Down Expand Up @@ -113,6 +132,55 @@ export default function ({ getService }: FtrProviderContext) {
.send(sharedBody)
.expect(409);
});

it('should allow to create policy with the system integration policy and increment correctly the name', async () => {
// load a bunch of fake system integration policy
await kibanaServer.savedObjects.create({
id: 'package-policy-1',
type: PACKAGE_POLICY_SAVED_OBJECT_TYPE,
overwrite: true,
attributes: {
name: 'system-456',
package: {
name: 'system',
},
},
});
packagePoliciesToDeleteIds.push('package-policy-1');
await kibanaServer.savedObjects.create({
id: 'package-policy-2',
type: PACKAGE_POLICY_SAVED_OBJECT_TYPE,
overwrite: true,
attributes: {
name: 'system-123',
package: {
name: 'system',
},
},
});
packagePoliciesToDeleteIds.push('package-policy-2');

// first one succeeds
const res = await supertest
.post(`/api/fleet/agent_policies`)
.query({
sys_monitoring: true,
})
.set('kbn-xsrf', 'xxxx')
.send({
name: `Policy with system monitoring ${Date.now()}`,
namespace: 'default',
})
.expect(200);

const {
body: { items: policies },
} = await supertest.get(`/api/fleet/agent_policies?full=true`).expect(200);

const policy = policies.find((p: any) => (p.id = res.body.item.id));

expect(policy.package_policies[0].name).be('system-457');
});
});

describe('POST /api/fleet/agent_policies/{agentPolicyId}/copy', () => {
Expand Down

0 comments on commit 015de2c

Please sign in to comment.