|
110 | 110 |
|
111 | 111 | MATCH_ENTRY_CONTENT = "%s/%s/*" % (XNAME_ENTRY, XNAME_CONTENT) |
112 | 112 |
|
113 | | -capabilities = record(dict([(k,k) for k in [ |
114 | | - "admin_all_objects", "change_authentication", |
115 | | - "change_own_password", "delete_by_keyword", |
116 | | - "edit_deployment_client", "edit_deployment_server", |
117 | | - "edit_dist_peer", "edit_forwarders", "edit_httpauths", |
118 | | - "edit_input_defaults", "edit_monitor", "edit_roles", |
119 | | - "edit_scripted", "edit_search_server", "edit_server", |
120 | | - "edit_splunktcp", "edit_splunktcp_ssl", "edit_tcp", |
121 | | - "edit_udp", "edit_user", "edit_web_settings", "get_metadata", |
122 | | - "get_typeahead", "indexes_edit", "license_edit", "license_tab", |
123 | | - "list_deployment_client", "list_forwarders", "list_httpauths", |
124 | | - "list_inputs", "request_remote_tok", "rest_apps_management", |
125 | | - "rest_apps_view", "rest_properties_get", "rest_properties_set", |
126 | | - "restart_splunkd", "rtsearch", "schedule_search", "search", |
127 | | - "use_file_operator"]])) |
128 | | - |
129 | | - |
130 | 113 | class NoSuchUserException(Exception): |
131 | 114 | pass |
132 | 115 |
|
@@ -154,6 +137,9 @@ class InvalidNameException(Exception): |
154 | 137 | class OperationFailedException(Exception): |
155 | 138 | pass |
156 | 139 |
|
| 140 | +class NoSuchCapability(Exception): |
| 141 | + pass |
| 142 | + |
157 | 143 | def trailing(template, *targets): |
158 | 144 | """Substring of *template* following all *targets*. |
159 | 145 |
|
@@ -2466,10 +2452,43 @@ def create(self, username, password, roles, **params): |
2466 | 2452 | def delete(self, name): |
2467 | 2453 | return Collection.delete(self, name.lower()) |
2468 | 2454 |
|
| 2455 | + |
| 2456 | +class Role(Entity): |
| 2457 | + def grant(self, *capabilities_to_grant): |
| 2458 | + possible_capabilities = self.service.capabilities |
| 2459 | + for capability in capabilities_to_grant: |
| 2460 | + if capability not in possible_capabilities: |
| 2461 | + raise NoSuchCapability(capability) |
| 2462 | + self.refresh() |
| 2463 | + new_capabilities = self['capabilities'] + list(capabilities_to_grant) |
| 2464 | + self.post(capabilities=new_capabilities) |
| 2465 | + return self |
| 2466 | + |
| 2467 | + def revoke(self, *capabilities_to_revoke): |
| 2468 | + possible_capabilities = self.service.capabilities |
| 2469 | + for capability in capabilities_to_revoke: |
| 2470 | + if capability not in possible_capabilities: |
| 2471 | + raise NoSuchCapability(capability) |
| 2472 | + self.refresh() |
| 2473 | + old_capabilities = self['capabilities'] |
| 2474 | + for capability in capabilities_to_revoke: |
| 2475 | + if capability not in old_capabilities: |
| 2476 | + raise ValueError("Role does not have capability %s" % capability) |
| 2477 | + new_capabilities = [] |
| 2478 | + for c in old_capabilities: |
| 2479 | + if c not in capabilities_to_revoke: |
| 2480 | + new_capabilities.append(c) |
| 2481 | + if new_capabilities == []: |
| 2482 | + new_capabilities = '' # Empty lists don't get passed in the body, so we have to force an empty argument. |
| 2483 | + self.post(capabilities=new_capabilities) |
| 2484 | + return self |
| 2485 | + |
| 2486 | + |
| 2487 | + |
2469 | 2488 | class Roles(Collection): |
2470 | 2489 | """Roles in the Splunk instance.""" |
2471 | 2490 | def __init__(self, service): |
2472 | | - return Collection.__init__(self, service, PATH_ROLES) |
| 2491 | + return Collection.__init__(self, service, PATH_ROLES, item=Role) |
2473 | 2492 |
|
2474 | 2493 | def __getitem__(self, key): |
2475 | 2494 | return Collection.__getitem__(self, key.lower()) |
|
0 commit comments