create-api-keys.asciidoc 7.9 KB

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