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

retry when preauthorized application are invalid #1175

Merged
merged 9 commits into from
Aug 25, 2021
50 changes: 35 additions & 15 deletions src/deployment/registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import argparse
import logging
import re
import time
import urllib.parse
from datetime import datetime, timedelta
Expand Down Expand Up @@ -85,15 +86,16 @@ def query_microsoft_graph(


def retry(
operation: Callable[[], OperationResult],
operation: Callable[[Any], OperationResult],
ranweiler marked this conversation as resolved.
Show resolved Hide resolved
description: str,
tries: int = 10,
wait_duration: int = 10,
data: Any = None,
) -> OperationResult:
count = 0
while True:
try:
return operation()
return operation(data)
except GraphQueryError as err:
error = err
# modeled after AZ-CLI's handling of missing application
Expand Down Expand Up @@ -279,7 +281,7 @@ def create_application_registration(
def add_application_password(
app_object_id: UUID, subscription_id: str
) -> Tuple[str, str]:
def create_password() -> Tuple[str, str]:
def create_password(data: Any) -> Tuple[str, str]:
ranweiler marked this conversation as resolved.
Show resolved Hide resolved
password = add_application_password_impl(app_object_id, subscription_id)
logger.info("app password created")
return password
Expand Down Expand Up @@ -384,18 +386,36 @@ def authorize_application(

onefuzz_app_id = onefuzz_app["id"]

def add_preauthorized_app() -> None:
query_microsoft_graph(
method="PATCH",
resource="applications/%s" % onefuzz_app_id,
body={
"api": {
"preAuthorizedApplications": preAuthorizedApplications.to_list()
}
},
)

retry(add_preauthorized_app, "authorize application")
def add_preauthorized_app(app_list: List[Dict]) -> None:
try:
query_microsoft_graph(
method="PATCH",
resource="applications/%s" % onefuzz_app_id,
body={"api": {"preAuthorizedApplications": app_list}},
)
except GraphQueryError as e:
m = re.search(
"Property PreAuthorizedApplication references "
"applications (.*) that cannot be found.",
ranweiler marked this conversation as resolved.
Show resolved Hide resolved
e.message,
)
if m:
invalid_app_id = m.group(1)
if invalid_app_id:
for app in app_list:
if app["appId"] == invalid_app_id:
logger.warning(
f"removing invalid id {invalid_app_id} for the next request"
)
app_list.remove(app)
chkeita marked this conversation as resolved.
Show resolved Hide resolved

raise e

retry(
add_preauthorized_app,
"authorize application",
data=preAuthorizedApplications.to_list(),
)
except AuthenticationError:
logger.warning("*** Browse to: %s", FIX_URL % onefuzz_app_id)
logger.warning("*** Then add the client application %s", registration_app_id)
Expand Down