Skip to content

Commit bdb62c9

Browse files
AOSM CLI- Fix UnAuthorized Action with az aosm nsd publish (#9553)
1 parent 0e04e57 commit bdb62c9

File tree

3 files changed

+55
-3
lines changed

3 files changed

+55
-3
lines changed

src/aosm/HISTORY.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
33
Release History
44
===============
5+
2.0.0b4
6+
++++++++
7+
* Fixing the unauthorized error when publishing to ACR
58

69
2.0.0b3
710
++++++++

src/aosm/azext_aosm/common/registry.py

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
logger = get_logger(__name__)
2121
ACR_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
2529
class 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:

src/aosm/setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
# Confirm this is the right version number you want and it matches your
1919
# HISTORY.rst entry.
20-
VERSION = "2.0.0b3"
20+
VERSION = "2.0.0b4"
2121

2222

2323
# The full list of classifiers is available at
@@ -35,7 +35,7 @@
3535

3636
DEPENDENCIES = [
3737
"oras==0.1.30",
38-
"jinja2==3.1.4",
38+
"jinja2==3.1.6",
3939
"genson==1.2.2",
4040
"ruamel.yaml==0.18.6",
4141
]

0 commit comments

Comments
 (0)