create-api-keys.asciidoc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. [role="xpack"]
  2. [[security-api-create-api-key]]
  3. === Create API key API
  4. ++++
  5. <titleabbrev>Create API keys</titleabbrev>
  6. ++++
  7. .New API reference
  8. [sidebar]
  9. --
  10. For the most up-to-date API details, refer to {api-es}/group/endpoint-security[Security APIs].
  11. --
  12. Creates an API key for access without requiring basic authentication.
  13. [[security-api-create-api-key-request]]
  14. ==== {api-request-title}
  15. `POST /_security/api_key`
  16. `PUT /_security/api_key`
  17. [[security-api-create-api-key-prereqs]]
  18. ==== {api-prereq-title}
  19. * To use this API, you must have at least the `manage_own_api_key` cluster privilege.
  20. IMPORTANT: If the credential that is used to authenticate this request is
  21. an API key, the derived API key cannot have any privileges. If you specify privileges, the API returns an error.
  22. See the note under <<api-key-role-descriptors,`role_descriptors`>>.
  23. [[security-api-create-api-key-desc]]
  24. ==== {api-description-title}
  25. The API keys are created by the {es} API key service, which is automatically enabled.
  26. For instructions on disabling the API key service, see <<api-key-service-settings>>.
  27. A successful request returns a JSON structure that contains the
  28. API key, its unique id, and its name. If applicable, it also returns expiration
  29. information for the API key in milliseconds.
  30. NOTE: By default, API keys never expire. You can specify expiration information
  31. when you create the API keys.
  32. See <<api-key-service-settings>> for configuration settings related to API key
  33. service.
  34. [[security-api-create-api-key-request-body]]
  35. ==== {api-request-body-title}
  36. The following parameters can be specified in the body of a POST or PUT request:
  37. `name`::
  38. (Required, string) Specifies the name for this API key.
  39. [[api-key-role-descriptors]]
  40. `role_descriptors`::
  41. (Optional, object) The role descriptors for this API
  42. key. This parameter is optional. When it is not specified or is an empty array,
  43. then the API key will have a _point in time snapshot of permissions of the
  44. authenticated user_. If you supply role descriptors then the resultant permissions
  45. would be an intersection of API keys permissions and authenticated user's permissions
  46. thereby limiting the access scope for API keys.
  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. +
  56. `applications`::: (list) A list of application privilege entries.
  57. `application` (required):::: (string) The name of the application to which this entry applies
  58. `privileges` (required):::: (list) A list of strings, where each element is the name of an application
  59. privilege or action.
  60. `resources` (required):::: (list) A list resources to which the privileges are applied.
  61. `cluster`::: (list) A list of cluster privileges. These privileges define the
  62. cluster level actions that API keys are able to execute.
  63. `global`::: (object) An object defining global privileges. A global privilege is
  64. a form of cluster privilege that is request-aware. Support for global privileges
  65. is currently limited to the management of application privileges.
  66. This field is optional.
  67. `indices`::: (list) A list of indices permissions entries.
  68. `field_security`:::: (object) The document fields that the API keys have
  69. read access to. For more information, see
  70. <<field-and-document-access-control>>.
  71. `names` (required):::: (list) A list of indices (or index name patterns) to which the
  72. permissions in this entry apply.
  73. `privileges`(required):::: (list) The index level privileges that the API keys
  74. have on the specified indices.
  75. `query`:::: A search query that defines the documents the API keys have
  76. read access to. A document within the specified indices must match this query in
  77. order for it to be accessible by the API keys.
  78. `metadata`::: (object) Optional meta-data. Within the `metadata` object, keys
  79. that begin with `_` are reserved for system usage.
  80. `restriction`::: (object) Optional restriction for when the role descriptor is allowed to be effective. For more information, see
  81. <<role-restriction>>.
  82. `workflows`:::: (list) A list of workflows to which the API key is restricted.
  83. For a full list see <<workflows-restriction>>.
  84. +
  85. --
  86. NOTE: In order to use role restriction, an API key must be created with a *single role descriptor*.
  87. --
  88. +
  89. `run_as`::: (list) A list of users that the API keys can impersonate.
  90. For more information, see
  91. <<run-as-privilege>>.
  92. `expiration`::
  93. (Optional, string) Expiration time for the API key. By default, API keys never
  94. expire.
  95. `metadata`::
  96. (Optional, object) Arbitrary metadata that you want to associate with the API key.
  97. It supports nested data structure.
  98. Within the `metadata` object, keys beginning with `_` are reserved for
  99. system usage.
  100. [[security-api-create-api-key-example]]
  101. ==== {api-examples-title}
  102. The following example creates an API key:
  103. [source,console]
  104. ----
  105. POST /_security/api_key
  106. {
  107. "name": "my-api-key",
  108. "expiration": "1d", <1>
  109. "role_descriptors": { <2>
  110. "role-a": {
  111. "cluster": ["all"],
  112. "indices": [
  113. {
  114. "names": ["index-a*"],
  115. "privileges": ["read"]
  116. }
  117. ]
  118. },
  119. "role-b": {
  120. "cluster": ["all"],
  121. "indices": [
  122. {
  123. "names": ["index-b*"],
  124. "privileges": ["all"]
  125. }
  126. ]
  127. }
  128. },
  129. "metadata": {
  130. "application": "my-application",
  131. "environment": {
  132. "level": 1,
  133. "trusted": true,
  134. "tags": ["dev", "staging"]
  135. }
  136. }
  137. }
  138. ----
  139. <1> Optional expiration for the API key being generated. If expiration is not
  140. provided then the API keys do not expire.
  141. <2> Optional role descriptors for this API key. If not provided, permissions
  142. of the authenticated user are applied.
  143. A successful call returns a JSON structure that provides
  144. API key information.
  145. [source,console-result]
  146. ----
  147. {
  148. "id": "VuaCfGcBCdbkQm-e5aOx", <1>
  149. "name": "my-api-key",
  150. "expiration": 1544068612110, <2>
  151. "api_key": "ui2lp2axTNmsyakw9tvNnw", <3>
  152. "encoded": "VnVhQ2ZHY0JDZGJrUW0tZTVhT3g6dWkybHAyYXhUTm1zeWFrdzl0dk5udw==" <4>
  153. }
  154. ----
  155. // TESTRESPONSE[s/VuaCfGcBCdbkQm-e5aOx/$body.id/]
  156. // TESTRESPONSE[s/1544068612110/$body.expiration/]
  157. // TESTRESPONSE[s/ui2lp2axTNmsyakw9tvNnw/$body.api_key/]
  158. // TESTRESPONSE[s/VnVhQ2ZHY0JDZGJrUW0tZTVhT3g6dWkybHAyYXhUTm1zeWFrdzl0dk5udw==/$body.encoded/]
  159. <1> Unique `id` for this API key
  160. <2> Optional expiration in milliseconds for this API key
  161. <3> Generated API key
  162. <4> API key credentials which is the Base64-encoding of the UTF-8
  163. representation of the `id` and `api_key` joined by a colon (`:`).
  164. To use the generated API key, send a request with an `Authorization` header that
  165. contains an `ApiKey` prefix followed by the API key credentials
  166. (the `encoded` value from the response).
  167. [source,shell]
  168. ----
  169. curl -H "Authorization: ApiKey VnVhQ2ZHY0JDZGJrUW0tZTVhT3g6dWkybHAyYXhUTm1zeWFrdzl0dk5udw==" \
  170. http://localhost:9200/_cluster/health\?pretty <1>
  171. ----
  172. // NOTCONSOLE
  173. <1> If your node has `xpack.security.http.ssl.enabled` set to `true`, then you
  174. must specify `https` when creating your API key
  175. On a Unix-like system, the `encoded` value can be created with the following
  176. command:
  177. [[concat-api-key]]
  178. [source,shell]
  179. ----
  180. echo -n "VuaCfGcBCdbkQm-e5aOx:ui2lp2axTNmsyakw9tvNnw" | base64 <1>
  181. ----
  182. <1> Use `-n` so that the `echo` command doesn't print the trailing newline
  183. character
  184. //tag::create-api-key-with-role-restriction-example[]
  185. The following example creates an API key with a <<role-restriction, restriction>> to the `search_application_query` workflow,
  186. which allows to call only <<search-application-search, Search Application Search API>>:
  187. [source,console]
  188. ----
  189. POST /_security/api_key
  190. {
  191. "name": "my-restricted-api-key",
  192. "role_descriptors": {
  193. "my-restricted-role-descriptor": {
  194. "indices": [
  195. {
  196. "names": ["my-search-app"],
  197. "privileges": ["read"]
  198. }
  199. ],
  200. "restriction": {
  201. "workflows": ["search_application_query"]
  202. }
  203. }
  204. }
  205. }
  206. ----
  207. //end::create-api-key-with-role-restriction-example[]