grant-api-keys.asciidoc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. [role="xpack"]
  2. [[security-api-grant-api-key]]
  3. === Grant API key API
  4. ++++
  5. <titleabbrev>Grant API keys</titleabbrev>
  6. ++++
  7. Creates an API key on behalf of another user.
  8. [[security-api-grant-api-key-request]]
  9. ==== {api-request-title}
  10. `POST /_security/api_key/grant`
  11. [[security-api-grant-api-key-prereqs]]
  12. ==== {api-prereq-title}
  13. * To use this API, you must have the `grant_api_key` cluster privilege.
  14. [[security-api-grant-api-key-desc]]
  15. ==== {api-description-title}
  16. This API is similar to <<security-api-create-api-key>>, however it creates the
  17. API key for a user that is different than the user that runs the API.
  18. The caller must have authentication credentials (either an access token,
  19. or a username and password) for the user on whose behalf the API key will be
  20. created. It is not possible to use this API to create an API key without that
  21. user's credentials.
  22. The user, for whom the authentication credentials is provided,
  23. can optionally <<run-as-privilege,"run as">> (impersonate) another user.
  24. In this case, the API key will be created on behalf of the impersonated user.
  25. This API is intended be used by applications that need to create and manage
  26. API keys for end users, but cannot guarantee that those users have permission
  27. to create API keys on their own behalf (see <<security-api-create-api-key-prereqs>>).
  28. The API keys are created by the {es} API key service, which is automatically
  29. enabled.
  30. A successful grant API key API call returns a JSON structure that contains the
  31. API key, its unique id, and its name. If applicable, it also returns expiration
  32. information for the API key in milliseconds.
  33. NOTE: By default, API keys never expire. You can specify expiration information
  34. when you create the API keys.
  35. See <<api-key-service-settings>> for configuration settings related to API key
  36. service.
  37. [[security-api-grant-api-key-request-body]]
  38. ==== {api-request-body-title}
  39. The following parameters can be specified in the body of a POST request:
  40. `access_token`::
  41. (Required*, string)
  42. The user's access token. If you specify the `access_token` grant type, this
  43. parameter is required. It is not valid with other grant types.
  44. `api_key`::
  45. (Required, object)
  46. Defines the API key.
  47. `expiration`:::
  48. (Optional, string) Expiration time for the API key. By default, API keys never
  49. expire.
  50. `name`:::
  51. (Required, string) Specifies the name for this API key.
  52. `role_descriptors`:::
  53. (Optional, object) The role descriptors for this API
  54. key. This parameter is optional. When it is not specified or is an empty array,
  55. the API key has a point in time snapshot of permissions of the specified user or
  56. access token. If you supply role descriptors, the resultant permissions are an
  57. intersection of API keys permissions and the permissions of the user or access
  58. token. The structure of a role descriptor is the same as the request for <<api-key-role-descriptors, create API keys API>>.
  59. `metadata`:::
  60. (Optional, object) Arbitrary metadata that you want to associate with the API key.
  61. It supports nested data structure.
  62. Within the `metadata` object, keys beginning with `_` are reserved for
  63. system usage.
  64. `grant_type`::
  65. (Required, string)
  66. The type of grant. Supported grant types are: `access_token`,`password`.
  67. `access_token`:::
  68. (Required*, string)
  69. In this type of grant, you must supply an access token that was created by the
  70. {es} token service. For more information, see
  71. <<security-api-get-token>> and <<encrypt-http-communication>>.
  72. `password`:::
  73. In this type of grant, you must supply the user ID and password for which you
  74. want to create the API key.
  75. `password`::
  76. (Optional*, string)
  77. The user's password. If you specify the `password` grant type, this parameter is
  78. required. It is not valid with other grant types.
  79. `username`::
  80. (Optional*, string)
  81. The user name that identifies the user. If you specify the `password` grant type,
  82. this parameter is required. It is not valid with other grant types.
  83. `run_as`::
  84. (Optional, string)
  85. The name of the user to be <<run-as-privilege,impersonated>>.
  86. [[security-api-grant-api-key-example]]
  87. ==== {api-examples-title}
  88. [source,console]
  89. ------------------------------------------------------------
  90. POST /_security/api_key/grant
  91. {
  92. "grant_type": "password",
  93. "username" : "test_admin",
  94. "password" : "x-pack-test-password",
  95. "api_key" : {
  96. "name": "my-api-key",
  97. "expiration": "1d",
  98. "role_descriptors": {
  99. "role-a": {
  100. "cluster": ["all"],
  101. "indices": [
  102. {
  103. "names": ["index-a*"],
  104. "privileges": ["read"]
  105. }
  106. ]
  107. },
  108. "role-b": {
  109. "cluster": ["all"],
  110. "indices": [
  111. {
  112. "names": ["index-b*"],
  113. "privileges": ["all"]
  114. }
  115. ]
  116. }
  117. },
  118. "metadata": {
  119. "application": "my-application",
  120. "environment": {
  121. "level": 1,
  122. "trusted": true,
  123. "tags": ["dev", "staging"]
  124. }
  125. }
  126. }
  127. }
  128. ------------------------------------------------------------
  129. The user (`test_admin`) whose credentials are provided can "run as" another user (`test_user`).
  130. The API key will be granted to the impersonated user (`test_user`).
  131. [source,console]
  132. ------------------------------------------------------------
  133. POST /_security/api_key/grant
  134. {
  135. "grant_type": "password",
  136. "username" : "test_admin", <1>
  137. "password" : "x-pack-test-password", <2>
  138. "run_as": "test_user", <3>
  139. "api_key" : {
  140. "name": "another-api-key"
  141. }
  142. }
  143. ------------------------------------------------------------
  144. <1> The user for which the credential is provided and performs "run as".
  145. <2> Credential for the above user
  146. <3> The impersonated user for whom the API key will be created for.