Skip to content

Commit d2a52d5

Browse files
committed
Rework unsupported security types
Introduce an environment-driven configuration for arches that don't support security type, expose it via a helper function, refactor security type handling to use this configuration, and add comprehensive tests for the new helper. New Features: Add UNSUPPORTED_SECURITY_TYPE_ARCHES constant to configure arches that don't support security type via environment variable Introduce get_unsupported_security_type_arches function to parse the configured arches Enhancements: Update get_safe_security_type logic to skip unsupported arches based on the new function Tests: Add unit tests covering default, multiple, three-value, and empty cases for get_unsupported_security_type_arches Signed-off-by: Jonathan Gangi <jgangi@redhat.com> Assisted-by: Cursor/Gemini
1 parent bd79d3a commit d2a52d5

File tree

2 files changed

+61
-5
lines changed

2 files changed

+61
-5
lines changed

cloudpub/ms_azure/utils.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# SPDX-License-Identifier: GPL-3.0-or-later
22
import logging
3+
import os
34
from operator import attrgetter
45
from typing import Any, Dict, List, Optional, Tuple, TypedDict
56

@@ -21,6 +22,19 @@
2122
log = logging.getLogger(__name__)
2223

2324

25+
UNSUPPORTED_SECURITY_TYPE_ARCHES = (
26+
os.environ.get("UNSUPPORTED_SECURITY_TYPE_ARCHES", "") or "x64Gen1"
27+
)
28+
"""
29+
The list of arches that don't support security type.
30+
31+
This is a comma-separated list of arches that don't support security type.
32+
It's used to skip the security type for the arches that don't support it.
33+
It's set as an environment variable to allow for customization.
34+
If the environment variable is empty, the default "x64Gen1" is used.
35+
"""
36+
37+
2438
class AzurePublishingMetadata(PublishingMetadata):
2539
"""A collection of metadata necessary for publishing a VHD Image into a product."""
2640

@@ -303,6 +317,12 @@ def _all_skus_present(old_skus: List[VMISku], disk_versions: List[DiskVersion])
303317
return True
304318

305319

320+
def get_unsupported_security_type_arches() -> List[str]:
321+
"""Return the list of arches that don't support security type."""
322+
arches = UNSUPPORTED_SECURITY_TYPE_ARCHES or "x64Gen1"
323+
return [arch.strip() for arch in arches.split(",")]
324+
325+
306326
def _build_skus(
307327
disk_versions: List[DiskVersion],
308328
default_gen: str,
@@ -316,8 +336,8 @@ def get_skuid(arch):
316336
return f"{plan_name}-{arch.lower()}"
317337

318338
def get_safe_security_type(image_type):
319-
# Arches which aren't x86Gen2 (like ARM64) doesn't work well with security type
320-
if image_type != "x64Gen2":
339+
# Some arches (like x86 Gen1) doesn't support security type, so we need to skip them.
340+
if image_type in get_unsupported_security_type_arches():
321341
return None
322342
return security_type
323343

@@ -348,8 +368,8 @@ def get_safe_security_type(image_type):
348368

349369

350370
def _get_security_type(old_skus: List[VMISku]) -> Optional[List[str]]:
351-
# The security type may exist only for x64 Gen2, so it iterates over all gens to find it
352-
# Get the security type for all gens
371+
# The security type may not be applied for certain arches, like x64 Gen1.
372+
# This function will return the proper security type for the arches that has it set.
353373
for osku in old_skus:
354374
if osku.security_type is not None:
355375
return osku.security_type

tests/ms_azure/test_utils.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import logging
22
from operator import attrgetter
33
from typing import Any, Dict
4+
from unittest import mock
45

56
import pytest
67
from _pytest.logging import LogCaptureFixture
@@ -17,6 +18,7 @@
1718
AzurePublishingMetadata,
1819
create_disk_version_from_scratch,
1920
get_image_type_mapping,
21+
get_unsupported_security_type_arches,
2022
is_azure_job_not_complete,
2123
is_sas_present,
2224
prepare_vm_images,
@@ -489,7 +491,7 @@ def test_update_existing_skus_mixed_arches(
489491
VMISku.from_json(x)
490492
for x in [
491493
{"imageType": "x64Gen2", "skuId": "plan1", "securityType": ["trusted"]},
492-
{"imageType": "arm64Gen2", "skuId": "plan1-arm64"},
494+
{"imageType": "arm64Gen2", "skuId": "plan1-arm64", "securityType": ["trusted"]},
493495
{"imageType": "x64Gen1", "skuId": "plan1-gen1"},
494496
]
495497
]
@@ -580,3 +582,37 @@ def test_create_disk_version_from_scratch_arm64(
580582
res.vm_images = sorted(res.vm_images, key=attrgetter("image_type"))
581583

582584
assert res == disk_version_arm64_obj
585+
586+
@mock.patch("cloudpub.ms_azure.utils.UNSUPPORTED_SECURITY_TYPE_ARCHES", "x64Gen1")
587+
def test_get_unsupported_security_type_arches_default(self) -> None:
588+
"""Test that the function returns the default value when env var is not set."""
589+
res = get_unsupported_security_type_arches()
590+
assert res == ["x64Gen1"]
591+
592+
@mock.patch("cloudpub.ms_azure.utils.UNSUPPORTED_SECURITY_TYPE_ARCHES", "x64Gen1,arm64Gen1")
593+
def test_get_unsupported_security_type_arches_multiple(self) -> None:
594+
"""Test that the function correctly splits comma-separated values."""
595+
res = get_unsupported_security_type_arches()
596+
assert res == ["x64Gen1", "arm64Gen1"]
597+
598+
@mock.patch(
599+
"cloudpub.ms_azure.utils.UNSUPPORTED_SECURITY_TYPE_ARCHES", "x64Gen1,x64Gen2,arm64Gen1"
600+
)
601+
def test_get_unsupported_security_type_arches_multiple_three(self) -> None:
602+
"""Test that the function correctly handles three comma-separated values."""
603+
res = get_unsupported_security_type_arches()
604+
assert res == ["x64Gen1", "x64Gen2", "arm64Gen1"]
605+
606+
@mock.patch("cloudpub.ms_azure.utils.UNSUPPORTED_SECURITY_TYPE_ARCHES", "")
607+
def test_get_unsupported_security_type_arches_empty(self) -> None:
608+
"""Test that the function returns default value when env var is empty."""
609+
res = get_unsupported_security_type_arches()
610+
assert res == ["x64Gen1"]
611+
612+
@mock.patch(
613+
"cloudpub.ms_azure.utils.UNSUPPORTED_SECURITY_TYPE_ARCHES", "x64Gen1 , arm64Gen2 , x64Gen2"
614+
)
615+
def test_get_unsupported_security_type_arches_with_spaces(self) -> None:
616+
"""Test that the function correctly strips whitespace from comma-separated values."""
617+
res = get_unsupported_security_type_arches()
618+
assert res == ["x64Gen1", "arm64Gen2", "x64Gen2"]

0 commit comments

Comments
 (0)