diff --git a/tuf/api/metadata.py b/tuf/api/metadata.py index 1d559f0684..9b9631b297 100644 --- a/tuf/api/metadata.py +++ b/tuf/api/metadata.py @@ -16,6 +16,7 @@ """ import abc +import logging import tempfile from datetime import datetime, timedelta from typing import Any, ClassVar, Dict, List, Mapping, Optional, Tuple, Type @@ -25,7 +26,7 @@ from securesystemslib.storage import FilesystemBackend, StorageBackendInterface from securesystemslib.util import persist_temp_file -from tuf import exceptions +from tuf import exceptions, SPECIFICATION_VERSION from tuf.api.serialization import ( MetadataDeserializer, MetadataSerializer, @@ -37,6 +38,8 @@ # and currently, we are above 1000 lines by a small margin. # pylint: disable=C0302 +logger = logging.getLogger(__name__) + class Metadata: """A container for signed TUF metadata. @@ -343,6 +346,23 @@ def __init__( expires: datetime, unrecognized_fields: Optional[Mapping[str, Any]] = None, ) -> None: + spec_list = spec_version.split(".") + if len(spec_list) != 3 or not all(el.isdigit() for el in spec_list): + raise ValueError( + f"spec_version must be in semver format, got {spec_version}" + ) + supported_ver = SPECIFICATION_VERSION.split(".") + if spec_list[0] != supported_ver[0]: + raise exceptions.UnsupportedSpecificationError( + f"Unsupported major spec_version, got {spec_list}, " + f"supported {supported_ver}" + ) + if spec_list[1] != supported_ver[1]: + logger.info( + "Different minor version than supported, got %s, supported %s", + spec_version, + supported_ver, + ) self.spec_version = spec_version self.expires = expires