create-roles.asciidoc 6.2 KB

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