query-role.asciidoc 8.3 KB

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