Skip to content

Commit

Permalink
[Fleet] copy inactivity_timeout when duplicating agent policy (elas…
Browse files Browse the repository at this point in the history
…tic#164544)

## Summary

Closes elastic#164532 

`inactivity_timeout` and a lot of other properties on the agent policy
saved object were not being copied over when duplicating an agent
policy.

The other properties that are now duplicated when cloning an agent
policy are :

- `inactivity_timeout` 
- `unenroll_timeout` 
- `agent_features` - this contains the hostname mode 
- `overrides` 
- `data_output_id` 
- `monitoring_output_id` 
- `download_source_id` 
- `fleet_server_host_id`

Automated test added.
  • Loading branch information
hop-dev authored Aug 23, 2023
1 parent 9a77123 commit ce2c18d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
2 changes: 2 additions & 0 deletions x-pack/plugins/fleet/common/types/models/agent_policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import type { Output } from './output';

export type AgentPolicyStatus = typeof agentPolicyStatuses;

// adding a property here? If it should be cloned when duplicating a policy, add it to `agentPolicyService.copy`
// x-pack/plugins/fleet/server/services/agent_policy.ts#L571
export interface NewAgentPolicy {
id?: string;
name: string;
Expand Down
18 changes: 13 additions & 5 deletions x-pack/plugins/fleet/server/services/agent_policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { omit, isEqual, keyBy, groupBy } from 'lodash';
import { omit, isEqual, keyBy, groupBy, pick } from 'lodash';
import { v5 as uuidv5 } from 'uuid';
import { safeDump } from 'js-yaml';
import pMap from 'p-map';
Expand Down Expand Up @@ -562,14 +562,22 @@ class AgentPolicyService {
if (!baseAgentPolicy) {
throw new Error('Agent policy not found');
}
// eslint-disable-next-line @typescript-eslint/naming-convention
const { namespace, monitoring_enabled } = baseAgentPolicy;
const newAgentPolicy = await this.create(
soClient,
esClient,
{
namespace,
monitoring_enabled,
...pick(baseAgentPolicy, [
'namespace',
'monitoring_enabled',
'inactivity_timeout',
'unenroll_timeout',
'agent_features',
'overrides',
'data_output_id',
'monitoring_output_id',
'download_source_id',
'fleet_server_host_id',
]),
...newAgentPolicyProps,
},
options
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,34 @@ export default function (providerContext: FtrProviderContext) {
});
});

it('should copy inactivity timeout', async () => {
const {
body: { item: policyWithTimeout },
} = await supertest
.post(`/api/fleet/agent_policies`)
.set('kbn-xsrf', 'xxxx')
.send({
name: 'Inactivity test',
namespace: 'default',
is_managed: true,
inactivity_timeout: 123,
})
.expect(200);

const {
body: { item: newPolicy },
} = await supertest
.post(`/api/fleet/agent_policies/${policyWithTimeout.id}/copy`)
.set('kbn-xsrf', 'xxxx')
.send({
name: 'Inactivity test copy',
description: 'Test',
})
.expect(200);

expect(newPolicy.inactivity_timeout).to.eql(123);
});

it('should increment package policy copy names', async () => {
async function getSystemPackagePolicyCopyVersion(policyId: string) {
const {
Expand Down

0 comments on commit ce2c18d

Please sign in to comment.