Skip to content

Commit

Permalink
Licensing plugin (#49345)
Browse files Browse the repository at this point in the history
* Add x-pack plugin for new platform browser licensing information

* Address next round of reviews

* Remove poller functionality in favor of inline observables

* More observable changes from review comments

* Fix outstanding tests

* More changes from review, adding additional testing

* Add additional tests for license comparisons and sessions

* Update test snapshot due to sessionstorage mock

* Next round of review feedback from restrry

* Fix more review requests from restrry, add additional tests

* Pass correct sign mock to license info changed test

* Improve doc comments, switch to I-interface pattern

* Test error polling sanity, do not expose signature, do not poll on client

* Fix type check issues from rebase

* Fix build error from rebase

* minimize config

* move all types to server with consistency with other code

* implement License

* implement license update & refactor has License changed check

* update tests for licensing extending route handler context

* implement client side side license plugin

* implement server side licensing plugin

* remove old code

* update testing harness

* update types for license status

* remove jest-localstorage-mock

* fix tests

* update license in security

* address comments. first pass

* error is a part of signature. pass error message to License

* move common license types under common folder

* rename feature props for BWC and unify name with ILicense

* test should work in any timezone

* make prettier happy

* remove obsolete comment

* address Pierre comments

* use sha256 for security reasons

* use stable stringify to avoid churn
  • Loading branch information
mshustov committed Nov 19, 2019
1 parent 551b03b commit 643f15c
Show file tree
Hide file tree
Showing 39 changed files with 1,783 additions and 1,068 deletions.
13 changes: 13 additions & 0 deletions src/core/public/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,22 @@ function createCoreContext(): CoreContext {
};
}

function createStorageMock() {
const storageMock: jest.Mocked<Storage> = {
getItem: jest.fn(),
setItem: jest.fn(),
removeItem: jest.fn(),
clear: jest.fn(),
key: jest.fn(),
length: 10,
};
return storageMock;
}

export const coreMock = {
createCoreContext,
createSetup: createCoreSetupMock,
createStart: createCoreStartMock,
createPluginInitializerContext: pluginInitializerContextMock,
createStorage: createStorageMock,
};
71 changes: 0 additions & 71 deletions src/core/utils/poller.test.ts

This file was deleted.

55 changes: 0 additions & 55 deletions src/core/utils/poller.ts

This file was deleted.

93 changes: 93 additions & 0 deletions x-pack/plugins/licensing/common/has_license_info_changed.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { License } from './license';
import { PublicLicense } from './types';
import { hasLicenseInfoChanged } from './has_license_info_changed';

function license({ error, ...customLicense }: { error?: string; [key: string]: any } = {}) {
const defaultLicense: PublicLicense = {
uid: 'uid-000000001234',
status: 'active',
type: 'basic',
expiryDateInMillis: 1000,
};

return new License({
error,
license: Object.assign(defaultLicense, customLicense),
signature: 'aaaaaaa',
});
}

// Each test should ensure that left-to-right and right-to-left comparisons are captured.
describe('has license info changed', () => {
describe('License', () => {
test('undefined <-> License', async () => {
expect(hasLicenseInfoChanged(undefined, license())).toBe(true);
});

test('the same License', async () => {
const licenseInstance = license();
expect(hasLicenseInfoChanged(licenseInstance, licenseInstance)).toBe(false);
});

test('type License <-> type License | mismatched type', async () => {
expect(hasLicenseInfoChanged(license({ type: 'basic' }), license({ type: 'gold' }))).toBe(
true
);
expect(hasLicenseInfoChanged(license({ type: 'gold' }), license({ type: 'basic' }))).toBe(
true
);
});

test('status License <-> status License | mismatched status', async () => {
expect(
hasLicenseInfoChanged(license({ status: 'active' }), license({ status: 'inactive' }))
).toBe(true);
expect(
hasLicenseInfoChanged(license({ status: 'inactive' }), license({ status: 'active' }))
).toBe(true);
});

test('expiry License <-> expiry License | mismatched expiry', async () => {
expect(
hasLicenseInfoChanged(
license({ expiryDateInMillis: 100 }),
license({ expiryDateInMillis: 200 })
)
).toBe(true);
expect(
hasLicenseInfoChanged(
license({ expiryDateInMillis: 200 }),
license({ expiryDateInMillis: 100 })
)
).toBe(true);
});
});

describe('error License', () => {
test('License <-> error License', async () => {
expect(hasLicenseInfoChanged(license({ error: 'reason' }), license())).toBe(true);
expect(hasLicenseInfoChanged(license(), license({ error: 'reason' }))).toBe(true);
});

test('error License <-> error License | matched messages', async () => {
expect(
hasLicenseInfoChanged(license({ error: 'reason-1' }), license({ error: 'reason-1' }))
).toBe(false);
});

test('error License <-> error License | mismatched messages', async () => {
expect(
hasLicenseInfoChanged(license({ error: 'reason-1' }), license({ error: 'reason-2' }))
).toBe(true);
expect(
hasLicenseInfoChanged(license({ error: 'reason-2' }), license({ error: 'reason-1' }))
).toBe(true);
});
});
});
24 changes: 24 additions & 0 deletions x-pack/plugins/licensing/common/has_license_info_changed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { ILicense } from './types';

/**
* Check if 2 potential license instances have changes between them
* @internal
*/
export function hasLicenseInfoChanged(currentLicense: ILicense | undefined, newLicense: ILicense) {
if (currentLicense === newLicense) return false;
if (!currentLicense) return true;

return (
newLicense.error !== currentLicense.error ||
newLicense.type !== currentLicense.type ||
newLicense.status !== currentLicense.status ||
newLicense.expiryDateInMillis !== currentLicense.expiryDateInMillis ||
newLicense.isAvailable !== currentLicense.isAvailable
);
}
44 changes: 44 additions & 0 deletions x-pack/plugins/licensing/common/license.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { PublicLicense, PublicFeatures } from './types';
import { License } from './license';

function createLicense({
license = {},
features = {},
signature = 'xxxxxxxxx',
}: {
license?: Partial<PublicLicense>;
features?: PublicFeatures;
signature?: string;
} = {}) {
const defaultLicense = {
uid: 'uid-000000001234',
status: 'active',
type: 'basic',
expiryDateInMillis: 5000,
};

const defaultFeatures = {
ccr: {
isEnabled: true,
isAvailable: true,
},
ml: {
isEnabled: false,
isAvailable: true,
},
};
return new License({
license: Object.assign(defaultLicense, license),
features: Object.assign(defaultFeatures, features),
signature,
});
}

export const licenseMock = {
create: createLicense,
};
Loading

0 comments on commit 643f15c

Please sign in to comment.