create-role-mappings.asciidoc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. If there are multiple groups, you can use array syntax for the groups field.
  162. This matches any of the groups (rather than all of the groups):
  163. [source,console]
  164. ------------------------------------------------------------
  165. POST /_security/role_mapping/mapping4
  166. {
  167. "roles": [ "superuser" ],
  168. "enabled": true,
  169. "rules": {
  170. "any": [
  171. {
  172. "field": {
  173. "username": "esadmin"
  174. }
  175. },
  176. {
  177. "field": {
  178. "groups": [
  179. "cn=admins,dc=example,dc=com",
  180. "cn=other,dc=example,dc=com"
  181. ]
  182. }
  183. }
  184. ]
  185. }
  186. }
  187. ------------------------------------------------------------
  188. However, in rare cases the names of your groups may be an exact match for the
  189. names of your {es} roles. This can be the case when your SAML Identity Provider
  190. includes its own "group mapping" feature and can be configured to release {es}
  191. role names in the user's SAML attributes.
  192. In these cases it is possible to use a template that treats the group names as
  193. role names.
  194. *Note*: This should only be done if you intend to define roles for all of the
  195. provided groups. Mapping a user to a large number of unnecessary or undefined
  196. roles is inefficient and can have a negative effect on system performance.
  197. If you only need to map a subset of the groups, then you should do this
  198. using explicit mappings.
  199. [source,console]
  200. ------------------------------------------------------------
  201. POST /_security/role_mapping/mapping5
  202. {
  203. "role_templates": [
  204. {
  205. "template": { "source": "{{#tojson}}groups{{/tojson}}" }, <1>
  206. "format" : "json" <2>
  207. }
  208. ],
  209. "rules": {
  210. "field" : { "realm.name" : "saml1" }
  211. },
  212. "enabled": true
  213. }
  214. ------------------------------------------------------------
  215. <1> The `tojson` mustache function is used to convert the list of
  216. group names into a valid JSON array.
  217. <2> Because the template produces a JSON array, the format must be
  218. set to `json`.
  219. The following example matches users within a specific LDAP sub-tree:
  220. [source,console]
  221. ------------------------------------------------------------
  222. POST /_security/role_mapping/mapping6
  223. {
  224. "roles": [ "example-user" ],
  225. "enabled": true,
  226. "rules": {
  227. "field" : { "dn" : "*,ou=subtree,dc=example,dc=com" }
  228. }
  229. }
  230. ------------------------------------------------------------
  231. The following example matches users within a particular LDAP sub-tree in a
  232. specific realm:
  233. [source,console]
  234. ------------------------------------------------------------
  235. POST /_security/role_mapping/mapping7
  236. {
  237. "roles": [ "ldap-example-user" ],
  238. "enabled": true,
  239. "rules": {
  240. "all": [
  241. { "field" : { "dn" : "*,ou=subtree,dc=example,dc=com" } },
  242. { "field" : { "realm.name" : "ldap1" } }
  243. ]
  244. }
  245. }
  246. ------------------------------------------------------------
  247. The rules can be more complex and include wildcard matching. For example, the
  248. following mapping matches any user where *all* of these conditions are met:
  249. - the _Distinguished Name_ matches the pattern `*,ou=admin,dc=example,dc=com`,
  250. or the username is `es-admin`, or the username is `es-system`
  251. - the user in the `cn=people,dc=example,dc=com` group
  252. - the user does not have a `terminated_date`
  253. [source,console]
  254. ------------------------------------------------------------
  255. POST /_security/role_mapping/mapping8
  256. {
  257. "roles": [ "superuser" ],
  258. "enabled": true,
  259. "rules": {
  260. "all": [
  261. {
  262. "any": [
  263. {
  264. "field": {
  265. "dn": "*,ou=admin,dc=example,dc=com"
  266. }
  267. },
  268. {
  269. "field": {
  270. "username": [ "es-admin", "es-system" ]
  271. }
  272. }
  273. ]
  274. },
  275. {
  276. "field": {
  277. "groups": "cn=people,dc=example,dc=com"
  278. }
  279. },
  280. {
  281. "except": {
  282. "field": {
  283. "metadata.terminated_date": null
  284. }
  285. }
  286. }
  287. ]
  288. }
  289. }
  290. ------------------------------------------------------------
  291. A templated role can be used to automatically map every user to their own
  292. custom role. The role itself can be defined through the
  293. <<security-api-put-role, Roles API>> or using a
  294. <<implementing-custom-roles-provider,custom roles provider>>.
  295. In this example every user who authenticates using the "cloud-saml" realm
  296. will be automatically mapped to two roles - the `"saml_user"` role and a
  297. role that is their username prefixed with `_user_`.
  298. As an example, the user `nwong` would be assigned the `saml_user` and
  299. `_user_nwong` roles.
  300. [source,console]
  301. ------------------------------------------------------------
  302. POST /_security/role_mapping/mapping9
  303. {
  304. "rules": { "field": { "realm.name": "cloud-saml" } },
  305. "role_templates": [
  306. { "template": { "source" : "saml_user" } }, <1>
  307. { "template": { "source" : "_user_{{username}}" } }
  308. ],
  309. "enabled": true
  310. }
  311. ------------------------------------------------------------
  312. <1> Because it is not possible to specify both `roles` and `role_templates` in
  313. the same role mapping, we can apply a "fixed name" role by using a template
  314. that has no substitutions.