-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
xpack_info.js
308 lines (259 loc) · 9.06 KB
/
xpack_info.js
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
* 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 { createHash } from 'crypto';
import moment from 'moment';
import { get, has } from 'lodash';
import { Poller } from '../../../../common/poller';
import { XPackInfoLicense } from './xpack_info_license';
/**
* A helper that provides a convenient way to access XPack Info returned by Elasticsearch.
*/
export class XPackInfo {
/**
* XPack License object.
* @type {XPackInfoLicense}
* @private
*/
_license;
/**
* Feature name <-> feature license check generator function mapping.
* @type {Map<string, Function>}
* @private
*/
_featureLicenseCheckResultsGenerators = new Map();
/**
* Set of listener functions that will be called whenever the license
* info changes
* @type {Set<Function>}
*/
_licenseInfoChangedListeners = new Set();
/**
* Cache that may contain last xpack info API response or error, json representation
* of xpack info and xpack info signature.
* @type {{response: Object|undefined, error: Object|undefined, json: Object|undefined, signature: string|undefined}}
* @private
*/
_cache = {};
/**
* XPack info poller.
* @type {Poller}
* @private
*/
_poller;
/**
* XPack License instance.
* @returns {XPackInfoLicense}
*/
get license() {
return this._license;
}
/**
* Constructs XPack info object.
* @param {Hapi.Server} server HapiJS server instance.
* @param {Object} options
* @property {string} [options.clusterSource] Type of the cluster that should be used
* to fetch XPack info (data, monitoring etc.). If not provided, `data` is used.
* @property {number} options.pollFrequencyInMillis Polling interval used to automatically
* refresh XPack Info by the internal poller.
*/
constructor(server, { clusterSource = 'data', pollFrequencyInMillis }) {
this._log = server.log.bind(server);
this._cluster = server.plugins.elasticsearch.getCluster(clusterSource);
this._clusterSource = clusterSource;
// Create a poller that will be (re)started inside of the `refreshNow` call.
this._poller = new Poller({
functionToPoll: () => this.refreshNow(),
trailing: true,
pollFrequencyInMillis,
continuePollingOnError: true
});
server.events.on('stop', () => {
this._poller.stop();
});
this._license = new XPackInfoLicense(
() => this._cache.response && this._cache.response.license
);
}
/**
* Checks whether XPack info is available.
* @returns {boolean}
*/
isAvailable() {
return !!this._cache.response && !!this._cache.response.license;
}
/**
* Checks whether ES was available
* @returns {boolean}
*/
isXpackUnavailable() {
return this._cache.error instanceof Error && this._cache.error.status === 400;
}
/**
* If present, describes the reason why XPack info is not available.
* @returns {Error|string}
*/
unavailableReason() {
if (!this._cache.error && this._cache.response && !this._cache.response.license) {
return `[${this._clusterSource}] Elasticsearch cluster did not respond with license information.`;
}
if (this.isXpackUnavailable()) {
return `X-Pack plugin is not installed on the [${this._clusterSource}] Elasticsearch cluster.`;
}
return this._cache.error;
}
onLicenseInfoChange(handler) {
this._licenseInfoChangedListeners.add(handler);
}
/**
* Queries server to get the updated XPack info.
* @returns {Promise.<XPackInfo>}
*/
async refreshNow() {
this._log(['license', 'debug', 'xpack'], (
`Calling [${this._clusterSource}] Elasticsearch _xpack API. Polling frequency: ${this._poller.getPollFrequency()}`
));
// We can reset polling timer since we force refresh here.
this._poller.stop();
try {
const response = await this._cluster.callWithInternalUser('transport.request', {
method: 'GET',
path: '/_xpack'
});
const licenseInfoChanged = this._hasLicenseInfoChanged(response);
if (licenseInfoChanged) {
const licenseInfoParts = [
`mode: ${get(response, 'license.mode')}`,
`status: ${get(response, 'license.status')}`,
];
if (has(response, 'license.expiry_date_in_millis')) {
const expiryDate = moment(response.license.expiry_date_in_millis, 'x').format();
licenseInfoParts.push(`expiry date: ${expiryDate}`);
}
const licenseInfo = licenseInfoParts.join(' | ');
this._log(
['license', 'info', 'xpack'],
`Imported ${this._cache.response ? 'changed ' : ''}license information` +
` from Elasticsearch for the [${this._clusterSource}] cluster: ${licenseInfo}`
);
}
this._cache = { response };
if (licenseInfoChanged) {
// call license info changed listeners
for (const listener of this._licenseInfoChangedListeners) {
listener();
}
}
} catch(error) {
this._log(
['license', 'warning', 'xpack'],
`License information from the X-Pack plugin could not be obtained from Elasticsearch` +
` for the [${this._clusterSource}] cluster. ${error}`
);
this._cache = { error };
}
this._poller.start();
return this;
}
/**
* Returns a wrapper around XPack info that gives an access to the properties of
* the specific feature.
* @param {string} name Name of the feature to get a wrapper for.
* @returns {Object}
*/
feature(name) {
return {
/**
* Checks whether feature is available (permitted by the current license).
* @returns {boolean}
*/
isAvailable: () => {
return !!get(this._cache.response, `features.${name}.available`);
},
/**
* Checks whether feature is enabled (not disabled by the configuration specifically).
* @returns {boolean}
*/
isEnabled: () => {
return !!get(this._cache.response, `features.${name}.enabled`);
},
/**
* Registers a `generator` function that will be called with XPackInfo instance as
* argument whenever XPack info changes. Whatever `generator` returns will be stored
* in XPackInfo JSON representation and can be accessed with `getLicenseCheckResults`.
* @param {Function} generator Function to call whenever XPackInfo changes.
*/
registerLicenseCheckResultsGenerator: (generator) => {
this._featureLicenseCheckResultsGenerators.set(name, generator);
// Since JSON representation and signature are cached we should invalidate them to
// include results from newly registered generator when they are requested.
this._cache.json = undefined;
this._cache.signature = undefined;
},
/**
* Returns license check results that were previously produced by the `generator` function.
* @returns {Object}
*/
getLicenseCheckResults: () => this.toJSON().features[name]
};
}
/**
* Extracts string md5 hash from the stringified version of license JSON representation.
* @returns {string}
*/
getSignature() {
if (this._cache.signature) {
return this._cache.signature;
}
this._cache.signature = createHash('md5')
.update(JSON.stringify(this.toJSON()))
.digest('hex');
return this._cache.signature;
}
/**
* Returns JSON representation of the license object that is suitable for serialization.
* @returns {Object}
*/
toJSON() {
if (this._cache.json) {
return this._cache.json;
}
this._cache.json = {
license: {
type: this.license.getType(),
isActive: this.license.isActive(),
expiryDateInMillis: this.license.getExpiryDateInMillis()
},
features: {}
};
// Set response elements specific to each feature. To do this,
// call the license check results generator for each feature, passing them
// the xpack info object
for (const [feature, licenseChecker] of this._featureLicenseCheckResultsGenerators) {
// return value expected to be a dictionary object.
this._cache.json.features[feature] = licenseChecker(this);
}
return this._cache.json;
}
/**
* Checks whether license within specified response differs from the current license.
* Comparison is based on license mode, status and expiration date.
* @param {Object} response xPack info response object returned from the backend.
* @returns {boolean} True if license within specified response object differs from
* the one we already have.
* @private
*/
_hasLicenseInfoChanged(response) {
const newLicense = get(response, 'license') || {};
const cachedLicense = get(this._cache.response, 'license') || {};
if (newLicense.mode !== cachedLicense.mode) {
return true;
}
if (newLicense.status !== cachedLicense.status) {
return true;
}
return newLicense.expiry_date_in_millis !== cachedLicense.expiry_date_in_millis;
}
}