query-role.asciidoc 7.8 KB

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