run-as-privilege.asciidoc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. [role="xpack"]
  2. [[run-as-privilege]]
  3. = Submitting requests on behalf of other users
  4. {es} roles support a `run_as` privilege that enables an authenticated user to
  5. submit requests on behalf of other users. For example, if your external
  6. application is trusted to authenticate users, {es} can authenticate the external
  7. application and use the _run as_ mechanism to issue authorized requests as
  8. other users without having to re-authenticate each user.
  9. To "run as" (impersonate) another user, the first user (the authenticating user)
  10. must be authenticated by a mechanism that supports run-as delegation. The second
  11. user (the `run_as` user) must be authorized by a mechanism that supports
  12. delegated run-as lookups by username.
  13. The `run_as` privilege essentially operates like a secondary form of
  14. <<authorization_realms,delegated authorization>>. Delegated authorization applies
  15. to the authenticating user, and the `run_as` privilege applies to the user who
  16. is being impersonated.
  17. Authenticating user::
  18. --
  19. For the authenticating user, the following realms (plus API keys) all support
  20. `run_as` delegation: `native`, `file`, Active Directory, JWT, Kerberos, LDAP and
  21. PKI.
  22. Service tokens, the {es} Token Service, SAML 2.0, and OIDC 1.0 do not
  23. support `run_as` delegation.
  24. --
  25. `run_as` user::
  26. --
  27. {es} supports `run_as` for any realm that supports user lookup.
  28. Not all realms support user lookup. Refer to the list of <<user-lookup,supported realms>>
  29. and ensure that the realm you wish to use is configured in a manner that
  30. supports user lookup.
  31. The `run_as` user must be retrieved from a <<realms,realm>> - it is not
  32. possible to run as a
  33. <<service-accounts,service account>>,
  34. <<token-authentication-api-key,API key>> or
  35. <<token-authentication-access-token,access token>>.
  36. --
  37. To submit requests on behalf of other users, you need to have the `run_as`
  38. privilege in your <<defining-roles,roles>>. For example, the following request
  39. creates a `my_director` role that grants permission to submit request on behalf
  40. of `jacknich` or `redeniro`:
  41. [source,console]
  42. ----
  43. POST /_security/role/my_director?refresh=true
  44. {
  45. "cluster": ["manage"],
  46. "indices": [
  47. {
  48. "names": [ "index1", "index2" ],
  49. "privileges": [ "manage" ]
  50. }
  51. ],
  52. "run_as": [ "jacknich", "rdeniro" ],
  53. "metadata" : {
  54. "version" : 1
  55. }
  56. }
  57. ----
  58. To submit a request as another user, you specify the user in the
  59. `es-security-runas-user` request header. For example:
  60. [source,sh]
  61. ----
  62. curl -H "es-security-runas-user: jacknich" -u es-admin -X GET http://localhost:9200/
  63. ----
  64. The `run_as` user passed in through the `es-security-runas-user` header must be
  65. available from a realm that supports delegated authorization lookup by username.
  66. Realms that don't support user lookup can't be used by `run_as` delegation from
  67. other realms.
  68. For example, JWT realms can authenticate external users specified in JWTs, and
  69. execute requests as a `run_as` user in the `native` realm. {es} will retrieve the
  70. indicated `runas` user and execute the request as that user using their roles.
  71. [[run-as-privilege-apply]]
  72. == Apply the `run_as` privilege to roles
  73. You can apply the `run_as` privilege when creating roles with the
  74. <<security-api-put-role,create or update roles API>>. Users who are assigned
  75. a role that contains the `run_as` privilege inherit all privileges from their
  76. role, and can also submit requests on behalf of the indicated users.
  77. NOTE: Roles for the authenticated user and the `run_as` user are not merged. If
  78. a user authenticates without specifying the `run_as` parameter, only the
  79. authenticated user's roles are used. If a user authenticates and their roles
  80. include the `run_as` parameter, only the `run_as` user's roles are used.
  81. After a user successfully authenticates to {es}, an authorization process determines whether the user behind an incoming request is allowed to run
  82. that request. If the authenticated user has the `run_as` privilege in their list
  83. of permissions and specifies the run-as header, {es} _discards_ the authenticated
  84. user and associated roles. It then looks in each of the configured realms in the
  85. realm chain until it finds the username that's associated with the `run_as` user,
  86. and uses those roles to execute any requests.
  87. Consider an admin role and an analyst role. The admin role has higher privileges,
  88. but might also want to submit requests as another user to test and verify their
  89. permissions.
  90. First, we'll create an admin role named `my_admin_role`. This role has `manage`
  91. <<security-privileges,privileges>> on the entire cluster, and on a subset of
  92. indices. This role also contains the `run_as` privilege, which enables any user
  93. with this role to submit requests on behalf of the specified `analyst_user`.
  94. [source,console]
  95. ----
  96. POST /_security/role/my_admin_role?refresh=true
  97. {
  98. "cluster": ["manage"],
  99. "indices": [
  100. {
  101. "names": [ "index1", "index2" ],
  102. "privileges": [ "manage" ]
  103. }
  104. ],
  105. "applications": [
  106. {
  107. "application": "myapp",
  108. "privileges": [ "admin", "read" ],
  109. "resources": [ "*" ]
  110. }
  111. ],
  112. "run_as": [ "analyst_user" ],
  113. "metadata" : {
  114. "version" : 1
  115. }
  116. }
  117. ----
  118. Next, we'll create an analyst role named `my_analyst_role`, which has more
  119. restricted `monitor` cluster privileges and `manage` privileges on a subset of
  120. indices.
  121. [source,console]
  122. ----
  123. POST /_security/role/my_analyst_role?refresh=true
  124. {
  125. "cluster": [ "monitor"],
  126. "indices": [
  127. {
  128. "names": [ "index1", "index2" ],
  129. "privileges": ["manage"]
  130. }
  131. ],
  132. "applications": [
  133. {
  134. "application": "myapp",
  135. "privileges": [ "read" ],
  136. "resources": [ "*" ]
  137. }
  138. ],
  139. "metadata" : {
  140. "version" : 1
  141. }
  142. }
  143. ----
  144. We'll create an administrator user and assign them the role named `my_admin_role`,
  145. which allows this user to submit requests as the `analyst_user`.
  146. [source,console]
  147. ----
  148. POST /_security/user/admin_user?refresh=true
  149. {
  150. "password": "l0ng-r4nd0m-p@ssw0rd",
  151. "roles": [ "my_admin_role" ],
  152. "full_name": "Eirian Zola",
  153. "metadata": { "intelligence" : 7}
  154. }
  155. ----
  156. We can also create an analyst user and assign them the role named
  157. `my_analyst_role`.
  158. [source,console]
  159. ----
  160. POST /_security/user/analyst_user?refresh=true
  161. {
  162. "password": "l0nger-r4nd0mer-p@ssw0rd",
  163. "roles": [ "my_analyst_role" ],
  164. "full_name": "Monday Jaffe",
  165. "metadata": { "innovation" : 8}
  166. }
  167. ----
  168. You can then authenticate to {es} as the `admin_user` or `analyst_user`. However, the `admin_user` could optionally submit requests on
  169. behalf of the `analyst_user`. The following request authenticates to {es} with a
  170. `Basic` authorization token and submits the request as the `analyst_user`:
  171. [source,sh]
  172. ----
  173. curl -s -X GET -H "Authorization: Basic YWRtaW5fdXNlcjpsMG5nLXI0bmQwbS1wQHNzdzByZA==" -H "es-security-runas-user: analyst_user" https://localhost:9200/_security/_authenticate
  174. ----
  175. The response indicates that the `analyst_user` submitted this request, using the
  176. `my_analyst_role` that's assigned to that user. When the `admin_user` submitted
  177. the request, {es} authenticated that user, discarded their roles, and then used
  178. the roles of the `run_as` user.
  179. [source,sh]
  180. ----
  181. {"username":"analyst_user","roles":["my_analyst_role"],"full_name":"Monday Jaffe","email":null,
  182. "metadata":{"innovation":8},"enabled":true,"authentication_realm":{"name":"native",
  183. "type":"native"},"lookup_realm":{"name":"native","type":"native"},"authentication_type":"realm"}
  184. %
  185. ----
  186. The `authentication_realm` and `lookup_realm` in the response both specify
  187. the `native` realm because both the `admin_user` and `analyst_user` are from
  188. that realm. If the two users are in different realms, the values for
  189. `authentication_realm` and `lookup_realm` are different (such as `pki` and
  190. `native`).