create-roles.asciidoc 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. [role="xpack"]
  2. [[security-api-put-role]]
  3. === Create or update roles API
  4. ++++
  5. <titleabbrev>Create or update roles</titleabbrev>
  6. ++++
  7. .New API reference
  8. [sidebar]
  9. --
  10. For the most up-to-date API details, refer to {api-es}/group/endpoint-security[Security APIs].
  11. --
  12. Adds and updates roles in the native realm.
  13. [[security-api-put-role-request]]
  14. ==== {api-request-title}
  15. `POST /_security/role/<name>` +
  16. `PUT /_security/role/<name>`
  17. [[security-api-put-role-prereqs]]
  18. ==== {api-prereq-title}
  19. * To use this API, you must have at least the `manage_security` cluster
  20. privilege.
  21. [[security-api-put-role-desc]]
  22. ==== {api-description-title}
  23. The role management APIs are generally the preferred way to manage roles, rather than using
  24. <<roles-management-file,file-based role management>>. The create
  25. or update roles API cannot update roles that are defined in roles files.
  26. [[security-api-put-role-path-params]]
  27. ==== {api-path-parms-title}
  28. `name`::
  29. (string) The name of the role.
  30. [[security-api-put-role-request-body]]
  31. ==== {api-request-body-title}
  32. The following parameters can be specified in the body of a PUT or POST request
  33. and pertain to adding a role:
  34. `applications`:: (list) A list of application privilege entries.
  35. `application` (required)::: (string) The name of the application to which this entry applies
  36. `privileges`::: (list) A list of strings, where each element is the name of an application
  37. privilege or action.
  38. `resources`::: (list) A list resources to which the privileges are applied.
  39. `cluster`:: (list) A list of cluster privileges. These privileges define the
  40. cluster level actions that users with this role are able to execute.
  41. `description`:: (string) A description of the role.
  42. The maximum length is `1000` chars.
  43. `global`:: (object) An object defining global privileges. A global privilege is
  44. a form of cluster privilege that is request-aware. Support for global privileges
  45. is currently limited to the management of application privileges.
  46. This field is optional.
  47. `indices`:: (list) A list of indices permissions entries.
  48. `field_security`::: (object) The document fields that the owners of the role have
  49. read access to. For more information, see
  50. <<field-and-document-access-control>>.
  51. `names` (required)::: (list) A list of indices (or index name patterns) to which the
  52. permissions in this entry apply.
  53. `privileges`(required)::: (list) The index level privileges that the owners of the role
  54. have on the specified indices.
  55. `query`::: A search query that defines the documents the owners of the role have
  56. read access to. A document within the specified indices must match this query in
  57. order for it to be accessible by the owners of the role.
  58. `metadata`:: (object) Optional meta-data. Within the `metadata` object, keys
  59. that begin with `_` are reserved for system usage.
  60. `run_as`:: (list) A list of users that the owners of this role can impersonate.
  61. For more information, see
  62. <<run-as-privilege>>.
  63. `remote_indices`:: (list) A list of remote indices permissions entries.
  64. +
  65. --
  66. NOTE: Remote indices are effective for <<remote-clusters-api-key,remote clusters configured with the API key based model>>.
  67. They have no effect for remote clusters configured with the <<remote-clusters-cert,certificate based model>>.
  68. --
  69. `clusters` (required)::: (list) A list of cluster aliases to which the permissions
  70. in this entry apply.
  71. `field_security`::: (object) The document fields that the owners of the role have
  72. read access to. For more information, see
  73. <<field-and-document-access-control>>.
  74. `names` (required)::: (list) A list of indices (or index name patterns) on the remote clusters
  75. (specified with `clusters`) to which the permissions in this entry apply.
  76. `privileges`(required)::: (list) The index level privileges that the owners of the role
  77. have on the specified indices.
  78. `query`::: A search query that defines the documents the owners of the role have
  79. read access to. A document within the specified indices must match this query in
  80. order for it to be accessible by the owners of the role.
  81. `remote_cluster`:: (list) A list of remote cluster permissions entries.
  82. +
  83. --
  84. NOTE: Remote cluster permissions are effective for <<remote-clusters-api-key,remote clusters configured with the API key based model>>.
  85. They have no effect for remote clusters configured with the <<remote-clusters-cert,certificate based model>>.
  86. --
  87. `clusters` (required)::: (list) A list of cluster aliases to which the permissions
  88. in this entry apply.
  89. `privileges`(required)::: (list) The cluster level privileges that the owners of the role
  90. have in the specified clusters. Note - only a subset of the cluster privileges are supported for remote clusters.
  91. The <<security-api-get-builtin-privileges-request, builtin privileges API>> can be used to determine
  92. which privileges are allowed per version.
  93. For more information, see <<defining-roles>>.
  94. [[security-api-put-role-example]]
  95. ==== {api-examples-title}
  96. The following example adds a role called `my_admin_role`:
  97. [source,console]
  98. --------------------------------------------------
  99. POST /_security/role/my_admin_role
  100. {
  101. "description": "Grants full access to all management features within the cluster.",
  102. "cluster": ["all"],
  103. "indices": [
  104. {
  105. "names": [ "index1", "index2" ],
  106. "privileges": ["all"],
  107. "field_security" : { // optional
  108. "grant" : [ "title", "body" ]
  109. },
  110. "query": "{\"match\": {\"title\": \"foo\"}}" // optional
  111. }
  112. ],
  113. "applications": [
  114. {
  115. "application": "myapp",
  116. "privileges": [ "admin", "read" ],
  117. "resources": [ "*" ]
  118. }
  119. ],
  120. "run_as": [ "other_user" ], // optional
  121. "metadata" : { // optional
  122. "version" : 1
  123. }
  124. }
  125. --------------------------------------------------
  126. A successful call returns a JSON structure that shows whether the role has been
  127. created or updated.
  128. [source,console-result]
  129. --------------------------------------------------
  130. {
  131. "role": {
  132. "created": true <1>
  133. }
  134. }
  135. --------------------------------------------------
  136. <1> When an existing role is updated, `created` is set to false.
  137. The following example configures a role that can run SQL in JDBC:
  138. // tag::sql-queries-permission[]
  139. [source,console]
  140. --------------------------------------------------
  141. POST /_security/role/cli_or_drivers_minimal
  142. {
  143. "cluster": ["cluster:monitor/main"],
  144. "indices": [
  145. {
  146. "names": ["test"],
  147. "privileges": ["read", "indices:admin/get"]
  148. }
  149. ]
  150. }
  151. --------------------------------------------------
  152. // end::sql-queries-permission[]
  153. The following example configures a role with remote indices and remote cluster privileges for a remote cluster:
  154. [source,console]
  155. --------------------------------------------------
  156. POST /_security/role/only_remote_access_role
  157. {
  158. "remote_indices": [
  159. {
  160. "clusters": ["my_remote"], <1>
  161. "names": ["logs*"], <2>
  162. "privileges": ["read", "read_cross_cluster", "view_index_metadata"] <3>
  163. }
  164. ],
  165. "remote_cluster": [
  166. {
  167. "clusters": ["my_remote"], <1>
  168. "privileges": ["monitor_stats"] <4>
  169. }
  170. ]
  171. }
  172. --------------------------------------------------
  173. <1> The remote indices and remote cluster privileges apply to remote cluster with the alias `my_remote`.
  174. <2> Privileges are granted for indices matching pattern `logs*` on the remote cluster (`my_remote`).
  175. <3> The actual <<privileges-list-indices,index privileges>> granted for `logs*` on `my_remote`.
  176. <4> The actual <<privileges-list-cluster,cluster privileges>> granted for `my_remote`.
  177. Note - only a subset of the cluster privileges are supported for remote clusters.