create-api-keys.asciidoc 4.2 KB

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