-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
Copy pathplugin.ts
136 lines (115 loc) · 4.21 KB
/
plugin.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
* 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 { Observable } from 'rxjs';
import { first, map } from 'rxjs/operators';
import moment from 'moment';
import {
CoreSetup,
CoreStart,
Logger,
Plugin as CorePlugin,
PluginInitializerContext,
} from 'src/core/server';
import { Poller } from '../../../../src/core/utils/poller';
import { LicensingConfigType, LicensingPluginSetup, ILicense } from './types';
import { LicensingConfig } from './licensing_config';
import { License } from './license';
export class Plugin implements CorePlugin<LicensingPluginSetup> {
private readonly logger: Logger;
private readonly config$: Observable<LicensingConfig>;
private poller!: Poller<ILicense>;
constructor(private readonly context: PluginInitializerContext) {
this.logger = this.context.logger.get();
this.config$ = this.context.config
.create<LicensingConfigType | { config: LicensingConfigType }>()
.pipe(
map(config =>
'config' in config
? new LicensingConfig(config.config, this.context.env)
: new LicensingConfig(config, this.context.env)
)
);
}
private hasLicenseInfoChanged(newLicense: any) {
const currentLicense = this.poller.subject$.getValue();
if ((currentLicense && !newLicense) || (newLicense && !currentLicense)) {
return true;
}
return (
newLicense.type !== currentLicense.type ||
newLicense.status !== currentLicense.status ||
newLicense.expiry_date_in_millis !== currentLicense.expiryDateInMillis
);
}
private async fetchInfo(core: CoreSetup, clusterSource: string, pollingFrequency: number) {
this.logger.debug(
`Calling [${clusterSource}] Elasticsearch _xpack API. Polling frequency: ${pollingFrequency}`
);
const cluster = await core.elasticsearch.dataClient$.pipe(first()).toPromise();
try {
const response = await cluster.callAsInternalUser('transport.request', {
method: 'GET',
path: '/_xpack',
});
const rawLicense = response && response.license;
const features = (response && response.features) || {};
const licenseInfoChanged = this.hasLicenseInfoChanged(rawLicense);
if (!licenseInfoChanged) {
return { license: false, error: null, features: null };
}
const currentLicense = this.poller.subject$.getValue();
const licenseInfo = [
'type' in rawLicense && `type: ${rawLicense.type}`,
'status' in rawLicense && `status: ${rawLicense.status}`,
'expiry_date_in_millis' in rawLicense &&
`expiry date: ${moment(rawLicense.expiry_date_in_millis, 'x').format()}`,
]
.filter(Boolean)
.join(' | ');
this.logger.info(
`Imported ${currentLicense ? 'changed ' : ''}license information` +
` from Elasticsearch for the [${clusterSource}] cluster: ${licenseInfo}`
);
return { license: rawLicense, error: null, features };
} catch (err) {
this.logger.warn(
`License information could not be obtained from Elasticsearch` +
` for the [${clusterSource}] cluster. ${err}`
);
return { license: null, error: err, features: {} };
}
}
private create({ clusterSource, pollingFrequency }: LicensingConfig, core: CoreSetup) {
this.poller = new Poller<ILicense>(
pollingFrequency,
new License(null, {}, null, clusterSource),
async () => {
const { license, features, error } = await this.fetchInfo(
core,
clusterSource,
pollingFrequency
);
if (license !== false) {
return new License(license, features, error, clusterSource);
}
}
);
return this.poller;
}
public async setup(core: CoreSetup) {
const config = await this.config$.pipe(first()).toPromise();
const poller = this.create(config, core);
return {
license$: poller.subject$.asObservable(),
};
}
public async start(core: CoreStart) {}
public stop() {
if (this.poller) {
this.poller.unsubscribe();
}
}
}