create-service-token.asciidoc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. [role="xpack"]
  2. [[security-api-create-service-token]]
  3. === Create service account token API
  4. ++++
  5. <titleabbrev>Create service account tokens</titleabbrev>
  6. ++++
  7. .New API reference
  8. [sidebar]
  9. --
  10. For the most up-to-date API details, refer to {api-es}/group/endpoint-security[Security APIs].
  11. --
  12. Creates a <<service-accounts,service accounts>> token for access without requiring basic
  13. authentication.
  14. [[security-api-create-service-token-request]]
  15. ==== {api-request-title}
  16. `POST /_security/service/<namespace>/<service>/credential/token/<token_name>`
  17. `PUT /_security/service/<namespace>/<service>/credential/token/<token_name>`
  18. `POST /_security/service/<namespace>/<service>/credential/token`
  19. [[security-api-create-service-token-prereqs]]
  20. ==== {api-prereq-title}
  21. * To use this API, you must have at least the `manage_service_account`
  22. <<privileges-list-cluster,cluster privilege>>.
  23. [[security-api-create-service-token-desc]]
  24. ==== {api-description-title}
  25. A successful create service account token API call returns a JSON structure
  26. that contains the service account token, its name, and its secret value.
  27. NOTE: Service account tokens never expire. You must actively <<security-api-delete-service-token,delete>> them if they are no longer needed.
  28. [[security-api-create-service-token-path-params]]
  29. ==== {api-path-parms-title}
  30. `namespace`::
  31. (Required, string) Name of the namespace.
  32. `service`::
  33. (Required, string) Name of the service name.
  34. `token_name`::
  35. (Optional, string) Name for the service account token. If omitted, a random name will be generated.
  36. +
  37. --
  38. Token names must be at least 1 and no more than 256 characters. They can contain
  39. alphanumeric characters (`a-z`, `A-Z`, `0-9`), dashes (`-`), and underscores
  40. (`_`), but cannot begin with an underscore.
  41. NOTE: Token names must be unique in the context of the associated service
  42. account. They must also be globally unique with their fully qualified names,
  43. which are comprised of the service account principal and token name, such as
  44. `<namespace>/<service>/<token-name>`.
  45. --
  46. [[security-api-create-service-token-example]]
  47. ==== {api-examples-title}
  48. The following request creates a service account token:
  49. [source,console]
  50. ----
  51. POST /_security/service/elastic/fleet-server/credential/token/token1
  52. ----
  53. The response includes the service account token, its name, and its secret value:
  54. [source,console-result]
  55. ----
  56. {
  57. "created": true,
  58. "token": {
  59. "name": "token1",
  60. "value": "AAEAAWVsYXN0aWM...vZmxlZXQtc2VydmVyL3Rva2VuMTo3TFdaSDZ" <1>
  61. }
  62. }
  63. ----
  64. // TESTRESPONSE[s/AAEAAWVsYXN0aWM...vZmxlZXQtc2VydmVyL3Rva2VuMTo3TFdaSDZ/$body.token.value/]
  65. <1> The secret value to use as a bearer token
  66. To use the service account token, include the generated token value in a
  67. request with an `Authorization: Bearer` header:
  68. [source,shell]
  69. ----
  70. curl -H "Authorization: Bearer AAEAAWVsYXN0aWM...vZmxlZXQtc2VydmVyL3Rva2VuMTo3TFdaSDZ" http://localhost:9200/_cluster/health
  71. ----
  72. // NOTCONSOLE
  73. NOTE: If your node has `xpack.security.http.ssl.enabled` set to `true`, then
  74. you must specify `https` in the request URL.
  75. The following request creates a service token with an auto-generated token name:
  76. [source,console]
  77. ----
  78. POST /_security/service/elastic/fleet-server/credential/token
  79. ----
  80. The response includes the service account token, its auto-generated name, and
  81. its secret value:
  82. [source,console-result]
  83. ----
  84. {
  85. "created": true,
  86. "token": {
  87. "name": "Jk5J1HgBuyBK5TpDrdo4",
  88. "value": "AAEAAWVsYXN0aWM...vZmxlZXQtc2VydmVyL3Rva2VuMTo3TFdaSDZ"
  89. }
  90. }
  91. ----
  92. // TESTRESPONSE[s/Jk5J1HgBuyBK5TpDrdo4/$body.token.name/]
  93. // TESTRESPONSE[s/AAEAAWVsYXN0aWM...vZmxlZXQtc2VydmVyL3Rva2VuMTo3TFdaSDZ/$body.token.value/]