# Buggy code
def _match_rbac_roles_and_principals(self):
    """
    Matches ARM role assignments to AAD service principals
    """
    if "rbac" in self.service_list and "aad" in self.service_list:
        for subscription in self.services["rbac"]["subscriptions"]:
            for assignment in self.services["rbac"]["subscriptions"][subscription][
                "role_assignments"
            ].values():
                role_id = assignment["role_definition_id"].split("/")[-1]
                for group in self.services["aad"]["groups"]:
                    if group == assignment["principal_id"]:
                        self.services["aad"]["groups"][group]["roles"].append(
                            {"subscription_id": subscription, "role_id": role_id}
                        )
                        self.services["rbac"]["subscriptions"][subscription]["roles"][
                            role_id
                        ]["assignments"]["groups"].append(group)
                        self.services["rbac"]["subscriptions"][subscription]["roles"][
                            role_id
                        ]["assignments_count"] += 1
                for user in self.services["aad"]["users"]:
                    if user == assignment["principal_id"]:
                        self.services["aad"]["users"][user]["roles"].append(
                            {"subscription_id": subscription, "role_id": role_id}
                        )
                        self.services["rbac"]["subscriptions"][subscription]["roles"][
                            role_id
                        ]["assignments"]["users"].append(user)
                        self.services["rbac"]["subscriptions"][subscription]["roles"][
                            role_id
                        ]["assignments_count"] += 1
                for service_principal in self.services["aad"]["service_principals"]:
                    if service_principal == assignment["principal_id"]:
                        self.services["aad"]["service_principals"][service_principal][
                            "roles"
                        ].append({"subscription_id": subscription, "role_id": role_id})
                        self.services["rbac"]["subscriptions"][subscription]["roles"][
                            role_id
                        ]["assignments"]["service_principals"].append(service_principal)
                        self.services["rbac"]["subscriptions"][subscription]["roles"][
                            role_id
                        ]["assignments_count"] += 1


# Traceback (azure.graphrbac.models.graph_error_py3.GraphErrorException)
2020-07-24 03:56:32 ubuntu scout[3614] ERROR aad.py L30: Failed to retrieve user xxx-xxx-xxx-xxx-xxx: Resource 'xxx-xxxx-xxx-xxx-xxx-xx' does not exist or one of its queried reference-property objects are not present.
Traceback (most recent call last):
File "/home/victor/Documents/scout_59/ScoutSuite/ScoutSuite/providers/azure/facade/aad.py", line 30, in get_user
return await run_concurrently(lambda: self.get_client().users.get(user_id))
File "/home/victor/Documents/scout_59/ScoutSuite/ScoutSuite/providers/utils.py", line 24, in run_concurrently
return await run_function_concurrently(function)
File "/usr/lib/python3.6/concurrent/futures/thread.py", line 56, in run
result = self.fn(*self.args, **self.kwargs)
File "/home/victor/Documents/scout_59/ScoutSuite/ScoutSuite/providers/azure/facade/aad.py", line 30, in <lambda>
return await run_concurrently(lambda: self.get_client().users.get(user_id))
File "/home/victor/.local/lib/python3.6/site-packages/azure/graphrbac/operations/users_operations.py", line 218, in get
raise models.GraphErrorException(self._deserialize, response)
azure.graphrbac.models.graph_error_py3.GraphErrorException: Resource 'xxx-xxx-xxx-xxx-xxx' does not exist or one of its queried reference-property objects are not present.
Traceback (most recent call last):
File "./scout.py", line 8, in <module>
sys.exit(run_from_cli())
File "/home/victor/Documents/scout_59/ScoutSuite/ScoutSuite/__main__.py", line 77, in run_from_cli
programmatic_execution=False)
File "/home/victor/Documents/scout_59/ScoutSuite/ScoutSuite/__main__.py", line 131, in run
result = loop.run_until_complete(_run(**locals()))  # pass through all the parameters
File "/usr/lib/python3.6/asyncio/base_events.py", line 484, in run_until_complete
return future.result()
File "/home/victor/Documents/scout_59/ScoutSuite/ScoutSuite/__main__.py", line 258, in _run
await cloud_provider.fetch(regions=regions, excluded_regions=excluded_regions)
File "/home/victor/Documents/scout_59/ScoutSuite/ScoutSuite/providers/base/provider.py", line 81, in fetch
await self.services.fetch(self.service_list, regions, excluded_regions)
File "/home/victor/Documents/scout_59/ScoutSuite/ScoutSuite/providers/azure/services.py", line 78, in fetch
await self.aad.fetch_additional_users(user_list)
File "/home/victor/Documents/scout_59/ScoutSuite/ScoutSuite/providers/azure/resources/aad/base.py", line 26, in fetch_additional_users
await additional_users.fetch_additional_users(user_list)
File "/home/victor/Documents/scout_59/ScoutSuite/ScoutSuite/providers/azure/resources/aad/users.py", line 17, in fetch_additional_users
id, user = await self._parse_user(raw_user)
File "/home/victor/Documents/scout_59/ScoutSuite/ScoutSuite/providers/azure/resources/aad/users.py", line 22, in _parse_user
user_dict['id'] = raw_user.object_id
AttributeError: 'list' object has no attribute 'object_id'

# Fixed code
def _match_rbac_roles_and_principals(self):
    """
    Matches ARM role assignments to AAD service principals
    """
    try:
        if "rbac" in self.service_list and "aad" in self.service_list:
            for subscription in self.services["rbac"]["subscriptions"]:
                for assignment in self.services["rbac"]["subscriptions"][subscription][
                    "role_assignments"
                ].values():
                    role_id = assignment["role_definition_id"].split("/")[-1]
                    for group in self.services["aad"]["groups"]:
                        if group == assignment["principal_id"]:
                            self.services["aad"]["groups"][group]["roles"].append(
                                {"subscription_id": subscription, "role_id": role_id}
                            )
                            self.services["rbac"]["subscriptions"][subscription][
                                "roles"
                            ][role_id]["assignments"]["groups"].append(group)
                            self.services["rbac"]["subscriptions"][subscription][
                                "roles"
                            ][role_id]["assignments_count"] += 1
                    for user in self.services["aad"]["users"]:
                        if user == assignment["principal_id"]:
                            self.services["aad"]["users"][user]["roles"].append(
                                {"subscription_id": subscription, "role_id": role_id}
                            )
                            self.services["rbac"]["subscriptions"][subscription][
                                "roles"
                            ][role_id]["assignments"]["users"].append(user)
                            self.services["rbac"]["subscriptions"][subscription][
                                "roles"
                            ][role_id]["assignments_count"] += 1
                    for service_principal in self.services["aad"]["service_principals"]:
                        if service_principal == assignment["principal_id"]:
                            self.services["aad"]["service_principals"][
                                service_principal
                            ]["roles"].append(
                                {"subscription_id": subscription, "role_id": role_id}
                            )
                            self.services["rbac"]["subscriptions"][subscription][
                                "roles"
                            ][role_id]["assignments"]["service_principals"].append(
                                service_principal
                            )
                            self.services["rbac"]["subscriptions"][subscription][
                                "roles"
                            ][role_id]["assignments_count"] += 1
    except Exception as e:
        print_exception("Unable to match RBAC roles and principals: {}".format(e))