1010import logging
1111
1212sys .path .append (os .path .dirname (os .path .dirname (os .path .abspath (__file__ ))))
13- from confidential_compute import ConfidentialCompute , ConfidentialComputeConfig , MissingConfig , ConfidentialComputeStartupException
14- from azure .identity import DefaultAzureCredential , CredentialUnavailableError
13+ from confidential_compute import ConfidentialCompute , MissingConfig , SecretAccessException , AuxiliariesException , ConfidentialComputeStartupException
14+ from azure .identity import DefaultAzureCredential
1515from azure .keyvault .secrets import SecretClient
16- from azure .core .exceptions import ResourceNotFoundError , HttpResponseError
1716
1817class AzureEntryPoint (ConfidentialCompute ):
1918
@@ -29,41 +28,14 @@ def __init__(self):
2928 super ().__init__ ()
3029
3130 def __check_env_variables (self ):
31+ # Check essential env variables
3232 if AzureEntryPoint .kv_name is None :
3333 raise MissingConfig (self .__class__ .__name__ , ["VAULT_NAME" ])
3434 if AzureEntryPoint .secret_name is None :
3535 raise MissingConfig (self .__class__ .__name__ , ["OPERATOR_KEY_SECRET_NAME" ])
3636 if AzureEntryPoint .env_name is None :
3737 raise MissingConfig (self .__class__ .__name__ , ["DEPLOYMENT_ENVIRONMENT" ])
38- logging .info ("Env variables validation success" )
39-
40- def __set_environment (self ):
41- self .configs ["environment" ] = AzureEntryPoint .env_name
42-
43- def _set_secret (self , secret_identifier : str = None ):
44- try :
45- credential = DefaultAzureCredential ()
46- kv_URL = f"https://{ AzureEntryPoint .kv_name } .vault.azure.net"
47- secret_client = SecretClient (vault_url = kv_URL , credential = credential )
48- secret = secret_client .get_secret (AzureEntryPoint .secret_name )
49- # print(f"Secret Value: {secret.value}")
50- self .configs ["api_token" ] = secret .value
51-
52- except CredentialUnavailableError as auth_error :
53- logging .error (f"Read operator key, authentication error: { auth_error } " )
54- raise
55-
56- except ResourceNotFoundError as not_found_error :
57- logging .error (f"Read operator key, secret not found: { AzureEntryPoint .secret_name } . Error: { not_found_error } " )
58- raise
59-
60- except HttpResponseError as http_error :
61- logging .error (f"Read operator key, HTTP error occurred: { http_error } " )
62- raise
63-
64- except Exception as e :
65- logging .error (f"Read operator key, an unexpected error occurred: { e } " )
66- raise
38+ logging .info ("Environment variables validation success" )
6739
6840 def __create_final_config (self ):
6941 TARGET_CONFIG = f"/app/conf/{ AzureEntryPoint .env_name } -uid2-config.json"
@@ -93,13 +65,37 @@ def __create_final_config(self):
9365
9466 with open (AzureEntryPoint .FINAL_CONFIG , "r" ) as file :
9567 logging .info (file .read ())
96-
97- def __set_baseurls (self ):
68+
69+ def __set_base_urls (self ):
9870 with open (AzureEntryPoint .FINAL_CONFIG , "r" ) as file :
9971 jdata = json .load (file )
10072 self .configs ["core_base_url" ] = jdata ["core_attest_url" ]
10173 self .configs ["optout_base_url" ] = jdata ["optout_api_uri" ]
10274
75+ def __set_api_token (self ):
76+ try :
77+ credential = DefaultAzureCredential ()
78+ kv_URL = f"https://{ AzureEntryPoint .kv_name } .vault.azure.net"
79+ secret_client = SecretClient (vault_url = kv_URL , credential = credential )
80+ secret = secret_client .get_secret (AzureEntryPoint .secret_name )
81+ # print(f"Secret Value: {secret.value}")
82+ self .configs ["api_token" ] = secret .value
83+
84+ except Exception as e :
85+ errormsg = f"Read operator key, an unexpected error occurred: { e } "
86+ logging .error (errormsg )
87+ raise SecretAccessException (self .__class__ .__name__ , errormsg )
88+
89+ def _set_confidential_config (self , secret_identifier : str = None ):
90+ self .configs ["skip_validations" ] = os .getenv ("SKIP_VALIDATIONS" , "false" ).lower () == "true"
91+ self .configs ["debug_mode" ] = os .getenv ("DEBUG_MODE" , "false" ).lower () == "true"
92+ self .configs ["environment" ] = AzureEntryPoint .env_name
93+
94+ # set self.configs["api_token"]
95+ self .__set_api_token ()
96+ # set base urls from final config file
97+ self .__set_base_urls ()
98+
10399 def __run_operator (self ):
104100
105101 # Start the operator
@@ -119,46 +115,46 @@ def __run_operator(self):
119115 logging .info ("-- starting java operator application" )
120116 self .run_command (java_command , separate_process = False )
121117
122- def __wait_for_sidecar (self ):
118+ def _setup_auxiliaries (self ):
123119 logging .info ("Waiting for sidecar ..." )
124120
125- url = "http://169.254.169.254/ping"
121+ MAX_RETRIES = 15
122+ PING_URL = "http://169.254.169.254/ping"
126123 delay = 1
127- max_retries = 15
128124
129- while True :
125+ for attempt in range ( 1 , MAX_RETRIES + 1 ) :
130126 try :
131- response = requests .get (url , timeout = 5 )
127+ response = requests .get (PING_URL , timeout = 5 )
132128 if response .status_code in [200 , 204 ]:
133- logging .info ("Sidecar started" )
129+ logging .info ("Sidecar started successfully. " )
134130 return
135131 else :
136- error_msg = f"Unexpected status code: { response .status_code } , response: { response .text } "
137- raise Exception (error_msg )
132+ logging .warning (
133+ f"Attempt { attempt } : Unexpected status code { response .status_code } . Response: { response .text } "
134+ )
138135 except Exception as e :
139- if delay > max_retries :
140- logging .error (f"Sidecar failed to start after { delay } retries with error { e } " , exc_info = True )
141- sys .exit (1 )
142- logging .info (f"Sidecar not started. Retrying in { delay } seconds... { e } " )
143- time .sleep (delay )
144- delay += 1
136+ logging .info (f"Attempt { attempt } : Error during request - { e } " )
137+
138+ if attempt == MAX_RETRIES :
139+ logging .error (
140+ f"Sidecar failed to start after { MAX_RETRIES } attempts. Exiting."
141+ )
142+ raise AuxiliariesException (self .__class__ .__name__ )
143+
144+ logging .info (f"Retrying in { delay } seconds... (Attempt { attempt } /{ MAX_RETRIES } )" )
145+ time .sleep (delay )
146+ delay += 1
145147
146148 def run_compute (self ) -> None :
147149 """Main execution flow for confidential compute."""
148150 self .__check_env_variables ()
149- self ._set_secret ()
150- self .__set_environment ()
151151 self .__create_final_config ()
152- self .__set_baseurls ()
152+ self ._set_confidential_config ()
153153 if not self .configs .get ("skip_validations" ):
154154 self .validate_configuration ()
155- self .__wait_for_sidecar ()
155+ self ._setup_auxiliaries ()
156156 self .__run_operator ()
157157
158- def _setup_auxiliaries (self ) -> None :
159- """ Sets up auxiliary processes required for confidential computing. """
160- pass
161-
162158 def _validate_auxiliaries (self ) -> None :
163159 """ Validates auxiliary services are running."""
164160 pass
0 commit comments