1
0

role-mapping.asciidoc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. [role="xpack"]
  2. [[security-api-role-mapping]]
  3. === Role Mapping APIs
  4. The Role Mapping API enables you to add, remove, and retrieve role mappings.
  5. ==== Request
  6. `GET /_xpack/security/role_mapping` +
  7. `GET /_xpack/security/role_mapping/<name>` +
  8. `DELETE /_xpack/security/role_mapping/<name>` +
  9. `POST /_xpack/security/role_mapping/<name>` +
  10. `PUT /_xpack/security/role_mapping/<name>`
  11. ==== Description
  12. Role mappings have _rules_ that identify users and a list of _roles_ that are
  13. granted to those users.
  14. NOTE: This API does not create roles. Rather, it maps users to existing roles.
  15. Roles can be created by using <<security-role-apis,role management APIs>> or
  16. {xpack-ref}/defining-roles.html#roles-management-file[roles files].
  17. The role mapping rule is a logical condition that is expressed using a JSON DSL.
  18. The DSL supports the following rule types:
  19. |=======================
  20. | Type | Value Type (child) | Description
  21. | `any` | An array of rules | If *any* of its children are true, it
  22. evaluates to `true`.
  23. | `all` | An array of rules | If *all* of its children are true, it
  24. evaluates to `true`.
  25. | `field` | An object | See <<mapping-roles-rule-field>>
  26. | `except` | A single rule as an object | Only valid as a child of an `all`
  27. rule. If its child is `false`, the
  28. `except` is `true`.
  29. |=======================
  30. [float]
  31. [[mapping-roles-rule-field]]
  32. ===== The Field Rule
  33. The `field` rule is the primary building block for a role-mapping expression.
  34. It takes a single object as its value and that object must contain a single
  35. member with key _F_ and value _V_. The field rule looks up the value of _F_
  36. within the user object and then tests whether the user value _matches_ the
  37. provided value _V_.
  38. The value specified in the field rule can be one of the following types:
  39. [cols="2,5,3m"]
  40. |=======================
  41. | Type | Description | Example
  42. | Simple String | Exactly matches the provided value. | "esadmin"
  43. | Wildcard String | Matches the provided value using a wildcard. | "*,dc=example,dc=com"
  44. | Regular Expression | Matches the provided value using a
  45. {ref}/query-dsl-regexp-query.html#regexp-syntax[Lucene regexp]. | "/.\*-admin[0-9]*/"
  46. | Number | Matches an equivalent numerical value. | 7
  47. | Null | Matches a null or missing value. | null
  48. | Array | Tests each element in the array in
  49. accordance with the above definitions.
  50. If _any_ of elements match, the match is successful. | ["admin", "operator"]
  51. |=======================
  52. ===== User Fields
  53. The _user object_ against which rules are evaluated has the following fields:
  54. [cols="1s,,,m"]
  55. |=======================
  56. | Name | Type | Description | Example
  57. | username | string | The username by which {security} knows this user. | `"username": "jsmith"`
  58. | dn | string | The _Distinguished Name_ of the user. | `"dn": "cn=jsmith,ou=users,dc=example,dc=com",`
  59. | groups | array-of-string | The groups to which the user belongs. | `"groups" : [ "cn=admin,ou=groups,dc=example,dc=com",
  60. "cn=esusers,ou=groups,dc=example,dc=com ]`
  61. | metadata | object | Additional metadata for the user. | `"metadata": { "cn": "John Smith" }`
  62. | realm | object | The realm that authenticated the user. The only field in this object is the realm name. | `"realm": { "name": "ldap1" }`
  63. |=======================
  64. The `groups` field is multi-valued; a user can belong to many groups. When a
  65. `field` rule is applied against a multi-valued field, it is considered to match
  66. if _at least one_ of the member values matches. For example, the following rule
  67. matches any user who is a member of the `admin` group, regardless of any
  68. other groups they belong to:
  69. [source, js]
  70. ------------------------------------------------------------
  71. { "field" : { "groups" : "admin" } }
  72. ------------------------------------------------------------
  73. // NOTCONSOLE
  74. For additional realm-specific details, see
  75. {xpack-ref}/mapping-roles.html#ldap-role-mapping[Mapping Users and Groups to Roles].
  76. ==== Path Parameters
  77. `name`::
  78. (string) The distinct name that identifies the role mapping. The name is
  79. used solely as an identifier to facilitate interaction via the API; it does
  80. not affect the behavior of the mapping in any way. If you do not specify this
  81. parameter for the Get Role Mappings API, it returns information about all
  82. role mappings.
  83. ==== Request Body
  84. The following parameters can be specified in the body of a PUT or POST request
  85. and pertain to adding a role mapping:
  86. `enabled` (required)::
  87. (boolean) Mappings that have `enabled` set to `false` are ignored when role
  88. mapping is performed.
  89. `metadata`::
  90. (object) Additional metadata that helps define which roles are assigned to each
  91. user. Within the `metadata` object, keys beginning with `_` are reserved for
  92. system usage.
  93. `roles` (required)::
  94. (list) A list of roles that are granted to the users that match the role-mapping
  95. rules.
  96. `rules` (required)::
  97. (object) The rules that determine which users should be matched by the mapping.
  98. A rule is a logical condition that is expressed by using a JSON DSL.
  99. ==== Authorization
  100. To use this API, you must have at least the `manage_security` cluster privilege.
  101. ==== Examples
  102. [[security-api-put-role-mapping]]
  103. To add a role mapping, submit a PUT or POST request to the `/_xpack/security/role_mapping/<name>` endpoint. The following example assigns
  104. the "user" role to all users:
  105. [source, js]
  106. ------------------------------------------------------------
  107. POST /_xpack/security/role_mapping/mapping1
  108. {
  109. "roles": [ "user"],
  110. "enabled": true, <1>
  111. "rules": {
  112. "field" : { "username" : "*" }
  113. },
  114. "metadata" : { <2>
  115. "version" : 1
  116. }
  117. }
  118. ------------------------------------------------------------
  119. // CONSOLE
  120. <1> Mappings that have `enabled` set to `false` are ignored when role mapping
  121. is performed.
  122. <2> Metadata is optional.
  123. A successful call returns a JSON structure that shows whether the mapping has
  124. been created or updated.
  125. [source,js]
  126. --------------------------------------------------
  127. {
  128. "role_mapping" : {
  129. "created" : true <1>
  130. }
  131. }
  132. --------------------------------------------------
  133. // TESTRESPONSE
  134. <1> When an existing mapping is updated, `created` is set to false.
  135. The following example assigns the "user" and "admin" roles to specific users:
  136. [source,js]
  137. --------------------------------------------------
  138. POST /_xpack/security/role_mapping/mapping2
  139. {
  140. "roles": [ "user", "admin" ],
  141. "enabled": true,
  142. "rules": {
  143. "field" : { "username" : [ "esadmin01", "esadmin02" ] }
  144. }
  145. }
  146. --------------------------------------------------
  147. // CONSOLE
  148. The following example matches any user where either the username is `esadmin`
  149. or the user is in the `cn=admin,dc=example,dc=com` group:
  150. [source, js]
  151. ------------------------------------------------------------
  152. POST /_xpack/security/role_mapping/mapping3
  153. {
  154. "roles": [ "superuser" ],
  155. "enabled": true,
  156. "rules": {
  157. "any": [
  158. {
  159. "field": {
  160. "username": "esadmin"
  161. }
  162. },
  163. {
  164. "field": {
  165. "groups": "cn=admins,dc=example,dc=com"
  166. }
  167. }
  168. ]
  169. }
  170. }
  171. ------------------------------------------------------------
  172. // CONSOLE
  173. The following example matches users who authenticated against a specific realm:
  174. [source, js]
  175. ------------------------------------------------------------
  176. POST /_xpack/security/role_mapping/mapping4
  177. {
  178. "roles": [ "ldap-user" ],
  179. "enabled": true,
  180. "rules": {
  181. "field" : { "realm.name" : "ldap1" }
  182. }
  183. }
  184. ------------------------------------------------------------
  185. // CONSOLE
  186. The following example matches users within a specific LDAP sub-tree:
  187. [source, js]
  188. ------------------------------------------------------------
  189. POST /_xpack/security/role_mapping/mapping5
  190. {
  191. "roles": [ "example-user" ],
  192. "enabled": true,
  193. "rules": {
  194. "field" : { "dn" : "*,ou=subtree,dc=example,dc=com" }
  195. }
  196. }
  197. ------------------------------------------------------------
  198. // CONSOLE
  199. The following example matches users within a particular LDAP sub-tree in a
  200. specific realm:
  201. [source, js]
  202. ------------------------------------------------------------
  203. POST /_xpack/security/role_mapping/mapping6
  204. {
  205. "roles": [ "ldap-example-user" ],
  206. "enabled": true,
  207. "rules": {
  208. "all": [
  209. { "field" : { "dn" : "*,ou=subtree,dc=example,dc=com" } },
  210. { "field" : { "realm.name" : "ldap1" } }
  211. ]
  212. }
  213. }
  214. ------------------------------------------------------------
  215. // CONSOLE
  216. The rules can be more complex and include wildcard matching. For example, the
  217. following mapping matches any user where *all* of these conditions are met:
  218. - the _Distinguished Name_ matches the pattern `*,ou=admin,dc=example,dc=com`,
  219. or the username is `es-admin`, or the username is `es-system`
  220. - the user in in the `cn=people,dc=example,dc=com` group
  221. - the user does not have a `terminated_date`
  222. [source, js]
  223. ------------------------------------------------------------
  224. POST /_xpack/security/role_mapping/mapping7
  225. {
  226. "roles": [ "superuser" ],
  227. "enabled": true,
  228. "rules": {
  229. "all": [
  230. {
  231. "any": [
  232. {
  233. "field": {
  234. "dn": "*,ou=admin,dc=example,dc=com"
  235. }
  236. },
  237. {
  238. "field": {
  239. "username": [ "es-admin", "es-system" ]
  240. }
  241. }
  242. ]
  243. },
  244. {
  245. "field": {
  246. "groups": "cn=people,dc=example,dc=com"
  247. }
  248. },
  249. {
  250. "except": {
  251. "field": {
  252. "metadata.terminated_date": null
  253. }
  254. }
  255. }
  256. ]
  257. }
  258. }
  259. ------------------------------------------------------------
  260. // CONSOLE
  261. [[security-api-get-role-mapping]]
  262. To retrieve a role mapping, issue a GET request to the
  263. `/_xpack/security/role_mapping/<name>` endpoint:
  264. [source,js]
  265. --------------------------------------------------
  266. GET /_xpack/security/role_mapping/mapping7
  267. --------------------------------------------------
  268. // CONSOLE
  269. // TEST[continued]
  270. A successful call retrieves an object, where the keys are the
  271. names of the request mappings, and the values are
  272. the JSON representation of those mappings.
  273. If there is no mapping with the requested name, the
  274. response will have status code `404`.
  275. [source,js]
  276. --------------------------------------------------
  277. {
  278. "mapping7": {
  279. "enabled": true,
  280. "roles": [
  281. "superuser"
  282. ],
  283. "rules": {
  284. "all": [
  285. {
  286. "any": [
  287. {
  288. "field": {
  289. "dn": "*,ou=admin,dc=example,dc=com"
  290. }
  291. },
  292. {
  293. "field": {
  294. "username": [
  295. "es-admin",
  296. "es-system"
  297. ]
  298. }
  299. }
  300. ]
  301. },
  302. {
  303. "field": {
  304. "groups": "cn=people,dc=example,dc=com"
  305. }
  306. },
  307. {
  308. "except": {
  309. "field": {
  310. "metadata.terminated_date": null
  311. }
  312. }
  313. }
  314. ]
  315. },
  316. "metadata": {}
  317. }
  318. }
  319. --------------------------------------------------
  320. // TESTRESPONSE
  321. You can specify multiple mapping names as a comma-separated list.
  322. To retrieve all mappings, omit the name entirely.
  323. [[security-api-delete-role-mapping]]
  324. To delete a role mapping, submit a DELETE request to the
  325. `/_xpack/security/role_mapping/<name>` endpoint:
  326. [source,js]
  327. --------------------------------------------------
  328. DELETE /_xpack/security/role_mapping/mapping1
  329. --------------------------------------------------
  330. // CONSOLE
  331. // TEST[setup:role_mapping]
  332. If the mapping is successfully deleted, the request returns `{"found": true}`.
  333. Otherwise, `found` is set to false.
  334. [source,js]
  335. --------------------------------------------------
  336. {
  337. "found" : true
  338. }
  339. --------------------------------------------------
  340. // TESTRESPONSE