query-api-key.asciidoc 12 KB

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