create-api-keys.asciidoc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. [role="xpack"]
  2. [[security-api-create-api-key]]
  3. === Create API Key API
  4. Creates an API key for access without requiring basic authentication.
  5. ==== Request
  6. `POST /_security/api_key`
  7. `PUT /_security/api_key`
  8. ==== Description
  9. The API keys are created by the {es} API key service, which is automatically enabled
  10. when you configure TLS on the HTTP interface. See <<tls-http>>. Alternatively,
  11. you can explicitly enable the `xpack.security.authc.api_key.enabled` setting. When
  12. you are running in production mode, a bootstrap check prevents you from enabling
  13. the API key service unless you also enable TLS on the HTTP interface.
  14. A successful create API key API call returns a JSON structure that contains
  15. the unique id, the name to identify API key, the API key and the expiration if
  16. applicable for the API key in milliseconds.
  17. NOTE: By default API keys never expire. You can specify expiration at the time of
  18. creation for the API keys.
  19. See <<api-key-service-settings>> for configuration settings related to API key service.
  20. ==== Request Body
  21. The following parameters can be specified in the body of a POST or PUT request:
  22. `name`::
  23. (string) Specifies the name for this API key.
  24. `role_descriptors`::
  25. (array-of-role-descriptor) Optional array of role descriptor for this API key. The role descriptor
  26. must be a subset of permissions of the authenticated user. The structure of role
  27. descriptor is same as the request for create role API. For more details on role
  28. see <<security-api-roles, Role Management APIs>>.
  29. If the role descriptors are not provided then permissions of the authenticated user are applied.
  30. `expiration`::
  31. (string) Optional expiration time for the API key. By default API keys never expire.
  32. ==== Examples
  33. The following example creates an API key:
  34. [source, js]
  35. ------------------------------------------------------------
  36. POST /_security/api_key
  37. {
  38. "name": "my-api-key",
  39. "expiration": "1d", <1>
  40. "role_descriptors": { <2>
  41. "role-a": {
  42. "cluster": ["all"],
  43. "index": [
  44. {
  45. "names": ["index-a*"],
  46. "privileges": ["read"]
  47. }
  48. ]
  49. },
  50. "role-b": {
  51. "cluster": ["all"],
  52. "index": [
  53. {
  54. "names": ["index-b*"],
  55. "privileges": ["all"]
  56. }
  57. ]
  58. }
  59. }
  60. }
  61. ------------------------------------------------------------
  62. // CONSOLE
  63. <1> optional expiration for the API key being generated. If expiration is not
  64. provided then the API keys do not expire.
  65. <2> optional role descriptors for this API key, if not provided then permissions
  66. of authenticated user are applied.
  67. A successful call returns a JSON structure that provides
  68. API key information.
  69. [source,js]
  70. --------------------------------------------------
  71. {
  72. "id":"VuaCfGcBCdbkQm-e5aOx", <1>
  73. "name":"my-api-key",
  74. "expiration":1544068612110, <2>
  75. "api_key":"ui2lp2axTNmsyakw9tvNnw" <3>
  76. }
  77. --------------------------------------------------
  78. // TESTRESPONSE[s/VuaCfGcBCdbkQm-e5aOx/$body.id/]
  79. // TESTRESPONSE[s/1544068612110/$body.expiration/]
  80. // TESTRESPONSE[s/ui2lp2axTNmsyakw9tvNnw/$body.api_key/]
  81. <1> unique id for this API key
  82. <2> optional expiration in milliseconds for this API key
  83. <3> generated API key
  84. The API key returned by this API can then be used by sending a request with a
  85. `Authorization` header with a value having the prefix `ApiKey ` followed
  86. by the _credentials_, where _credentials_ is the base64 encoding of `id` and `api_key` joined by a colon.
  87. [source,shell]
  88. --------------------------------------------------
  89. curl -H "Authorization: ApiKey VnVhQ2ZHY0JDZGJrUW0tZTVhT3g6dWkybHAyYXhUTm1zeWFrdzl0dk5udw==" http://localhost:9200/_cluster/health
  90. --------------------------------------------------
  91. // NOTCONSOLE