create-role-mappings.asciidoc 11 KB

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