create-service-token.asciidoc 3.5 KB

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