Skip to content

Commit 6cc23b7

Browse files
Adam Lockeelasticmachine
andauthored
[DOCS] Clarify API key format for create API key (#75076) (#75305)
* [DOCS] Clarify API key format for create API key * Removing unnecessary NOTCONSOLE * Clarifying information about UTF-8 format Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com> # Conflicts: # x-pack/docs/en/rest-api/security/create-api-keys.asciidoc
1 parent a2ad2e7 commit 6cc23b7

File tree

1 file changed

+74
-28
lines changed

1 file changed

+74
-28
lines changed

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

Lines changed: 74 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,18 @@ Creates an API key for access without requiring basic authentication.
2121

2222
IMPORTANT: If the credential that is used to authenticate this request is
2323
an API key, the derived API key cannot have any privileges. If you specify privileges, the API returns an error.
24-
See the note under `role_descriptors`.
24+
See the note under <<api-key-role-descriptors,`role_descriptors`>>.
2525

2626
[[security-api-create-api-key-desc]]
2727
==== {api-description-title}
2828

2929
The API keys are created by the {es} API key service, which is automatically enabled
30-
when you configure TLS on the HTTP interface. See <<encrypt-http-communication>>. Alternatively,
30+
when you <<encrypt-http-communication,configure TLS on the HTTP interface>>. Alternatively,
3131
you can explicitly enable the `xpack.security.authc.api_key.enabled` setting. When
3232
you are running in production mode, a bootstrap check prevents you from enabling
3333
the API key service unless you also enable TLS on the HTTP interface.
3434

35-
A successful create API key API call returns a JSON structure that contains the
35+
A successful request returns a JSON structure that contains the
3636
API key, its unique id, and its name. If applicable, it also returns expiration
3737
information for the API key in milliseconds.
3838

@@ -51,6 +51,7 @@ The following parameters can be specified in the body of a POST or PUT request:
5151
`name`::
5252
(Required, string) Specifies the name for this API key.
5353

54+
[[api-key-role-descriptors]]
5455
`role_descriptors`::
5556
(Optional, array-of-role-descriptor) An array of role descriptors for this API
5657
key. This parameter is optional. When it is not specified or is an empty array,
@@ -86,11 +87,11 @@ system usage.
8687
The following example creates an API key:
8788

8889
[source,console]
89-
------------------------------------------------------------
90+
----
9091
POST /_security/api_key
9192
{
9293
"name": "my-api-key",
93-
"expiration": "1d", <1>
94+
"expiration": "1d", <1>
9495
"role_descriptors": { <2>
9596
"role-a": {
9697
"cluster": ["all"],
@@ -120,48 +121,93 @@ POST /_security/api_key
120121
}
121122
}
122123
}
123-
------------------------------------------------------------
124-
<1> optional expiration for the API key being generated. If expiration is not
124+
----
125+
<1> Optional expiration for the API key being generated. If expiration is not
125126
provided then the API keys do not expire.
126-
<2> optional role descriptors for this API key, if not provided then permissions
127-
of authenticated user are applied.
127+
<2> Optional role descriptors for this API key. If not provided, permissions
128+
of the authenticated user are applied.
128129

129130
A successful call returns a JSON structure that provides
130131
API key information.
131132

132133
[source,console-result]
133-
--------------------------------------------------
134+
----
134135
{
135-
"id":"VuaCfGcBCdbkQm-e5aOx", <1>
136+
"id":"VuaCfGcBCdbkQm-e5aOx", <1>
136137
"name":"my-api-key",
137-
"expiration":1544068612110, <2>
138+
"expiration":1544068612110, <2>
138139
"api_key":"ui2lp2axTNmsyakw9tvNnw" <3>
139140
}
140-
--------------------------------------------------
141+
----
141142
// TESTRESPONSE[s/VuaCfGcBCdbkQm-e5aOx/$body.id/]
142143
// TESTRESPONSE[s/1544068612110/$body.expiration/]
143144
// TESTRESPONSE[s/ui2lp2axTNmsyakw9tvNnw/$body.api_key/]
144-
<1> unique id for this API key
145-
<2> optional expiration in milliseconds for this API key
146-
<3> generated API key
147-
148-
The API key returned by this API can then be used by sending a request with a
149-
`Authorization` header with a value having the prefix `ApiKey` followed
150-
by the _credentials_, where _credentials_ is the base64 encoding of `id` and `api_key` joined by a colon.
145+
<1> Unique `id` for this API key
146+
<2> Optional expiration in milliseconds for this API key
147+
<3> Generated API key
151148

152-
NOTE: If your node has `xpack.security.http.ssl.enabled` set to `true`, then you must specify `https` when creating your API key.
149+
To use the generated API key, send a request with an `Authorization` header that
150+
contains an `ApiKey` prefix followed by the API key credentials. The credentials
151+
are a Base64-encoded string in UTF-8 format that you create by combining the
152+
`id` and `api_key` with a colon (`:`). For example:
153153

154154
[source,shell]
155-
--------------------------------------------------
156-
curl -H "Authorization: ApiKey VnVhQ2ZHY0JDZGJrUW0tZTVhT3g6dWkybHAyYXhUTm1zeWFrdzl0dk5udw==" http://localhost:9200/_cluster/health
157-
--------------------------------------------------
155+
----
156+
curl -H "Authorization: ApiKey <credentials>" \
157+
http://localhost:9200/_cluster/health\?pretty <1>
158+
----
158159
// NOTCONSOLE
160+
<1> If your node has `xpack.security.http.ssl.enabled` set to `true`, then you
161+
must specify `https` when creating your API key
162+
163+
.Use UTF-8 encoding
164+
****
165+
When converting the concatenated String of `id` and `api_key` into bytes, the
166+
format must be UTF-8. Authentication will fail if you use UTF-16 or UTF-32
167+
encoding.
168+
169+
If you're concatenating `id` and `api_key` and then getting the bytes of that
170+
String from the command line (like in <<concat-api-key,this example>>), the
171+
`echo` command defaults to ASCII formatting, which is equivalent to UTF-8
172+
encoding.
173+
174+
However, some other tools require an explicit encoding when converting a String
175+
into bytes. For example, in Java, you might use something like the following
176+
code, which assumes that `result` is the response from the create API key API.
177+
This conversion ensures that the bytes of the concatenated string is in UTF-8
178+
format:
179+
180+
[source,java]
181+
----
182+
var bytes = (result.id + ":" + result.api_key).getBytes(StandardCharsets.UTF_8);
183+
var header = "ApiKey " + Base64.getEncoder().encodeToString(bytes);
184+
----
185+
****
186+
187+
On a Unix-like system, the following command combines the `id` and `api_key`
188+
from the previous response. The concatenation of these parameters should be in
189+
UTF-8 format:
159190

160-
One way to create the credentials from CLI on an Unix-like system is as the follows:
191+
[[concat-api-key]]
192+
[source,shell]
193+
----
194+
echo -n "VuaCfGcBCdbkQm-e5aOx:ui2lp2axTNmsyakw9tvNnw" | base64 <1>
195+
----
196+
<1> Use `-n` so that the `echo` command doesn't print the trailing newline
197+
character
198+
199+
The command outputs a Base64-encoded String:
200+
201+
[source,shell]
202+
----
203+
VnVhQ2ZHY0JDZGJrUW0tZTVhT3g6dWkybHAyYXhUTm1zeWFrdzl0dk5udw==
204+
----
205+
206+
Use this String in a request to authenticate with your cluster:
161207

162208
[source,shell]
163209
----
164-
# Please note the use of "-n" to instruct echo to not print the trailing newline character.
165-
# It should return VnVhQ2ZHY0JDZGJrUW0tZTVhT3g6dWkybHAyYXhUTm1zeWFrdzl0dk5udw==
166-
echo -n "VuaCfGcBCdbkQm-e5aOx:ui2lp2axTNmsyakw9tvNnw" | base64
210+
curl -H "Authorization: ApiKey VnVhQ2ZHY0JDZGJrUW0tZTVhT3g6dWkybHAyYXhUTm1zeWFrdzl0dk5udw==" \
211+
http://localhost:9200/_cluster/health\?pretty
167212
----
213+
// NOTCONSOLE

0 commit comments

Comments
 (0)