kibana/x-pack/plugins/licensing
Pierre Gayvallet 3b3327dbc3
Migrate most plugins to synchronous lifecycle (#89562)
* first pass

* migrate more plugins

* migrate yet more plugins

* more oss plugins

* fix test file

* change Plugin signature on the client-side too

* fix test types

* migrate OSS client-side plugins

* migrate OSS client-side test plugins

* migrate xpack client-side plugins

* revert fix attempt on fleet plugin

* fix presentation start signature

* fix yet another signature

* add warnings for server-side async plugins in dev mode

* remove unused import

* fix isPromise

* Add client-side deprecations

* update migration examples

* update generated doc

* fix xpack unit tests

* nit

* (will be reverted) explicitly await for license to be ready in the auth hook

* Revert "(will be reverted) explicitly await for license to be ready in the auth hook"

This reverts commit fdf73feb

* restore await on on promise contracts

* Revert "(will be reverted) explicitly await for license to be ready in the auth hook"

This reverts commit fdf73feb

* Revert "restore await on on promise contracts"

This reverts commit c5f2fe51

* add delay before starting tests in FTR

* update deprecation ts doc

* add explicit contract for monitoring setup

* migrate monitoring plugin to sync

* change plugin timeout to 10sec

* use delay instead of silence
2021-02-08 10:19:54 +01:00
..
common Elastic License 2.0 (#90099) 2021-02-03 18:12:39 -08:00
public Migrate most plugins to synchronous lifecycle (#89562) 2021-02-08 10:19:54 +01:00
server Migrate most plugins to synchronous lifecycle (#89562) 2021-02-08 10:19:54 +01:00
jest.config.js Elastic License 2.0 (#90099) 2021-02-03 18:12:39 -08:00
kibana.json [kbn/optimizer] implement "requiredBundles" property of KP plugins (#70911) 2020-07-09 18:43:17 -07:00
README.md
tsconfig.json Migrate App services plugins to TS projects (#87294) 2021-01-07 09:30:30 +01:00

Licensing plugin

API:

Server-side

The licensing plugin retrieves license data from Elasticsearch at regular configurable intervals.

  • license$: Observable<ILicense> Provides a steam of license data ILicense. Plugin emits new value whenever it detects changes in license info. If the plugin cannot retrieve a license from Elasticsearch, it will emit an empty license object.
  • refresh: () => Promise<ILicense> allows a plugin to enforce license retrieval.

Client-side

The licensing plugin retrieves license data from licensing Kibana plugin and does not communicate with Elasticsearch directly.

  • license$: Observable<ILicense> Provides a steam of license data ILicense. Plugin emits new value whenever it detects changes in license info. If the plugin cannot retrieve a license from Kibana, it will emit an empty license object.
  • refresh: () => Promise<ILicense> allows a plugin to enforce license retrieval.

Migration example

The new platform licensing plugin became stateless now. It means that instead of storing all your data from checkLicense within the plugin, you should react on license data change on both the client and server sides.

Before

// my_plugin/server/plugin.ts
function checkLicense(xpackLicenseInfo: XPackInfo){
  if (!xpackLicenseInfo || !xpackLicenseInfo.isAvailable()) {
     return {
      isAvailable: false,
      showLinks: true,
     }
  }
  if (!xpackLicenseInfo.feature('name').isEnabled()) {
    return {
      isAvailable: false,
      showLinks: false,
    }
  }
  const hasRequiredLicense = xPackInfo.license.isOneOf([
    'gold',
    'platinum',
    'trial',
  ]);
  return {
    isAvailable: hasRequiredLicense,
    showLinks: hasRequiredLicense,
  }
}
xpackMainPlugin.info.feature(pluginId).registerLicenseCheckResultsGenerator(checkLicense);

// my_plugin/client/plugin.ts
chrome.navLinks.update('myPlugin', {
  hidden: !xpackInfo.get('features.myPlugin.showLinks', false)
});

After

// kibana.json
"requiredPlugins": ["licensing"],

// my_plugin/server/plugin.ts
import { LicensingPluginSetup } from '../licensing/server'

interface SetupDeps {
  licensing: LicensingPluginSetup;
}

class MyPlugin {
  setup(core: CoreSetup, deps: SetupDeps) {
    deps.licensing.license$.subscribe(license => {
      const { state, message } = license.check('myPlugin', 'gold')
      const hasRequiredLicense = state === 'valid';
      if (hasRequiredLicense && license.getFeature('name').isAvailable) {
        // enable some server side logic 
      } else {
        log(message);
        // disable some server side logic 
      }
    })
  }
}

// my_plugin/public/plugin.ts
import { LicensingPluginSetup } from '../licensing/public'
class MyPlugin {
  setup(core: CoreSetup, deps: SetupDeps) {
    deps.licensing.license$.subscribe(license => {
      const { state, message } = license.check('myPlugin', 'gold')
      const hasRequiredLicense = state === 'valid';
      const showLinks = hasRequiredLicense && license.getFeature('name').isAvailable;

      chrome.navLinks.update('myPlugin', {
        hidden: !showLinks
      });
    })
  }
}

The list of breaking changes

state

LP: The plugin allows consumers to calculate state on license change event and store this The signature calculation is based on this state + license content NP: We decided that license service doesn't keep plugins state https://github.com/elastic/kibana/pull/49345#issuecomment-553451472. Plugins have to react on license change and calculate license state on every license change. If another plugin needs that information, it should be exposed via a plugin contract. This change makes NP & LP licensing service not compatible. We have to keep both until all plugins migrate to the new platform service. The legacy plugin consumes license data from the LP plugin.

Network request failures

LP: The licensing plugin didnt emit a license in case of network errors. NP: Emits the license even if the request failed.

clusterSource

LP: Allows specifying cluster source to perform polling. NP: The plugin always uses a data client. Provides createLicensePoller on the server-side to create a license poller with custom ES cluster.

Initial value on the client

LP: Passed on the page via inlined xpackInitialInfo NP: Should be fetched

Config

LP: xpack.xpack_main.xpack_api_polling_frequency_millis NP: xpack.licensing.api_polling_frequency

License

NP: mode field is provided, but deprecated.

sessionStorage

LP: License and signature were stored under different keys in session storage NP: License and signature were stored under one key xpack.licensing

isOneOf

isOneOf removed, use check or hasAtLeast instead

Endpoint

/api/xpack/v1/info API endpoint is going to be removed. switch to /api/licensing/info instead

Fetch error

getUnavailableReason doesn't return Error object anymore, but string