|
| 1 | +[role="xpack"] |
| 2 | +[[security-api-put-role-mapping]] |
| 3 | +=== Add role mappings API |
| 4 | + |
| 5 | +Adds and updates role mappings. |
| 6 | + |
| 7 | +==== Request |
| 8 | + |
| 9 | +`POST /_xpack/security/role_mapping/<name>` + |
| 10 | + |
| 11 | +`PUT /_xpack/security/role_mapping/<name>` |
| 12 | + |
| 13 | + |
| 14 | +==== Description |
| 15 | + |
| 16 | +Role mappings define which roles are assigned to each user. Each mapping has |
| 17 | +_rules_ that identify users and a list of _roles_ that are |
| 18 | +granted to those users. |
| 19 | + |
| 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 | + |
| 24 | +For more information, see |
| 25 | +{stack-ov}/mapping-roles.html[Mapping users and groups to roles]. |
| 26 | + |
| 27 | + |
| 28 | +==== Path Parameters |
| 29 | + |
| 30 | +`name`:: |
| 31 | + (string) The distinct name that identifies the role mapping. The name is |
| 32 | + used solely as an identifier to facilitate interaction via the API; it does |
| 33 | + not affect the behavior of the mapping in any way. |
| 34 | + |
| 35 | + |
| 36 | +==== Request Body |
| 37 | + |
| 38 | +The following parameters can be specified in the body of a PUT or POST request |
| 39 | +and pertain to adding a role mapping: |
| 40 | + |
| 41 | +`enabled` (required):: |
| 42 | +(boolean) Mappings that have `enabled` set to `false` are ignored when role |
| 43 | +mapping is performed. |
| 44 | + |
| 45 | +`metadata`:: |
| 46 | +(object) Additional metadata that helps define which roles are assigned to each |
| 47 | +user. Within the `metadata` object, keys beginning with `_` are reserved for |
| 48 | +system usage. |
| 49 | + |
| 50 | +`roles` (required):: |
| 51 | +(list) A list of roles that are granted to the users that match the role mapping |
| 52 | +rules. |
| 53 | + |
| 54 | +`rules` (required):: |
| 55 | +(object) The rules that determine which users should be matched by the mapping. |
| 56 | +A rule is a logical condition that is expressed by using a JSON DSL. See |
| 57 | +<<role-mapping-resources>>. |
| 58 | + |
| 59 | + |
| 60 | +==== Authorization |
| 61 | + |
| 62 | +To use this API, you must have at least the `manage_security` cluster privilege. |
| 63 | + |
| 64 | + |
| 65 | +==== Examples |
| 66 | + |
| 67 | +The following example assigns the "user" role to all users: |
| 68 | + |
| 69 | +[source, js] |
| 70 | +------------------------------------------------------------ |
| 71 | +POST /_xpack/security/role_mapping/mapping1 |
| 72 | +{ |
| 73 | + "roles": [ "user"], |
| 74 | + "enabled": true, <1> |
| 75 | + "rules": { |
| 76 | + "field" : { "username" : "*" } |
| 77 | + }, |
| 78 | + "metadata" : { <2> |
| 79 | + "version" : 1 |
| 80 | + } |
| 81 | +} |
| 82 | +------------------------------------------------------------ |
| 83 | +// CONSOLE |
| 84 | +<1> Mappings that have `enabled` set to `false` are ignored when role mapping |
| 85 | + is performed. |
| 86 | +<2> Metadata is optional. |
| 87 | + |
| 88 | +A successful call returns a JSON structure that shows whether the mapping has |
| 89 | +been created or updated. |
| 90 | + |
| 91 | +[source,js] |
| 92 | +-------------------------------------------------- |
| 93 | +{ |
| 94 | + "role_mapping" : { |
| 95 | + "created" : true <1> |
| 96 | + } |
| 97 | +} |
| 98 | +-------------------------------------------------- |
| 99 | +// TESTRESPONSE |
| 100 | +<1> When an existing mapping is updated, `created` is set to false. |
| 101 | + |
| 102 | +The following example assigns the "user" and "admin" roles to specific users: |
| 103 | + |
| 104 | +[source,js] |
| 105 | +-------------------------------------------------- |
| 106 | +POST /_xpack/security/role_mapping/mapping2 |
| 107 | +{ |
| 108 | + "roles": [ "user", "admin" ], |
| 109 | + "enabled": true, |
| 110 | + "rules": { |
| 111 | + "field" : { "username" : [ "esadmin01", "esadmin02" ] } |
| 112 | + } |
| 113 | +} |
| 114 | +-------------------------------------------------- |
| 115 | +// CONSOLE |
| 116 | + |
| 117 | +The following example matches any user where either the username is `esadmin` |
| 118 | +or the user is in the `cn=admin,dc=example,dc=com` group: |
| 119 | + |
| 120 | +[source, js] |
| 121 | +------------------------------------------------------------ |
| 122 | +POST /_xpack/security/role_mapping/mapping3 |
| 123 | +{ |
| 124 | + "roles": [ "superuser" ], |
| 125 | + "enabled": true, |
| 126 | + "rules": { |
| 127 | + "any": [ |
| 128 | + { |
| 129 | + "field": { |
| 130 | + "username": "esadmin" |
| 131 | + } |
| 132 | + }, |
| 133 | + { |
| 134 | + "field": { |
| 135 | + "groups": "cn=admins,dc=example,dc=com" |
| 136 | + } |
| 137 | + } |
| 138 | + ] |
| 139 | + } |
| 140 | +} |
| 141 | +------------------------------------------------------------ |
| 142 | +// CONSOLE |
| 143 | + |
| 144 | +The following example matches users who authenticated against a specific realm: |
| 145 | +[source, js] |
| 146 | +------------------------------------------------------------ |
| 147 | +POST /_xpack/security/role_mapping/mapping4 |
| 148 | +{ |
| 149 | + "roles": [ "ldap-user" ], |
| 150 | + "enabled": true, |
| 151 | + "rules": { |
| 152 | + "field" : { "realm.name" : "ldap1" } |
| 153 | + } |
| 154 | +} |
| 155 | +------------------------------------------------------------ |
| 156 | +// CONSOLE |
| 157 | + |
| 158 | +The following example matches users within a specific LDAP sub-tree: |
| 159 | + |
| 160 | +[source, js] |
| 161 | +------------------------------------------------------------ |
| 162 | +POST /_xpack/security/role_mapping/mapping5 |
| 163 | +{ |
| 164 | + "roles": [ "example-user" ], |
| 165 | + "enabled": true, |
| 166 | + "rules": { |
| 167 | + "field" : { "dn" : "*,ou=subtree,dc=example,dc=com" } |
| 168 | + } |
| 169 | +} |
| 170 | +------------------------------------------------------------ |
| 171 | +// CONSOLE |
| 172 | + |
| 173 | +The following example matches users within a particular LDAP sub-tree in a |
| 174 | +specific realm: |
| 175 | + |
| 176 | +[source, js] |
| 177 | +------------------------------------------------------------ |
| 178 | +POST /_xpack/security/role_mapping/mapping6 |
| 179 | +{ |
| 180 | + "roles": [ "ldap-example-user" ], |
| 181 | + "enabled": true, |
| 182 | + "rules": { |
| 183 | + "all": [ |
| 184 | + { "field" : { "dn" : "*,ou=subtree,dc=example,dc=com" } }, |
| 185 | + { "field" : { "realm.name" : "ldap1" } } |
| 186 | + ] |
| 187 | + } |
| 188 | +} |
| 189 | +------------------------------------------------------------ |
| 190 | +// CONSOLE |
| 191 | + |
| 192 | +The rules can be more complex and include wildcard matching. For example, the |
| 193 | +following mapping matches any user where *all* of these conditions are met: |
| 194 | + |
| 195 | +- the _Distinguished Name_ matches the pattern `*,ou=admin,dc=example,dc=com`, |
| 196 | + or the username is `es-admin`, or the username is `es-system` |
| 197 | +- the user in in the `cn=people,dc=example,dc=com` group |
| 198 | +- the user does not have a `terminated_date` |
| 199 | + |
| 200 | + |
| 201 | +[source, js] |
| 202 | +------------------------------------------------------------ |
| 203 | +POST /_xpack/security/role_mapping/mapping7 |
| 204 | +{ |
| 205 | + "roles": [ "superuser" ], |
| 206 | + "enabled": true, |
| 207 | + "rules": { |
| 208 | + "all": [ |
| 209 | + { |
| 210 | + "any": [ |
| 211 | + { |
| 212 | + "field": { |
| 213 | + "dn": "*,ou=admin,dc=example,dc=com" |
| 214 | + } |
| 215 | + }, |
| 216 | + { |
| 217 | + "field": { |
| 218 | + "username": [ "es-admin", "es-system" ] |
| 219 | + } |
| 220 | + } |
| 221 | + ] |
| 222 | + }, |
| 223 | + { |
| 224 | + "field": { |
| 225 | + "groups": "cn=people,dc=example,dc=com" |
| 226 | + } |
| 227 | + }, |
| 228 | + { |
| 229 | + "except": { |
| 230 | + "field": { |
| 231 | + "metadata.terminated_date": null |
| 232 | + } |
| 233 | + } |
| 234 | + } |
| 235 | + ] |
| 236 | + } |
| 237 | +} |
| 238 | +------------------------------------------------------------ |
| 239 | +// CONSOLE |
0 commit comments