[Fleet] Fix error in agent policy duplication (#143884) (#143935)

(cherry picked from commit d75015c620)

# Conflicts:
#	x-pack/test/fleet_api_integration/apis/agent_policy/agent_policy.ts
This commit is contained in:
Nicolas Chaulet 2022-10-25 08:36:41 -04:00 committed by GitHub
parent e758c80e76
commit 794da3f676
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 97 additions and 8 deletions

View file

@ -39,20 +39,23 @@ export async function incrementPackagePolicyCopyName(
// find all pacakge policies starting with the same name and increment the name
const packagePolicyData = await packagePolicyService.list(soClient, {
perPage: SO_SEARCH_LIMIT,
kuery: `${PACKAGE_POLICY_SAVED_OBJECT_TYPE}.name: ${packageName}*`,
// split package name on first space as KQL do not support wildcard and space
kuery: `${PACKAGE_POLICY_SAVED_OBJECT_TYPE}.name: ${packageName.split(' ')[0]}*`,
});
const maxVersion =
packagePolicyData.items.length > 0
? Math.max(
...packagePolicyData.items.map((item) => {
const matches = item.name.match(/^(.*)\s\(copy\s?([0-9]*)\)$/);
if (matches) {
return parseInt(matches[2], 10) || 1;
}
...packagePolicyData.items
.filter((item) => item.name.startsWith(packageName))
.map((item) => {
const matches = item.name.match(/^(.*)\s\(copy\s?([0-9]*)\)$/);
if (matches) {
return parseInt(matches[2], 10) || 1;
}
return 0;
})
return 0;
})
)
: 0;

View file

@ -334,6 +334,7 @@ export default function (providerContext: FtrProviderContext) {
return 0;
}
const {
body: {
item: { id: originalPolicyId },
@ -396,6 +397,91 @@ export default function (providerContext: FtrProviderContext) {
expect(await getSystemPackagePolicyCopyVersion(copy3Id)).to.be(3);
});
it('should work with package policy with space in name', async () => {
const policyId = 'package-policy-test-1';
const getPkRes = await supertest
.get(`/api/fleet/epm/packages/system`)
.set('kbn-xsrf', 'xxxx')
.expect(200);
const systemPkgVersion = getPkRes.body.item.version;
// we must first force install the system package to override package verification error on policy create
const installPromise = supertest
.post(`/api/fleet/epm/packages/system-${systemPkgVersion}`)
.set('kbn-xsrf', 'xxxx')
.send({ force: true })
.expect(200);
await Promise.all([
installPromise,
kibanaServer.savedObjects.create({
id: policyId,
type: PACKAGE_POLICY_SAVED_OBJECT_TYPE,
overwrite: true,
attributes: {
name: `system-1`,
package: {
name: 'system',
},
},
}),
]);
const {
body: {
item: { id: originalPolicyId },
},
} = await supertest
.post(`/api/fleet/agent_policies`)
.set('kbn-xsrf', 'xxxx')
.query({
sys_monitoring: false,
})
.send({
name: 'original policy with package policy with space in name',
namespace: 'default',
})
.expect(200);
await supertest
.post(`/api/fleet/package_policies`)
.set('kbn-xsrf', 'xxxx')
.send({
name: 'Filetest with space in name',
description: '',
namespace: 'default',
policy_id: originalPolicyId,
enabled: true,
inputs: [],
package: {
name: 'filetest',
title: 'For File Tests',
version: '0.1.0',
},
})
.expect(200);
const {
body: {
item: { id: copy1Id },
},
} = await supertest
.post(`/api/fleet/agent_policies/${originalPolicyId}/copy`)
.set('kbn-xsrf', 'xxxx')
.send({
name: 'copy 123',
description: 'Test',
})
.expect(200);
const {
body: {
item: { package_policies: packagePolicies },
},
} = await supertest.get(`/api/fleet/agent_policies/${copy1Id}`).expect(200);
expect(packagePolicies[0].name).to.eql('Filetest with space in name (copy)');
});
it('should return a 404 with invalid source policy', async () => {
await supertest
.post(`/api/fleet/agent_policies/INVALID_POLICY_ID/copy`)