Skip to content

Commit 6c1f248

Browse files
authored
Fix wrong impor (#52994)
* Licensing plugin and XPackInfo uses the same license data (#52507) * convert xpackinfo to TS * use NP Licensing plugin in XPackInfo * update mocks * put license regresh hack back. otherwise new license won't be re-fetched when signature changed. was deleted by mistake * add functional test for legacy xpackmain * declare setup types on client & server explicitly * rename mock license --> licensing to match plugin name * add tests for createLicensePoller * fix type error * adopt tests for xpack_info * createXPackInfo uses new platform API * put back error mute * address comments * fix renamed import * address comment * update tests to reduce delays * deprecate xpack.xpack_main.xpack_api_polling_frequency_millis * use snake_case in config * fix wrong import
1 parent a97d264 commit 6c1f248

40 files changed

+712
-772
lines changed

.eslintrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ module.exports = {
326326
allowSameFolder: true,
327327
},
328328
{
329-
target: ['src/core/**/*'],
329+
target: ['src/**/*'],
330330
from: ['x-pack/**/*'],
331331
errorMessage: 'OSS cannot import x-pack files.',
332332
},

src/legacy/ui/public/new_platform/new_platform.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import { DevToolsSetup, DevToolsStart } from '../../../../plugins/dev_tools/publ
3232
import { KibanaLegacySetup, KibanaLegacyStart } from '../../../../plugins/kibana_legacy/public';
3333
import { HomePublicPluginSetup, HomePublicPluginStart } from '../../../../plugins/home/public';
3434
import { SharePluginSetup, SharePluginStart } from '../../../../plugins/share/public';
35-
import { LicensingPluginSetup } from '../../../../../x-pack/plugins/licensing/common/types';
3635

3736
export interface PluginsSetup {
3837
data: ReturnType<DataPlugin['setup']>;
@@ -44,7 +43,6 @@ export interface PluginsSetup {
4443
dev_tools: DevToolsSetup;
4544
kibana_legacy: KibanaLegacySetup;
4645
share: SharePluginSetup;
47-
licensing: LicensingPluginSetup;
4846
}
4947

5048
export interface PluginsStart {

x-pack/legacy/common/poller.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,7 @@ export declare class Poller {
88
constructor(options: any);
99

1010
public start(): void;
11+
public stop(): void;
12+
public isRunning(): boolean;
13+
public getPollFrequency(): number;
1114
}

x-pack/legacy/plugins/graph/public/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { SavedObjectRegistryProvider } from 'ui/saved_objects/saved_object_regis
1717
import { npSetup, npStart } from 'ui/new_platform';
1818
import { Storage } from '../../../../../src/plugins/kibana_utils/public';
1919
import { start as navigation } from '../../../../../src/legacy/core_plugins/navigation/public/legacy';
20+
import { LicensingPluginSetup } from '../../../../plugins/licensing/public';
2021
import { GraphPlugin } from './plugin';
2122

2223
// @ts-ignore
@@ -39,13 +40,17 @@ async function getAngularInjectedDependencies(): Promise<LegacyAngularInjectedDe
3940
};
4041
}
4142

43+
type XpackNpSetupDeps = typeof npSetup.plugins & {
44+
licensing: LicensingPluginSetup;
45+
};
46+
4247
(async () => {
4348
const instance = new GraphPlugin();
4449
instance.setup(npSetup.core, {
4550
__LEGACY: {
4651
Storage,
4752
},
48-
...npSetup.plugins,
53+
...(npSetup.plugins as XpackNpSetupDeps),
4954
});
5055
instance.start(npStart.core, {
5156
npData: npStart.plugins.data,

x-pack/legacy/plugins/graph/public/plugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { CoreSetup, CoreStart, Plugin, SavedObjectsClientContract } from 'src/co
99
import { Plugin as DataPlugin } from 'src/plugins/data/public';
1010
import { LegacyAngularInjectedDependencies } from './render_app';
1111
import { NavigationStart } from '../../../../../src/legacy/core_plugins/navigation/public';
12-
import { LicensingPluginSetup } from '../../../../plugins/licensing/common/types';
12+
import { LicensingPluginSetup } from '../../../../plugins/licensing/public';
1313

1414
export interface GraphPluginStartDependencies {
1515
npData: ReturnType<DataPlugin['start']>;

x-pack/legacy/plugins/graph/public/render_app.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import {
3838
IndexPatternsContract,
3939
} from '../../../../../src/plugins/data/public';
4040
import { NavigationStart } from '../../../../../src/legacy/core_plugins/navigation/public';
41-
import { LicensingPluginSetup } from '../../../../plugins/licensing/common/types';
41+
import { LicensingPluginSetup } from '../../../../plugins/licensing/public';
4242
import { checkLicense } from '../../../../plugins/graph/common/check_license';
4343

4444
/**

x-pack/legacy/plugins/xpack_main/index.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66

77
import { resolve } from 'path';
88
import dedent from 'dedent';
9-
import {
10-
XPACK_INFO_API_DEFAULT_POLL_FREQUENCY_IN_MILLIS
11-
} from '../../server/lib/constants';
129
import { mirrorPluginStatus } from '../../server/lib/mirror_plugin_status';
1310
import { replaceInjectedVars } from './server/lib/replace_injected_vars';
1411
import { setupXPackMain } from './server/lib/setup_xpack_main';
@@ -34,7 +31,6 @@ export const xpackMain = (kibana) => {
3431
enabled: Joi.boolean().default(),
3532
url: Joi.string().default(),
3633
}).default(), // deprecated
37-
xpack_api_polling_frequency_millis: Joi.number().default(XPACK_INFO_API_DEFAULT_POLL_FREQUENCY_IN_MILLIS),
3834
}).default();
3935
},
4036

@@ -47,6 +43,9 @@ export const xpackMain = (kibana) => {
4743
},
4844

4945
uiExports: {
46+
hacks: [
47+
'plugins/xpack_main/hacks/check_xpack_info_change',
48+
],
5049
replaceInjectedVars,
5150
injectDefaultVars(server) {
5251
const config = server.config();
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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 { identity } from 'lodash';
8+
import { uiModules } from 'ui/modules';
9+
import { Path } from 'plugins/xpack_main/services/path';
10+
import { xpackInfo } from 'plugins/xpack_main/services/xpack_info';
11+
import { xpackInfoSignature } from 'plugins/xpack_main/services/xpack_info_signature';
12+
13+
const module = uiModules.get('xpack_main', []);
14+
15+
module.factory('checkXPackInfoChange', ($q, Private, $injector) => {
16+
/**
17+
* Intercept each network response to look for the kbn-xpack-sig header.
18+
* When that header is detected, compare its value with the value cached
19+
* in the browser storage. When the value is new, call `xpackInfo.refresh()`
20+
* so that it will pull down the latest x-pack info
21+
*
22+
* @param {object} response - the angular $http response object
23+
* @param {function} handleResponse - callback, expects to receive the response
24+
* @return
25+
*/
26+
function interceptor(response, handleResponse) {
27+
if (Path.isUnauthenticated()) {
28+
return handleResponse(response);
29+
}
30+
31+
const currentSignature = response.headers('kbn-xpack-sig');
32+
const cachedSignature = xpackInfoSignature.get();
33+
34+
if (currentSignature && cachedSignature !== currentSignature) {
35+
// Signature from the server differ from the signature of our
36+
// cached info, so we need to refresh it.
37+
// Intentionally swallowing this error
38+
// because nothing catches it and it's an ugly console error.
39+
xpackInfo.refresh($injector).catch(() => {});
40+
}
41+
42+
return handleResponse(response);
43+
}
44+
45+
return {
46+
response: (response) => interceptor(response, identity),
47+
responseError: (response) => interceptor(response, $q.reject)
48+
};
49+
});
50+
51+
module.config(($httpProvider) => {
52+
$httpProvider.interceptors.push('checkXPackInfoChange');
53+
});

x-pack/legacy/plugins/xpack_main/server/lib/__tests__/setup_xpack_main.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7+
import { BehaviorSubject } from 'rxjs';
78
import sinon from 'sinon';
89
import { XPackInfo } from '../xpack_info';
910
import { setupXPackMain } from '../setup_xpack_main';
1011
import * as InjectXPackInfoSignatureNS from '../inject_xpack_info_signature';
1112

13+
1214
describe('setupXPackMain()', () => {
1315
const sandbox = sinon.createSandbox();
1416

@@ -39,17 +41,16 @@ describe('setupXPackMain()', () => {
3941
elasticsearch: mockElasticsearchPlugin,
4042
xpack_main: mockXPackMainPlugin
4143
},
42-
newPlatform: { setup: { plugins: { features: {} } } },
44+
newPlatform: { setup: { plugins: { features: {}, licensing: { license$: new BehaviorSubject() } } } },
4345
events: { on() {} },
4446
log() {},
4547
config() {},
4648
expose() {},
4749
ext() {}
4850
});
4951

50-
// Make sure we don't misspell config key.
52+
// Make sure plugins doesn't consume config
5153
const configGetStub = sinon.stub().throws(new Error('`config.get` is called with unexpected key.'));
52-
configGetStub.withArgs('xpack.xpack_main.xpack_api_polling_frequency_millis').returns(1234);
5354
mockServer.config.returns({ get: configGetStub });
5455
});
5556

0 commit comments

Comments
 (0)