Skip to content

Commit

Permalink
address Pierre comments
Browse files Browse the repository at this point in the history
  • Loading branch information
mshustov committed Nov 18, 2019
1 parent 94c5509 commit 1e06de4
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { PublicLicense } from './types';
import { hasLicenseInfoChanged } from './has_license_info_changed';

function license({ error, ...customLicense }: { error?: string; [key: string]: any } = {}) {
const defaultLicense: PublicLicense['license'] = {
const defaultLicense: PublicLicense = {
uid: 'uid-000000001234',
status: 'active',
type: 'basic',
Expand Down
6 changes: 3 additions & 3 deletions x-pack/plugins/licensing/common/license.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
* 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 } from './types';
import { PublicLicense, PublicFeatures } from './types';
import { License } from './license';

function createLicense({
license = {},
features = {},
signature = 'xxxxxxxxx',
}: {
license?: Partial<PublicLicense['license']>;
features?: PublicLicense['features'];
license?: Partial<PublicLicense>;
features?: PublicFeatures;
signature?: string;
} = {}) {
const defaultLicense = {
Expand Down
42 changes: 27 additions & 15 deletions x-pack/plugins/licensing/common/license.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,49 @@ import {
LicenseStatus,
LICENSE_CHECK_STATE,
LICENSE_TYPE,
PublicLicenseJSON,
PublicLicense,
PublicFeatures,
} from './types';

/**
* @public
*/
export class License implements ILicense {
private readonly license?: PublicLicense['license'];
private readonly features?: PublicLicense['features'];
private readonly license?: PublicLicense;
private readonly features?: PublicFeatures;

public error?: string;
public isActive: boolean;
public isAvailable: boolean;
public isBasic: boolean;
public isNotBasic: boolean;
public readonly error?: string;
public readonly isActive: boolean;
public readonly isAvailable: boolean;
public readonly isBasic: boolean;
public readonly isNotBasic: boolean;

public uid?: string;
public status?: LicenseStatus;
public expiryDateInMillis?: number;
public type?: LicenseType;
public signature: string;
public readonly uid?: string;
public readonly status?: LicenseStatus;
public readonly expiryDateInMillis?: number;
public readonly type?: LicenseType;
public readonly signature: string;

/**
* @internal
* Generate a License instance from json representation.
*/
static fromJSON(json: PublicLicense) {
static fromJSON(json: PublicLicenseJSON) {
return new License(json);
}

constructor({ license, features, signature, error }: PublicLicense & { error?: string }) {
constructor({
license,
features,
error,
signature,
}: {
license?: PublicLicense;
features?: PublicFeatures;
error?: string;
signature: string;
}) {
this.isAvailable = Boolean(license);
this.license = license;
this.features = features;
Expand All @@ -68,7 +80,7 @@ export class License implements ILicense {
};
}

public getUnavailableReason() {
getUnavailableReason() {
if (this.error) return this.error;
if (!this.isAvailable) {
return 'X-Pack plugin is not installed on the Elasticsearch cluster.';
Expand Down
59 changes: 36 additions & 23 deletions x-pack/plugins/licensing/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,28 +38,41 @@ export interface LicenseFeature {
* @public
* */
export interface PublicLicense {
license?: {
/**
* UID for license.
*/
uid: string;

/**
* The validity status of the license.
*/
status: LicenseStatus;

/**
* Unix epoch of the expiration date of the license.
*/
expiryDateInMillis: number;

/**
* The license type, being usually one of basic, standard, gold, platinum, or trial.
*/
type: LicenseType;
};
features?: Record<string, LicenseFeature>;
/**
* UID for license.
*/
uid: string;

/**
* The validity status of the license.
*/
status: LicenseStatus;

/**
* Unix epoch of the expiration date of the license.
*/
expiryDateInMillis: number;

/**
* The license type, being usually one of basic, standard, gold, platinum, or trial.
*/
type: LicenseType;
}

/**
* Provides information about feature availability for the current license.
* @public
* */
export type PublicFeatures = Record<string, LicenseFeature>;

/**
* Subset of license & features data considered as non-sensitive information.
* Structured as json to be passed to the client.
* @public
* */
export interface PublicLicenseJSON {
license?: PublicLicense;
features?: PublicFeatures;
signature: string;
}

Expand Down Expand Up @@ -129,7 +142,7 @@ export interface ILicense {
/**
* Returns
*/
toJSON: () => PublicLicense;
toJSON: () => PublicLicenseJSON;

/**
* A potential error denoting the failure of the license from being retrieved.
Expand Down
35 changes: 25 additions & 10 deletions x-pack/plugins/licensing/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ import {
IClusterClient,
} from 'src/core/server';

import { ILicense, LicensingPluginSetup, PublicLicense } from '../common/types';
import { ILicense, LicensingPluginSetup, PublicLicense, PublicFeatures } from '../common/types';
import { License } from '../common/license';
import { createLicenseUpdate } from '../common/license_update';

import { ElasticsearchError, RawLicense, RawFeatures } from './types';
import { LicenseConfigType } from './licensing_config';
import { createRouteHandlerContext } from './licensing_route_handler_context';

function normalizeServerLicense(license: RawLicense): PublicLicense['license'] {
function normalizeServerLicense(license: RawLicense): PublicLicense {
return {
uid: license.uid,
type: license.type,
Expand All @@ -36,7 +36,7 @@ function normalizeServerLicense(license: RawLicense): PublicLicense['license'] {
}

function normalizeFeatures(rawFeatures: RawFeatures) {
const features: PublicLicense['features'] = {};
const features: PublicFeatures = {};
for (const [name, feature] of Object.entries(rawFeatures)) {
features[name] = {
isAvailable: feature.available,
Expand All @@ -46,12 +46,23 @@ function normalizeFeatures(rawFeatures: RawFeatures) {
return features;
}

function sign(
licenseJSON: Partial<Pick<PublicLicense, 'license' | 'features'>>,
error: string = ''
) {
function sign({
license,
features,
error,
}: {
license?: PublicLicense;
features?: PublicFeatures;
error?: string;
}) {
return createHash('md5')
.update(JSON.stringify({ ...licenseJSON, error }))
.update(
JSON.stringify({
license,
features,
error,
})
)
.digest('hex');
}

Expand Down Expand Up @@ -126,7 +137,11 @@ export class LicensingPlugin implements Plugin<LicensingPluginSetup> {

const normalizedLicense = normalizeServerLicense(response.license);
const normalizedFeatures = normalizeFeatures(response.features);
const signature = sign({ license: normalizedLicense, features: normalizedFeatures });
const signature = sign({
license: normalizedLicense,
features: normalizedFeatures,
error: '',
});

return new License({
license: normalizedLicense,
Expand All @@ -138,7 +153,7 @@ export class LicensingPlugin implements Plugin<LicensingPluginSetup> {
`License information could not be obtained from Elasticsearch due to ${error} error`
);
const errorMessage = this.getErrorMessage(error);
const signature = sign({}, errorMessage);
const signature = sign({ error: errorMessage });

return new License({
error: this.getErrorMessage(error),
Expand Down

0 comments on commit 1e06de4

Please sign in to comment.