2020logger = get_logger (__name__ )
2121ACR_REGISTRY_NAME_PATTERN = r"^([a-zA-Z0-9]+\.azurecr\.io)"
2222
23+ # Azure Container Registry constants
24+ ACR_OAUTH_SCOPE = "https://containerregistry.azure.net/.default"
25+ ACR_IMPORT_SERVICE_PRINCIPAL_ID = "00000000-0000-0000-0000-000000000000"
26+
2327
2428# pylint: disable=too-few-public-methods
2529class ContainerRegistry :
@@ -239,7 +243,15 @@ def copy_image_to_target_acr(
239243 # the format of input.json. Our usage here won't work cross-tenant since
240244 # we're attempting to get the token (source) with the same context as that
241245 # in which we are creating the ACR (i.e. the target tenant)
242- get_token_cmd = [str (shutil .which ("az" )), "account" , "get-access-token" ]
246+
247+ # Get access token with ACR scope to ensure proper repository permissions
248+ get_token_cmd = [
249+ str (shutil .which ("az" )),
250+ "account" ,
251+ "get-access-token" ,
252+ "--scope" ,
253+ ACR_OAUTH_SCOPE
254+ ]
243255 # Dont use call_subprocess_raise_output here as we don't want to log the
244256 # output
245257 called_process = subprocess .run ( # noqa: S603
@@ -269,6 +281,39 @@ def copy_image_to_target_acr(
269281 )
270282
271283 try :
284+ # Extract source registry name for the --registry parameter
285+ source_registry_name = self .registry_name .replace (".azurecr.io" , "" )
286+
287+ # Get the full resource ID for the source registry
288+ source_registry_id_cmd = [
289+ str (shutil .which ("az" )),
290+ "acr" ,
291+ "show" ,
292+ "--name" ,
293+ source_registry_name ,
294+ "--query" ,
295+ "id" ,
296+ "--output" ,
297+ "tsv"
298+ ]
299+ try :
300+ called_process = subprocess .run ( # noqa: S603
301+ source_registry_id_cmd ,
302+ encoding = "utf-8" ,
303+ capture_output = True ,
304+ text = True ,
305+ check = True ,
306+ )
307+ source_registry_id = called_process .stdout .strip ()
308+ except subprocess .CalledProcessError as exc :
309+ error_output = exc .stderr or exc .stdout or str (exc )
310+ raise ClientRequestError (
311+ "Failed to resolve source registry "
312+ f"'{ source_registry_name } '. Please ensure the registry exists "
313+ "and that you have sufficient permissions. "
314+ f"Details: { error_output } "
315+ ) from exc
316+
272317 acr_import_image_cmd = [
273318 str (shutil .which ("az" )),
274319 "acr" ,
@@ -279,8 +324,12 @@ def copy_image_to_target_acr(
279324 source_image ,
280325 "--image" ,
281326 f"{ image_name } :{ image_version } " ,
327+ "--username" ,
328+ ACR_IMPORT_SERVICE_PRINCIPAL_ID ,
282329 "--password" ,
283330 access_token ,
331+ "--registry" ,
332+ source_registry_id ,
284333 ]
285334 call_subprocess_raise_output (acr_import_image_cmd )
286335 except CLIError as error :
0 commit comments