creating-classic-plugins.asciidoc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. [[creating-classic-plugins]]
  2. === Creating classic plugins
  3. Classic plugins provide {es} with mechanisms for custom authentication,
  4. authorization, scoring, and more.
  5. [IMPORTANT]
  6. .Plugin release lifecycle
  7. ==============================================
  8. Classic plugins require you to build a new version for each new {es} release.
  9. This version is checked when the plugin is installed and when it is loaded. {es}
  10. will refuse to start in the presence of plugins with the incorrect
  11. `elasticsearch.version`.
  12. ==============================================
  13. [discrete]
  14. ==== Classic plugin file structure
  15. Classic plugins are ZIP files composed of JAR files and
  16. <<plugin-descriptor-file-{plugin-type},a metadata file called
  17. `plugin-descriptor.properties`>>, a Java properties file that describes the
  18. plugin.
  19. Note that only JAR files at the root of the plugin are added to the classpath
  20. for the plugin. If you need other resources, package them into a resources JAR.
  21. [discrete]
  22. ==== Example plugins
  23. The {es} repository contains {es-repo}tree/main/plugins/examples[examples of plugins]. Some of these include:
  24. * a plugin with {es-repo}tree/main/plugins/examples/custom-settings[custom settings]
  25. * a plugin with a {es-repo}tree/main/plugins/examples/custom-processor[custom ingest processor]
  26. * adding {es-repo}tree/main/plugins/examples/rest-handler[custom rest endpoints]
  27. * adding a {es-repo}tree/main/plugins/examples/rescore[custom rescorer]
  28. * a script {es-repo}tree/main/plugins/examples/script-expert-scoring[implemented in Java]
  29. These examples provide the bare bones needed to get started. For more
  30. information about how to write a plugin, we recommend looking at the
  31. {es-repo}tree/main/plugins/[source code of existing plugins] for inspiration.
  32. [discrete]
  33. ==== Testing your plugin
  34. Use `bin/elasticsearch-plugin install file:///path/to/your/plugin`
  35. to install your plugin for testing. The Java plugin is auto-loaded only if it's in the
  36. `plugins/` directory.
  37. [discrete]
  38. [[_entitlements_policy]]
  39. ==== Entitlements policy
  40. Some plugins may need additional _entitlements_.
  41. {es} limits the ability to perform certain security-sensitive actions as part of its _Entitlement_ security mechanism (e.g. to limit the potential fallout from remote code execution (RCE) vulnerabilities).
  42. The Entitlement model is based on Java modules.
  43. An _entitlement_ granted to a Java module allows the module's code to perform the security-sensitive action associated with that entitlement. For example, the ability to create threads is limited to modules that have the `manage_threads` entitlement; likewise, the ability to read a file from the filesystem is limited to modules that have the `files` entitlement for that particular file.
  44. In practice, an entitlement allows plugin code to call a well-defined set of corresponding JDK methods; without the entitlement calls to those JDK methods are denied and throw a `NotEntitledException`. Plugin can include the optional `entitlement-policy.yaml` file to define modules and required entitlements. Any additional entitlement requested by the plugin will be displayed to the user with a large warning, and users will have to confirm them when installing the plugin interactively. Therefore, it is best to avoid requesting any spurious entitlement!
  45. If you are using the {es} Gradle build system, place this file in `src/main/plugin-metadata` and it will be applied during unit tests as well.
  46. An entitlement policy applies to all of your plugin jars (your own code and third party dependencies). You have to write your policy file accordingly. For example, if a plugin uses the Example API client to perform network operations, it will need a policy that may look like this:
  47. ```YAML
  48. org.elasticsearch.example-plugin:
  49. - manage_threads
  50. com.example.api.client:
  51. - set_https_connection_properties
  52. - outbound_network
  53. ```
  54. Note how the network related entitlements are granted to the `com.example.api.client` module, as the code performing the sensitive network operations is in the `example-api-client` dependency.
  55. If your plugin is not modular, all entitlements must be specified under the catch-all `ALL-UNNAMED` module name:
  56. ```YAML
  57. ALL-UNNAMED:
  58. - manage_threads
  59. - set_https_connection_properties
  60. - outbound_network
  61. ```
  62. ==== Entitlements
  63. The entitlements currently implemented and enforced in {es} that are available to plugins are the following ones:
  64. ===== `manage_threads`
  65. Allows code to call methods that create or modify properties on Java Threads, for example `Thread#start` or `ThreadGroup#setMaxPriority`. In general, setting the name, priority, daemon state and context class loader are things no plugins should do when executing on
  66. {es} threadpools; however, many 3rd party libraries that support async operations (e.g. Apache HTTP client) need to manage their own threads. In this case it is justifiable to request this entitlement.
  67. Example:
  68. ```yaml
  69. org.example.module: # or 'ALL-UNNAMED' if the plugin is non-modular
  70. - manage_threads
  71. ```
  72. ===== `outbound_network`
  73. Allows code to call methods to make a network connection. {es} does not grant any network access by default; each plugin that needs to directly connect to an external resource (e.g. to upload or download data) must request this entitlement.
  74. Example:
  75. ```yaml
  76. org.example.module: # or 'ALL-UNNAMED' if the plugin is non-modular
  77. - outbound_network
  78. ```
  79. ===== `set_https_connection_properties`
  80. Allows code to call methods to change properties on an established HTTPS connection. While this is generally innocuous (e.g. the google API client uses it to modify the HTTPS connections they just created), these methods can allow code to change arbitrary connections.
  81. Example:
  82. ```yaml
  83. org.example.module: # or 'ALL-UNNAMED' if the plugin is non-modular
  84. - set_https_connection_properties
  85. ```
  86. ===== `inbound_network` (deprecated)
  87. Allows code to call methods to listen for incoming connections, so external resources can connect directly to your plugin. This entitlement should only be used when absolutely necessary (e.g. if a library you depend on requires it for authentication). Granting it makes the {es} node more vulnerable to attacks. This entitlement is deprecated, and can be removed in a future version of {es}.
  88. Example:
  89. ```yaml
  90. org.example.module: # or 'ALL-UNNAMED' if the plugin is non-modular
  91. - inbound_network
  92. ```
  93. ===== `load_native_libraries`
  94. Allows code to load native libraries and call https://docs.oracle.com/en/java/javase/24/core/restricted-methods.html[restricted methods]. This entitlement also enables native access for modules it is granted to. Native code may alter the JVM or circumvent access checks such as file or network restrictions.
  95. Example:
  96. ```yaml
  97. org.example.module: # or 'ALL-UNNAMED' if the plugin is non-modular
  98. - load_native_libraries
  99. ```
  100. ===== `files`
  101. Allows code to access the filesystem, to read or write paths as specified by the entitlement's fields. The filesystem of the OS hosting {es} may contain sensitive files, for example credentials. Some files are meant to be always accessible to {es}, but plugins can not access them directly: {es} enforces that certain files can only be read by its core code, while some other files can not be read or written at all. A plugin is always granted `read` access to the {es} config directory and `read_write` access to the temp directory; if the plugin requires to read, write or access additional files or directories, it must specify them via this entitlement.
  102. It is possible to specify 3 different types of file entitlement:
  103. * `path` to specify an absolute path
  104. * `relative_path` to specify a relative path. The path will be resolved via the `relative_to` field, which is used to qualify the relative path. It can be a specific {es} directory (`config` or `data`), or to the user home directory (`home`) (the home of the user running {es})
  105. * `relative_path` to specify a path resolved via the `relative_to` field, which can have the following values:
  106. - `config`: the {es} https://www.elastic.co/guide/en/elasticsearch/reference/current/settings.html#config-files-location[config directory]
  107. - `data`: the {es} https://www.elastic.co/guide/en/elasticsearch/reference/current/path-settings-overview.html[data directory]
  108. - `home`: the home directory of the user running {es}
  109. - `path_setting` to specify a path defined via an {es} setting. The path can be absolute or relative; in the latter case, the path will be resolved using the `basedir_if_relative` path (which can assume the same values as `relative_to`)
  110. Each of the 3 types has some additional fields:
  111. * `mode` (required): can be either `read` or `read_write`
  112. * `platform` (optional): indicates this item applies only to one platform, which can be one of `linux`, `macos` or `windows`. On other platforms, the item is ignored. If this field is not specified, the item applies to all platforms.
  113. * `exclusive`: access to this path is exclusive for this plugin; this means that other plugins will not be able to access to it, not even if they have an entitlement that would normally grant access to that path.
  114. Example:
  115. ```yaml
  116. org.example.module: # or 'ALL-UNNAMED' if the plugin is non-modular
  117. - files:
  118. - path: "/absolute/path"
  119. mode: read
  120. - relative_path: "relative/file.txt"
  121. relative_to: data
  122. mode: read_write
  123. - path_setting: setting.name
  124. basedir_if_relative: data
  125. mode: read
  126. ```
  127. ===== `write_system_properties`
  128. Allows code to set one or more system properties (e.g. by calling `System#setProperty`). The code to which this entitlement is granted can change the properties listed in the `properties` field. In general, it's best to avoid changing a system property dynamically as this can have effects on code which later reads the property. The global nature of system properties means one plugin could then affect another, depending on load order.
  129. Example:
  130. ```yaml
  131. org.example.module: # or 'ALL-UNNAMED' if the plugin is non-modular
  132. - write_system_properties:
  133. properties:
  134. - property.one
  135. - property.two
  136. ```
  137. Check the Entitlements {es-repo}tree/main/libs/entitlement/README.md[README in the elasticsearch repository] for more information.
  138. [[plugin-descriptor-file-classic]]
  139. ==== The plugin descriptor file for classic plugins
  140. include::plugin-descriptor-file.asciidoc[]