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

[Fleet] Fix policy revision number getting bumped for no reason #104696

Merged
merged 2 commits into from
Jul 7, 2021
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
77 changes: 76 additions & 1 deletion x-pack/plugins/fleet/server/services/preconfiguration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ import type { AgentPolicy, NewPackagePolicy, Output } from '../types';

import { AGENT_POLICY_SAVED_OBJECT_TYPE } from '../constants';

import { ensurePreconfiguredPackagesAndPolicies } from './preconfiguration';
import {
ensurePreconfiguredPackagesAndPolicies,
comparePreconfiguredPolicyToCurrent,
} from './preconfiguration';

jest.mock('./agent_policy_update');

Expand Down Expand Up @@ -279,3 +282,75 @@ describe('policy preconfiguration', () => {
expect(policiesB[0].updated_at).toEqual(policiesA[0].updated_at);
});
});

describe('comparePreconfiguredPolicyToCurrent', () => {
const baseConfig = {
name: 'Test policy',
namespace: 'default',
description: 'This is a test policy',
id: 'test-id',
unenroll_timeout: 60,
package_policies: [
{
package: { name: 'test_package' },
name: 'Test package',
},
],
};

const basePackagePolicy: AgentPolicy = {
id: 'test-id',
namespace: 'default',
monitoring_enabled: ['logs', 'metrics'],
name: 'Test policy',
description: 'This is a test policy',
unenroll_timeout: 60,
is_preconfigured: true,
status: 'active',
is_managed: true,
revision: 1,
updated_at: '2021-07-07T16:29:55.144Z',
updated_by: 'system',
package_policies: [
{
package: { name: 'test_package', title: 'Test package', version: '1.0.0' },
name: 'Test package',
namespace: 'default',
enabled: true,
id: 'test-package-id',
revision: 1,
updated_at: '2021-07-07T16:29:55.144Z',
updated_by: 'system',
created_at: '2021-07-07T16:29:55.144Z',
created_by: 'system',
inputs: [],
policy_id: 'abc123',
output_id: 'default',
},
],
};

it('should return hasChanged when a top-level policy field changes', () => {
const { hasChanged } = comparePreconfiguredPolicyToCurrent(
{ ...baseConfig, unenroll_timeout: 120 },
basePackagePolicy
);
expect(hasChanged).toBe(true);
});

it('should not return hasChanged when no top-level fields change', () => {
const { hasChanged } = comparePreconfiguredPolicyToCurrent(
{
...baseConfig,
package_policies: [
{
package: { name: 'different_package' },
name: 'Different package',
},
],
},
basePackagePolicy
);
expect(hasChanged).toBe(false);
});
});
25 changes: 20 additions & 5 deletions x-pack/plugins/fleet/server/services/preconfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import type { ElasticsearchClient, SavedObjectsClientContract } from 'src/core/server';
import { i18n } from '@kbn/i18n';
import { groupBy, omit, isEqual } from 'lodash';
import { groupBy, omit, pick, isEqual } from 'lodash';

import type {
NewPackagePolicy,
Expand Down Expand Up @@ -142,14 +142,16 @@ export async function ensurePreconfiguredPackagesAndPolicies(

if (!created) {
if (!policy?.is_managed) return { created, policy };
const configTopLevelFields = omit(preconfiguredAgentPolicy, 'package_policies', 'id');
const currentTopLevelFields = omit(policy, 'package_policies');
if (!isEqual(configTopLevelFields, currentTopLevelFields)) {
const { hasChanged, fields } = comparePreconfiguredPolicyToCurrent(
preconfiguredAgentPolicy,
policy
);
if (hasChanged) {
const updatedPolicy = await agentPolicyService.update(
soClient,
esClient,
String(preconfiguredAgentPolicy.id),
configTopLevelFields
fields
);
return { created, policy: updatedPolicy };
}
Expand Down Expand Up @@ -243,6 +245,19 @@ export async function ensurePreconfiguredPackagesAndPolicies(
};
}

export function comparePreconfiguredPolicyToCurrent(
policyFromConfig: PreconfiguredAgentPolicy,
currentPolicy: AgentPolicy
) {
const configTopLevelFields = omit(policyFromConfig, 'package_policies', 'id');
const currentTopLevelFields = pick(currentPolicy, ...Object.keys(configTopLevelFields));

return {
hasChanged: !isEqual(configTopLevelFields, currentTopLevelFields),
fields: configTopLevelFields,
};
}

async function addPreconfiguredPolicyPackages(
soClient: SavedObjectsClientContract,
esClient: ElasticsearchClient,
Expand Down