This is a MVP script for loading bulk users to Superset via the API. It expects a csv file, see /src/template.csv for a sample.
- Requires an admin account to manage roles, RLS policies and users.
- When working with the different entities, there are a few things to keep in mind:
- roles created have permissions allowing them access to desired datasources
- RLS policies are attached to roles and ensure role based access to the datasources
- users are assigned roles, and can be assigned multiple roles
- Different Superset roles, RLS policies and users can be created depending on the user roles e.g CHA, County User, Subcounty User, National
- The API does not allow bulk upserts of roles, RLS policies and users. You will hit a rate limit if you make too many requests.
- Populate your csv file, refer to the
./src/template.csvfor a sample - Define your environment variables - make a copy of
.env.templateand rename to.env - Install dependencies: run
npm i
Functions to create and manage roles are found in ./src/service/role-service.ts. The process for creating roles is as follows:
- There are already a number of roles present on Superset. They can be fetched from the API using the
getRolesfunction. This method will fetch roles from Redis or Superset and return them as an array of SupersetRole objects. - You can use the
getRoleByNamefunction to fetch a specific role by name. - You can use the
createRolesfunction to create new roles on Superset. Only the name is required for this step. - You can use the
saveSupersetRolesfunction to save the roles to Redis. This step is optional. Given the large number of roles present, it is recommended to save them to Redis for faster access as opposed to fetching them from Superset every time. - Update the role's permissions using the
updateRolePermissionsfunction. This will update the permissions for the role on Superset and grant access to the datasources specified in the permissions array. The permissions array is a list of permission IDs for datasets. - Base Roles are defined for CHA and County. They can be fetched using the
fetchBaseRolesfunction. The permissions for these can be used to update other roles. More base roles can be added as needed. - Creating a new role should be a two step process. First create the role, then update the permissions. This is to ensure the permissions are applied to the role.
Functions to create and manage RLS policies are found in ./src/service/rls-service.ts. The process for creating RLS policies is as follows:
- RLS policies can be fetched using the
fetchRLSPoliciesfunction. This function returns an array of RLS policies. - RLS policies can be created using the
createRLSPolicyfunction. This function requires a name, group_key, clause, description, filter_type, roles and tables.Group keydefines the column used in filtering e.gcounty_name,chu_codeetc. The column must exist in the table for the policy to work.Clauseis what is filled in the filter e.gcounty_name='Nairobi'Rolesan array of role IDs that the policy should apply to.Tablesan array of table IDs where the policy applies.Filter typeis always set toRegular.
- Base RLS policies are defined for County and CHU. More can be added as needed. These can be used to easily create more policies.
- RLS policy tables can be updated using the
updateRLSTablesfunction. In most cases, you will copy over the tables from the respective base RLS policy.
Functions to manage users are found in ./src/service/user-service.ts. The steps for user creation are as follows:
- Create a user using the
createUserOnSupersetfunction. This function requires a username, email, first name, last name, roles, and password.Rolesis an array of role IDs that the user be given. This determines their access level.Passwordis optional. It is recommended to use the existing passwords that users already have for eCHIS to prevent them from having too many passwords.
- There
create-chas-from-csvscript can be leveraged as a guide to create users from a CSV file. It does the following:- Fetches and creates roles.
- Creates RLS policies.
- Creates users.
- Detects and skips duplicates (on Superset) for roles, users and RLS policies.
- User details missing usernames and passwords are auto-generated.
- Creates CSV file with user details.
This script can be run multiple times without worry of duplicates.
- Always test roles and RLS policies in a non-production environment first
- Regularly audit user permissions and access levels
- Back up existing configurations before making bulk changes
- Monitor API rate limits when creating multiple entities