-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
Initial implementation of server licensing service #41026
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
export const SERVICE_NAME = 'licensing'; | ||
export const DEFAULT_POLLING_FREQUENCY = 30001; // 30 seconds | ||
export enum LICENSE_STATUS { | ||
Unavailable = 'UNAVAILABLE', | ||
Invalid = 'INVALID', | ||
Expired = 'EXPIRED', | ||
Valid = 'VALID', | ||
} | ||
export enum LICENSE_TYPE { | ||
basic = 10, | ||
standard = 20, | ||
gold = 30, | ||
platinum = 40, | ||
trial = 50, | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
export * from './types'; | ||
export { LicensingService } from './licensing_service'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import { LicensingServiceSetup } from './licensing_service_setup'; | ||
import { LicenseFeatureSerializer } from './types'; | ||
|
||
export class LicenseFeature { | ||
private serializable: LicenseFeatureSerializer = service => ({ | ||
name: this.name, | ||
isAvailable: this.isAvailable, | ||
isEnabled: this.isEnabled, | ||
}); | ||
|
||
constructor( | ||
public name: string, | ||
private feature: any = {}, | ||
private service: LicensingServiceSetup | ||
) {} | ||
|
||
get isAvailable() { | ||
return !!this.feature.available; | ||
} | ||
|
||
get isEnabled() { | ||
return !!this.feature.enabled; | ||
} | ||
|
||
onObject(serializable: LicenseFeatureSerializer) { | ||
this.serializable = serializable; | ||
} | ||
|
||
toObject() { | ||
return this.serializable(this.service); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import { Env } from '../config'; | ||
import { LicensingConfigType } from './types'; | ||
|
||
export class LicensingConfig { | ||
public enabled: boolean; | ||
public clusterSource: string; | ||
public pollingFrequency: number; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
constructor(rawConfig: LicensingConfigType, env: Env) { | ||
this.enabled = rawConfig.enabled; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this comes from configuration that we control, this is a good idea. Will revise. |
||
this.clusterSource = rawConfig.clusterSource; | ||
this.pollingFrequency = rawConfig.pollingFrequency; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import { BehaviorSubject, Observable, Subscription, from, interval } from 'rxjs'; | ||
import { first, map, switchMap, tap } from 'rxjs/operators'; | ||
import moment from 'moment'; | ||
import { CoreService } from '../../types'; | ||
import { Logger } from '../logging'; | ||
import { CoreContext } from '../core_context'; | ||
import { LicensingServiceSubject, LicensingConfigType, LicensingSetupDependencies } from './types'; | ||
import { SERVICE_NAME, LICENSE_TYPE } from './constants'; | ||
import { LicensingConfig } from './licensing_config'; | ||
import { LicensingServiceSetup } from './licensing_service_setup'; | ||
|
||
/** @internal */ | ||
export class LicensingService | ||
implements CoreService<LicensingServiceSubject, LicensingServiceSubject> { | ||
private readonly config$: Observable<LicensingConfig>; | ||
private readonly logger: Logger; | ||
private poller$!: Observable<number>; | ||
private pollerSubscription!: Subscription; | ||
private service$!: LicensingServiceSubject; | ||
private license: any; | ||
|
||
constructor(private readonly coreContext: CoreContext) { | ||
this.logger = coreContext.logger.get(SERVICE_NAME); | ||
this.config$ = coreContext.configService | ||
.atPath<LicensingConfigType>(SERVICE_NAME) | ||
.pipe(map(rawConfig => new LicensingConfig(rawConfig, coreContext.env))); | ||
} | ||
|
||
private hasLicenseInfoChanged(newLicense: any) { | ||
return ( | ||
newLicense.mode !== this.license.mode || | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was preexisting from x-pack legacy, so I'll see about making this clearer. |
||
newLicense.status !== this.license.status || | ||
newLicense.expiry_date_in_millis !== this.license.expiry_date_in_millis | ||
); | ||
} | ||
|
||
private async fetchInfo( | ||
{ http }: LicensingSetupDependencies, | ||
clusterSource: string, | ||
pollingFrequency: number | ||
) { | ||
this.logger.debug( | ||
`Calling [${clusterSource}] Elasticsearch _xpack API. Polling frequency: ${pollingFrequency}` | ||
); | ||
|
||
const cluster = http.server.plugins.elasticsearch.getCluster(clusterSource); | ||
|
||
try { | ||
const response = await cluster.callWithInternalUser('transport.request', { | ||
method: 'GET', | ||
path: '/_xpack', | ||
}); | ||
const newLicense = (response && response.license) || {}; | ||
const features = (response && response.features) || {}; | ||
const licenseInfoChanged = this.hasLicenseInfoChanged(newLicense); | ||
|
||
if (licenseInfoChanged) { | ||
const licenseInfo = [ | ||
`mode: ${newLicense.mode}`, | ||
`status: ${newLicense.status}`, | ||
'expiry_date_in_millis' in newLicense && | ||
`expiry date: ${moment(newLicense.expiry_date_in_millis, 'x').format()}`, | ||
] | ||
.filter(Boolean) | ||
.join(' | '); | ||
|
||
this.logger.info( | ||
`Imported ${this.license ? 'changed ' : ''}license information` + | ||
` from Elasticsearch for the [${clusterSource}] cluster: ${licenseInfo}` | ||
); | ||
|
||
return { license: false, error: null, features }; | ||
} | ||
|
||
return { license: newLicense, 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, | ||
deps: LicensingSetupDependencies | ||
) { | ||
if (this.service$) { | ||
return this.service$; | ||
} | ||
|
||
const service$ = new BehaviorSubject<LicensingServiceSetup | null>(null); | ||
|
||
this.poller$ = interval(pollingFrequency); | ||
this.poller$.pipe( | ||
switchMap(_ => | ||
from(this.fetchInfo(deps, clusterSource, pollingFrequency)).pipe( | ||
tap(({ license, error, features }) => { | ||
// If license is false, the license did not change and we don't need to push | ||
// a new one | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On line 91 it returns false if the license did change? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch! |
||
if (license !== false) { | ||
service$.next(new LicensingServiceSetup(license, features, error, clusterSource)); | ||
} | ||
}) | ||
) | ||
) | ||
); | ||
|
||
this.pollerSubscription = this.poller$.subscribe(); | ||
|
||
deps.http.server.events.on('stop', () => { | ||
this.stop(); | ||
}); | ||
|
||
return service$; | ||
} | ||
|
||
public async setup(deps: LicensingSetupDependencies) { | ||
const config = await this.config$.pipe(first()).toPromise(); | ||
const service$ = this.create(config, deps); | ||
|
||
this.service$ = service$; | ||
|
||
return this.service$; | ||
} | ||
|
||
public async start(deps: LicensingSetupDependencies) { | ||
const config = await this.config$.pipe(first()).toPromise(); | ||
const service$ = this.create(config, deps); | ||
|
||
return service$; | ||
} | ||
|
||
public async stop() { | ||
if (this.pollerSubscription) { | ||
this.pollerSubscription.unsubscribe(); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the difference between
isAvailable
andisEnabled
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a clue, I'll see what I can find out. 😄