create-role-mappings.asciidoc 11 KB

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