@@ -21,18 +21,18 @@ Creates an API key for access without requiring basic authentication.
21
21
22
22
IMPORTANT: If the credential that is used to authenticate this request is
23
23
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`>> .
25
25
26
26
[[security-api-create-api-key-desc]]
27
27
==== {api-description-title}
28
28
29
29
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,
31
31
you can explicitly enable the `xpack.security.authc.api_key.enabled` setting. When
32
32
you are running in production mode, a bootstrap check prevents you from enabling
33
33
the API key service unless you also enable TLS on the HTTP interface.
34
34
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
36
36
API key, its unique id, and its name. If applicable, it also returns expiration
37
37
information for the API key in milliseconds.
38
38
@@ -51,6 +51,7 @@ The following parameters can be specified in the body of a POST or PUT request:
51
51
`name`::
52
52
(Required, string) Specifies the name for this API key.
53
53
54
+ [[api-key-role-descriptors]]
54
55
`role_descriptors`::
55
56
(Optional, array-of-role-descriptor) An array of role descriptors for this API
56
57
key. This parameter is optional. When it is not specified or is an empty array,
@@ -86,11 +87,11 @@ system usage.
86
87
The following example creates an API key:
87
88
88
89
[source,console]
89
- ------------------------------------------------------------
90
+ ----
90
91
POST /_security/api_key
91
92
{
92
93
"name": "my-api-key",
93
- "expiration": "1d", <1>
94
+ "expiration": "1d", <1>
94
95
"role_descriptors": { <2>
95
96
"role-a": {
96
97
"cluster": ["all"],
@@ -120,48 +121,93 @@ POST /_security/api_key
120
121
}
121
122
}
122
123
}
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
125
126
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.
128
129
129
130
A successful call returns a JSON structure that provides
130
131
API key information.
131
132
132
133
[source,console-result]
133
- --------------------------------------------------
134
+ ----
134
135
{
135
- "id":"VuaCfGcBCdbkQm-e5aOx", <1>
136
+ "id":"VuaCfGcBCdbkQm-e5aOx", <1>
136
137
"name":"my-api-key",
137
- "expiration":1544068612110, <2>
138
+ "expiration":1544068612110, <2>
138
139
"api_key":"ui2lp2axTNmsyakw9tvNnw" <3>
139
140
}
140
- --------------------------------------------------
141
+ ----
141
142
// TESTRESPONSE[s/VuaCfGcBCdbkQm-e5aOx/$body.id/]
142
143
// TESTRESPONSE[s/1544068612110/$body.expiration/]
143
144
// 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
151
148
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:
153
153
154
154
[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
+ ----
158
159
// 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:
159
190
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:
161
207
162
208
[source,shell]
163
209
----
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
167
212
----
213
+ // NOTCONSOLE
0 commit comments