create-api-keys.asciidoc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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, array-of-role-descriptor) An array of 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. The structure of role descriptor is the same as the request for create role API.
  43. For more details, see <<security-api-put-role, create or update roles API>>.
  44. +
  45. --
  46. NOTE: Due to the way in which this permission intersection is calculated, it is not
  47. possible to create an API key that is a child of another API key, unless the derived
  48. key is created without any privileges. In this case, you must explicitly specify a
  49. role descriptor with no privileges. The derived API key can be used for
  50. authentication; it will not have authority to call {es} APIs.
  51. --
  52. `expiration`::
  53. (Optional, string) Expiration time for the API key. By default, API keys never
  54. expire.
  55. `metadata`::
  56. (Optional, object) Arbitrary metadata that you want to associate with the API key.
  57. It supports nested data structure.
  58. Within the `metadata` object, keys beginning with `_` are reserved for
  59. system usage.
  60. [[security-api-create-api-key-example]]
  61. ==== {api-examples-title}
  62. The following example creates an API key:
  63. [source,console]
  64. ----
  65. POST /_security/api_key
  66. {
  67. "name": "my-api-key",
  68. "expiration": "1d", <1>
  69. "role_descriptors": { <2>
  70. "role-a": {
  71. "cluster": ["all"],
  72. "index": [
  73. {
  74. "names": ["index-a*"],
  75. "privileges": ["read"]
  76. }
  77. ]
  78. },
  79. "role-b": {
  80. "cluster": ["all"],
  81. "index": [
  82. {
  83. "names": ["index-b*"],
  84. "privileges": ["all"]
  85. }
  86. ]
  87. }
  88. },
  89. "metadata": {
  90. "application": "my-application",
  91. "environment": {
  92. "level": 1,
  93. "trusted": true,
  94. "tags": ["dev", "staging"]
  95. }
  96. }
  97. }
  98. ----
  99. <1> Optional expiration for the API key being generated. If expiration is not
  100. provided then the API keys do not expire.
  101. <2> Optional role descriptors for this API key. If not provided, permissions
  102. of the authenticated user are applied.
  103. A successful call returns a JSON structure that provides
  104. API key information.
  105. [source,console-result]
  106. ----
  107. {
  108. "id": "VuaCfGcBCdbkQm-e5aOx", <1>
  109. "name": "my-api-key",
  110. "expiration": 1544068612110, <2>
  111. "api_key": "ui2lp2axTNmsyakw9tvNnw", <3>
  112. "encoded": "VnVhQ2ZHY0JDZGJrUW0tZTVhT3g6dWkybHAyYXhUTm1zeWFrdzl0dk5udw==" <4>
  113. }
  114. ----
  115. // TESTRESPONSE[s/VuaCfGcBCdbkQm-e5aOx/$body.id/]
  116. // TESTRESPONSE[s/1544068612110/$body.expiration/]
  117. // TESTRESPONSE[s/ui2lp2axTNmsyakw9tvNnw/$body.api_key/]
  118. // TESTRESPONSE[s/VnVhQ2ZHY0JDZGJrUW0tZTVhT3g6dWkybHAyYXhUTm1zeWFrdzl0dk5udw==/$body.encoded/]
  119. <1> Unique `id` for this API key
  120. <2> Optional expiration in milliseconds for this API key
  121. <3> Generated API key
  122. <4> API key credentials which is the Base64-encoding of the UTF-8
  123. representation of the `id` and `api_key` joined by a colon (`:`).
  124. To use the generated API key, send a request with an `Authorization` header that
  125. contains an `ApiKey` prefix followed by the API key credentials
  126. (the `encoded` value from the response).
  127. [source,shell]
  128. ----
  129. curl -H "Authorization: ApiKey VnVhQ2ZHY0JDZGJrUW0tZTVhT3g6dWkybHAyYXhUTm1zeWFrdzl0dk5udw==" \
  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. On a Unix-like system, the `encoded` value can be created with the following
  136. command:
  137. [[concat-api-key]]
  138. [source,shell]
  139. ----
  140. echo -n "VuaCfGcBCdbkQm-e5aOx:ui2lp2axTNmsyakw9tvNnw" | base64 <1>
  141. ----
  142. <1> Use `-n` so that the `echo` command doesn't print the trailing newline
  143. character