query-role.asciidoc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. [role="xpack"]
  2. [[security-api-query-role]]
  3. === Query Role API
  4. ++++
  5. <titleabbrev>Query Role</titleabbrev>
  6. ++++
  7. Retrieves roles with <<query-dsl,Query DSL>> in a <<paginate-search-results,paginated>> fashion.
  8. [[security-api-query-role-request]]
  9. ==== {api-request-title}
  10. `GET /_security/_query/role`
  11. `POST /_security/_query/role`
  12. [[security-api-query-role-prereqs]]
  13. ==== {api-prereq-title}
  14. * To use this API, you must have at least the `read_security` cluster privilege.
  15. [[security-api-query-role-desc]]
  16. ==== {api-description-title}
  17. The role management APIs are generally the preferred way to manage roles, rather than using
  18. <<roles-management-file,file-based role management>>.
  19. The query roles API does not retrieve roles that are defined in roles files, nor <<built-in-roles,built-in>> ones.
  20. You can optionally filter the results with a query. Also, the results can be paginated and sorted.
  21. [[security-api-query-role-request-body]]
  22. ==== {api-request-body-title}
  23. You can specify the following parameters in the request body:
  24. `query`::
  25. (Optional, string) A <<query-dsl,query>> to filter which roles to return.
  26. The query supports a subset of query types, including
  27. <<query-dsl-match-all-query,`match_all`>>, <<query-dsl-bool-query,`bool`>>,
  28. <<query-dsl-term-query,`term`>>, <<query-dsl-terms-query,`terms`>>,
  29. <<query-dsl-match-query,`match`>>, <<query-dsl-ids-query,`ids`>>,
  30. <<query-dsl-prefix-query,`prefix`>>, <<query-dsl-wildcard-query,`wildcard`>>,
  31. <<query-dsl-exists-query,`exists`>>, <<query-dsl-range-query,`range`>>,
  32. and <<query-dsl-simple-query-string-query,`simple query string`>>.
  33. +
  34. You can query the following values associated with a role.
  35. +
  36. .Valid values for `query`
  37. [%collapsible%open]
  38. ====
  39. `name`::
  40. (keyword) The <<security-api-put-role-path-params,name>> of the role.
  41. `description`::
  42. (text) The <<defining-roles,description>> of the role.
  43. `metadata`::
  44. (flattened) Metadata field associated with the <<defining-roles,role>>, such as `metadata.app_tag`.
  45. Note that metadata is internally indexed as a <<flattened,flattened>> field type.
  46. This means that all sub-fields act like `keyword` fields when querying and sorting.
  47. It also implies that it is not possible to refer to a subset of metadata fields using wildcard patterns,
  48. e.g. `metadata.field*`, even for query types that support field name patterns.
  49. Lastly, all the metadata fields can be searched together when simply mentioning the
  50. `metadata` field (i.e. not followed by any dot and sub-field name).
  51. `applications`::
  52. The list of <<roles-application-priv,application privileges>> that the role grants.
  53. `application`:::
  54. (keyword) The name of the application associated to the privileges and resources.
  55. `privileges`:::
  56. (keyword) The names of the privileges that the role grants.
  57. `resources`:::
  58. (keyword) The resources to which the privileges apply.
  59. ====
  60. include::{es-ref-dir}/rest-api/common-parms.asciidoc[tag=from]
  61. +
  62. By default, you cannot page through more than 10,000 hits using the `from` and
  63. `size` parameters. To page through more hits, use the
  64. <<search-after,`search_after`>> parameter.
  65. `size`::
  66. (Optional, integer) The number of hits to return. Must not be negative and defaults to `10`.
  67. +
  68. By default, you cannot page through more than 10,000 hits using the `from` and
  69. `size` parameters. To page through more hits, use the
  70. <<search-after,`search_after`>> parameter.
  71. `sort`::
  72. (Optional, object) <<sort-search-results,Sort definition>>. You can sort on `username`, `roles` or `enabled`.
  73. In addition, sort can also be applied to the `_doc` field to sort by index order.
  74. `search_after`::
  75. (Optional, array) <<search-after,Search after>> definition.
  76. [[security-api-query-role-response-body]]
  77. ==== {api-response-body-title}
  78. This API returns the following top level fields:
  79. `total`::
  80. The total number of roles found.
  81. `count`::
  82. The number of roles returned in the response.
  83. `roles`::
  84. A list of roles that match the query.
  85. The returned role format is an extension of the <<defining-roles,role definition>> format.
  86. It adds the `transient_metadata.enabled` and the `_sort` fields.
  87. `transient_metadata.enabled` is set to `false` in case the role is automatically disabled,
  88. for example when the role grants privileges that are not allowed by the installed license.
  89. `_sort` is present when the search query sorts on some field.
  90. It contains the array of values that have been used for sorting.
  91. [[security-api-query-role-example]]
  92. ==== {api-examples-title}
  93. The following request lists all roles, sorted by the role name:
  94. [source,console]
  95. ----
  96. POST /_security/_query/role
  97. {
  98. "sort": ["name"]
  99. }
  100. ----
  101. // TEST[setup:admin_role,user_role]
  102. A successful call returns a JSON structure that contains the information
  103. retrieved for one or more roles:
  104. [source,console-result]
  105. ----
  106. {
  107. "total": 2,
  108. "count": 2,
  109. "roles": [ <1>
  110. {
  111. "name" : "my_admin_role",
  112. "cluster" : [
  113. "all"
  114. ],
  115. "indices" : [
  116. {
  117. "names" : [
  118. "index1",
  119. "index2"
  120. ],
  121. "privileges" : [
  122. "all"
  123. ],
  124. "field_security" : {
  125. "grant" : [
  126. "title",
  127. "body"
  128. ]
  129. },
  130. "allow_restricted_indices" : false
  131. }
  132. ],
  133. "applications" : [ ],
  134. "run_as" : [
  135. "other_user"
  136. ],
  137. "metadata" : {
  138. "version" : 1
  139. },
  140. "transient_metadata" : {
  141. "enabled" : true
  142. },
  143. "description" : "Grants full access to all management features within the cluster.",
  144. "_sort" : [
  145. "my_admin_role"
  146. ]
  147. },
  148. {
  149. "name" : "my_user_role",
  150. "cluster" : [ ],
  151. "indices" : [
  152. {
  153. "names" : [
  154. "index1",
  155. "index2"
  156. ],
  157. "privileges" : [
  158. "all"
  159. ],
  160. "field_security" : {
  161. "grant" : [
  162. "title",
  163. "body"
  164. ]
  165. },
  166. "allow_restricted_indices" : false
  167. }
  168. ],
  169. "applications" : [ ],
  170. "run_as" : [ ],
  171. "metadata" : {
  172. "version" : 1
  173. },
  174. "transient_metadata" : {
  175. "enabled" : true
  176. },
  177. "description" : "Grants user access to some indicies.",
  178. "_sort" : [
  179. "my_user_role"
  180. ]
  181. }
  182. ]
  183. }
  184. ----
  185. // TEST[continued]
  186. <1> The list of roles that were retrieved for this request
  187. Similarly, the following request can be used to query only the user access role,
  188. given its description:
  189. [source,console]
  190. ----
  191. POST /_security/_query/role
  192. {
  193. "query": {
  194. "match": {
  195. "description": {
  196. "query": "user access"
  197. }
  198. }
  199. },
  200. "size": 1 <1>
  201. }
  202. ----
  203. // TEST[continued]
  204. <1> Return only the best matching role
  205. [source,console-result]
  206. ----
  207. {
  208. "total": 2,
  209. "count": 1,
  210. "roles": [
  211. {
  212. "name" : "my_user_role",
  213. "cluster" : [ ],
  214. "indices" : [
  215. {
  216. "names" : [
  217. "index1",
  218. "index2"
  219. ],
  220. "privileges" : [
  221. "all"
  222. ],
  223. "field_security" : {
  224. "grant" : [
  225. "title",
  226. "body"
  227. ]
  228. },
  229. "allow_restricted_indices" : false
  230. }
  231. ],
  232. "applications" : [ ],
  233. "run_as" : [ ],
  234. "metadata" : {
  235. "version" : 1
  236. },
  237. "transient_metadata" : {
  238. "enabled" : true
  239. },
  240. "description" : "Grants user access to some indicies."
  241. }
  242. ]
  243. }
  244. ----