custom-authorization.asciidoc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. [role="xpack"]
  2. [[custom-roles-authorization]]
  3. === Customizing roles and authorization
  4. If you need to retrieve user roles from a system not supported out-of-the-box
  5. or if the authorization system that is provided by the {es} {security-features}
  6. does not meet your needs, a SPI loaded security extension can be implemented to
  7. customize role retrieval and/or the authorization system. The SPI loaded
  8. security extension is part of an ordinary elasticsearch plugin.
  9. [[implementing-custom-roles-provider]]
  10. ==== Implementing a custom roles provider
  11. To create a custom roles provider:
  12. . Implement the interface `BiConsumer<Set<String>, ActionListener<Set<RoleDescriptor>>>`.
  13. That is to say, the implementation consists of one method that takes a set of strings,
  14. which are the role names to resolve, and an ActionListener, on which the set of resolved
  15. role descriptors are passed on as the response.
  16. . The custom roles provider implementation must take special care to not block on any I/O
  17. operations. It is the responsibility of the implementation to ensure asynchronous behavior
  18. and non-blocking calls, which is made easier by the fact that the `ActionListener` is
  19. provided on which to send the response when the roles have been resolved and the response
  20. is ready.
  21. To package your custom roles provider as a plugin:
  22. . Implement an extension class for your roles provider that implements
  23. `org.elasticsearch.xpack.core.security.SecurityExtension`. There you need to
  24. override one or more of the following methods:
  25. +
  26. [source,java]
  27. ----------------------------------------------------
  28. @Override
  29. public List<BiConsumer<Set<String>, ActionListener<Set<RoleDescriptor>>>>
  30. getRolesProviders(Settings settings, ResourceWatcherService resourceWatcherService) {
  31. ...
  32. }
  33. ----------------------------------------------------
  34. +
  35. The `getRolesProviders` method is used to provide a list of custom roles providers that
  36. will be used to resolve role names, if the role names could not be resolved by the reserved
  37. roles or native roles stores. The list should be returned in the order that the custom role
  38. providers should be invoked to resolve roles. For example, if `getRolesProviders` returns two
  39. instances of roles providers, and both of them are able to resolve role `A`, then the resolved
  40. role descriptor that will be used for role `A` will be the one resolved by the first roles
  41. provider in the list.
  42. [[implementing-authorization-engine]]
  43. ==== Implementing an authorization engine
  44. To create an authorization engine, you need to:
  45. . Implement the `org.elasticsearch.xpack.core.security.authz.AuthorizationEngine`
  46. interface in a class with the desired authorization behavior.
  47. . Implement the `org.elasticsearch.xpack.core.security.authz.Authorization.AuthorizationInfo`
  48. interface in a class that contains the necessary information to authorize the request.
  49. To package your authorization engine as a plugin:
  50. . Implement an extension class for your authorization engine that extends
  51. `org.elasticsearch.xpack.core.security.SecurityExtension`. There you need to
  52. override the following method:
  53. +
  54. [source,java]
  55. ----------------------------------------------------
  56. @Override
  57. public AuthorizationEngine getAuthorizationEngine(Settings settings) {
  58. ...
  59. }
  60. ----------------------------------------------------
  61. +
  62. The `getAuthorizationEngine` method is used to provide the authorization engine
  63. implementation.
  64. Sample code that illustrates the structure and implementation of a custom
  65. authorization engine is provided in the
  66. https://github.com/elastic/elasticsearch/tree/master/plugins/examples/security-authorization-engine[elasticsearch]
  67. repository on GitHub. You can use this code as a starting point for creating your
  68. own authorization engine.
  69. [[packing-extension-plugin]]
  70. ==== Implement an elasticsearch plugin
  71. In order to register the security extension for your custom roles provider or
  72. authorization engine, you need to also implement an elasticsearch plugin that
  73. contains the extension:
  74. . Implement a plugin class that extends `org.elasticsearch.plugins.Plugin`
  75. . Create a build configuration file for the plugin; Gradle is our recommendation.
  76. . Create a `plugin-descriptor.properties` file as described in
  77. {plugins}/plugin-authors.html[Help for plugin authors].
  78. . Create a `META-INF/services/org.elasticsearch.xpack.core.security.SecurityExtension` descriptor file for the
  79. extension that contains the fully qualified class name of your `org.elasticsearch.xpack.core.security.SecurityExtension` implementation
  80. . Bundle all in a single zip file.
  81. [[using-security-extension]]
  82. ==== Using the security extension
  83. To use a security extension:
  84. . Install the plugin with the extension on each node in the cluster. You run
  85. `bin/elasticsearch-plugin` with the `install` sub-command and specify the URL
  86. pointing to the zip file that contains the extension. For example:
  87. +
  88. [source,shell]
  89. ----------------------------------------
  90. bin/elasticsearch-plugin install file:///<path>/my-extension-plugin-1.0.zip
  91. ----------------------------------------
  92. . Add any configuration parameters for implementations in the extension to the
  93. `elasticsearch.yml` file. The settings are not namespaced and you have access to any
  94. settings when constructing the extensions, although it is recommended to have a
  95. namespacing convention for extensions to keep your `elasticsearch.yml`
  96. configuration easy to understand.
  97. +
  98. For example, if you have a custom roles provider that
  99. resolves roles from reading a blob in an S3 bucket on AWS, then you would specify settings
  100. in `elasticsearch.yml` such as:
  101. +
  102. [source,js]
  103. ----------------------------------------
  104. custom_roles_provider.s3_roles_provider.bucket: roles
  105. custom_roles_provider.s3_roles_provider.region: us-east-1
  106. custom_roles_provider.s3_roles_provider.secret_key: xxx
  107. custom_roles_provider.s3_roles_provider.access_key: xxx
  108. ----------------------------------------
  109. // NOTCONSOLE
  110. +
  111. These settings are passed as arguments to the methods in the `SecurityExtension` interface.
  112. . Restart Elasticsearch.