query-api-key.asciidoc 12 KB

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