get-api-keys.asciidoc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. [role="xpack"]
  2. [[security-api-get-api-key]]
  3. === Get API key information API
  4. ++++
  5. <titleabbrev>Get API key information</titleabbrev>
  6. ++++
  7. Retrieves information for one or more API keys.
  8. [[security-api-get-api-key-request]]
  9. ==== {api-request-title}
  10. `GET /_security/api_key`
  11. [[security-api-get-api-key-prereqs]]
  12. ==== {api-prereq-title}
  13. * To use this API, you must have at least the `manage_api_key` cluster privilege.
  14. [[security-api-get-api-key-desc]]
  15. ==== {api-description-title}
  16. The information for the API keys created by
  17. <<security-api-create-api-key,create API Key>> can be retrieved using this API.
  18. [[security-api-get-api-key-path-params]]
  19. ==== {api-path-parms-title}
  20. The following parameters can be specified in the query parameters of a GET request and
  21. pertain to retrieving api keys:
  22. `id`::
  23. (Optional, string) An API key id. This parameter cannot be used with any of
  24. `name`, `realm_name` or `username` are used.
  25. `name`::
  26. (Optional, string) An API key name. This parameter cannot be used with any of
  27. `id`, `realm_name` or `username` are used. It supports prefix search with wildcard.
  28. `realm_name`::
  29. (Optional, string) The name of an authentication realm. This parameter cannot be
  30. used with either `id` or `name` or when `owner` flag is set to `true`.
  31. `username`::
  32. (Optional, string) The username of a user. This parameter cannot be used with
  33. either `id` or `name` or when `owner` flag is set to `true`.
  34. `owner`::
  35. (Optional, boolean) A boolean flag that can be used to query API keys owned
  36. by the currently authenticated user. Defaults to false.
  37. The 'realm_name' or 'username' parameters cannot be specified when this
  38. parameter is set to 'true' as they are assumed to be the currently authenticated ones.
  39. NOTE: When none of the parameters "id", "name", "username" and "realm_name"
  40. are specified, and the "owner" is set to false then it will retrieve all API
  41. keys if the user is authorized. If the user is not authorized to retrieve other user's
  42. API keys, then an error will be returned.
  43. [[security-api-get-api-key-example]]
  44. ==== {api-examples-title}
  45. If you create an API key as follows:
  46. [source,console]
  47. ------------------------------------------------------------
  48. POST /_security/api_key
  49. {
  50. "name": "my-api-key",
  51. "role_descriptors": {}
  52. }
  53. ------------------------------------------------------------
  54. A successful call returns a JSON structure that provides
  55. API key information. For example:
  56. [source,console-result]
  57. --------------------------------------------------
  58. {
  59. "id":"VuaCfGcBCdbkQm-e5aOx",
  60. "name":"my-api-key",
  61. "api_key":"ui2lp2axTNmsyakw9tvNnw"
  62. }
  63. --------------------------------------------------
  64. // TESTRESPONSE[s/VuaCfGcBCdbkQm-e5aOx/$body.id/]
  65. // TESTRESPONSE[s/ui2lp2axTNmsyakw9tvNnw/$body.api_key/]
  66. You can use the following example to retrieve the API key by ID:
  67. [source,console]
  68. --------------------------------------------------
  69. GET /_security/api_key?id=VuaCfGcBCdbkQm-e5aOx
  70. --------------------------------------------------
  71. // TEST[s/VuaCfGcBCdbkQm-e5aOx/$body.id/]
  72. // TEST[continued]
  73. You can use the following example to retrieve the API key by name:
  74. [source,console]
  75. --------------------------------------------------
  76. GET /_security/api_key?name=my-api-key
  77. --------------------------------------------------
  78. // TEST[continued]
  79. API key name supports prefix search by using wildcard:
  80. [source,console]
  81. --------------------------------------------------
  82. GET /_security/api_key?name=my-*
  83. --------------------------------------------------
  84. // TEST[continued]
  85. The following example retrieves all API keys for the `native1` realm:
  86. [source,console]
  87. --------------------------------------------------
  88. GET /_security/api_key?realm_name=native1
  89. --------------------------------------------------
  90. // TEST[continued]
  91. The following example retrieves all API keys for the user `myuser` in all realms:
  92. [source,console]
  93. --------------------------------------------------
  94. GET /_security/api_key?username=myuser
  95. --------------------------------------------------
  96. // TEST[continued]
  97. The following example retrieves all API keys owned by the currently authenticated user:
  98. [source,console]
  99. --------------------------------------------------
  100. GET /_security/api_key?owner=true
  101. --------------------------------------------------
  102. // TEST[continued]
  103. The following example retrieves all API keys if the user is authorized to do so:
  104. [source,console]
  105. --------------------------------------------------
  106. GET /_security/api_key
  107. --------------------------------------------------
  108. // TEST[continued]
  109. Following creates an API key
  110. [source,console]
  111. ------------------------------------------------------------
  112. POST /_security/api_key
  113. {
  114. "name": "my-api-key-1"
  115. }
  116. ------------------------------------------------------------
  117. The following example retrieves the API key identified by the specified `id` if
  118. it is owned by the currently authenticated user:
  119. [source,console]
  120. --------------------------------------------------
  121. GET /_security/api_key?id=VuaCfGcBCdbkQm-e5aOx&owner=true
  122. --------------------------------------------------
  123. // TEST[s/VuaCfGcBCdbkQm-e5aOx/$body.id/]
  124. // TEST[continued]
  125. Finally, the following example retrieves all API keys for the user `myuser` in
  126. the `native1` realm immediately:
  127. [source,console]
  128. --------------------------------------------------
  129. GET /_security/api_key?username=myuser&realm_name=native1
  130. --------------------------------------------------
  131. // TEST[continued]
  132. A successful call returns a JSON structure that contains the information of one or more API keys that were retrieved.
  133. [source,js]
  134. --------------------------------------------------
  135. {
  136. "api_keys": [ <1>
  137. {
  138. "id": "dGhpcyBpcyBub3QgYSByZWFsIHRva2VuIGJ1dCBpdCBpcyBvbmx5IHRlc3QgZGF0YS4gZG8gbm90IHRyeSB0byByZWFkIHRva2VuIQ==", <2>
  139. "name": "hadoop_myuser_key", <3>
  140. "creation": 1548550550158, <4>
  141. "expiration": 1548551550158, <5>
  142. "invalidated": false, <6>
  143. "username": "myuser", <7>
  144. "realm": "native1" <8>
  145. },
  146. {
  147. "id": "api-key-id-2",
  148. "name": "api-key-name-2",
  149. "creation": 1548550550158,
  150. "invalidated": false,
  151. "username": "user-y",
  152. "realm": "realm-2"
  153. }
  154. ]
  155. }
  156. --------------------------------------------------
  157. // NOTCONSOLE
  158. <1> The list of API keys that were retrieved for this request.
  159. <2> Id for the API key
  160. <3> Name of the API key
  161. <4> Creation time for the API key in milliseconds
  162. <5> Optional expiration time for the API key in milliseconds
  163. <6> Invalidation status for the API key. If the key has been invalidated, it has
  164. a value of `true`. Otherwise, it is `false`.
  165. <7> Principal for which this API key was created
  166. <8> Realm name of the principal for which this API key was created