create-api-keys.asciidoc 3.7 KB

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