Skip to content

Commit e1fc5b3

Browse files
Merge pull request #88 from veracode/identity-roles
Identity roles
2 parents bb6330c + d069ce9 commit e1fc5b3

File tree

5 files changed

+176
-2
lines changed

5 files changed

+176
-2
lines changed

docs/docs.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ See the topics below for more information on how to use this library.
2323
* [Teams](teams.md) - create, update, access, and delete teams.
2424
* [Business Units](businessunits.md) - create, update, access, and delete business units.
2525
* [API Credentials](apicreds.md) - create, access, renew, and revoke API credentials.
26+
* [Roles and Permissions](roles.md) - access system roles and permissions; create, update, access, and delete custom roles.
27+
* [JIT Default Settings](jitdefaults.md) - create and update default Just-In-Time Provisioning settings.
2628

2729
## API Object
2830

docs/jitdefaults.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Just In Time Provisioning Default Settings
2+
3+
The following methods call Veracode REST APIs and return JSON. More information about the JIT settings is available in the [Veracode Docs](https://docs.veracode.com/r/Configure_SAML_Self_Registration).
4+
5+
- `JITDefaultSettings().get()` - retrieve the current Just In Time (JIT) default settings.
6+
- `JITDefaultSettings().create(ip_restricted(opt),prefer_veracode_data(opt), allowed_ip_addresses(opt), use_csv_for_roles_claim(opt), use_csv_for_teams_claim(opt), use_csv_for_teams_managed_claim(opt), use_csv_for_ip_address_claim(opt),teams(opt),roles(opt))` - create new Just In Time (JIT) default settings. Settings include:
7+
- `ip_restricted`: set to `True` to apply IP restrictions (defined in `allowed_ip_addresses`) for a JIT user.
8+
- `prefer_veracode_data`: set to `True` to allow an administrator to manage roles, teams, and other settings for users in the Veracode administrative console after user creation. If set to `False`, the SAML assertion sent from the customer's Identity Provider must contain these values.
9+
- `allowed_ip_addresses`: an array of IP addresses. See the [Veracode Docs](https://docs.veracode.com/r/admin_ip) for more information.
10+
- `use_csv_for_roles_claim`: set to `True` if your IDP will send a comma separated list of roles (instead of an array).
11+
- `use_csv_for_teams_claim`: set to `True` if your IDP will send a comma separated list of teams (instead of an array).
12+
- `use_csv_for_teams_managed_claim`: set to `True` if your IDP will send a comma separated list of teams managed by a team admin (instead of an array).
13+
- `use_csv_for_ip_address_claim`: set to `True` if your IDP will send a comma separated list of IP address restrictions (instead of an array).
14+
- `teams`: an array of team IDs (UUIDs) that should be assigned to a JIT user by default.
15+
- `roles`: an array of role IDs (UUIDs) that should be assigned to a JIT user by default.
16+
- `JITDefaultSettings().update(jit_default_id, ip_restricted(opt),prefer_veracode_data(opt), allowed_ip_addresses(opt), use_csv_for_roles_claim(opt), use_csv_for_teams_claim(opt), use_csv_for_teams_managed_claim(opt), use_csv_for_ip_address_claim(opt),teams(opt),roles(opt))` - update existing Just In Time (JIT) default settings identified by `jit_default_id`.
17+
- `JITDefaultSettings().delete(jit_default_id)` - delete the Just In Time (JIT) default settings identified by `jit_default_id`.
18+
19+
[All docs](docs.md)

docs/roles.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Roles and Permissions
2+
3+
The following methods call Veracode REST APIs and return JSON.
4+
5+
- `Roles().get_all()`: get the list of roles for the organization.
6+
- `Roles().get(role_guid)`: get the role definition for a given role.
7+
- `Roles().create(role_name,role_description,is_api (opt),jit_assignable(opt),jit_assignable_default (opt),permissions (opt),child_roles (opt))`: create a role named `role_name`. You must specify either `permissions` or `child_roles`, or both. Arguments include:
8+
- `role_description`: The human readable description of the role.
9+
- `is_api`: Set to `True` to create a role for an API user. Defaults to `False`.
10+
- `jit_assignable`: Set to `True` to allow the role to be assigned by a SAML assertion using just-in-time provisioning. Defaults to `True`.
11+
- `jit_assignable_default`: Set to `True` to allow the role to be assigned by default during just-in-time provisioning. Defaults to `True`.
12+
- `permissions`: An array of permission names. Use `Permissions().get_all()` to see the list of assignable permissions.
13+
- `child_roles`: An array of role names. Adding a child role to a custom role gives the user all the permissions contained in the child role, in addition to any permissions defined in `permissinos`. You can add more than one child role.
14+
- `Roles().update(role_name,role_description,role_guid,is_api (opt),jit_assignable(opt),jit_assignable_default (opt),permissions (opt),child_roles (opt))`: update the role identified by `role_guid` with the provided information.
15+
- `Roles().delete(role_guid)`: delete the role identified by `role_guid`. Note: You can only delete custom roles.
16+
- `Permissions().get_all()`: get the list of permissions that can be part of custom roles.
17+
- `Permissions().get(permission_guid)`: get the permission definition for a given permission.
18+
19+
20+
[All docs](docs.md)

veracode_api_py/api.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from .policy import Policies
2525
from .sca import ComponentActivity, Workspaces, SBOM, SCAApplications
2626
from .collections import Collections
27-
from .identity import Users, Teams, BusinessUnits, APICredentials, Roles
27+
from .identity import Users, Teams, BusinessUnits, APICredentials, Roles, Permissions
2828
from .healthcheck import Healthcheck
2929
from .dynamic import Analyses, Scans, Occurrences, Configuration, CodeGroups, ScanCapacitySummary, ScanOccurrences, \
3030
ScannerVariables, DynUtils
@@ -314,6 +314,33 @@ def revoke_creds(self, api_id):
314314

315315
def get_roles(self):
316316
return Roles().get_all()
317+
318+
def get_role(self, role_guid: GUID):
319+
return Roles().get(role_guid=role_guid)
320+
321+
def create_role(self, role_name, role_description, is_api=False, jit_assignable=True,
322+
jit_assignable_default=True, permissions=[], child_roles=[]):
323+
return Roles().create(role_name=role_name, role_description=role_description,
324+
is_api=is_api, jit_assignable=jit_assignable,
325+
jit_assignable_default=jit_assignable_default,
326+
permissions=permissions, child_roles=child_roles)
327+
328+
def update_role(self, role_name, role_description, role_guid: UUID, is_api=False,
329+
jit_assignable=True, jit_assignable_default=True,
330+
permissions=[], child_roles=[]):
331+
return Roles().update(role_name=role_name, role_description=role_description,
332+
role_guid=role_guid, is_api=is_api, jit_assignable=jit_assignable,
333+
jit_assignable_default=jit_assignable_default,
334+
permissions=permissions, child_roles=child_roles)
335+
336+
def delete_role(self, role_guid: UUID):
337+
return Roles().delete(role_guid=role_guid)
338+
339+
def get_permissions(self):
340+
return Permissions().get_all()
341+
342+
def get_permission(self, permission_guid: UUID):
343+
return Permissions().get(permission_guid=permission_guid)
317344

318345
## SCA APIs - note must be human user to use these, not API user
319346

veracode_api_py/identity.py

Lines changed: 107 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,5 +251,111 @@ def revoke (self, api_id):
251251
return APIHelper()._rest_request(self.base_uri + '/{}'.format(api_id), "DELETE")
252252

253253
class Roles():
254+
base_uri = "api/authn/v2/roles"
254255
def get_all(self):
255-
return APIHelper()._rest_paged_request("api/authn/v2/roles","GET","roles",{'page':0})
256+
return APIHelper()._rest_paged_request(self.base_uri,"GET","roles",{'page':0})
257+
258+
def get(self, role_guid: UUID):
259+
return APIHelper()._rest_request("{}/{}".format(self.base_uri,role_guid),"GET")
260+
261+
def create(self, role_name, role_description, is_api=False, jit_assignable=True,
262+
jit_assignable_default=True, permissions=[], child_roles=[]):
263+
return self._create_or_update("CREATE", role_name=role_name, role_description=role_description,
264+
is_api=is_api, jit_assignable=jit_assignable,
265+
jit_assignable_default=jit_assignable_default,
266+
permissions=permissions, child_roles=child_roles)
267+
268+
def update(self, role_name, role_description, role_guid: UUID, is_api=False,
269+
jit_assignable=True, jit_assignable_default=True,
270+
permissions=[], child_roles=[]):
271+
# TODO handle partial and incremental
272+
return self._create_or_update("UPDATE", role_name=role_name, role_description=role_description,
273+
role_guid=role_guid, is_api=is_api, jit_assignable=jit_assignable,
274+
jit_assignable_default=jit_assignable_default,
275+
permissions=permissions, child_roles=child_roles)
276+
277+
def delete(self, role_guid: UUID):
278+
return APIHelper()._rest_request("{}/{}".format(self.base_uri,role_guid),"DELETE")
279+
280+
def _create_or_update(self, method, role_name, role_description, role_guid: UUID=None, is_api=False,
281+
jit_assignable=True,jit_assignable_default=True,
282+
permissions=[], child_roles=[]):
283+
uri = self.base_uri
284+
if method == 'CREATE':
285+
httpmethod = 'POST'
286+
elif method == 'UPDATE':
287+
uri = uri + '/{}'.format(role_guid)
288+
httpmethod = 'PUT'
289+
else:
290+
return
291+
292+
role_def = { 'role_name': role_name, 'role_description': role_description, 'is_api': is_api,
293+
'jit_assignable': jit_assignable, 'jit_assignable_default': jit_assignable_default}
294+
295+
if len(permissions) > 0:
296+
role_def['permissions'] = permissions
297+
298+
if len(child_roles) > 0:
299+
role_def['child_roles'] = child_roles
300+
301+
payload = json.dumps({"profile": role_def})
302+
return APIHelper()._rest_request(uri,httpmethod,body=payload)
303+
304+
class Permissions():
305+
base_uri = "api/authn/v2/permissions"
306+
def get_all(self):
307+
return APIHelper()._rest_paged_request( self.base_uri,"GET","permissions",{'page':0})
308+
309+
def get(self, permission_guid: UUID):
310+
return APIHelper()._rest_request("{}/{}".format(self.base_uri,permission_guid),"GET")
311+
312+
class JITDefaultSettings():
313+
base_uri = "api/authn/v2/jit_default_settings"
314+
315+
def get(self):
316+
return APIHelper()._rest_request( self.base_uri, "GET")
317+
318+
def create(self, ip_restricted=False,prefer_veracode_data=True, allowed_ip_addresses=[],
319+
use_csv_for_roles_claim=False, use_csv_for_teams_claim=False, use_csv_for_teams_managed_claim=False,
320+
use_csv_for_ip_address_claim=True,teams=[],roles=[]):
321+
return self._create_or_update("CREATE", ip_restricted=ip_restricted, prefer_veracode_data=prefer_veracode_data,
322+
allowed_ip_addresses=allowed_ip_addresses, use_csv_for_roles_claim=use_csv_for_roles_claim,
323+
use_csv_for_teams_claim=use_csv_for_teams_claim,
324+
use_csv_for_teams_managed_claim=use_csv_for_teams_managed_claim,
325+
use_csv_for_ip_address_claim=use_csv_for_ip_address_claim, teams=teams, roles=roles)
326+
327+
def update(self, jit_default_id: UUID, ip_restricted=False,prefer_veracode_data=True, allowed_ip_addresses=[],
328+
use_csv_for_roles_claim=False, use_csv_for_teams_claim=False, use_csv_for_teams_managed_claim=False,
329+
use_csv_for_ip_address_claim=True,teams=[],roles=[]):
330+
return self._create_or_update("UPDATE", jit_default_id = jit_default_id, ip_restricted=ip_restricted,
331+
prefer_veracode_data=prefer_veracode_data,allowed_ip_addresses=allowed_ip_addresses,
332+
use_csv_for_roles_claim=use_csv_for_roles_claim,
333+
use_csv_for_teams_claim=use_csv_for_teams_claim,
334+
use_csv_for_teams_managed_claim=use_csv_for_teams_managed_claim,
335+
use_csv_for_ip_address_claim=use_csv_for_ip_address_claim, teams=teams, roles=roles)
336+
337+
def _create_or_update(self, method, jit_default_id: UUID=None, ip_restricted=False,prefer_veracode_data=True, allowed_ip_addresses=[],
338+
use_csv_for_roles_claim=False, use_csv_for_teams_claim=False, use_csv_for_teams_managed_claim=False,
339+
use_csv_for_ip_address_claim=True,teams=[],roles=[]):
340+
341+
if method == "CREATE":
342+
uri = self.base_uri
343+
httpmethod = "POST"
344+
elif method == "UPDATE":
345+
uri = '{}/{}'.format(self.base_uri, jit_default_id)
346+
httpmethod = "PUT"
347+
else:
348+
return
349+
350+
params = { 'ip_restricted': ip_restricted, 'prefer_veracode_data': prefer_veracode_data, 'allowed_ip_addresses': allowed_ip_addresses,
351+
'use_csv_for_roles_claim': use_csv_for_roles_claim, 'use_csv_for_teams_claim': use_csv_for_teams_claim,
352+
'use_csv_for_teams_managed_claim': use_csv_for_teams_managed_claim, 'use_csv_for_ip_address_claim': use_csv_for_ip_address_claim,
353+
'teams': teams, 'roles': roles}
354+
355+
body = json.dumps(params)
356+
357+
return APIHelper()._rest_request(url=uri, method=httpmethod, params=body)
358+
359+
def delete(self, jit_default_id: UUID):
360+
uri = '{}/{}'.format(self.base_uri, jit_default_id)
361+
return APIHelper()._rest_request( uri, "DELETE")

0 commit comments

Comments
 (0)