create-api-keys.asciidoc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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_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 `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. when you configure TLS on the HTTP interface. See <<encrypt-http-communication>>. Alternatively,
  22. you can explicitly enable the `xpack.security.authc.api_key.enabled` setting. When
  23. you are running in production mode, a bootstrap check prevents you from enabling
  24. the API key service unless you also enable TLS on the HTTP interface.
  25. A successful create API key API call returns a JSON structure that contains the
  26. API key, its unique id, and its name. If applicable, it also returns expiration
  27. information for the API key in milliseconds.
  28. NOTE: By default, API keys never expire. You can specify expiration information
  29. when you create the API keys.
  30. See <<api-key-service-settings>> for configuration settings related to API key
  31. service.
  32. [[security-api-create-api-key-request-body]]
  33. ==== {api-request-body-title}
  34. The following parameters can be specified in the body of a POST or PUT request:
  35. `name`::
  36. (Required, string) Specifies the name for this API key.
  37. `role_descriptors`::
  38. (Optional, array-of-role-descriptor) An array of role descriptors for this API
  39. key. This parameter is optional. When it is not specified or is an empty array,
  40. then the API key will have a _point in time snapshot of permissions of the
  41. authenticated user_. If you supply role descriptors then the resultant permissions
  42. would be an intersection of API keys permissions and authenticated user's permissions
  43. thereby limiting the access scope for API keys.
  44. The structure of role descriptor is the same as the request for create role API.
  45. For more details, see <<security-api-put-role, create or update roles API>>.
  46. +
  47. --
  48. NOTE: Due to the way in which this permission intersection is calculated, it is not
  49. possible to create an API key that is a child of another API key, unless the derived
  50. key is created without any privileges. In this case, you must explicitly specify a
  51. role descriptor with no privileges. The derived API key can be used for
  52. authentication; it will not have authority to call {es} APIs.
  53. --
  54. `expiration`::
  55. (Optional, string) Expiration time for the API key. By default, API keys never
  56. expire.
  57. [[security-api-create-api-key-example]]
  58. ==== {api-examples-title}
  59. The following example creates an API key:
  60. [source,console]
  61. ------------------------------------------------------------
  62. POST /_security/api_key
  63. {
  64. "name": "my-api-key",
  65. "expiration": "1d", <1>
  66. "role_descriptors": { <2>
  67. "role-a": {
  68. "cluster": ["all"],
  69. "index": [
  70. {
  71. "names": ["index-a*"],
  72. "privileges": ["read"]
  73. }
  74. ]
  75. },
  76. "role-b": {
  77. "cluster": ["all"],
  78. "index": [
  79. {
  80. "names": ["index-b*"],
  81. "privileges": ["all"]
  82. }
  83. ]
  84. }
  85. }
  86. }
  87. ------------------------------------------------------------
  88. <1> optional expiration for the API key being generated. If expiration is not
  89. provided then the API keys do not expire.
  90. <2> optional role descriptors for this API key, if not provided then permissions
  91. of authenticated user are applied.
  92. A successful call returns a JSON structure that provides
  93. API key information.
  94. [source,console-result]
  95. --------------------------------------------------
  96. {
  97. "id":"VuaCfGcBCdbkQm-e5aOx", <1>
  98. "name":"my-api-key",
  99. "expiration":1544068612110, <2>
  100. "api_key":"ui2lp2axTNmsyakw9tvNnw" <3>
  101. }
  102. --------------------------------------------------
  103. // TESTRESPONSE[s/VuaCfGcBCdbkQm-e5aOx/$body.id/]
  104. // TESTRESPONSE[s/1544068612110/$body.expiration/]
  105. // TESTRESPONSE[s/ui2lp2axTNmsyakw9tvNnw/$body.api_key/]
  106. <1> unique id for this API key
  107. <2> optional expiration in milliseconds for this API key
  108. <3> generated API key
  109. The API key returned by this API can then be used by sending a request with an
  110. `Authorization` header with a value having the prefix `ApiKey` followed
  111. by the _credentials_, where _credentials_ are the base64 encoding of `id` and `api_key` joined by a colon.
  112. NOTE: If your node has `xpack.security.http.ssl.enabled` set to `true`, then you must specify `https` when creating your API key.
  113. [source,shell]
  114. --------------------------------------------------
  115. curl -H "Authorization: ApiKey VnVhQ2ZHY0JDZGJrUW0tZTVhT3g6dWkybHAyYXhUTm1zeWFrdzl0dk5udw==" http://localhost:9200/_cluster/health
  116. --------------------------------------------------
  117. // NOTCONSOLE
  118. One way to create the credentials from CLI on an Unix-like system is as the follows:
  119. [source,shell]
  120. ----
  121. # Please note the use of "-n" to instruct echo to not print the trailing newline character.
  122. # It should return VnVhQ2ZHY0JDZGJrUW0tZTVhT3g6dWkybHAyYXhUTm1zeWFrdzl0dk5udw==
  123. echo -n "VuaCfGcBCdbkQm-e5aOx:ui2lp2axTNmsyakw9tvNnw" | base64
  124. ----