Skip to content

Commit f54e617

Browse files
authored
Add docs for QueryApiKey API (#76521) (#76691)
This PR adds documentation for the new QueryApiKey API added in #75335 and #76144 Relates: #71023
1 parent da3f14a commit f54e617

File tree

2 files changed

+336
-2
lines changed

2 files changed

+336
-2
lines changed

x-pack/docs/en/rest-api/security/get-api-keys.asciidoc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ A successful call returns a JSON structure that contains the information of one
179179
{
180180
"api_keys": [ <1>
181181
{
182-
"id": "dGhpcyBpcyBub3QgYSByZWFsIHRva2VuIGJ1dCBpdCBpcyBvbmx5IHRlc3QgZGF0YS4gZG8gbm90IHRyeSB0byByZWFkIHRva2VuIQ==", <2>
182+
"id": "0GF5GXsBCXxz2eDxWwFN", <2>
183183
"name": "hadoop_myuser_key", <3>
184184
"creation": 1548550550158, <4>
185185
"expiration": 1548551550158, <5>
@@ -191,7 +191,7 @@ A successful call returns a JSON structure that contains the information of one
191191
}
192192
},
193193
{
194-
"id": "api-key-id-2",
194+
"id": "6wHJmcQpReKBa42EHV5SBw",
195195
"name": "api-key-name-2",
196196
"creation": 1548550550158,
197197
"invalidated": false,
Lines changed: 334 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,334 @@
1+
[role="xpack"]
2+
[[security-api-query-api-key]]
3+
=== Query API key information API
4+
5+
++++
6+
<titleabbrev>Query API key information</titleabbrev>
7+
++++
8+
9+
Retrieves information for API keys with <<query-dsl,Query DSL>>
10+
in a <<paginate-search-results,paginated>> fashion.
11+
12+
[[security-api-query-api-key-request]]
13+
==== {api-request-title}
14+
15+
`GET /_security/_query/api_key`
16+
17+
`POST /_security/_query/api_key`
18+
19+
[[security-api-query-api-key-prereqs]]
20+
==== {api-prereq-title}
21+
22+
* To use this API, you must have at least the `manage_own_api_key` cluster
23+
privilege.
24+
* If you have only the `manage_own_api_key` privilege, this API returns only
25+
the API keys that you own. If you have the `manage_api_key` or greater
26+
privileges (including `manage_security`), this API returns all API keys
27+
regardless of ownership.
28+
29+
[[security-api-query-api-key-desc]]
30+
==== {api-description-title}
31+
32+
Use this API to retrieve the API keys created with the
33+
<<security-api-create-api-key,create API key API>> in a paginated manner.
34+
You can optionally filter the results with a query.
35+
36+
[[security-api-query-api-key-request-body]]
37+
==== {api-request-body-title}
38+
39+
You can specify the following parameters in the request body:
40+
41+
`query`::
42+
(Optional, string) A <<query-dsl,query>> to filter which API keys to return.
43+
The query supports a subset of query types, including
44+
<<query-dsl-match-all-query,`match_all`>>, <<query-dsl-bool-query,`bool`>>,
45+
<<query-dsl-term-query,`term`>>, <<query-dsl-terms-query,`terms`>>, <<query-dsl-ids-query,`ids`>>,
46+
<<query-dsl-prefix-query,`prefix`>>, <<query-dsl-wildcard-query,`wildcard`>>, and <<query-dsl-range-query,`range`>>.
47+
+
48+
You can query all public information associated with an API key, including the
49+
following values.
50+
+
51+
.Valid values for `query`
52+
[%collapsible%open]
53+
====
54+
`id`::
55+
ID of the API key. Note `id` must be queried with the <<query-dsl-ids-query,`ids`>> query.
56+
57+
`name`::
58+
Name of the API key.
59+
60+
`creation`::
61+
Creation time of the API key in milliseconds.
62+
63+
`expiration`::
64+
Expiration time of the API key in milliseconds.
65+
66+
`invalidated`::
67+
Indicates whether the API key is invalidated. If `true`, the key is invalidated.
68+
Defaults to `false`.
69+
70+
`username`::
71+
Username of the API key owner.
72+
73+
`realm`::
74+
Realm name of the API key owner.
75+
76+
`metadata`::
77+
Metadata field associated with the API key, such as `metadata.my_field`. Because
78+
metadata is stored as a <<flattened,flattened>> field type, all fields act like
79+
`keyword` fields when querying and sorting.
80+
====
81+
82+
include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=from]
83+
+
84+
By default, you cannot page through more than 10,000 hits using the `from` and
85+
`size` parameters. To page through more hits, use the
86+
<<search-after,`search_after`>> parameter.
87+
88+
`size`::
89+
(Optional, integer) The number of hits to return. Must not be negative and defaults to `10`.
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+
95+
`sort`::
96+
(Optional, object) <<sort-search-results,Sort definition>>. Other than `id`,
97+
all public fields of an API key are eligible for sorting. In addition, sort can
98+
also be applied to the `_doc` field to sort by index order.
99+
100+
`search_after`::
101+
(Optional, array) <<search-after,Search after>> definition.
102+
103+
104+
[[security-api-query-api-key-response-body]]
105+
==== {api-response-body-title}
106+
107+
This API returns the following top level fields:
108+
109+
`total`::
110+
The total number of API keys found.
111+
112+
`count`::
113+
The number of API keys returned in the response.
114+
115+
`api_keys`::
116+
A list of API key information.
117+
118+
[[security-api-query-api-key-example]]
119+
==== {api-examples-title}
120+
121+
The following request lists all API keys, assuming you have the
122+
`manage_api_key` privilege:
123+
124+
[source,console]
125+
----
126+
GET /_security/_query/api_key
127+
----
128+
129+
A successful call returns a JSON structure that contains the information
130+
retrieved from one or more API keys:
131+
132+
[source,js]
133+
----
134+
{
135+
"total": 3,
136+
"count": 3,
137+
"api_keys": [ <1>
138+
{
139+
"id": "nkvrGXsB8w290t56q3Rg",
140+
"name": "my-api-key-1",
141+
"creation": 1628227480421,
142+
"expiration": 1629091480421,
143+
"invalidated": false,
144+
"username": "elastic",
145+
"realm": "reserved",
146+
"metadata": {
147+
"letter": "a"
148+
}
149+
},
150+
{
151+
"id": "oEvrGXsB8w290t5683TI",
152+
"name": "my-api-key-2",
153+
"creation": 1628227498953,
154+
"expiration": 1628313898953,
155+
"invalidated": false,
156+
"username": "elastic",
157+
"realm": "reserved",
158+
"metadata": {
159+
"letter": "b"
160+
}
161+
}
162+
]
163+
}
164+
----
165+
// NOTCONSOLE
166+
167+
<1> The list of API keys that were retrieved for this request
168+
169+
If you create an API key with the following details:
170+
171+
[source,console]
172+
----
173+
POST /_security/api_key
174+
{
175+
"name": "application-key-1",
176+
"metadata": { "application": "my-application"}
177+
}
178+
----
179+
180+
A successful call returns a JSON structure that provides
181+
API key information. For example:
182+
183+
[source,console-result]
184+
----
185+
{
186+
"id": "VuaCfGcBCdbkQm-e5aOx",
187+
"name": "application-key-1",
188+
"api_key": "ui2lp2axTNmsyakw9tvNnw"
189+
}
190+
----
191+
// TESTRESPONSE[s/VuaCfGcBCdbkQm-e5aOx/$body.id/]
192+
// TESTRESPONSE[s/ui2lp2axTNmsyakw9tvNnw/$body.api_key/]
193+
194+
Use the information from the response to retrieve the API key by ID:
195+
196+
[source,console]
197+
----
198+
GET /_security/_query/api_key
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+
212+
You can also retrieve the API key by name:
213+
214+
[source,console]
215+
----
216+
GET /_security/_query/api_key
217+
{
218+
"query": {
219+
"term": {
220+
"name": {
221+
"value": "application-key-1"
222+
}
223+
}
224+
}
225+
}
226+
----
227+
// TEST[continued]
228+
229+
Use a `bool` query to issue complex logical conditions and use
230+
`from`, `size`, `sort` to help paginate the result:
231+
232+
[source,js]
233+
----
234+
{
235+
"query": {
236+
"bool": {
237+
"must": [
238+
{
239+
"prefix": {
240+
"name": "app1-key-" <1>
241+
}
242+
},
243+
{
244+
"term": {
245+
"invalidated": "false" <2>
246+
}
247+
}
248+
],
249+
"must_not": [
250+
{
251+
"term": {
252+
"name": "app1-key-01" <3>
253+
}
254+
}
255+
],
256+
"filter": [
257+
{
258+
"wildcard": {
259+
"username": "org-*-user" <4>
260+
}
261+
},
262+
{
263+
"term": {
264+
"metadata.environment": "production" <5>
265+
}
266+
}
267+
]
268+
}
269+
},
270+
"from": 20, <6>
271+
"size": 10, <7>
272+
"sort": [ <8>
273+
{ "creation": { "order": "desc", "format": "date_time" } },
274+
"name"
275+
]
276+
}
277+
----
278+
// NOTCONSOLE
279+
280+
<1> The API key name must begin with `app1-key-`
281+
<2> The API key must still be valid
282+
<3> The API key name must not be `app1-key-01`
283+
<4> The API key must be owned by a username of the <<query-dsl-wildcard-query,wildcard>> pattern `org-*-user`
284+
<5> The API key must have the metadata field `environment` that has the value of `production`
285+
<6> The offset to begin the search result is the 20th (zero-based index) API key
286+
<7> The page size of the response is 10 API keys
287+
<8> The result is first sorted by `creation` date in descending order, then by name in ascending order
288+
289+
The response contains a list of matched API keys along with their sort values:
290+
291+
[source,js]
292+
----
293+
{
294+
"total": 100,
295+
"count": 10,
296+
"api_keys": [
297+
{
298+
"id": "CLXgVnsBOGkf8IyjcXU7",
299+
"name": "app1-key-79",
300+
"creation": 1629250154811,
301+
"invalidated": false,
302+
"username": "org-admin-user",
303+
"realm": "native1",
304+
"metadata": {
305+
"environment": "production"
306+
},
307+
"_sort": [
308+
"2021-08-18T01:29:14.811Z", <1>
309+
"app1-key-79" <2>
310+
]
311+
},
312+
{
313+
"id": "BrXgVnsBOGkf8IyjbXVB",
314+
"name": "app1-key-78",
315+
"creation": 1629250153794,
316+
"invalidated": false,
317+
"username": "org-admin-user",
318+
"realm": "native1",
319+
"metadata": {
320+
"environment": "production"
321+
},
322+
"_sort": [
323+
"2021-08-18T01:29:13.794Z",
324+
"app1-key-78"
325+
]
326+
},
327+
...
328+
]
329+
}
330+
----
331+
// NOTCONSOLE
332+
333+
<1> The first sort value is creation time, which is displayed in `date_time` <<mapping-date-format,format>> as defined in the request
334+
<2> The second sort value is the API key name

0 commit comments

Comments
 (0)