Skip to content

Commit

Permalink
[Fleet] Do not mutate package policy update object (#125622)
Browse files Browse the repository at this point in the history
* do not modify object property

* add unit test
  • Loading branch information
hop-dev authored Feb 15, 2022
1 parent 743733d commit 7934cd5
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
52 changes: 51 additions & 1 deletion x-pack/plugins/fleet/server/services/package_policy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
savedObjectsClientMock,
httpServerMock,
} from 'src/core/server/mocks';

import { produce } from 'immer';
import type {
SavedObjectsClient,
SavedObjectsClientContract,
Expand All @@ -25,6 +25,7 @@ import type {
PostPackagePolicyDeleteCallback,
RegistryDataStream,
PackagePolicyInputStream,
PackagePolicy,
} from '../types';
import { createPackagePolicyMock } from '../../common/mocks';

Expand Down Expand Up @@ -949,6 +950,55 @@ describe('Package policy service', () => {

expect(result.elasticsearch).toMatchObject({ privileges: { cluster: ['monitor'] } });
});

it('should not mutate packagePolicyUpdate object when trimming whitespace', async () => {
const savedObjectsClient = savedObjectsClientMock.create();
const mockPackagePolicy = createPackagePolicyMock();

const attributes = {
...mockPackagePolicy,
inputs: [],
};

savedObjectsClient.get.mockResolvedValue({
id: 'test',
type: 'abcd',
references: [],
version: 'test',
attributes,
});

savedObjectsClient.update.mockImplementation(
async (
type: string,
id: string,
attrs: any
): Promise<SavedObjectsUpdateResponse<PackagePolicySOAttributes>> => {
savedObjectsClient.get.mockResolvedValue({
id: 'test',
type: 'abcd',
references: [],
version: 'test',
attributes: attrs,
});
return attrs;
}
);
const elasticsearchClient = elasticsearchServiceMock.createClusterClient().asInternalUser;

const result = await packagePolicyService.update(
savedObjectsClient,
elasticsearchClient,
'the-package-policy-id',
// this mimics the way that OSQuery plugin create immutable objects
produce<PackagePolicy>(
{ ...mockPackagePolicy, name: ' test ', inputs: [] },
(draft) => draft
)
);

expect(result.name).toEqual('test');
});
});

describe('runDeleteExternalCallbacks', () => {
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/fleet/server/services/package_policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,11 +363,11 @@ class PackagePolicyService {
soClient: SavedObjectsClientContract,
esClient: ElasticsearchClient,
id: string,
packagePolicy: UpdatePackagePolicy,
packagePolicyUpdate: UpdatePackagePolicy,
options?: { user?: AuthenticatedUser },
currentVersion?: string
): Promise<PackagePolicy> {
packagePolicy.name = packagePolicy.name.trim();
const packagePolicy = { ...packagePolicyUpdate, name: packagePolicyUpdate.name.trim() };
const oldPackagePolicy = await this.get(soClient, id);
const { version, ...restOfPackagePolicy } = packagePolicy;

Expand Down

0 comments on commit 7934cd5

Please sign in to comment.