2019-02-04 22:21:57 -05:00
|
|
|
[role="xpack"]
|
|
|
|
[[security-api-create-api-key]]
|
2019-03-19 14:00:42 -04:00
|
|
|
=== Create API key API
|
2019-03-04 18:06:00 -05:00
|
|
|
++++
|
|
|
|
<titleabbrev>Create API keys</titleabbrev>
|
|
|
|
++++
|
2019-02-04 22:21:57 -05:00
|
|
|
|
|
|
|
Creates an API key for access without requiring basic authentication.
|
|
|
|
|
2019-08-02 13:56:05 -04:00
|
|
|
[[security-api-create-api-key-request]]
|
|
|
|
==== {api-request-title}
|
2019-02-04 22:21:57 -05:00
|
|
|
|
|
|
|
`POST /_security/api_key`
|
|
|
|
`PUT /_security/api_key`
|
|
|
|
|
2019-08-02 13:56:05 -04:00
|
|
|
[[security-api-create-api-key-prereqs]]
|
|
|
|
==== {api-prereq-title}
|
|
|
|
|
|
|
|
* To use this API, you must have at least the `manage_api_key` cluster privilege.
|
|
|
|
|
|
|
|
[[security-api-create-api-key-desc]]
|
|
|
|
==== {api-description-title}
|
2019-02-04 22:21:57 -05:00
|
|
|
|
|
|
|
The API keys are created by the {es} API key service, which is automatically enabled
|
|
|
|
when you configure TLS on the HTTP interface. See <<tls-http>>. Alternatively,
|
|
|
|
you can explicitly enable the `xpack.security.authc.api_key.enabled` setting. When
|
|
|
|
you are running in production mode, a bootstrap check prevents you from enabling
|
|
|
|
the API key service unless you also enable TLS on the HTTP interface.
|
|
|
|
|
2019-03-19 14:00:42 -04:00
|
|
|
A successful create API key API call returns a JSON structure that contains the
|
|
|
|
API key, its unique id, and its name. If applicable, it also returns expiration
|
|
|
|
information for the API key in milliseconds.
|
2019-02-04 22:21:57 -05:00
|
|
|
|
2019-03-19 14:00:42 -04:00
|
|
|
NOTE: By default, API keys never expire. You can specify expiration information
|
|
|
|
when you create the API keys.
|
2019-02-04 22:21:57 -05:00
|
|
|
|
2019-03-19 14:00:42 -04:00
|
|
|
See <<api-key-service-settings>> for configuration settings related to API key
|
|
|
|
service.
|
2019-02-06 04:58:22 -05:00
|
|
|
|
2019-08-02 13:56:05 -04:00
|
|
|
|
|
|
|
[[security-api-create-api-key-request-body]]
|
|
|
|
==== {api-request-body-title}
|
2019-02-04 22:21:57 -05:00
|
|
|
|
|
|
|
The following parameters can be specified in the body of a POST or PUT request:
|
|
|
|
|
|
|
|
`name`::
|
2019-10-12 07:22:16 -04:00
|
|
|
(Optional, string) Specifies the name for this API key.
|
2019-02-04 22:21:57 -05:00
|
|
|
|
2019-08-02 13:56:05 -04:00
|
|
|
`role_descriptors`::
|
|
|
|
(Optional, array-of-role-descriptor) An array of role descriptors for this API
|
|
|
|
key. This parameter is optional. When it is not specified or is an empty array,
|
2019-09-26 03:56:48 -04:00
|
|
|
then the API key will have a _point in time snapshot of permissions of the
|
|
|
|
authenticated user_. If you supply role descriptors then the resultant permissions
|
|
|
|
would be an intersection of API keys permissions and authenticated user's permissions
|
|
|
|
thereby limiting the access scope for API keys.
|
|
|
|
The structure of role descriptor is the same as the request for create role API.
|
2019-12-26 07:49:41 -05:00
|
|
|
For more details, see <<security-api-put-role, create or update roles API>>.
|
2019-02-04 22:21:57 -05:00
|
|
|
|
|
|
|
`expiration`::
|
2019-08-02 13:56:05 -04:00
|
|
|
(Optional, string) Expiration time for the API key. By default, API keys never
|
|
|
|
expire.
|
2019-07-02 23:51:44 -04:00
|
|
|
|
|
|
|
|
2019-08-02 13:56:05 -04:00
|
|
|
[[security-api-create-api-key-example]]
|
|
|
|
==== {api-examples-title}
|
2019-02-04 22:21:57 -05:00
|
|
|
|
|
|
|
The following example creates an API key:
|
|
|
|
|
2019-09-05 14:12:39 -04:00
|
|
|
[source,console]
|
2019-02-04 22:21:57 -05:00
|
|
|
------------------------------------------------------------
|
|
|
|
POST /_security/api_key
|
|
|
|
{
|
|
|
|
"name": "my-api-key",
|
|
|
|
"expiration": "1d", <1>
|
|
|
|
"role_descriptors": { <2>
|
|
|
|
"role-a": {
|
|
|
|
"cluster": ["all"],
|
|
|
|
"index": [
|
|
|
|
{
|
|
|
|
"names": ["index-a*"],
|
|
|
|
"privileges": ["read"]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
"role-b": {
|
|
|
|
"cluster": ["all"],
|
|
|
|
"index": [
|
|
|
|
{
|
|
|
|
"names": ["index-b*"],
|
|
|
|
"privileges": ["all"]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
------------------------------------------------------------
|
|
|
|
<1> optional expiration for the API key being generated. If expiration is not
|
|
|
|
provided then the API keys do not expire.
|
|
|
|
<2> optional role descriptors for this API key, if not provided then permissions
|
|
|
|
of authenticated user are applied.
|
|
|
|
|
|
|
|
A successful call returns a JSON structure that provides
|
|
|
|
API key information.
|
|
|
|
|
2019-09-05 14:12:39 -04:00
|
|
|
[source,console-result]
|
2019-02-04 22:21:57 -05:00
|
|
|
--------------------------------------------------
|
|
|
|
{
|
|
|
|
"id":"VuaCfGcBCdbkQm-e5aOx", <1>
|
|
|
|
"name":"my-api-key",
|
|
|
|
"expiration":1544068612110, <2>
|
|
|
|
"api_key":"ui2lp2axTNmsyakw9tvNnw" <3>
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
// TESTRESPONSE[s/VuaCfGcBCdbkQm-e5aOx/$body.id/]
|
|
|
|
// TESTRESPONSE[s/1544068612110/$body.expiration/]
|
|
|
|
// TESTRESPONSE[s/ui2lp2axTNmsyakw9tvNnw/$body.api_key/]
|
|
|
|
<1> unique id for this API key
|
|
|
|
<2> optional expiration in milliseconds for this API key
|
|
|
|
<3> generated API key
|
2019-02-06 04:58:22 -05:00
|
|
|
|
|
|
|
The API key returned by this API can then be used by sending a request with a
|
2019-12-17 11:08:38 -05:00
|
|
|
`Authorization` header with a value having the prefix `ApiKey` followed
|
2019-02-06 04:58:22 -05:00
|
|
|
by the _credentials_, where _credentials_ is the base64 encoding of `id` and `api_key` joined by a colon.
|
|
|
|
|
|
|
|
[source,shell]
|
|
|
|
--------------------------------------------------
|
|
|
|
curl -H "Authorization: ApiKey VnVhQ2ZHY0JDZGJrUW0tZTVhT3g6dWkybHAyYXhUTm1zeWFrdzl0dk5udw==" http://localhost:9200/_cluster/health
|
|
|
|
--------------------------------------------------
|
|
|
|
// NOTCONSOLE
|