get-api-keys.asciidoc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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_own_api_key` or the
  14. `read_security` cluster privileges.
  15. ** If you only have the `manage_own_api_key` privilege, this API only returns
  16. the API keys that you own.
  17. +
  18. NOTE: Authenticating with an API key that has the `manage_own_api_key` privilege
  19. does not allow retrieving the authenticated user's own keys. Instead,
  20. authenticate the user with basic credentials.
  21. ** If you have `read_security`, `manage_api_key` or greater
  22. privileges (including `manage_security`), this API returns all API keys
  23. regardless of ownership.
  24. [[security-api-get-api-key-desc]]
  25. ==== {api-description-title}
  26. The information for the API keys created by
  27. <<security-api-create-api-key,create API Key>> can be retrieved using this API.
  28. [[security-api-get-api-key-query-params]]
  29. ==== {api-path-parms-title}
  30. The following parameters can be specified in the query parameters of a GET request and
  31. pertain to retrieving api keys:
  32. `id`::
  33. (Optional, string) An API key id. This parameter cannot be used with any of
  34. `name`, `realm_name` or `username` are used.
  35. `name`::
  36. (Optional, string) An API key name. This parameter cannot be used with any of
  37. `id`, `realm_name` or `username` are used. It supports prefix search with wildcard.
  38. `realm_name`::
  39. (Optional, string) The name of an authentication realm. This parameter cannot be
  40. used with either `id` or `name` or when `owner` flag is set to `true`.
  41. `username`::
  42. (Optional, string) The username of a user. This parameter cannot be used with
  43. either `id` or `name` or when `owner` flag is set to `true`.
  44. `owner`::
  45. (Optional, Boolean) A boolean flag that can be used to query API keys owned
  46. by the currently authenticated user. Defaults to false.
  47. The 'realm_name' or 'username' parameters cannot be specified when this
  48. parameter is set to 'true' as they are assumed to be the currently authenticated ones.
  49. `with_limited_by`::
  50. (Optional, Boolean) A boolean flag to return the snapshot of the owner user's role descriptors
  51. associated with the API key. An API key's actual permission is the intersection of
  52. its <<api-key-role-descriptors,assigned role descriptors>> and the owner user's role descriptors
  53. (effectively limited by it). An API key must have `manage_api_key` or higher privileges to retrieve the limited-by role descriptors of any API key, including itself.
  54. `active_only`::
  55. (Optional, Boolean) A boolean flag that can be used to query API keys that are currently active.
  56. An API key is considered active if it is neither invalidated, nor expired at query time. You can specify this together
  57. with other parameters such as `owner` or `name`. If `active_only` is `false`, the response will
  58. include both active and inactive (expired or invalidated) keys. Defaults to `false`.
  59. NOTE: When none of the parameters "id", "name", "username" and "realm_name"
  60. are specified, and the "owner" is set to false then it will retrieve all API
  61. keys if the user is authorized. If the user is not authorized to retrieve other user's
  62. API keys, then an error will be returned.
  63. [[security-api-get-api-key-example]]
  64. ==== {api-examples-title}
  65. If you create an API key as follows:
  66. [source,console]
  67. ------------------------------------------------------------
  68. POST /_security/api_key
  69. {
  70. "name": "my-api-key",
  71. "role_descriptors": {},
  72. "metadata": {
  73. "application": "myapp"
  74. }
  75. }
  76. ------------------------------------------------------------
  77. A successful call returns a JSON structure that provides
  78. API key information. For example:
  79. [source,console-result]
  80. --------------------------------------------------
  81. {
  82. "id":"VuaCfGcBCdbkQm-e5aOx",
  83. "name":"my-api-key",
  84. "api_key":"ui2lp2axTNmsyakw9tvNnw",
  85. "encoded": "VnVhQ2ZHY0JDZGJrUW0tZTVhT3g6dWkybHAyYXhUTm1zeWFrdzl0dk5udw=="
  86. }
  87. --------------------------------------------------
  88. // TESTRESPONSE[s/VuaCfGcBCdbkQm-e5aOx/$body.id/]
  89. // TESTRESPONSE[s/ui2lp2axTNmsyakw9tvNnw/$body.api_key/]
  90. // TESTRESPONSE[s/VnVhQ2ZHY0JDZGJrUW0tZTVhT3g6dWkybHAyYXhUTm1zeWFrdzl0dk5udw==/$body.encoded/]
  91. You can use the following example to retrieve the API key by ID:
  92. [source,console]
  93. --------------------------------------------------
  94. GET /_security/api_key?id=VuaCfGcBCdbkQm-e5aOx&with_limited_by=true
  95. --------------------------------------------------
  96. // TEST[s/VuaCfGcBCdbkQm-e5aOx/$body.id/]
  97. // TEST[continued]
  98. A successful call returns a JSON structure that contains the information of the API key:
  99. [source,js]
  100. --------------------------------------------------
  101. {
  102. "api_keys": [ <1>
  103. {
  104. "id": "VuaCfGcBCdbkQm-e5aOx", <2>
  105. "name": "my-api-key", <3>
  106. "creation": 1548550550158, <4>
  107. "expiration": 1548551550158, <5>
  108. "invalidated": false, <6>
  109. "username": "myuser", <7>
  110. "realm": "native1", <8>
  111. "realm_type": "native",
  112. "metadata": { <9>
  113. "application": "myapp"
  114. },
  115. "role_descriptors": { }, <10>
  116. "limited_by": [ <11>
  117. {
  118. "role-power-user": {
  119. "cluster": [
  120. "monitor"
  121. ],
  122. "indices": [
  123. {
  124. "names": [
  125. "*"
  126. ],
  127. "privileges": [
  128. "read"
  129. ],
  130. "allow_restricted_indices": false
  131. }
  132. ],
  133. "applications": [ ],
  134. "run_as": [ ],
  135. "metadata": { },
  136. "transient_metadata": {
  137. "enabled": true
  138. }
  139. }
  140. }
  141. ]
  142. }
  143. ]
  144. }
  145. --------------------------------------------------
  146. // NOTCONSOLE
  147. <1> The list of API keys that were retrieved for this request.
  148. <2> Id for the API key
  149. <3> Name of the API key
  150. <4> Creation time for the API key in milliseconds
  151. <5> Optional expiration time for the API key in milliseconds
  152. <6> Invalidation status for the API key. If the key has been invalidated, it has
  153. a value of `true` and an additional field with the `invalidation` time in milliseconds. Otherwise, it is `false`.
  154. <7> Principal for which this API key was created
  155. <8> Realm name of the principal for which this API key was created
  156. <9> Metadata of the API key
  157. <10> The role descriptors assigned to this API key when it was <<api-key-role-descriptors,created>>
  158. or last <<security-api-update-api-key-api-key-role-descriptors,updated>>.
  159. An empty role descriptor means the API key inherits the owner user's
  160. permissions.
  161. <11> The owner user's permissions associated with the API key.
  162. It is a point-in-time snapshot captured at <<security-api-create-api-key,creation>> and
  163. subsequent <<security-api-update-api-key,updates>>. An API key's
  164. effective permissions are an intersection of its assigned privileges and
  165. the owner user's permissions.
  166. You can use the following example to retrieve the API key by name:
  167. [source,console]
  168. --------------------------------------------------
  169. GET /_security/api_key?name=my-api-key
  170. --------------------------------------------------
  171. // TEST[continued]
  172. API key name supports prefix search by using wildcard:
  173. [source,console]
  174. --------------------------------------------------
  175. GET /_security/api_key?name=my-*
  176. --------------------------------------------------
  177. // TEST[continued]
  178. The following example retrieves all API keys for the `native1` realm:
  179. [source,console]
  180. --------------------------------------------------
  181. GET /_security/api_key?realm_name=native1
  182. --------------------------------------------------
  183. // TEST[continued]
  184. The following example retrieves all API keys for the user `myuser` in all realms:
  185. [source,console]
  186. --------------------------------------------------
  187. GET /_security/api_key?username=myuser
  188. --------------------------------------------------
  189. // TEST[continued]
  190. The following example retrieves all API keys owned by the currently authenticated user:
  191. [source,console]
  192. --------------------------------------------------
  193. GET /_security/api_key?owner=true
  194. --------------------------------------------------
  195. // TEST[continued]
  196. The following example retrieves all API keys if the user is authorized to do so:
  197. [source,console]
  198. --------------------------------------------------
  199. GET /_security/api_key
  200. --------------------------------------------------
  201. // TEST[continued]
  202. The following example retrieves all active API keys if the user is authorized to do so:
  203. [source,console]
  204. --------------------------------------------------
  205. GET /_security/api_key?active_only=true
  206. --------------------------------------------------
  207. // TEST[continued]
  208. Following creates an API key
  209. [source,console]
  210. ------------------------------------------------------------
  211. POST /_security/api_key
  212. {
  213. "name": "my-api-key-1",
  214. "metadata": {
  215. "application": "my-application"
  216. }
  217. }
  218. ------------------------------------------------------------
  219. The following example retrieves the API key identified by the specified `id` if
  220. it is owned by the currently authenticated user:
  221. [source,console]
  222. --------------------------------------------------
  223. GET /_security/api_key?id=VuaCfGcBCdbkQm-e5aOx&owner=true
  224. --------------------------------------------------
  225. // TEST[s/VuaCfGcBCdbkQm-e5aOx/$body.id/]
  226. // TEST[continued]
  227. Finally, the following example retrieves all API keys for the user `myuser` in
  228. the `native1` realm immediately:
  229. [source,console]
  230. --------------------------------------------------
  231. GET /_security/api_key?username=myuser&realm_name=native1
  232. --------------------------------------------------
  233. // TEST[continued]
  234. A successful call returns a JSON structure that contains the information of one or more API keys that were retrieved.
  235. [source,js]
  236. --------------------------------------------------
  237. {
  238. "api_keys": [
  239. {
  240. "id": "0GF5GXsBCXxz2eDxWwFN",
  241. "name": "hadoop_myuser_key",
  242. "creation": 1548550550158,
  243. "expiration": 1548551550158,
  244. "invalidated": false,
  245. "username": "myuser",
  246. "realm": "native1",
  247. "realm_type": "native",
  248. "metadata": {
  249. "application": "myapp"
  250. },
  251. "role_descriptors": {
  252. "role-a": {
  253. "cluster": [
  254. "monitor"
  255. ],
  256. "indices": [
  257. {
  258. "names": [
  259. "index-a"
  260. ],
  261. "privileges": [
  262. "read"
  263. ],
  264. "allow_restricted_indices": false
  265. }
  266. ],
  267. "applications": [ ],
  268. "run_as": [ ],
  269. "metadata": { },
  270. "transient_metadata": {
  271. "enabled": true
  272. }
  273. }
  274. }
  275. },
  276. {
  277. "id": "6wHJmcQpReKBa42EHV5SBw",
  278. "name": "api-key-name-2",
  279. "creation": 1548550550158,
  280. "invalidated": false,
  281. "username": "user-y",
  282. "realm": "realm-2",
  283. "metadata": {},
  284. "role_descriptors": { }
  285. }
  286. ]
  287. }
  288. --------------------------------------------------
  289. // NOTCONSOLE