Skip to content

Commit

Permalink
Handle exceptions in Azure StorageAccounts facade.
Browse files Browse the repository at this point in the history
  • Loading branch information
misg committed Apr 10, 2019
1 parent afe8305 commit 4d6e346
Showing 1 changed file with 27 additions and 13 deletions.
40 changes: 27 additions & 13 deletions ScoutSuite/providers/azure/facade/storageaccounts.py
Expand Up @@ -3,6 +3,7 @@
from azure.mgmt.storage import StorageManagementClient
from azure.mgmt.monitor import MonitorManagementClient
from ScoutSuite.providers.utils import run_concurrently, get_and_set_concurrently
from ScoutSuite.core.console import print_exception


class StorageAccountsFacade:
Expand All @@ -12,17 +13,25 @@ def __init__(self, credentials, subscription_id):
self._client = StorageManagementClient(credentials, subscription_id)

async def get_storage_accounts(self):
storage_accounts = await run_concurrently(
lambda: list(self._client.storage_accounts.list())
)
await get_and_set_concurrently([self._get_and_set_activity_logs], storage_accounts)

return storage_accounts
try:
storage_accounts = await run_concurrently(
lambda: list(self._client.storage_accounts.list())
)
except Exception as e:
print_exception('Failed to retrieve storage accounts: {}'.format(e))
return []
else:
await get_and_set_concurrently([self._get_and_set_activity_logs], storage_accounts)
return storage_accounts

async def get_blob_containers(self, resource_group_name, storage_account_name):
return await run_concurrently(
lambda: list(self._client.blob_containers.list(resource_group_name, storage_account_name).value)
)
try:
return await run_concurrently(
lambda: list(self._client.blob_containers.list(resource_group_name, storage_account_name).value)
)
except Exception as e:
print_exception('Failed to retrieve blob containers: {}'.format(e))
return []

async def _get_and_set_activity_logs(self, storage_account):
client = MonitorManagementClient(self._credentials, self._subscription_id)
Expand All @@ -40,7 +49,12 @@ async def _get_and_set_activity_logs(self, storage_account):
"eventTimestamp le {}".format(utc_now.strftime(time_format)),
"resourceId eq {}".format(storage_account.id),
])
activity_logs = await run_concurrently(
lambda: list(client.activity_logs.list(filter=logs_filter, select="eventTimestamp, operationName"))
)
setattr(storage_account, 'activity_logs', activity_logs)
try:
activity_logs = await run_concurrently(
lambda: list(client.activity_logs.list(filter=logs_filter, select="eventTimestamp, operationName"))
)
except Exception as e:
print_exception('Failed to retrieve activity logs: {}'.format(e))
setattr(storage_account, 'activity_logs', [])
else:
setattr(storage_account, 'activity_logs', activity_logs)

0 comments on commit 4d6e346

Please sign in to comment.