create-api-keys.asciidoc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. [role="xpack"]
  2. [[security-api-create-api-key]]
  3. === Create API key API
  4. ++++
  5. <titleabbrev>Create API keys</titleabbrev>
  6. ++++
  7. Creates an API key for access without requiring basic authentication.
  8. [[security-api-create-api-key-request]]
  9. ==== {api-request-title}
  10. `POST /_security/api_key`
  11. `PUT /_security/api_key`
  12. [[security-api-create-api-key-prereqs]]
  13. ==== {api-prereq-title}
  14. * To use this API, you must have at least the `manage_api_key` cluster privilege.
  15. IMPORTANT: If the credential that is used to authenticate this request is
  16. an API key, the derived API key cannot have any privileges. If you specify privileges, the API returns an error.
  17. See the note under <<api-key-role-descriptors,`role_descriptors`>>.
  18. [[security-api-create-api-key-desc]]
  19. ==== {api-description-title}
  20. The API keys are created by the {es} API key service, which is automatically enabled
  21. when you <<encrypt-http-communication,configure TLS on the HTTP interface>>. Alternatively,
  22. you can explicitly enable the `xpack.security.authc.api_key.enabled` setting. When
  23. you are running in production mode, a bootstrap check prevents you from enabling
  24. the API key service unless you also enable TLS on the HTTP interface.
  25. A successful request returns a JSON structure that contains the
  26. API key, its unique id, and its name. If applicable, it also returns expiration
  27. information for the API key in milliseconds.
  28. NOTE: By default, API keys never expire. You can specify expiration information
  29. when you create the API keys.
  30. See <<api-key-service-settings>> for configuration settings related to API key
  31. service.
  32. [[security-api-create-api-key-request-body]]
  33. ==== {api-request-body-title}
  34. The following parameters can be specified in the body of a POST or PUT request:
  35. `name`::
  36. (Required, string) Specifies the name for this API key.
  37. [[api-key-role-descriptors]]
  38. `role_descriptors`::
  39. (Optional, array-of-role-descriptor) An array of role descriptors for this API
  40. key. This parameter is optional. When it is not specified or is an empty array,
  41. then the API key will have a _point in time snapshot of permissions of the
  42. authenticated user_. If you supply role descriptors then the resultant permissions
  43. would be an intersection of API keys permissions and authenticated user's permissions
  44. thereby limiting the access scope for API keys.
  45. The structure of role descriptor is the same as the request for create role API.
  46. For more details, see <<security-api-put-role, create or update roles API>>.
  47. +
  48. --
  49. NOTE: Due to the way in which this permission intersection is calculated, it is not
  50. possible to create an API key that is a child of another API key, unless the derived
  51. key is created without any privileges. In this case, you must explicitly specify a
  52. role descriptor with no privileges. The derived API key can be used for
  53. authentication; it will not have authority to call {es} APIs.
  54. --
  55. `expiration`::
  56. (Optional, string) Expiration time for the API key. By default, API keys never
  57. expire.
  58. `metadata`::
  59. (Optional, object) Arbitrary metadata that you want to associate with the API key.
  60. It supports nested data structure.
  61. Within the `metadata` object, keys beginning with `_` are reserved for
  62. system usage.
  63. [[security-api-create-api-key-example]]
  64. ==== {api-examples-title}
  65. The following example creates an API key:
  66. [source,console]
  67. ----
  68. POST /_security/api_key
  69. {
  70. "name": "my-api-key",
  71. "expiration": "1d", <1>
  72. "role_descriptors": { <2>
  73. "role-a": {
  74. "cluster": ["all"],
  75. "index": [
  76. {
  77. "names": ["index-a*"],
  78. "privileges": ["read"]
  79. }
  80. ]
  81. },
  82. "role-b": {
  83. "cluster": ["all"],
  84. "index": [
  85. {
  86. "names": ["index-b*"],
  87. "privileges": ["all"]
  88. }
  89. ]
  90. }
  91. },
  92. "metadata": {
  93. "application": "my-application",
  94. "environment": {
  95. "level": 1,
  96. "trusted": true,
  97. "tags": ["dev", "staging"]
  98. }
  99. }
  100. }
  101. ----
  102. <1> Optional expiration for the API key being generated. If expiration is not
  103. provided then the API keys do not expire.
  104. <2> Optional role descriptors for this API key. If not provided, permissions
  105. of the authenticated user are applied.
  106. A successful call returns a JSON structure that provides
  107. API key information.
  108. [source,console-result]
  109. ----
  110. {
  111. "id":"VuaCfGcBCdbkQm-e5aOx", <1>
  112. "name":"my-api-key",
  113. "expiration":1544068612110, <2>
  114. "api_key":"ui2lp2axTNmsyakw9tvNnw" <3>
  115. }
  116. ----
  117. // TESTRESPONSE[s/VuaCfGcBCdbkQm-e5aOx/$body.id/]
  118. // TESTRESPONSE[s/1544068612110/$body.expiration/]
  119. // TESTRESPONSE[s/ui2lp2axTNmsyakw9tvNnw/$body.api_key/]
  120. <1> Unique `id` for this API key
  121. <2> Optional expiration in milliseconds for this API key
  122. <3> Generated API key
  123. To use the generated API key, send a request with an `Authorization` header that
  124. contains an `ApiKey` prefix followed by the API key credentials. The credentials
  125. are a Base64-encoded string in UTF-8 format that you create by combining the
  126. `id` and `api_key` with a colon (`:`). For example:
  127. [source,shell]
  128. ----
  129. curl -H "Authorization: ApiKey <credentials>" \
  130. http://localhost:9200/_cluster/health\?pretty <1>
  131. ----
  132. // NOTCONSOLE
  133. <1> If your node has `xpack.security.http.ssl.enabled` set to `true`, then you
  134. must specify `https` when creating your API key
  135. .Use UTF-8 encoding
  136. ****
  137. When converting the concatenated String of `id` and `api_key` into bytes, the
  138. format must be UTF-8. Authentication will fail if you use UTF-16 or UTF-32
  139. encoding.
  140. If you're concatenating `id` and `api_key` and then getting the bytes of that
  141. String from the command line (like in <<concat-api-key,this example>>), the
  142. `echo` command defaults to ASCII formatting, which is equivalent to UTF-8
  143. encoding.
  144. However, some other tools require an explicit encoding when converting a String
  145. into bytes. For example, in Java, you might use something like the following
  146. code, which assumes that `result` is the response from the create API key API.
  147. This conversion ensures that the bytes of the concatenated string is in UTF-8
  148. format:
  149. [source,java]
  150. ----
  151. var bytes = (result.id + ":" + result.api_key).getBytes(StandardCharsets.UTF_8);
  152. var header = "ApiKey " + Base64.getEncoder().encodeToString(bytes);
  153. ----
  154. ****
  155. On a Unix-like system, the following command combines the `id` and `api_key`
  156. from the previous response. The concatenation of these parameters should be in
  157. UTF-8 format:
  158. [[concat-api-key]]
  159. [source,shell]
  160. ----
  161. echo -n "VuaCfGcBCdbkQm-e5aOx:ui2lp2axTNmsyakw9tvNnw" | base64 <1>
  162. ----
  163. <1> Use `-n` so that the `echo` command doesn't print the trailing newline
  164. character
  165. The command outputs a Base64-encoded String:
  166. [source,shell]
  167. ----
  168. VnVhQ2ZHY0JDZGJrUW0tZTVhT3g6dWkybHAyYXhUTm1zeWFrdzl0dk5udw==
  169. ----
  170. Use this String in a request to authenticate with your cluster:
  171. [source,shell]
  172. ----
  173. curl -H "Authorization: ApiKey VnVhQ2ZHY0JDZGJrUW0tZTVhT3g6dWkybHAyYXhUTm1zeWFrdzl0dk5udw==" \
  174. http://localhost:9200/_cluster/health\?pretty
  175. ----
  176. // NOTCONSOLE