Skip to content

Commit a4a1416

Browse files
authored
Add support for creating keycloak roles with default groups (#229)
1 parent 3d299b4 commit a4a1416

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

importer/README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@ This script is used to setup keycloak roles and groups. It takes in a csv file w
1212
- `csv_file` : (Required) The csv file with the list of roles
1313
- `group` : (Not required) This is the actual group name. If not passed then the roles will just be created but not assigned to any group
1414
- `roles_max` : (Not required) This is the maximum number of roles to pull from the api. The default is set to 500. If the number of roles in your setup is more than this you will need to change this value
15+
- `defaultgroups` : (Not Required)
1516

1617

1718
### To run script
1819
1. Create virtualenv
1920
2. Install requirements.txt - `pip install -r requirements.txt`
2021
3. Create a `config.py` file. The `sample_config.py` is an example of what this should look like. Populate it with the right credentials, you can either provide an access token or client credentials. Ensure that the user whose details you provide in this config file has the necessary permissions/privilleges.
21-
4. Run script - `python3 main.py --setup roles --csv_file csv/setup/roles.csv --group Supervisor`
22+
4. Run script - `python3 main.py --setup roles --csv_file csv/setup/roles.csv --group Supervisor --defaultgroups true`
2223
5. If you are running the script without `https` setup e.g locally or a server without https setup, you will need to set the `OAUTHLIB_INSECURE_TRANSPORT` environment variable to 1. For example `export OAUTHLIB_INSECURE_TRANSPORT=1 && python3 main.py --setup roles --csv_file csv/setup/roles.csv --group OpenSRP_Provider --log_level debug`
2324
6. You can turn on logging by passing a `--log_level` to the command line as `info`, `debug` or `error`. For example `python3 main.py --setup roles --csv_file csv/setup/roles.csv --group Supervisor --log_level debug`
2425

importer/csv/setup/roles.csv

+1
Original file line numberDiff line numberDiff line change
@@ -115,5 +115,6 @@ PUT_SERVICEREQUEST,,
115115
PUT_STRUCTUREMAP,,
116116
PUT_TASK,,
117117
WEB_CLIENT,,
118+
ANDROID_CLIENT,,
118119
EDIT_KEYCLOAK_USERS,TRUE,manage-users|query-users
119120
VIEW_KEYCLOAK_USERS,TRUE,view-users|query-users|query-groups

importer/main.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@
2020
exit()
2121

2222
global_access_token = ""
23-
23+
DEFAULT_GROUPS = {
24+
"ANDROID_PRACTITIONER" : ["ANDROID_CLIENT"],
25+
"WEB_PRACTITIONER": ["WEB_CLIENT"]
26+
}
2427

2528
# This function takes in a csv file
2629
# reads it and returns a list of strings/lines
@@ -1356,6 +1359,11 @@ def assign_group_roles(role_list, group, roles_max):
13561359
)
13571360

13581361

1362+
def assign_default_groups_roles(roles_max):
1363+
for group_name, roles in DEFAULT_GROUPS.items():
1364+
assign_group_roles(roles, group_name, roles_max)
1365+
1366+
13591367
def delete_resource(resource_type, resource_id, cascade):
13601368
if cascade:
13611369
cascade = "?_cascade=delete"
@@ -1804,6 +1812,7 @@ def filter(self, record):
18041812
@click.option("--setup", required=False)
18051813
@click.option("--group", required=False)
18061814
@click.option("--roles_max", required=False, default=500)
1815+
@click.option("--defaultgroups", required=False, default=False)
18071816
@click.option("--cascade_delete", required=False, default=False)
18081817
@click.option("--only_response", required=False)
18091818
@click.option(
@@ -1832,6 +1841,7 @@ def main(
18321841
setup,
18331842
group,
18341843
roles_max,
1844+
default_groups,
18351845
cascade_delete,
18361846
only_response,
18371847
log_level,
@@ -1954,6 +1964,8 @@ def main(
19541964
if group:
19551965
assign_group_roles(resource_list, group, roles_max)
19561966
logging.info("Processing complete")
1967+
if default_groups:
1968+
assign_default_groups_roles(roles_max)
19571969
elif setup == "clean_duplicates":
19581970
logging.info(
19591971
"You are about to clean/delete Practitioner resources on the HAPI server"

0 commit comments

Comments
 (0)