Skip to content

Commit fd647c7

Browse files
authored
Merge pull request #25 from blackducksoftware/gsnyder/project-and-user-roles-improvements
Extending capabilities for role management of users, user groups, and project roles
2 parents d16ae3f + 1f9b618 commit fd647c7

26 files changed

+1411
-33
lines changed

blackduck/HubRestApi.py

Lines changed: 266 additions & 21 deletions
Large diffs are not rendered by default.

blackduck/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11

2-
VERSION = (0, 0, 9)
2+
VERSION = (0, 0, 10)
33

44
__version__ = '.'.join(map(str, VERSION))
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
3+
import argparse
4+
import json
5+
import logging
6+
import sys
7+
8+
9+
from blackduck.HubRestApi import HubInstance
10+
11+
12+
parser = argparse.ArgumentParser()
13+
parser.add_argument("project_name")
14+
parser.add_argument("application_id")
15+
parser.add_argument("--overwrite", default = False, action='store_true', help="If overwrite is set will over write the current application id with the new one")
16+
17+
args = parser.parse_args()
18+
19+
hub = HubInstance()
20+
21+
# TODO: Debug overwrite, keep getting 412 response on PUT
22+
23+
response = hub.assign_project_application_id(args.project_name, args.application_id, overwrite=args.overwrite)
24+
25+
if response and response.status_code == 201:
26+
logging.info("successfully assigned application id {} to project {}".format(args.application_id, args.project_name))
27+
else:
28+
logging.warning("Failed to assign application id {} to project {}".format(args.application_id, args.project_name))
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env python
2+
3+
import argparse
4+
import json
5+
6+
from blackduck.HubRestApi import HubInstance
7+
8+
hub = HubInstance()
9+
10+
global_roles = [
11+
"Component Manager",
12+
"Global Code Scanner",
13+
"License Manager",
14+
"Policy Manager",
15+
"Project Creator",
16+
"Super User",
17+
"System Administrator",
18+
"All"
19+
]
20+
21+
parser = argparse.ArgumentParser("Assign a global role to a user group")
22+
parser.add_argument("group_name", help="The user group name")
23+
parser.add_argument("role", choices=global_roles, help="Assign a global role to the user group. If you choose 'All' then all global roles will be assigned to the user group")
24+
25+
args = parser.parse_args()
26+
27+
user_groups = hub.get_user_groups(parameters={'q':args.group_name})
28+
29+
if user_groups['totalCount'] == 1:
30+
user_group = user_groups['items'][0]
31+
32+
if user_group:
33+
if args.role == 'All':
34+
roles_to_assign = [r for r in global_roles if r != 'All']
35+
else:
36+
roles_to_assign = [args.role]
37+
for role_to_assign in roles_to_assign:
38+
response = hub.assign_role_to_user_or_group(role_to_assign, user_group)
39+
if response.status_code == 201:
40+
print("Successfully assigned role {} to user group {}".format(role_to_assign, args.group_name))
41+
elif response.status_code == 412:
42+
print("Failed to assign role {} to group {} due to status code 412. Has the role already been assigned?".format(role_to_assign, args.group_name))
43+
else:
44+
print("Failed to assign role {} to group {}. status code: {}".format(role_to_assign, args.group_name, response.status_code))
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'''
2+
Created on January 21, 2019
3+
4+
@author: gsnyder
5+
6+
Assign a user group to a project, providing the project-specific roles the user group should have (on the project)
7+
8+
'''
9+
10+
import argparse
11+
import json
12+
import logging
13+
import sys
14+
15+
from blackduck.HubRestApi import HubInstance
16+
17+
18+
project_roles = [
19+
"BOM Manager",
20+
"Policy Violation Reviewer",
21+
"Project Code Scanner",
22+
"Project Manager",
23+
"Security Manager",
24+
]
25+
26+
project_roles_str = ",".join(project_roles)
27+
28+
parser = argparse.ArgumentParser("Assign a user group to a project along with a list of project roles (optional)")
29+
parser.add_argument("group", help="The name of a user group to assign to the project")
30+
parser.add_argument("project", help="The name of the project you want to assign the group to")
31+
parser.add_argument(
32+
"--project_roles", help="A file with the project-specific roles ({}) that will be granted to the user group, one per line".format(
33+
project_roles_str))
34+
35+
args = parser.parse_args()
36+
37+
logging.basicConfig(format='%(asctime)s:%(levelname)s:%(message)s', stream=sys.stdout, level=logging.DEBUG)
38+
39+
hub = HubInstance()
40+
41+
if args.project_roles:
42+
project_roles_l = list()
43+
with open(args.project_roles) as f:
44+
project_roles_l = [role.strip() for role in f.readlines()]
45+
else:
46+
project_roles_l = []
47+
48+
response = hub.assign_user_group_to_project(args.project, args.group, project_roles_l)
49+
if response and response.status_code == 201:
50+
logging.info("Successfully assigned user group {} to project {} with project-roles {}".format(
51+
args.group, args.project, project_roles_l))
52+
else:
53+
logging.warning("Failed to assign group {} to project {}".format(args.group, args.project))

examples/create_policy.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from blackduck.HubRestApi import HubInstance
2+
3+
hub = HubInstance()
4+
5+
6+
policy_data = {"name":"new-rule",
7+
"description":"description",
8+
"severity":"BLOCKER",
9+
"enabled":True,
10+
"overridable":True,
11+
"policyType":"BOM_COMPONENT_DISALLOW",
12+
"expression":{
13+
"operator":"AND",
14+
"expressions":[{
15+
"name":"HIGH_SEVERITY_VULN_COUNT",
16+
"operation":"EQ",
17+
"parameters":{"values":["0"]}}]},"wait":True}
18+
19+
import pdb; pdb.set_trace()
20+
result = hub.create_policy(policy_data)
21+
22+
print(result)

examples/create_project.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
import argparse
3+
import json
4+
5+
from blackduck.HubRestApi import HubInstance
6+
7+
parser = argparse.ArgumentParser()
8+
parser.add_argument("project_name")
9+
parser.add_argument("--version", default="1.0", type=str)
10+
parser.add_argument("--description", default="", type=str)
11+
12+
args = parser.parse_args()
13+
14+
hub = HubInstance()
15+
16+
response = hub.create_project(args.project_name, args.version, parameters = {
17+
"description": args.description
18+
})
19+
print(response.status_code)

examples/create_user_group.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
'''
2+
Created on Dec 4, 2018
3+
4+
@author: gsnyder
5+
6+
Create a new user_group
7+
8+
'''
9+
import argparse
10+
import json
11+
import logging
12+
from pprint import pprint
13+
import sys
14+
15+
from blackduck.HubRestApi import HubInstance
16+
17+
18+
user_group_types = ['INTERNAL', 'EXTERNAL']
19+
20+
parser = argparse.ArgumentParser("Create a new user_group")
21+
parser.add_argument("usergroupname")
22+
parser.add_argument("--externalGroupName", default=None)
23+
parser.add_argument("--type", choices=user_group_types, default="INTERNAL")
24+
parser.add_argument("--active", default=True)
25+
26+
args = parser.parse_args()
27+
28+
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
29+
30+
hub = HubInstance()
31+
32+
if args.externalGroupName:
33+
external_group_name = args.externalGroupName
34+
else:
35+
external_group_name = args.usergroupname
36+
37+
if args.type == 'INTERNAL':
38+
location = hub.create_user_group({
39+
'name': args.usergroupname,
40+
'createdFrom': args.type,
41+
'active': args.active,
42+
})
43+
elif args.type == 'EXTERNAL':
44+
location = hub.create_user_group({
45+
'name': args.usergroupname,
46+
'externalName': external_group_name,
47+
'createdFrom': args.type,
48+
'active': args.active,
49+
})
50+
else:
51+
print("You must choose a valid type {}".format(user_group_types))
52+
53+
logging.info("Created user_group {} at location {}".format(args.usergroupname, location))
54+
55+
56+
57+
58+
59+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import argparse
2+
3+
4+
from blackduck.HubRestApi import HubInstance
5+
6+
global_roles = [
7+
"Component Manager",
8+
"Global Code Scanner",
9+
"License Manager",
10+
"Policy Manager",
11+
"Project Creator",
12+
"Super User",
13+
"System Administrator",
14+
"All"
15+
]
16+
17+
parser = argparse.ArgumentParser("Delete a global role from a user group")
18+
parser.add_argument("group_name")
19+
parser.add_argument("role", choices=global_roles, help="Delete a global role from the user group. If set to 'All' will delete all global roles from the user group")
20+
21+
args = parser.parse_args()
22+
23+
hub = HubInstance()
24+
25+
group = hub.get_user_group_by_name(args.group_name)
26+
if group:
27+
if args.role == 'All':
28+
roles_to_delete = [r for r in global_roles if r != 'All']
29+
else:
30+
roles_to_delete = [args.role]
31+
for role_to_delete in roles_to_delete:
32+
print("Deleting role {} from user group {}".format(role_to_delete, args.group_name))
33+
response = hub.delete_role_from_user_or_group(role_to_delete, group)
34+
print("Deleted role {}".format(role_to_delete))

examples/dump_user_groups.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,7 @@ def serialize_to_file(file, data):
3939
user_group_data_prepped_to_copy = []
4040

4141
for user_group in user_groups_to_copy['items']:
42-
user_group_roles_response = source_hub.get_roles_for_user_or_group(user_group)
43-
if user_group_roles_response:
44-
user_group_roles = user_group_roles_response.json()
45-
else:
46-
user_group_roles = []
42+
user_group_roles = source_hub.get_roles_for_user_or_group(user_group)
4743

4844
user_group_data_raw.append({
4945
'user_group': user_group, 'roles': user_group_roles

0 commit comments

Comments
 (0)