create-role-mappings.asciidoc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. [role="xpack"]
  2. [[security-api-put-role-mapping]]
  3. === Create or update role mappings API
  4. ++++
  5. <titleabbrev>Create or update role mappings</titleabbrev>
  6. ++++
  7. Creates and updates role mappings.
  8. [[security-api-put-role-mapping-request]]
  9. ==== {api-request-title}
  10. `POST /_security/role_mapping/<name>` +
  11. `PUT /_security/role_mapping/<name>`
  12. [[security-api-put-role-mapping-prereqs]]
  13. ==== {api-prereq-title}
  14. * To use this API, you must have at least the `manage_security` cluster privilege.
  15. [[security-api-put-role-mapping-desc]]
  16. ==== {api-description-title}
  17. Role mappings define which roles are assigned to each user. Each mapping has
  18. _rules_ that identify users and a list of _roles_ that are
  19. granted to those users.
  20. NOTE: This API does not create roles. Rather, it maps users to existing roles.
  21. Roles can be created by using <<security-api-roles, Role Management APIs>> or
  22. {stack-ov}/defining-roles.html#roles-management-file[roles files].
  23. For more information, see
  24. {stack-ov}/mapping-roles.html[Mapping users and groups to roles].
  25. [[security-api-put-role-mapping-path-params]]
  26. ==== {api-path-parms-title}
  27. `name`::
  28. (string) The distinct name that identifies the role mapping. The name is
  29. used solely as an identifier to facilitate interaction via the API; it does
  30. not affect the behavior of the mapping in any way.
  31. [[security-api-put-role-mapping-request-body]]
  32. ==== {api-request-body-title}
  33. The following parameters can be specified in the body of a PUT or POST request
  34. and pertain to adding a role mapping:
  35. `enabled`::
  36. (Required, boolean) Mappings that have `enabled` set to `false` are ignored when
  37. role mapping is performed.
  38. `metadata`::
  39. (object) Additional metadata that helps define which roles are assigned to each
  40. user. Within the `metadata` object, keys beginning with `_` are reserved for
  41. system usage.
  42. `roles`::
  43. (list of strings) A list of role names that are granted to the users that match
  44. the role mapping rules.
  45. _Exactly one of `roles` or `role_templates` must be specified_.
  46. `role_templates`::
  47. (list of objects) A list of mustache templates that will be evaluated to
  48. determine the roles names that should granted to the users that match the role
  49. mapping rules.
  50. The format of these objects is defined below.
  51. _Exactly one of `roles` or `role_templates` must be specified_.
  52. `rules`::
  53. (Required, object) The rules that determine which users should be matched by the
  54. mapping. A rule is a logical condition that is expressed by using a JSON DSL.
  55. See <<role-mapping-resources>>.
  56. ==== Role Templates
  57. The most common use for role mappings is to create a mapping from a known value
  58. on the user to a fixed role name.
  59. For example, all users in the `cn=admin,dc=example,dc=com` LDAP group should be
  60. given the `superuser` role in {es}.
  61. The `roles` field is used for this purpose.
  62. For more complex needs it is possible to use Mustache templates to dynamically
  63. determine the names of the roles that should be granted to the user.
  64. The `role_templates` field is used for this purpose.
  65. All of the <<role-mapping-resources,user fields>> that are available in the
  66. role mapping `rules` are also available in the role templates. Thus it is possible
  67. to assign a user to a role that reflects their `username`, their `groups` or the
  68. name of the `realm` to which they authenticated.
  69. By default a template is evaluated to produce a single string that is the name
  70. of the role which should be assigned to the user. If the `format` of the template
  71. is set to `"json"` then the template is expected to produce a JSON string, or an
  72. array of JSON strings for the role name(s).
  73. The Examples section below demonstrates the use of templated role names.
  74. [[security-api-put-role-mapping-example]]
  75. ==== {api-examples-title}
  76. The following example assigns the "user" role to all users:
  77. [source, js]
  78. ------------------------------------------------------------
  79. POST /_security/role_mapping/mapping1
  80. {
  81. "roles": [ "user"],
  82. "enabled": true, <1>
  83. "rules": {
  84. "field" : { "username" : "*" }
  85. },
  86. "metadata" : { <2>
  87. "version" : 1
  88. }
  89. }
  90. ------------------------------------------------------------
  91. // CONSOLE
  92. <1> Mappings that have `enabled` set to `false` are ignored when role mapping
  93. is performed.
  94. <2> Metadata is optional.
  95. A successful call returns a JSON structure that shows whether the mapping has
  96. been created or updated.
  97. [source,js]
  98. --------------------------------------------------
  99. {
  100. "role_mapping" : {
  101. "created" : true <1>
  102. }
  103. }
  104. --------------------------------------------------
  105. // TESTRESPONSE
  106. <1> When an existing mapping is updated, `created` is set to false.
  107. The following example assigns the "user" and "admin" roles to specific users:
  108. [source,js]
  109. --------------------------------------------------
  110. POST /_security/role_mapping/mapping2
  111. {
  112. "roles": [ "user", "admin" ],
  113. "enabled": true,
  114. "rules": {
  115. "field" : { "username" : [ "esadmin01", "esadmin02" ] }
  116. }
  117. }
  118. --------------------------------------------------
  119. // CONSOLE
  120. The following example matches users who authenticated against a specific realm:
  121. [source, js]
  122. ------------------------------------------------------------
  123. POST /_security/role_mapping/mapping3
  124. {
  125. "roles": [ "ldap-user" ],
  126. "enabled": true,
  127. "rules": {
  128. "field" : { "realm.name" : "ldap1" }
  129. }
  130. }
  131. ------------------------------------------------------------
  132. // CONSOLE
  133. The following example matches any user where either the username is `esadmin`
  134. or the user is in the `cn=admin,dc=example,dc=com` group:
  135. [source, js]
  136. ------------------------------------------------------------
  137. POST /_security/role_mapping/mapping4
  138. {
  139. "roles": [ "superuser" ],
  140. "enabled": true,
  141. "rules": {
  142. "any": [
  143. {
  144. "field": {
  145. "username": "esadmin"
  146. }
  147. },
  148. {
  149. "field": {
  150. "groups": "cn=admins,dc=example,dc=com"
  151. }
  152. }
  153. ]
  154. }
  155. }
  156. ------------------------------------------------------------
  157. // CONSOLE
  158. The example above is useful when the group names in your identity management
  159. system (such as Active Directory, or a SAML Identity Provider) do not have a
  160. 1-to-1 correspondence with the names of roles in {es}. The role mapping is the
  161. means by which you link a _group name_ with a _role name_.
  162. However, in rare cases the names of your groups may be an exact match for the
  163. names of your {es} roles. This can be the case when your SAML Identity Provider
  164. includes its own "group mapping" feature and can be configured to release {es}
  165. role names in the user's SAML attributes.
  166. In these cases it is possible to use a template that treats the group names as
  167. role names.
  168. *Note*: This should only be done if you intend to define roles for all of the
  169. provided groups. Mapping a user to a large number of unnecessary or undefined
  170. roles is inefficient and can have a negative effect on system performance.
  171. If you only need to map a subset of the groups, then you should do this
  172. using explicit mappings.
  173. [source, js]
  174. ------------------------------------------------------------
  175. POST /_security/role_mapping/mapping5
  176. {
  177. "role_templates": [
  178. {
  179. "template": { "source": "{{#tojson}}groups{{/tojson}}" }, <1>
  180. "format" : "json" <2>
  181. }
  182. ],
  183. "rules": {
  184. "field" : { "realm.name" : "saml1" }
  185. },
  186. "enabled": true
  187. }
  188. ------------------------------------------------------------
  189. // CONSOLE
  190. <1> The `tojson` mustache function is used to convert the list of
  191. group names into a valid JSON array.
  192. <2> Because the template produces a JSON array, the format must be
  193. set to `json`.
  194. The following example matches users within a specific LDAP sub-tree:
  195. [source, js]
  196. ------------------------------------------------------------
  197. POST /_security/role_mapping/mapping6
  198. {
  199. "roles": [ "example-user" ],
  200. "enabled": true,
  201. "rules": {
  202. "field" : { "dn" : "*,ou=subtree,dc=example,dc=com" }
  203. }
  204. }
  205. ------------------------------------------------------------
  206. // CONSOLE
  207. The following example matches users within a particular LDAP sub-tree in a
  208. specific realm:
  209. [source, js]
  210. ------------------------------------------------------------
  211. POST /_security/role_mapping/mapping7
  212. {
  213. "roles": [ "ldap-example-user" ],
  214. "enabled": true,
  215. "rules": {
  216. "all": [
  217. { "field" : { "dn" : "*,ou=subtree,dc=example,dc=com" } },
  218. { "field" : { "realm.name" : "ldap1" } }
  219. ]
  220. }
  221. }
  222. ------------------------------------------------------------
  223. // CONSOLE
  224. The rules can be more complex and include wildcard matching. For example, the
  225. following mapping matches any user where *all* of these conditions are met:
  226. - the _Distinguished Name_ matches the pattern `*,ou=admin,dc=example,dc=com`,
  227. or the username is `es-admin`, or the username is `es-system`
  228. - the user in in the `cn=people,dc=example,dc=com` group
  229. - the user does not have a `terminated_date`
  230. [source, js]
  231. ------------------------------------------------------------
  232. POST /_security/role_mapping/mapping8
  233. {
  234. "roles": [ "superuser" ],
  235. "enabled": true,
  236. "rules": {
  237. "all": [
  238. {
  239. "any": [
  240. {
  241. "field": {
  242. "dn": "*,ou=admin,dc=example,dc=com"
  243. }
  244. },
  245. {
  246. "field": {
  247. "username": [ "es-admin", "es-system" ]
  248. }
  249. }
  250. ]
  251. },
  252. {
  253. "field": {
  254. "groups": "cn=people,dc=example,dc=com"
  255. }
  256. },
  257. {
  258. "except": {
  259. "field": {
  260. "metadata.terminated_date": null
  261. }
  262. }
  263. }
  264. ]
  265. }
  266. }
  267. ------------------------------------------------------------
  268. // CONSOLE
  269. A templated role can be used to automatically map every user to their own
  270. custom role. The role itself can be defined through the
  271. <<security-api-put-role, Roles API>> or using a
  272. {stack-ov}/custom-roles-authorization.html#implementing-custom-roles-provider[custom roles provider].
  273. In this example every user who authenticates using the "cloud-saml" realm
  274. will be automatically mapped to two roles - the `"saml_user"` role and a
  275. role that is their username prefixed with `_user_`.
  276. As an example, the user `nwong` would be assigned the `saml_user` and
  277. `_user_nwong` roles.
  278. [source, js]
  279. ------------------------------------------------------------
  280. POST /_security/role_mapping/mapping9
  281. {
  282. "rules": { "field": { "realm.name": "cloud-saml" } },
  283. "role_templates": [
  284. { "template": { "source" : "saml_user" } }, <1>
  285. { "template": { "source" : "_user_{{username}}" } }
  286. ],
  287. "enabled": true
  288. }
  289. ------------------------------------------------------------
  290. // CONSOLE
  291. <1> Because it is not possible to specify both `roles` and `role_templates` in
  292. the same role mapping, we can apply a "fixed name" role by using a template
  293. that has no substitutions.