MCPcopy Create free account
hub / github.com/Keeper-Security/Commander / execute

Method execute

keepercommander/commands/msp.py:1460–1555  ·  view source on GitHub ↗
(self, params, **kwargs)

Source from the content-addressed store, hash-verified

1458 return msp_copy_role_parser
1459
1460 def execute(self, params, **kwargs):
1461 src_roles = {}
1462 roles = kwargs.get('role')
1463 if not roles:
1464 raise CommandError('msp-copy-role', f'Source role parameter is required')
1465
1466 if not isinstance(roles, list):
1467 roles = [roles]
1468
1469 for role_name in roles:
1470 if not isinstance(role_name, str):
1471 role_name = str(role_name)
1472 matched_roles = list(MSPCopyRoleCommand.find_roles(params, role_name))
1473 if len(matched_roles) == 1:
1474 role = matched_roles[0]
1475 src_roles[role['role_id']] = role
1476 elif len(matched_roles) > 1:
1477 raise Exception(f'There are more than one roles with name \"{role_name}\". Use Role ID')
1478 else:
1479 raise Exception(f'Role \"{role_name}\" not found')
1480
1481 managed_companies = params.enterprise.get('managed_companies', [])
1482 mcs = {}
1483 for mc_name in kwargs.get('mc') or []:
1484 mc = get_mc_by_name_or_id(managed_companies, mc_name)
1485 if not mc:
1486 raise CommandError('msp-copy-role', f'Managed Company \"{mc_name}\" not found')
1487 mcs[mc['mc_enterprise_id']] = mc
1488
1489 for mc in mcs.values():
1490 mc_id = mc['mc_enterprise_id']
1491 mc_params = api.login_and_get_mc_params_login_v3(params, mc_id)
1492 node_id = next((x['node_id'] for x in mc_params.enterprise.get('nodes', []) if not x.get('parent_id')), None)
1493 mc_rqs = []
1494 for role in src_roles.values():
1495 src_role_id = role['role_id']
1496 role_name = role['data'].get('displayname') or ''
1497 if not role_name:
1498 continue
1499 dst_roles = list(MSPCopyRoleCommand.find_roles(mc_params, role_name))
1500 if len(dst_roles) > 1:
1501 logging.warning('MC # %d: There are more than one roles with name \"%s\". Skipping', mc_id, role_name)
1502 continue
1503
1504 if len(dst_roles) == 1:
1505 dst_role_id = dst_roles[0]['role_id']
1506 else:
1507 dst_role_id = self.get_enterprise_id(mc_params)
1508 dt = json.dumps({ "displayname": role_name })
1509 mc_rqs.append({
1510 "command": 'role_add',
1511 "role_id": dst_role_id,
1512 "node_id": node_id,
1513 "encrypted_data": utils.base64_url_encode(
1514 crypto.encrypt_aes_v1(dt.encode('utf-8'), mc_params.enterprise['unencrypted_tree_key'])),
1515 "visible_below": role.get('visible_below', True),
1516 "new_user_inherit": role.get('new_user_inherit', False)
1517 })

Callers

nothing calls this directly

Calls 9

get_mc_by_name_or_idFunction · 0.85
find_rolesMethod · 0.80
warningMethod · 0.80
get_enterprise_idMethod · 0.80
get_enforcement_valueMethod · 0.80
CommandErrorClass · 0.70
getMethod · 0.45
encodeMethod · 0.45
infoMethod · 0.45

Tested by

no test coverage detected