bulk-create-roles.asciidoc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. [role="xpack"]
  2. [[security-api-bulk-put-role]]
  3. === Bulk create or update roles API
  4. ++++
  5. <titleabbrev>Bulk create or update roles API</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. Bulk adds and updates roles in the native realm.
  13. [[security-api-bulk-put-role-request]]
  14. ==== {api-request-title}
  15. `POST /_security/role/` +
  16. [[security-api-bulk-put-role-prereqs]]
  17. ==== {api-prereq-title}
  18. * To use this API, you must have at least the `manage_security` cluster
  19. privilege.
  20. [[security-api-bulk-put-role-desc]]
  21. ==== {api-description-title}
  22. The role management APIs are generally the preferred way to manage roles, rather than using
  23. <<roles-management-file,file-based role management>>. The bulk create
  24. or update roles API cannot update roles that are defined in roles files.
  25. [[security-api-bulk-put-role-path-params]]
  26. ==== {api-path-parms-title}
  27. `refresh`::
  28. Optional setting of the {ref}/docs-refresh.html[refresh policy] for the write request. Defaults to Immediate.
  29. [[security-api-bulk-put-role-request-body]]
  30. ==== {api-request-body-title}
  31. The following parameters can be specified in the body of a POST request
  32. and pertain to adding a set of roles:
  33. `roles`::
  34. (object) The roles to add as a role name to role map.
  35. ====
  36. `<role_name>` (required):: (string) The role name.
  37. `applications`:: (list) A list of application privilege entries.
  38. `application` (required)::: (string) The name of the application to which this entry applies.
  39. `privileges`::: (list) A list of strings, where each element is the name of an application
  40. privilege or action.
  41. `resources`::: (list) A list resources to which the privileges are applied.
  42. `cluster`:: (list) A list of cluster privileges. These privileges define the
  43. cluster level actions that users with this role are able to execute.
  44. `global`:: (object) An object defining global privileges. A global privilege is
  45. a form of cluster privilege that is request-aware. Support for global privileges
  46. is currently limited to the management of application privileges.
  47. `indices`:: (list) A list of indices permissions entries.
  48. `field_security`::: (object) The document fields that the owners of the role have
  49. read access to. For more information, see
  50. <<field-and-document-access-control>>.
  51. `names` (required)::: (list) A list of indices (or index name patterns) to which the
  52. permissions in this entry apply.
  53. `privileges`(required)::: (list) The index level privileges that the owners of the role
  54. have on the specified indices.
  55. `query`::: A search query that defines the documents the owners of the role have
  56. read access to. A document within the specified indices must match this query in
  57. order for it to be accessible by the owners of the role.
  58. `metadata`:: (object) Optional meta-data. Within the `metadata` object, keys
  59. that begin with `_` are reserved for system usage.
  60. `run_as`:: (list) A list of users that the owners of this role can impersonate.
  61. For more information, see
  62. <<run-as-privilege>>.
  63. `remote_indices`:: (list) A list of remote indices permissions entries.
  64. +
  65. --
  66. NOTE: Remote indices are effective for <<remote-clusters-api-key,remote clusters configured with the API key based model>>.
  67. They have no effect for remote clusters configured with the <<remote-clusters-cert,certificate based model>>.
  68. --
  69. `clusters` (required)::: (list) A list of cluster aliases to which the permissions
  70. in this entry apply.
  71. `field_security`::: (object) The document fields that the owners of the role have
  72. read access to. For more information, see
  73. <<field-and-document-access-control>>.
  74. `names` (required)::: (list) A list of indices (or index name patterns) on the remote clusters
  75. (specified with `clusters`) to which the permissions in this entry apply.
  76. `privileges`(required)::: (list) The index level privileges that the owners of the role
  77. have on the specified indices.
  78. `query`::: A search query that defines the documents the owners of the role have
  79. read access to. A document within the specified indices must match this query in
  80. order for it to be accessible by the owners of the role.
  81. `remote_cluster`:: (list) A list of remote cluster permissions entries.
  82. +
  83. --
  84. NOTE: Remote cluster permissions are effective for <<remote-clusters-api-key,remote clusters configured with the API key based model>>.
  85. They have no effect for remote clusters configured with the <<remote-clusters-cert,certificate based model>>.
  86. --
  87. `clusters` (required)::: (list) A list of cluster aliases to which the permissions
  88. in this entry apply.
  89. `privileges`(required)::: (list) The cluster level privileges that the owners of the role
  90. have in the specified clusters. Note - only a subset of the cluster privileges are supported for remote clusters.
  91. The <<security-api-get-builtin-privileges-request, builtin privileges API>> can be used to determine
  92. which privileges are allowed per version.
  93. For more information, see <<defining-roles>>.
  94. ====
  95. [[security-bulk-api-put-role-example]]
  96. ==== {api-examples-title}
  97. The following example adds the roles called `my_admin_role` and `my_user_role`:
  98. [source,console]
  99. --------------------------------------------------
  100. POST /_security/role
  101. {
  102. "roles": {
  103. "my_admin_role": {
  104. "cluster": [
  105. "all"
  106. ],
  107. "indices": [
  108. {
  109. "names": [
  110. "index1",
  111. "index2"
  112. ],
  113. "privileges": [
  114. "all"
  115. ],
  116. "field_security": {
  117. "grant": [
  118. "title",
  119. "body"
  120. ]
  121. },
  122. "query": "{\"match\": {\"title\": \"foo\"}}"
  123. }
  124. ],
  125. "applications": [
  126. {
  127. "application": "myapp",
  128. "privileges": [
  129. "admin",
  130. "read"
  131. ],
  132. "resources": [
  133. "*"
  134. ]
  135. }
  136. ],
  137. "run_as": [
  138. "other_user"
  139. ],
  140. "metadata": {
  141. "version": 1
  142. }
  143. },
  144. "my_user_role": {
  145. "cluster": [
  146. "all"
  147. ],
  148. "indices": [
  149. {
  150. "names": [
  151. "index1"
  152. ],
  153. "privileges": [
  154. "read"
  155. ],
  156. "field_security": {
  157. "grant": [
  158. "title",
  159. "body"
  160. ]
  161. },
  162. "query": "{\"match\": {\"title\": \"foo\"}}"
  163. }
  164. ],
  165. "applications": [
  166. {
  167. "application": "myapp",
  168. "privileges": [
  169. "admin",
  170. "read"
  171. ],
  172. "resources": [
  173. "*"
  174. ]
  175. }
  176. ],
  177. "run_as": [
  178. "other_user"
  179. ],
  180. "metadata": {
  181. "version": 1
  182. }
  183. }
  184. }
  185. }
  186. --------------------------------------------------
  187. A successful call returns a JSON structure that shows whether the role has been
  188. created, updated, or had no changes made.
  189. [source,console-result]
  190. --------------------------------------------------
  191. {
  192. "created": [ <1>
  193. "my_admin_role", <2>
  194. "my_user_role"
  195. ]
  196. }
  197. --------------------------------------------------
  198. <1> Result type, one of `created`, `updated`, `noop`, `errors`.
  199. <2> A list of the roles that were created.
  200. Because errors are handled individually for each role create or update, the API allows partial success.
  201. The following query would throw an error for `my_admin_role` because the privilege `bad_cluster_privilege`
  202. doesn't exist, but would be successful for the `my_user_role`.
  203. [source,console]
  204. --------------------------------------------------
  205. POST /_security/role
  206. {
  207. "roles": {
  208. "my_admin_role": {
  209. "cluster": [
  210. "bad_cluster_privilege"
  211. ],
  212. "indices": [
  213. {
  214. "names": [
  215. "index1",
  216. "index2"
  217. ],
  218. "privileges": ["all"],
  219. "field_security": {
  220. "grant": [
  221. "title",
  222. "body"
  223. ]
  224. },
  225. "query": "{\"match\": {\"title\": \"foo\"}}"
  226. }
  227. ],
  228. "applications": [
  229. {
  230. "application": "myapp",
  231. "privileges": [
  232. "admin",
  233. "read"
  234. ],
  235. "resources": [
  236. "*"
  237. ]
  238. }
  239. ],
  240. "run_as": [
  241. "other_user"
  242. ],
  243. "metadata": {
  244. "version": 1
  245. }
  246. },
  247. "my_user_role": {
  248. "cluster": [
  249. "all"
  250. ],
  251. "indices": [
  252. {
  253. "names": [
  254. "index1"
  255. ],
  256. "privileges": [
  257. "read"
  258. ],
  259. "field_security": {
  260. "grant": [
  261. "title",
  262. "body"
  263. ]
  264. },
  265. "query": "{\"match\": {\"title\": \"foo\"}}"
  266. }
  267. ],
  268. "applications": [
  269. {
  270. "application": "myapp",
  271. "privileges": [
  272. "admin",
  273. "read"
  274. ],
  275. "resources": [
  276. "*"
  277. ]
  278. }
  279. ],
  280. "run_as": [
  281. "other_user"
  282. ],
  283. "metadata": {
  284. "version": 1
  285. }
  286. }
  287. }
  288. }
  289. --------------------------------------------------
  290. The result would then have the `errors` field set to `true` and hold the error for the `my_admin_role` update.
  291. [source,console-result]
  292. --------------------------------------------------
  293. {
  294. "created": [
  295. "my_user_role" <1>
  296. ],
  297. "errors": { <2>
  298. "count": 1, <3>
  299. "details": {
  300. "my_admin_role": { <4>
  301. "type": "action_request_validation_exception",
  302. "reason": "Validation Failed: 1: unknown cluster privilege [bad_cluster_privilege]. a privilege must be either one of the predefined cluster privilege names [manage_own_api_key,manage_data_stream_global_retention,monitor_data_stream_global_retention,none,cancel_task,cross_cluster_replication,cross_cluster_search,delegate_pki,grant_api_key,manage_autoscaling,manage_index_templates,manage_logstash_pipelines,manage_oidc,manage_saml,manage_search_application,manage_search_query_rules,manage_search_synonyms,manage_service_account,manage_token,manage_user_profile,monitor_connector,monitor_enrich,monitor_inference,monitor_ml,monitor_rollup,monitor_snapshot,monitor_stats,monitor_text_structure,monitor_watcher,post_behavioral_analytics_event,read_ccr,read_connector_secrets,read_fleet_secrets,read_ilm,read_pipeline,read_security,read_slm,transport_client,write_connector_secrets,write_fleet_secrets,create_snapshot,manage_behavioral_analytics,manage_ccr,manage_connector,manage_enrich,manage_ilm,manage_inference,manage_ml,manage_rollup,manage_slm,manage_watcher,monitor_data_frame_transforms,monitor_transform,manage_api_key,manage_ingest_pipelines,manage_pipeline,manage_data_frame_transforms,manage_transform,manage_security,monitor,manage,all] or a pattern over one of the available cluster actions;"
  303. }
  304. }
  305. }
  306. }
  307. --------------------------------------------------
  308. <1> The successfully created role.
  309. <2> The errors encountered.
  310. <3> The number of put role requests that resulted in an error.
  311. <4> The error keyed by role name.