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

Add x-pack plugin for new platform browser licensing information #44922

Closed
wants to merge 15 commits into from
Closed
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
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.

2 changes: 2 additions & 0 deletions x-pack/dev-tools/jest/setup/polyfills.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ Object.defineProperty(window, 'MutationObserver', { value: MutationObserver });

const URL = { createObjectURL: () => '' };
Object.defineProperty(window, 'URL', { value: URL });

require('jest-localstorage-mock');

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions x-pack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@
"hapi": "^17.5.3",
"jest": "^24.9.0",
"jest-cli": "^24.9.0",
"jest-localstorage-mock": "^2.4.0",
"jest-styled-components": "^6.3.3",
"jsdom": "^12.2.0",
"madge": "3.4.4",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
* you may not use this file except in compliance with the Elastic License.
*/

export const SERVICE_NAME = 'licensing';
export const API_ROUTE = '/api/xpack/v1/info';
export const LICENSING_SESSION = 'xpack.licensing';
export const LICENSING_SESSION_SIGNATURE = 'xpack.licensing.signature';
export const SIGNATURE_HEADER = 'kbn-xpack-sig';
export const DEFAULT_POLLING_FREQUENCY = 30001; // 30 seconds
export enum LICENSE_STATUS {
export enum LICENSE_CHECK_STATE {
Unavailable = 'UNAVAILABLE',
Invalid = 'INVALID',
Expired = 'EXPIRED',
Expand Down
11 changes: 11 additions & 0 deletions x-pack/plugins/licensing/common/delay.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* 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.
*/

/**
* Utility function to delay execution of the event loop for a specified duration.
* @param duration {number} Minimum amount of time in milliseconds to delay execution
*/
export const delay = (duration: number) => new Promise(r => setTimeout(r, duration));
128 changes: 128 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,128 @@
/*
* 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 { hasLicenseInfoChanged } from './has_license_info_changed';

function license({ error, ...rawLicense }: { error?: Error; [key: string]: any } = {}) {
return new License({
error,
license: Object.keys(rawLicense).length ? rawLicense : undefined,
});
}

// Each test should ensure that left-to-right and right-to-left comparisons are captured.

describe('has license info changed', () => {
describe('undefined', () => {
test('undefined <-> undefined', async () => {
expect(hasLicenseInfoChanged(undefined, undefined)).toBe(false);
});

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

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

test('uid License <-> uid License', async () => {
expect(hasLicenseInfoChanged(license({ uid: 'alpha' }), license({ uid: 'alpha' }))).toBe(
false
);
expect(hasLicenseInfoChanged(license({ uid: 'alpha' }), license({ uid: 'beta' }))).toBe(
false
);
expect(hasLicenseInfoChanged(license({ uid: 'beta' }), license({ uid: 'alpha' }))).toBe(
false
);
});

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

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('License <-> status License', async () => {
expect(hasLicenseInfoChanged(license({ status: 'active' }), license())).toBe(true);
expect(hasLicenseInfoChanged(license(), license({ status: 'active' }))).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('License <-> expiry License', async () => {
expect(hasLicenseInfoChanged(license({ expiry_date_in_millis: 100 }), license())).toBe(true);
expect(hasLicenseInfoChanged(license(), license({ expiry_date_in_millis: 100 }))).toBe(true);
});

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

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

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

test('error License <-> error License | mismatched messages', async () => {
expect(
hasLicenseInfoChanged(
license({ error: new Error('alpha') }),
license({ error: new Error('beta') })
)
).toBe(true);
expect(
hasLicenseInfoChanged(
license({ error: new Error('beta') }),
license({ error: new Error('alpha') })
)
).toBe(true);
});
});
});
45 changes: 45 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,45 @@
/*
* 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';

/**
* @public
* Check if 2 potential license instances have changes between them
*/
export function hasLicenseInfoChanged(
currentLicense: License | undefined,
newLicense: License | undefined
) {
// If we have 2 valid license instances, let's check:
// 1. That if they both contain an error that the message has changed
// 2. Check if the type has changed
// 3. Check if the status has changed
// 4. Check if the expiration date has changed
// 5. Check is the availability of the license has changed.
if (currentLicense && newLicense) {
if (
(currentLicense.error && !newLicense.error) ||
(!currentLicense.error && newLicense.error) ||
(newLicense.error &&
currentLicense.error &&
newLicense.error.message !== currentLicense.error.message)
) {
return true;
}

return (
newLicense.type !== currentLicense.type ||
newLicense.status !== currentLicense.status ||
newLicense.expiryDateInMillis !== currentLicense.expiryDateInMillis ||
newLicense.isAvailable !== currentLicense.isAvailable
);
}

// If we have made it here, one or both of the licenses are undefined.
// If they match (both undefined), nothing has changed, otherwise it did.
return currentLicense !== newLicense;
}
Loading