Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check if resource types used in the VNF ARM template are valid #186

Merged
merged 4 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/aosm/HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@

Release History
===============

2.0.0b2
++++++++
* added a check to make sure resource type used in ARM template are in allowed list
2.0.0b1
++++++++
* Renamed nfdvName to nfdv in CGVs
* Added useful comments to input files
* Added 1:1 mapping between NFVIsFromSite and NF RETs
* Added expose_all parameter in input file to expose all parameters in deployParameters and CGS
* Removed multiple_instances and depends_on from input file
* Added: mutating webhook for injectArtifactStoreDetails
Expand All @@ -23,7 +28,6 @@ Release History
* Fixed: Manifest name built from ACR name, so clashes
* Fixed: Nexus image version must be semver
* Fixed: Sensible error when no type given in helm chart schema
* No changes, building wheel from correct branch
* Fixed: customLocation missing from Nexus
* Fixed: helm charts not uploading correctly
* Added: Nexus support
Expand Down
1 change: 1 addition & 0 deletions src/aosm/azext_aosm/aaz/latest/aosm/__cmd_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

@register_command_group(
"aosm",
is_preview=True,
)
class __CMDGroup(AAZCommandGroup):
"""Manage Azure Operator Service Manager resources.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

@register_command_group(
"aosm publisher",
is_preview=True,
)
class __CMDGroup(AAZCommandGroup):
"""Commands to manage publisher resources.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

@register_command_group(
"aosm publisher artifact-manifest",
is_preview=True,
)
class __CMDGroup(AAZCommandGroup):
"""Commands to manage artifact manifest resources.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
)
class List(AAZCommand):
"""List information about the artifact manifest.

:example: List information about the artifact manifest in the 'contoso' artifact store of the 'contoso' publisher
az aosm publisher artifact-manifest list --resource-group contoso-aosm --publisher-name contoso --artifact-store-name contoso
"""

_aaz_info = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@

@register_command(
"aosm publisher artifact-manifest list-credential",
is_preview=True,
)
class ListCredential(AAZCommand):
"""List credential for publishing artifacts defined in artifact manifest.

:example: List credential to use for publishing an artifact from the 'contoso-manifest' manifest
az aosm publisher artifact-manifest list-credential --resource-group contoso-aosm --publisher-name contoso --artifact-store-name contoso --name contoso-manifest
"""

_aaz_info = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
)
class UpdateState(AAZCommand):
"""Update state for artifact manifest.

:example: Update the 'contoso-manifest' artifact manifest's state to 'Uploaded'
az aosm publisher artifact-manifest update-state --resource-group contoso-aosm --publisher-name contoso --artifact-store-name contoso --name contoso-manifest --state Uploaded
"""

_aaz_info = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
)
class List(AAZCommand):
"""List all the available artifacts in the parent Artifact Store.

:example: List all available artifacts in the 'contoso' artifact store of the 'contoso' publisher
az aosm publisher artifact-store artifact list --resource-group contoso-aosm --publisher-name contoso --artifact-store-name contoso
"""

_aaz_info = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
)
class List(AAZCommand):
"""List a Artifact overview information.

:example: List overview information for the 'nginx' artifact in the 'contoso' artifact store of the 'contoso' publisher
az aosm publisher artifact-store artifact version list --resource-group contoso-aosm --publisher-name contoso --artifact-store-name contoso --artifact-name nginx
"""

_aaz_info = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
)
class UpdateState(AAZCommand):
"""Update artifact state defined in artifact store.

:example: Deprecate the 1.0.0 version of the 'nginx' artifact in the 'contoso' artifact store of the 'contoso' publisher
az aosm publisher artifact-store artifact version update-state --resource-group contoso-aosm --publisher-name contoso --artifact-store-name contoso --artifact-name nginx --name 1.0.0 --artifact-state Deprecated
"""

_aaz_info = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
)
class List(AAZCommand):
"""List information of the configuration group schemas under a publisher.

:example: List the configuration group schemas defined under the contoso publisher
az aosm publisher configuration-group-schema list --resource-group contoso-aosm --publisher-name contoso
"""

_aaz_info = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
)
class UpdateState(AAZCommand):
"""Update configuration group schema state.

:example: Change the 'nginx-cgs' config group schema to 'Active' state
az aosm publisher configuration-group-schema update-state --resource-group contoso-aosm --publisher-name contoso --name nginx-cgs --version-state Active
"""

_aaz_info = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
is_preview=True,
)
class List(AAZCommand):
"""List information about a list of network function definition versions under a network function definition group.
"""List information about the network function definition versions available in the specified network function definition group.

:example: List information about the network function definition versions available in the 'nginx' network function definition group
az aosm publisher network-function-definition version list --resource-group contoso-aosm --publisher-name contoso --group-name nginx
"""

_aaz_info = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
)
class UpdateState(AAZCommand):
"""Update network function definition version state.

:example: Change version 2.0.0 of the 'nginx' network function definition group to 'Active' state
az aosm publisher network-function-definition version update-state --resource-group contoso-aosm --publisher-name contoso --group-name nginx --version-name 2.0.0 --version-state Active
"""

_aaz_info = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
is_preview=True,
)
class List(AAZCommand):
"""List information about a list of network service design versions under a network service design group.
"""List information about the network service design versions available under the specified network service design group.

:example: List information about the network service design versions available in the 'contoso-service' network service design group
az aosm publisher network-function-definition version list --resource-group contoso-aosm --publisher-name contoso --group-name contoso-service
"""

_aaz_info = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
)
class UpdateState(AAZCommand):
"""Update network service design version state.

:example: Change version 1.0.0 of the 'contoso-service' network service design group to 'Deprecated' state
az aosm publisher network-service-design version update-state --resource-group contoso-aosm --publisher-name contoso --group-name contoso-service --version-name 1.0.0 --version-state Deprecated
"""

_aaz_info = {
Expand Down
13 changes: 13 additions & 0 deletions src/aosm/azext_aosm/cli_handlers/onboarding_vnf_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

from __future__ import annotations
import json

from pathlib import Path
from abc import abstractmethod
from typing import Optional
Expand Down Expand Up @@ -218,6 +220,17 @@ def _generate_type_specific_nf_application(self, processor):
def _generate_type_specific_artifact_manifest(self, processor):
return NotImplementedError

def _validate_arm_template(self):
for processor in self.processors:
if isinstance(processor, BaseArmBuildProcessor):
processor.input_artifact.validate_resource_types()

def pre_validate_build(self):
"""Run all validation functions required before building the vnf."""
logger.debug("Pre-validating build")

self._validate_arm_template()

@property
def input_config(self):
raise NotImplementedError
Expand Down
10 changes: 10 additions & 0 deletions src/aosm/azext_aosm/common/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ class ManifestsExist(str, Enum):

NEXUS_IMAGE_REGEX = r"^[\~]?(\d+)\.(\d+)\.(\d+)$"

VALID_VNF_TEMPLATE_RESOURCE_TYPES = [
'Microsoft.Compute',
'Microsoft.Network',
'Microsoft.NetworkCloud',
'Microsoft.Storage',
'Microsoft.NetworkFabric',
'Microsoft.Authorization',
'Microsoft.ManagedIdentity'
]

#################
# OLD CONSTANTS #
#################
Expand Down
4 changes: 4 additions & 0 deletions src/aosm/azext_aosm/common/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,7 @@ class DefaultValuesNotFoundError(UserFault):

class TemplateValidationError(Exception):
"""Raised when template validation fails"""


class InvalidARMResourceType(Exception):
"""Raise when Resource type used in the ARM template is not in the allowed list"""
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class OnboardingCNFInputConfig(OnboardingNFDBaseInputConfig):
metadata={
"comment": (
"List of registries from which to pull the image(s).\n"
"For example [sourceacr.azurecr.io/test, myacr2.azurecr.io, ghcr.io/path].\n"
'For example ["sourceacr.azurecr.io/test", "myacr2.azurecr.io", "ghcr.io/path"].\n'
"For non Azure Container Registries, ensure you have run a docker login command before running build.\n"
)
},
Expand Down
19 changes: 19 additions & 0 deletions src/aosm/azext_aosm/inputs/arm_template_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@

from azext_aosm.common.constants import BASE_SCHEMA
from azext_aosm.inputs.base_input import BaseInput
from azext_aosm.common.constants import (
VALID_VNF_TEMPLATE_RESOURCE_TYPES
)

from azext_aosm.common.exceptions import (
InvalidARMResourceType
)

logger = get_logger(__name__)

Expand Down Expand Up @@ -110,3 +117,15 @@ def _generate_schema_from_arm_params(
schema["properties"][key] = {"type": value["type"]}
if "defaultValue" in value:
schema["properties"][key]["default"] = value["defaultValue"]

def validate_resource_types(self):
with open(Path(self.template_path).absolute()) as f:
data = json.load(f)
arm_resource_types = [resource['type'] for resource in data['resources']]
for resource_type in arm_resource_types:
resource_type_prefix = resource_type.split('/')[0] if '/' in resource_type else resource_type
if resource_type_prefix not in VALID_VNF_TEMPLATE_RESOURCE_TYPES:
raise InvalidARMResourceType(
Copy link
Collaborator

@jordlay jordlay May 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you raise a CLI error isntead, you won't get the trace printed to the user. I just tested it with InvalidTemplateError but there may be a better one. To use these errirs import like this:

from azure.cli.core.azclierror import InvalidTemplateError

image

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For reference, the errors available for use in the CLI are all declared here:
https://github.com/Azure/azure-cli/blob/dev/src/azure-cli-core/azure/cli/core/azclierror.py

(Or of course you can click through to the azure.cli.core.azclierror module in VS Code to find them too.)

f"ERROR: The resource type '{resource_type_prefix}' "
" used in the ARM template is not valid. "
f" Valid types are: {', '.join(VALID_VNF_TEMPLATE_RESOURCE_TYPES)}")
2 changes: 1 addition & 1 deletion src/aosm/azext_aosm/inputs/helm_chart_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def from_chart_path(
unpacked_chart_path = extract_tarfile(chart_path, temp_dir)

name, version = HelmChartInput._get_name_and_version(unpacked_chart_path)

shutil.rmtree(temp_dir)
logger.debug("Deleted temporary directory %s", temp_dir)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"metadata": {
"_generator": {
"name": "bicep",
"version": "0.15.31.15270",
"templateHash": "1656082395923655778"
}
},
"parameters": {
"location": {
"type": "string",
"defaultValue": "uksouth"
}
},
"variables": {
"imageResourceGroup": "[resourceGroup().name]",
"subscriptionId": "[subscription().subscriptionId]",
"vmSizeSku": "Standard_D2s_v3"
},
"resources": [
{
"type": "Microsoft.ContainerService/managedClusters",
"apiVersion": "2022-05-02-preview",
"name": "[parameters('clusterName')]",
"location": "[parameters('location')]",
"identity": {
"type": "SystemAssigned"
},
"properties": {}
},
{
"type": "Microsoft.Storage/storageAccounts",
"apiVersion": "2023-01-01",
"name": "string",
"location": "string",
"tags": {
"tagName1": "tagValue1",
"tagName2": "tagValue2"
},
"sku": {
"name": "string"
},
"kind": "string",
"extendedLocation": {
"name": "string",
"type": "EdgeZone"
},
"identity": {
"type": "string",
"userAssignedIdentities": {
"{customized property}": {}
}
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"metadata": {
"_generator": {
"name": "bicep",
"version": "0.15.31.15270",
"templateHash": "1656082395923655778"
}
},
"parameters": {
"location": {
"type": "string",
"defaultValue": "uksouth"
}
},
"variables": {
"userAssignedIdentities_uamiTestIdentity_name": "uamiTestIdentity"
},
"resources": [
{
"type": "Microsoft.ManagedIdentity/userAssignedIdentities",
"apiVersion": "2015-08-31-preview",
"name": "[parameters('userAssignedIdentities_uamiTestIdentity_name')]",
"location": "eastus2euap"
}
]
}
Loading
Loading