|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | + |
| 7 | +import { Subject } from 'rxjs'; |
| 8 | +import { loggingSystemMock, savedObjectsServiceMock } from 'src/core/server/mocks'; |
| 9 | +import { LicenseService } from '../../../../common/license/license'; |
| 10 | +import { createPackagePolicyServiceMock } from '../../../../../fleet/server/mocks'; |
| 11 | +import { PolicyWatcher } from './license_watch'; |
| 12 | +import { ILicense } from '../../../../../licensing/common/types'; |
| 13 | +import { licenseMock } from '../../../../../licensing/common/licensing.mock'; |
| 14 | +import { PackagePolicyServiceInterface } from '../../../../../fleet/server'; |
| 15 | +import { PackagePolicy } from '../../../../../fleet/common'; |
| 16 | +import { createPackagePolicyMock } from '../../../../../fleet/common/mocks'; |
| 17 | +import { factory } from '../../../../common/endpoint/models/policy_config'; |
| 18 | +import { PolicyConfig } from '../../../../common/endpoint/types'; |
| 19 | + |
| 20 | +const MockPPWithEndpointPolicy = (cb?: (p: PolicyConfig) => PolicyConfig): PackagePolicy => { |
| 21 | + const packagePolicy = createPackagePolicyMock(); |
| 22 | + if (!cb) { |
| 23 | + // eslint-disable-next-line no-param-reassign |
| 24 | + cb = (p) => p; |
| 25 | + } |
| 26 | + const policyConfig = cb(factory()); |
| 27 | + packagePolicy.inputs[0].config = { policy: { value: policyConfig } }; |
| 28 | + return packagePolicy; |
| 29 | +}; |
| 30 | + |
| 31 | +describe('Policy-Changing license watcher', () => { |
| 32 | + const logger = loggingSystemMock.create().get('license_watch.test'); |
| 33 | + const soStartMock = savedObjectsServiceMock.createStartContract(); |
| 34 | + let packagePolicySvcMock: jest.Mocked<PackagePolicyServiceInterface>; |
| 35 | + |
| 36 | + const Platinum = licenseMock.createLicense({ license: { type: 'platinum', mode: 'platinum' } }); |
| 37 | + const Gold = licenseMock.createLicense({ license: { type: 'gold', mode: 'gold' } }); |
| 38 | + const Basic = licenseMock.createLicense({ license: { type: 'basic', mode: 'basic' } }); |
| 39 | + |
| 40 | + beforeEach(() => { |
| 41 | + packagePolicySvcMock = createPackagePolicyServiceMock(); |
| 42 | + }); |
| 43 | + |
| 44 | + it('is activated on license changes', () => { |
| 45 | + // mock a license-changing service to test reactivity |
| 46 | + const licenseEmitter: Subject<ILicense> = new Subject(); |
| 47 | + const licenseService = new LicenseService(); |
| 48 | + const pw = new PolicyWatcher(packagePolicySvcMock, soStartMock, logger); |
| 49 | + |
| 50 | + // swap out watch function, just to ensure it gets called when a license change happens |
| 51 | + const mockWatch = jest.fn(); |
| 52 | + pw.watch = mockWatch; |
| 53 | + |
| 54 | + // licenseService is watching our subject for incoming licenses |
| 55 | + licenseService.start(licenseEmitter); |
| 56 | + pw.start(licenseService); // and the PolicyWatcher under test, uses that to subscribe as well |
| 57 | + |
| 58 | + // Enqueue a license change! |
| 59 | + licenseEmitter.next(Platinum); |
| 60 | + |
| 61 | + // policywatcher should have triggered |
| 62 | + expect(mockWatch.mock.calls.length).toBe(1); |
| 63 | + |
| 64 | + pw.stop(); |
| 65 | + licenseService.stop(); |
| 66 | + licenseEmitter.complete(); |
| 67 | + }); |
| 68 | + |
| 69 | + it('pages through all endpoint policies', async () => { |
| 70 | + const TOTAL = 247; |
| 71 | + |
| 72 | + // set up the mocked package policy service to return and do what we want |
| 73 | + packagePolicySvcMock.list |
| 74 | + .mockResolvedValueOnce({ |
| 75 | + items: Array.from({ length: 100 }, () => MockPPWithEndpointPolicy()), |
| 76 | + total: TOTAL, |
| 77 | + page: 1, |
| 78 | + perPage: 100, |
| 79 | + }) |
| 80 | + .mockResolvedValueOnce({ |
| 81 | + items: Array.from({ length: 100 }, () => MockPPWithEndpointPolicy()), |
| 82 | + total: TOTAL, |
| 83 | + page: 2, |
| 84 | + perPage: 100, |
| 85 | + }) |
| 86 | + .mockResolvedValueOnce({ |
| 87 | + items: Array.from({ length: TOTAL - 200 }, () => MockPPWithEndpointPolicy()), |
| 88 | + total: TOTAL, |
| 89 | + page: 3, |
| 90 | + perPage: 100, |
| 91 | + }); |
| 92 | + |
| 93 | + const pw = new PolicyWatcher(packagePolicySvcMock, soStartMock, logger); |
| 94 | + await pw.watch(Gold); // just manually trigger with a given license |
| 95 | + |
| 96 | + expect(packagePolicySvcMock.list.mock.calls.length).toBe(3); // should have asked for 3 pages of resuts |
| 97 | + |
| 98 | + // Assert: on the first call to packagePolicy.list, we asked for page 1 |
| 99 | + expect(packagePolicySvcMock.list.mock.calls[0][1].page).toBe(1); |
| 100 | + expect(packagePolicySvcMock.list.mock.calls[1][1].page).toBe(2); // second call, asked for page 2 |
| 101 | + expect(packagePolicySvcMock.list.mock.calls[2][1].page).toBe(3); // etc |
| 102 | + }); |
| 103 | + |
| 104 | + it('alters no-longer-licensed features', async () => { |
| 105 | + const CustomMessage = 'Custom string'; |
| 106 | + |
| 107 | + // mock a Policy with a higher-tiered feature enabled |
| 108 | + packagePolicySvcMock.list.mockResolvedValueOnce({ |
| 109 | + items: [ |
| 110 | + MockPPWithEndpointPolicy( |
| 111 | + (pc: PolicyConfig): PolicyConfig => { |
| 112 | + pc.windows.popup.malware.message = CustomMessage; |
| 113 | + return pc; |
| 114 | + } |
| 115 | + ), |
| 116 | + ], |
| 117 | + total: 1, |
| 118 | + page: 1, |
| 119 | + perPage: 100, |
| 120 | + }); |
| 121 | + |
| 122 | + const pw = new PolicyWatcher(packagePolicySvcMock, soStartMock, logger); |
| 123 | + |
| 124 | + // emulate a license change below paid tier |
| 125 | + await pw.watch(Basic); |
| 126 | + |
| 127 | + expect(packagePolicySvcMock.update).toHaveBeenCalled(); |
| 128 | + expect( |
| 129 | + packagePolicySvcMock.update.mock.calls[0][2].inputs[0].config!.policy.value.windows.popup |
| 130 | + .malware.message |
| 131 | + ).not.toEqual(CustomMessage); |
| 132 | + }); |
| 133 | +}); |
0 commit comments