Skip to content
This repository has been archived by the owner on Nov 1, 2023. It is now read-only.

Re-enable the retry logic for App Password creation #338

Merged
merged 5 commits into from
Nov 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/deployment/data_migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ def migrate_notification_keys(table_service: TableService) -> None:
notifications = table_service.query_entities(
table_name, select="PartitionKey,RowKey,config"
)
partitionKey = None
bmc-msft marked this conversation as resolved.
Show resolved Hide resolved

count = 0
for entry in notifications:
Expand Down
17 changes: 3 additions & 14 deletions src/deployment/deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,19 +226,7 @@ def check_region(self) -> None:
sys.exit(1)

def create_password(self, object_id: UUID) -> Tuple[str, str]:
# Work-around the race condition where the app is created but passwords cannot
# be created yet.
count = 0
wait = 5
timeout_seconds = 60
while True:
time.sleep(wait)
count += 1
password = add_application_password(object_id)
if password:
return password
if count > timeout_seconds / wait:
raise Exception("creating password failed, trying again")
return add_application_password(object_id)

def setup_rbac(self) -> None:
"""
Expand Down Expand Up @@ -689,7 +677,8 @@ def deploy_app(self) -> None:
if i + 1 < max_tries:
logger.debug("func failure error: %s", err)
logger.warning(
"function failed to deploy, waiting 60 seconds and trying again"
"function failed to deploy, waiting 60 "
"seconds and trying again"
)
time.sleep(60)
if error is not None:
Expand Down
49 changes: 36 additions & 13 deletions src/deployment/registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
from azure.graphrbac.models import (
Application,
ApplicationCreateParameters,
AppRole,
RequiredResourceAccess,
ResourceAccess,
)
Expand All @@ -32,6 +31,7 @@
class GraphQueryError(Exception):
def __init__(self, message: str, status_code: int) -> None:
super(GraphQueryError, self).__init__(message)
self.message = message
self.status_code = status_code


Expand Down Expand Up @@ -211,6 +211,28 @@ def create_application_registration(


def add_application_password(app_object_id: UUID) -> Tuple[str, str]:
# Work-around the race condition where the app is created but passwords cannot
bmc-msft marked this conversation as resolved.
Show resolved Hide resolved
# be created yet.

error: Optional[GraphQueryError] = None
count = 0
tries = 10
wait_duration = 10
while count < tries:
count += 1
try:
return add_application_password_impl(app_object_id)
except GraphQueryError as err:
error = err
logging.warning("unable to create app password: %s", err.message)
time.sleep(wait_duration)
if error:
raise error
else:
raise Exception("unable to create password")


def add_application_password_impl(app_object_id: UUID) -> Tuple[str, str]:
key = uuid4()
password_request = {
"passwordCredential": {
Expand All @@ -222,17 +244,14 @@ def add_application_password(app_object_id: UUID) -> Tuple[str, str]:
),
}
}
try:
password: Dict = query_microsoft_graph(
method="POST",
resource="applications/%s/addPassword" % app_object_id,
body=password_request,
)

return (str(key), password["secretText"])
except GraphQueryError as err:
logger.warning("creating password failed : %s" % err)
raise err
password: Dict = query_microsoft_graph(
method="POST",
resource="applications/%s/addPassword" % app_object_id,
body=password_request,
)

return (str(key), password["secretText"])


def get_application(app_id: UUID) -> Optional[Any]:
Expand Down Expand Up @@ -312,7 +331,10 @@ def update_pool_registration(onefuzz_instance_name: str) -> None:


def assign_scaleset_role(onefuzz_instance_name: str, scaleset_name: str) -> None:
""" Allows the nodes in the scaleset to access the service by assigning their managed identity to the ManagedNode Role """
"""
Allows the nodes in the scaleset to access the service by assigning
their managed identity to the ManagedNode Role
"""

onefuzz_service_appId = query_microsoft_graph(
method="GET",
Expand Down Expand Up @@ -354,7 +376,8 @@ def assign_scaleset_role(onefuzz_instance_name: str, scaleset_name: str) -> None

if not managed_node_role:
raise Exception(
"ManagedNode role not found int the onefuzz application registration. Please redeploy the instance"
"ManagedNode role not found in the OneFuzz application "
"registration. Please redeploy the instance"
)

assignments = query_microsoft_graph(
Expand Down