query-api-key.asciidoc 13 KB

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