Skip to content

Commit

Permalink
Bug: Nexus Image Version Must be Semver (#149)
Browse files Browse the repository at this point in the history
* added semver checking to input config validation; moved split image path to utils

* changed semver regex; renamed function

* fixed typo

* markups

* fix typo

---------

Co-authored-by: Jordan <jordan.layton@metaswitch.com>
  • Loading branch information
jordlay and Jordan authored Feb 26, 2024
1 parent b6c781b commit 84e5a3b
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 12 deletions.
11 changes: 2 additions & 9 deletions src/aosm/azext_aosm/cli_handlers/onboarding_nexus_vnf_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
from azext_aosm.inputs.arm_template_input import ArmTemplateInput
from azext_aosm.inputs.nexus_image_input import NexusImageFileInput
from .onboarding_vnf_handler import OnboardingVNFCLIHandler
from azext_aosm.common.utils import render_bicep_contents_from_j2, get_template_path
from azext_aosm.common.utils import render_bicep_contents_from_j2, get_template_path, split_image_path

logger = get_logger(__name__)

Expand Down Expand Up @@ -79,8 +79,7 @@ def _get_processor_list(self) -> [BaseInputProcessor]:
)
# For each image, instantiate image processor
for image in self.config.images:
(source_acr_registry, name, version) = self._split_image_path(image)

(source_acr_registry, name, version) = split_image_path(image)
image_input = NexusImageFileInput(
artifact_name=name,
artifact_version=version,
Expand Down Expand Up @@ -122,12 +121,6 @@ def build_all_parameters_json(self) -> JSONDefinitionElementBuilder:
)
return base_file

def _split_image_path(self, image) -> "tuple[str, str, str]":
"""Split the image path into source acr registry, name and version."""
(source_acr_registry, name_and_version) = image.split("/", 2)
(name, version) = name_and_version.split(":", 2)
return (source_acr_registry, name, version)

def _generate_type_specific_nf_application(self, processor) -> "tuple[list, list]":
"""Generate the type specific nf application."""
arm_nf = []
Expand Down
2 changes: 2 additions & 0 deletions src/aosm/azext_aosm/common/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ class ManifestsExist(str, Enum):
CNF_VALUES_SCHEMA_FILENAME = "values.schema.json"
CNF_TEMPLATE_FOLDER_NAME = "cnf"

NEXUS_IMAGE_REGEX = r"^[\~]?(\d+)\.(\d+)\.(\d+)$"
# SEMVER_REGEX = r"^(?P<major>0|[1-9]\d*)\.(?P<minor>0|[1-9]\d*)\.(?P<patch>0|[1-9]\d*)(?:-(?P<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?P<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$"
#################
# OLD CONSTANTS #
#################
Expand Down
19 changes: 17 additions & 2 deletions src/aosm/azext_aosm/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

import re
import os
import tarfile
from pathlib import Path
Expand All @@ -13,7 +13,7 @@
import tempfile

from knack.log import get_logger

from azext_aosm.common.constants import NEXUS_IMAGE_REGEX
from azext_aosm.common.exceptions import InvalidFileTypeError, MissingDependency

logger = get_logger(__name__)
Expand Down Expand Up @@ -123,3 +123,18 @@ def check_tool_installed(tool_name: str) -> None:
"""
if shutil.which(tool_name) is None:
raise MissingDependency(f"You must install {tool_name} to use this command.")

def split_image_path(image) -> "tuple[str, str, str]":
"""Split the image path into source acr registry, name and version."""
(source_acr_registry, name_and_version) = image.split("/", 2)
(name, version) = name_and_version.split(":", 2)
return (source_acr_registry, name, version)

def is_valid_nexus_image_version(string):
"""Check if image version is valid.
This is based on validation in pez repo.
It requires the image version to be major.minor.patch,
but does not enforce full semver validation.
"""
return re.match(NEXUS_IMAGE_REGEX, string) is not None
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------
from __future__ import annotations

from dataclasses import dataclass, field
from typing import List

Expand All @@ -13,6 +12,7 @@
from azext_aosm.configuration_models.onboarding_nfd_base_input_config import (
OnboardingNFDBaseInputConfig,
)
from azext_aosm.common.utils import split_image_path, is_valid_nexus_image_version


@dataclass
Expand Down Expand Up @@ -231,6 +231,11 @@ def validate(self):
raise ValidationError("arm_template must be set")
if not self.images:
raise ValidationError("You must include at least one image")
for image in self.images:
(_, _, version) = split_image_path(image)
if not is_valid_nexus_image_version(version):
raise ValidationError(f"{image} has invalid version '{version}'.\n"
"Allowed format is major.minor.patch")
if not self.arm_templates:
raise ValidationError("You must include at least one arm template")
for arm_template in self.arm_templates:
Expand Down

0 comments on commit 84e5a3b

Please sign in to comment.