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