|
| 1 | +[role="xpack"] |
| 2 | +[[security-api-put-role]] |
| 3 | +=== Create roles API |
| 4 | + |
| 5 | +Adds roles in the native realm. |
| 6 | + |
| 7 | +==== Request |
| 8 | + |
| 9 | +`POST /_xpack/security/role/<name>` + |
| 10 | + |
| 11 | +`PUT /_xpack/security/role/<name>` |
| 12 | + |
| 13 | + |
| 14 | +==== Description |
| 15 | + |
| 16 | +The role API is generally the preferred way to manage roles, rather than using |
| 17 | +file-based role management. For more information about the native realm, see |
| 18 | +{stack-ov}/realms.html[Realms] and <<configuring-native-realm>>. |
| 19 | + |
| 20 | + |
| 21 | +==== Path Parameters |
| 22 | + |
| 23 | +`name`:: |
| 24 | + (string) The name of the role. |
| 25 | + |
| 26 | + |
| 27 | +==== Request Body |
| 28 | + |
| 29 | +The following parameters can be specified in the body of a PUT or POST request |
| 30 | +and pertain to adding a role: |
| 31 | + |
| 32 | +`cluster`:: (list) A list of cluster privileges. These privileges define the |
| 33 | +cluster level actions that users with this role are able to execute. |
| 34 | + |
| 35 | +`indices`:: (list) A list of indices permissions entries. |
| 36 | +`field_security`::: (list) The document fields that the owners of the role have |
| 37 | +read access to. For more information, see |
| 38 | +{stack-ov}/field-and-document-access-control.html[Setting up field and document level security]. |
| 39 | +`names` (required)::: (list) A list of indices (or index name patterns) to which the |
| 40 | +permissions in this entry apply. |
| 41 | +`privileges`(required)::: (list) The index level privileges that the owners of the role |
| 42 | +have on the specified indices. |
| 43 | +`query`::: A search query that defines the documents the owners of the role have |
| 44 | +read access to. A document within the specified indices must match this query in |
| 45 | +order for it to be accessible by the owners of the role. |
| 46 | + |
| 47 | +`metadata`:: (object) Optional meta-data. Within the `metadata` object, keys |
| 48 | +that begin with `_` are reserved for system usage. |
| 49 | + |
| 50 | +`run_as`:: (list) A list of users that the owners of this role can impersonate. |
| 51 | +For more information, see |
| 52 | +{stack-ov}/run-as-privilege.html[Submitting requests on behalf of other users]. |
| 53 | + |
| 54 | +For more information, see {stack-ov}/defining-roles.html[Defining roles]. |
| 55 | + |
| 56 | + |
| 57 | +==== Authorization |
| 58 | + |
| 59 | +To use this API, you must have at least the `manage_security` cluster |
| 60 | +privilege. |
| 61 | + |
| 62 | + |
| 63 | +==== Examples |
| 64 | + |
| 65 | +The following example adds a role called `my_admin_role`: |
| 66 | + |
| 67 | +[source,js] |
| 68 | +-------------------------------------------------- |
| 69 | +POST /_xpack/security/role/my_admin_role |
| 70 | +{ |
| 71 | + "cluster": ["all"], |
| 72 | + "indices": [ |
| 73 | + { |
| 74 | + "names": [ "index1", "index2" ], |
| 75 | + "privileges": ["all"], |
| 76 | + "field_security" : { // optional |
| 77 | + "grant" : [ "title", "body" ] |
| 78 | + }, |
| 79 | + "query": "{\"match\": {\"title\": \"foo\"}}" // optional |
| 80 | + } |
| 81 | + ], |
| 82 | + "run_as": [ "other_user" ], // optional |
| 83 | + "metadata" : { // optional |
| 84 | + "version" : 1 |
| 85 | + } |
| 86 | +} |
| 87 | +-------------------------------------------------- |
| 88 | +// CONSOLE |
| 89 | + |
| 90 | +A successful call returns a JSON structure that shows whether the role has been |
| 91 | +created or updated. |
| 92 | + |
| 93 | +[source,js] |
| 94 | +-------------------------------------------------- |
| 95 | +{ |
| 96 | + "role": { |
| 97 | + "created": true <1> |
| 98 | + } |
| 99 | +} |
| 100 | +-------------------------------------------------- |
| 101 | +// TESTRESPONSE |
| 102 | +<1> When an existing role is updated, `created` is set to false. |
| 103 | + |
0 commit comments