query-api-key.asciidoc 13 KB

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