-
Notifications
You must be signed in to change notification settings - Fork 8.2k
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
[Fleet] Restrict integration changes for managed policies #90675
Changes from all commits
30225be
6d9f231
90d93ac
66ba36a
45f420c
a810ce6
3399029
28526e0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ | |
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import expect from '@kbn/expect'; | ||
import { FtrProviderContext } from '../../../api_integration/ftr_provider_context'; | ||
import { warnAndSkipTest } from '../../helpers'; | ||
|
||
|
@@ -39,6 +39,52 @@ export default function ({ getService }: FtrProviderContext) { | |
.send({ agentPolicyId }); | ||
}); | ||
|
||
it('should fail for managed agent policies', async function () { | ||
if (server.enabled) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nitpick we have a helper that does the same thing but I found it less verbose https://github.com/elastic/kibana/blob/master/x-pack/test/fleet_api_integration/apis/epm/data_stream.ts/#L37 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the reminder. I was just copy/pasting from the rest of the file. I'll update |
||
// get a managed policy | ||
const { | ||
body: { item: managedPolicy }, | ||
} = await supertest | ||
.post(`/api/fleet/agent_policies`) | ||
.set('kbn-xsrf', 'xxxx') | ||
.send({ | ||
name: `Managed policy from ${Date.now()}`, | ||
namespace: 'default', | ||
is_managed: true, | ||
}); | ||
|
||
// try to add an integration to the managed policy | ||
const { body } = await supertest | ||
.post(`/api/fleet/package_policies`) | ||
.set('kbn-xsrf', 'xxxx') | ||
.send({ | ||
name: 'filetest-1', | ||
description: '', | ||
namespace: 'default', | ||
policy_id: managedPolicy.id, | ||
enabled: true, | ||
output_id: '', | ||
inputs: [], | ||
package: { | ||
name: 'filetest', | ||
title: 'For File Tests', | ||
version: '0.1.0', | ||
}, | ||
}) | ||
.expect(400); | ||
|
||
expect(body.statusCode).to.be(400); | ||
expect(body.message).to.contain('Cannot add integrations to managed policy'); | ||
|
||
// delete policy we just made | ||
await supertest.post(`/api/fleet/agent_policies/delete`).set('kbn-xsrf', 'xxxx').send({ | ||
agentPolicyId: managedPolicy.id, | ||
}); | ||
} else { | ||
warnAndSkipTest(this, log); | ||
} | ||
}); | ||
|
||
it('should work with valid values', async function () { | ||
if (server.enabled) { | ||
await supertest | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
import expect from '@kbn/expect'; | ||
import { FtrProviderContext } from '../../../api_integration/ftr_provider_context'; | ||
import { skipIfNoDockerRegistry } from '../../helpers'; | ||
|
||
export default function (providerContext: FtrProviderContext) { | ||
const { getService } = providerContext; | ||
const supertest = getService('supertest'); | ||
|
||
// use function () {} and not () => {} here | ||
// because `this` has to point to the Mocha context | ||
// see https://mochajs.org/#arrow-functions | ||
|
||
describe('Package Policy - delete', async function () { | ||
skipIfNoDockerRegistry(providerContext); | ||
let agentPolicy: any; | ||
let packagePolicy: any; | ||
|
||
before(async function () { | ||
let agentPolicyResponse = await supertest | ||
.post(`/api/fleet/agent_policies`) | ||
.set('kbn-xsrf', 'xxxx') | ||
.send({ | ||
name: 'Test policy', | ||
namespace: 'default', | ||
is_managed: false, | ||
}); | ||
|
||
// if one already exists, re-use that | ||
if (agentPolicyResponse.body.statusCode === 409) { | ||
const errorRegex = /^agent policy \'(?<id>[\w,\-]+)\' already exists/i; | ||
const result = errorRegex.exec(agentPolicyResponse.body.message); | ||
if (result?.groups?.id) { | ||
agentPolicyResponse = await supertest | ||
.put(`/api/fleet/agent_policies/${result.groups.id}`) | ||
.set('kbn-xsrf', 'xxxx') | ||
.send({ | ||
name: 'Test policy', | ||
namespace: 'default', | ||
is_managed: false, | ||
}); | ||
} | ||
} | ||
agentPolicy = agentPolicyResponse.body.item; | ||
|
||
const { body: packagePolicyResponse } = await supertest | ||
.post(`/api/fleet/package_policies`) | ||
.set('kbn-xsrf', 'xxxx') | ||
.send({ | ||
name: 'filetest-1', | ||
description: '', | ||
namespace: 'default', | ||
policy_id: agentPolicy.id, | ||
enabled: true, | ||
output_id: '', | ||
inputs: [], | ||
package: { | ||
name: 'filetest', | ||
title: 'For File Tests', | ||
version: '0.1.0', | ||
}, | ||
}); | ||
packagePolicy = packagePolicyResponse.item; | ||
}); | ||
|
||
after(async function () { | ||
await supertest | ||
.post(`/api/fleet/agent_policies/delete`) | ||
.set('kbn-xsrf', 'xxxx') | ||
.send({ agentPolicyId: agentPolicy.id }); | ||
|
||
await supertest | ||
.post(`/api/fleet/package_policies/delete`) | ||
.set('kbn-xsrf', 'xxxx') | ||
.send({ packagePolicyIds: [packagePolicy.id] }); | ||
}); | ||
|
||
it('should fail on managed agent policies', async function () { | ||
// update existing policy to managed | ||
await supertest | ||
.put(`/api/fleet/agent_policies/${agentPolicy.id}`) | ||
.set('kbn-xsrf', 'xxxx') | ||
.send({ | ||
name: agentPolicy.name, | ||
namespace: agentPolicy.namespace, | ||
is_managed: true, | ||
}) | ||
.expect(200); | ||
|
||
// try to delete | ||
const { body: results } = await supertest | ||
.post(`/api/fleet/package_policies/delete`) | ||
.set('kbn-xsrf', 'xxxx') | ||
.send({ packagePolicyIds: [packagePolicy.id] }) | ||
.expect(200); | ||
|
||
// delete always succeeds (returns 200) with Array<{success: boolean}> | ||
expect(Array.isArray(results)); | ||
expect(results.length).to.be(1); | ||
expect(results[0].success).to.be(false); | ||
expect(results[0].body.message).to.contain('Cannot remove integrations of managed policy'); | ||
|
||
// revert existing policy to unmanaged | ||
await supertest | ||
.put(`/api/fleet/agent_policies/${agentPolicy.id}`) | ||
.set('kbn-xsrf', 'xxxx') | ||
.send({ | ||
name: agentPolicy.name, | ||
namespace: agentPolicy.namespace, | ||
is_managed: false, | ||
}) | ||
.expect(200); | ||
}); | ||
|
||
it('should work for unmanaged policies', async function () { | ||
await supertest | ||
.post(`/api/fleet/package_policies/delete`) | ||
.set('kbn-xsrf', 'xxxx') | ||
.send({ packagePolicyIds: [packagePolicy.id] }); | ||
}); | ||
}); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(not blocking) I have the feeling that is time to refactor L64-L80 to a new function something like
_validateParentAgentPolicy
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I wanted to consolidate the two sources of the "There is already a package named..." errors
kibana/x-pack/plugins/fleet/server/services/package_policy.ts
Lines 297 to 304 in 45f420c
And perhaps give it a specific error name/type. I plan on circling back to that after I get the other features in.
I'll open a ticket