From f6271513a0e1a315f562f11af51edea40f1ac232 Mon Sep 17 00:00:00 2001
From: James Robinson <james.em.robinson@gmail.com>
Date: Thu, 11 Apr 2024 14:18:44 +0100
Subject: [PATCH] :bug: Remove infinite loop following suggestion from
 @JimMadge

Co-authored-by: Jim Madge <jim+github@jmadge.com>
---
 data_safe_haven/external/api/graph_api.py | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/data_safe_haven/external/api/graph_api.py b/data_safe_haven/external/api/graph_api.py
index de4fb1bb99..ea6db2dac3 100644
--- a/data_safe_haven/external/api/graph_api.py
+++ b/data_safe_haven/external/api/graph_api.py
@@ -240,13 +240,20 @@ def create_application(
                     self.grant_application_role_permissions(application_name, scope)
                 for scope in delegated_scopes:
                     self.grant_delegated_role_permissions(application_name, scope)
-                while True:
+                attempts = 0
+                max_attempts = 5
+                while attempts < max_attempts:
                     if application_sp := self.get_service_principal_by_name(
                         application_name
                     ):
                         if self.read_application_permissions(application_sp["id"]):
                             break
                     time.sleep(10)
+                    attempts += 1
+
+                if attempts == max_attempts:
+                    msg = "Maximum attempts to validate service principle permissions exceeded"
+                    raise DataSafeHavenMicrosoftGraphError(msg)
 
             # Return JSON representation of the AzureAD application
             return json_response