query-api-key.asciidoc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. [role="xpack"]
  2. [[security-api-query-api-key]]
  3. === Query API key information API
  4. ++++
  5. <titleabbrev>Query API key information</titleabbrev>
  6. ++++
  7. Retrieves information for API keys with <<query-dsl,Query DSL>>
  8. in a <<paginate-search-results,paginated>> fashion.
  9. [[security-api-query-api-key-request]]
  10. ==== {api-request-title}
  11. `GET /_security/_query/api_key`
  12. `POST /_security/_query/api_key`
  13. [[security-api-query-api-key-prereqs]]
  14. ==== {api-prereq-title}
  15. * To use this API, you must have at least the `manage_own_api_key` or the `read_security`
  16. cluster privileges.
  17. * If you have only the `manage_own_api_key` privilege, this API returns only
  18. the API keys that you own. If you have the `read_security`, `manage_api_key` or greater
  19. privileges (including `manage_security`), this API returns all API keys
  20. regardless of ownership.
  21. [[security-api-query-api-key-desc]]
  22. ==== {api-description-title}
  23. Use this API to retrieve the API keys created with the
  24. <<security-api-create-api-key,create API key API>> in a paginated manner.
  25. You can optionally filter the results with a query.
  26. [[security-api-query-api-key-query-params]]
  27. ==== {api-path-parms-title}
  28. `with_limited_by`::
  29. (Optional, Boolean) A boolean flag to return the snapshot of the owner user's role descriptors
  30. associated with the API key. An API key's actual permission is the intersection of
  31. its <<api-key-role-descriptors,assigned role descriptors>> and the owner user's role descriptors
  32. (effectively limited by it). An API key cannot retrieve any API key's limited-by role descriptors
  33. (including itself) unless it has `manage_api_key` or higher privileges.
  34. [[security-api-query-api-key-request-body]]
  35. ==== {api-request-body-title}
  36. You can specify the following parameters in the request body:
  37. `query`::
  38. (Optional, string) A <<query-dsl,query>> to filter which API keys to return.
  39. The query supports a subset of query types, including
  40. <<query-dsl-match-all-query,`match_all`>>, <<query-dsl-bool-query,`bool`>>,
  41. <<query-dsl-term-query,`term`>>, <<query-dsl-terms-query,`terms`>>, <<query-dsl-ids-query,`ids`>>,
  42. <<query-dsl-prefix-query,`prefix`>>, <<query-dsl-wildcard-query,`wildcard`>>, <<query-dsl-exists-query,`exists`>>,
  43. and <<query-dsl-range-query,`range`>>.
  44. +
  45. You can query the following public values associated with an API key.
  46. +
  47. .Valid values for `query`
  48. [%collapsible%open]
  49. ====
  50. `id`::
  51. ID of the API key. Note `id` must be queried with the <<query-dsl-ids-query,`ids`>> query.
  52. `name`::
  53. Name of the API key.
  54. `creation`::
  55. Creation time of the API key in milliseconds.
  56. `expiration`::
  57. Expiration time of the API key in milliseconds.
  58. `invalidated`::
  59. Indicates whether the API key is invalidated. If `true`, the key is invalidated.
  60. Defaults to `false`.
  61. `invalidation`::
  62. Invalidation time of the API key in milliseconds. This field is only set for invalidated API keys.
  63. `username`::
  64. Username of the API key owner.
  65. `realm`::
  66. Realm name of the API key owner.
  67. `metadata`::
  68. Metadata field associated with the API key, such as `metadata.my_field`. Because
  69. metadata is stored as a <<flattened,flattened>> field type, all fields act like
  70. `keyword` fields when querying and sorting.
  71. NOTE: You cannot query the role descriptors of an API key.
  72. ====
  73. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=from]
  74. +
  75. By default, you cannot page through more than 10,000 hits using the `from` and
  76. `size` parameters. To page through more hits, use the
  77. <<search-after,`search_after`>> parameter.
  78. `size`::
  79. (Optional, integer) The number of hits to return. Must not be negative and defaults to `10`.
  80. +
  81. By default, you cannot page through more than 10,000 hits using the `from` and
  82. `size` parameters. To page through more hits, use the
  83. <<search-after,`search_after`>> parameter.
  84. `sort`::
  85. (Optional, object) <<sort-search-results,Sort definition>>. Other than `id`,
  86. all public fields of an API key are eligible for sorting. In addition, sort can
  87. also be applied to the `_doc` field to sort by index order.
  88. `search_after`::
  89. (Optional, array) <<search-after,Search after>> definition.
  90. [[security-api-query-api-key-response-body]]
  91. ==== {api-response-body-title}
  92. This API returns the following top level fields:
  93. `total`::
  94. The total number of API keys found.
  95. `count`::
  96. The number of API keys returned in the response.
  97. `api_keys`::
  98. A list of API key information.
  99. [[security-api-query-api-key-example]]
  100. ==== {api-examples-title}
  101. The following request lists all API keys, assuming you have the
  102. `manage_api_key` privilege:
  103. [source,console]
  104. ----
  105. GET /_security/_query/api_key
  106. ----
  107. A successful call returns a JSON structure that contains the information
  108. retrieved from one or more API keys:
  109. [source,js]
  110. ----
  111. {
  112. "total": 3,
  113. "count": 3,
  114. "api_keys": [ <1>
  115. {
  116. "id": "nkvrGXsB8w290t56q3Rg",
  117. "name": "my-api-key-1",
  118. "creation": 1628227480421,
  119. "expiration": 1629091480421,
  120. "invalidated": false,
  121. "username": "elastic",
  122. "realm": "reserved",
  123. "metadata": {
  124. "letter": "a"
  125. },
  126. "role_descriptors": { <2>
  127. "role-a": {
  128. "cluster": [
  129. "monitor"
  130. ],
  131. "indices": [
  132. {
  133. "names": [
  134. "index-a"
  135. ],
  136. "privileges": [
  137. "read"
  138. ],
  139. "allow_restricted_indices": false
  140. }
  141. ],
  142. "applications": [ ],
  143. "run_as": [ ],
  144. "metadata": { },
  145. "transient_metadata": {
  146. "enabled": true
  147. }
  148. }
  149. }
  150. },
  151. {
  152. "id": "oEvrGXsB8w290t5683TI",
  153. "name": "my-api-key-2",
  154. "creation": 1628227498953,
  155. "expiration": 1628313898953,
  156. "invalidated": false,
  157. "username": "elastic",
  158. "realm": "reserved",
  159. "metadata": {
  160. "letter": "b"
  161. },
  162. "role_descriptors": { } <3>
  163. }
  164. ]
  165. }
  166. ----
  167. // NOTCONSOLE
  168. <1> The list of API keys that were retrieved for this request
  169. <2> The role descriptors that are assigned to this API key when it was <<api-key-role-descriptors,created>>
  170. or last <<security-api-update-api-key-api-key-role-descriptors,updated>>. Note the API key's
  171. effective permissions are an intersection of its assigned privileges and the point-in-time snapshot of
  172. the owner user's permissions.
  173. <3> An empty role descriptors means the API key inherits the owner user's permissions.
  174. If you create an API key with the following details:
  175. [source,console]
  176. ----
  177. POST /_security/api_key
  178. {
  179. "name": "application-key-1",
  180. "metadata": { "application": "my-application"}
  181. }
  182. ----
  183. A successful call returns a JSON structure that provides
  184. API key information. For example:
  185. [source,console-result]
  186. ----
  187. {
  188. "id": "VuaCfGcBCdbkQm-e5aOx",
  189. "name": "application-key-1",
  190. "api_key": "ui2lp2axTNmsyakw9tvNnw",
  191. "encoded": "VnVhQ2ZHY0JDZGJrUW0tZTVhT3g6dWkybHAyYXhUTm1zeWFrdzl0dk5udw=="
  192. }
  193. ----
  194. // TESTRESPONSE[s/VuaCfGcBCdbkQm-e5aOx/$body.id/]
  195. // TESTRESPONSE[s/ui2lp2axTNmsyakw9tvNnw/$body.api_key/]
  196. // TESTRESPONSE[s/VnVhQ2ZHY0JDZGJrUW0tZTVhT3g6dWkybHAyYXhUTm1zeWFrdzl0dk5udw==/$body.encoded/]
  197. Use the information from the response to retrieve the API key by ID:
  198. [source,console]
  199. ----
  200. GET /_security/_query/api_key?with_limited_by=true
  201. {
  202. "query": {
  203. "ids": {
  204. "values": [
  205. "VuaCfGcBCdbkQm-e5aOx"
  206. ]
  207. }
  208. }
  209. }
  210. ----
  211. // TEST[s/VuaCfGcBCdbkQm-e5aOx/$body.id/]
  212. // TEST[continued]
  213. A successful call returns a JSON structure for API key information including its limited-by role descriptors:
  214. [source,js]
  215. --------------------------------------------------
  216. {
  217. "api_keys": [
  218. {
  219. "id": "VuaCfGcBCdbkQm-e5aOx",
  220. "name": "application-key-1",
  221. "creation": 1548550550158,
  222. "expiration": 1548551550158,
  223. "invalidated": false,
  224. "username": "myuser",
  225. "realm": "native1",
  226. "metadata": {
  227. "application": "my-application"
  228. },
  229. "role_descriptors": { },
  230. "limited_by": [ <1>
  231. {
  232. "role-power-user": {
  233. "cluster": [
  234. "monitor"
  235. ],
  236. "indices": [
  237. {
  238. "names": [
  239. "*"
  240. ],
  241. "privileges": [
  242. "read"
  243. ],
  244. "allow_restricted_indices": false
  245. }
  246. ],
  247. "applications": [ ],
  248. "run_as": [ ],
  249. "metadata": { },
  250. "transient_metadata": {
  251. "enabled": true
  252. }
  253. }
  254. }
  255. ]
  256. }
  257. ]
  258. }
  259. --------------------------------------------------
  260. // NOTCONSOLE
  261. <1> The owner user's permissions associated with the API key.
  262. It is a point-in-time snapshot captured at <<security-api-create-api-key,creation>> and
  263. subsequent <<security-api-update-api-key,updates>>. An API key's
  264. effective permissions are an intersection of its assigned privileges and
  265. the owner user's permissions.
  266. You can also retrieve the API key by name:
  267. [source,console]
  268. ----
  269. GET /_security/_query/api_key
  270. {
  271. "query": {
  272. "term": {
  273. "name": {
  274. "value": "application-key-1"
  275. }
  276. }
  277. }
  278. }
  279. ----
  280. // TEST[continued]
  281. Use a `bool` query to issue complex logical conditions and use
  282. `from`, `size`, `sort` to help paginate the result:
  283. [source,js]
  284. ----
  285. GET /_security/_query/api_key
  286. {
  287. "query": {
  288. "bool": {
  289. "must": [
  290. {
  291. "prefix": {
  292. "name": "app1-key-" <1>
  293. }
  294. },
  295. {
  296. "term": {
  297. "invalidated": "false" <2>
  298. }
  299. }
  300. ],
  301. "must_not": [
  302. {
  303. "term": {
  304. "name": "app1-key-01" <3>
  305. }
  306. }
  307. ],
  308. "filter": [
  309. {
  310. "wildcard": {
  311. "username": "org-*-user" <4>
  312. }
  313. },
  314. {
  315. "term": {
  316. "metadata.environment": "production" <5>
  317. }
  318. }
  319. ]
  320. }
  321. },
  322. "from": 20, <6>
  323. "size": 10, <7>
  324. "sort": [ <8>
  325. { "creation": { "order": "desc", "format": "date_time" } },
  326. "name"
  327. ]
  328. }
  329. ----
  330. // NOTCONSOLE
  331. <1> The API key name must begin with `app1-key-`
  332. <2> The API key must still be valid
  333. <3> The API key name must not be `app1-key-01`
  334. <4> The API key must be owned by a username of the <<query-dsl-wildcard-query,wildcard>> pattern `org-*-user`
  335. <5> The API key must have the metadata field `environment` that has the value of `production`
  336. <6> The offset to begin the search result is the 20th (zero-based index) API key
  337. <7> The page size of the response is 10 API keys
  338. <8> The result is first sorted by `creation` date in descending order, then by name in ascending order
  339. The response contains a list of matched API keys along with their sort values:
  340. [source,js]
  341. ----
  342. {
  343. "total": 100,
  344. "count": 10,
  345. "api_keys": [
  346. {
  347. "id": "CLXgVnsBOGkf8IyjcXU7",
  348. "name": "app1-key-79",
  349. "creation": 1629250154811,
  350. "invalidated": false,
  351. "username": "org-admin-user",
  352. "realm": "native1",
  353. "metadata": {
  354. "environment": "production"
  355. },
  356. "role_descriptors": { },
  357. "_sort": [
  358. "2021-08-18T01:29:14.811Z", <1>
  359. "app1-key-79" <2>
  360. ]
  361. },
  362. {
  363. "id": "BrXgVnsBOGkf8IyjbXVB",
  364. "name": "app1-key-78",
  365. "creation": 1629250153794,
  366. "invalidated": false,
  367. "username": "org-admin-user",
  368. "realm": "native1",
  369. "metadata": {
  370. "environment": "production"
  371. },
  372. "role_descriptors": { },
  373. "_sort": [
  374. "2021-08-18T01:29:13.794Z",
  375. "app1-key-78"
  376. ]
  377. },
  378. ...
  379. ]
  380. }
  381. ----
  382. // NOTCONSOLE
  383. <1> The first sort value is creation time, which is displayed in `date_time` <<mapping-date-format,format>> as defined in the request
  384. <2> The second sort value is the API key name
  385. You can use the following request to retrieve all valid API keys, i.e. not invalidated and not expired:
  386. [source,js]
  387. ----
  388. GET /_security/_query/api_key
  389. {
  390. "query": {
  391. "bool": {
  392. "must": {
  393. "term": {
  394. "invalidated": false <1>
  395. }
  396. },
  397. "should": [ <2>
  398. {
  399. "range": {
  400. "expiration": {
  401. "gte": "now"
  402. }
  403. }
  404. },
  405. {
  406. "bool": {
  407. "must_not": {
  408. "exists": {
  409. "field": "expiration"
  410. }
  411. }
  412. }
  413. }
  414. ],
  415. "minimum_should_match": 1
  416. }
  417. }
  418. }
  419. ----
  420. // NOTCONSOLE
  421. <1> Matching API keys must not be invalidated
  422. <2> Matching API keys must be either not expired or does not have an expiration date