|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +"""Root ecosystem helper.""" |
| 15 | + |
| 16 | +import re |
| 17 | +from .ecosystems_base import OrderedEcosystem |
| 18 | + |
| 19 | + |
| 20 | +class Root(OrderedEcosystem): |
| 21 | + """Root container security ecosystem. |
| 22 | +
|
| 23 | + Root provides patched container images across multiple base distributions |
| 24 | + and application ecosystems. The ecosystem uses hierarchical variants: |
| 25 | + - Root:Alpine:3.18 - Alpine Linux 3.18 based images |
| 26 | + - Root:Debian:12 - Debian 12 based images |
| 27 | + - Root:Ubuntu:22.04 - Ubuntu 22.04 based images |
| 28 | + - Root:PyPI - Python packages |
| 29 | + - Root:npm - npm packages |
| 30 | +
|
| 31 | + Version formats: |
| 32 | + - Alpine: <version>-r<patch_number> (e.g., 1.0.0-r10071) |
| 33 | + - Python: <version>+root.io.<patch_number> (e.g., 1.0.0+root.io.1) |
| 34 | + - Others: <version>.root.io.<patch_number> (e.g., 1.0.0.root.io.1) |
| 35 | + """ |
| 36 | + |
| 37 | + def _sort_key(self, version: str): |
| 38 | + """Generate sort key for Root version strings. |
| 39 | +
|
| 40 | + Handles multiple version formats: |
| 41 | + - Alpine: 1.0.0-r10071 |
| 42 | + - Python: 1.0.0+root.io.1 |
| 43 | + - Others: 1.0.0.root.io.1 |
| 44 | +
|
| 45 | + Args: |
| 46 | + version: Version string to parse |
| 47 | +
|
| 48 | + Returns: |
| 49 | + Tuple suitable for sorting |
| 50 | + """ |
| 51 | + # Try Alpine format: <version>-r<number> |
| 52 | + alpine_match = re.match(r'^(.+?)-r(\d+)$', version) |
| 53 | + if alpine_match: |
| 54 | + upstream = alpine_match.group(1) |
| 55 | + root_patch = int(alpine_match.group(2)) |
| 56 | + return self._parse_upstream_version(upstream) + (root_patch,) |
| 57 | + |
| 58 | + # Try Python format: <version>+root.io.<number> |
| 59 | + python_match = re.match(r'^(.+?)\+root\.io\.(\d+)$', version) |
| 60 | + if python_match: |
| 61 | + upstream = python_match.group(1) |
| 62 | + root_patch = int(python_match.group(2)) |
| 63 | + return self._parse_upstream_version(upstream) + (root_patch,) |
| 64 | + |
| 65 | + # Try other format: <version>.root.io.<number> |
| 66 | + other_match = re.match(r'^(.+?)\.root\.io\.(\d+)$', version) |
| 67 | + if other_match: |
| 68 | + upstream = other_match.group(1) |
| 69 | + root_patch = int(other_match.group(2)) |
| 70 | + return self._parse_upstream_version(upstream) + (root_patch,) |
| 71 | + |
| 72 | + # Fallback: treat as generic version |
| 73 | + return self._parse_upstream_version(version) |
| 74 | + |
| 75 | + def _parse_upstream_version(self, version: str): |
| 76 | + """Parse upstream version component. |
| 77 | +
|
| 78 | + Attempts to extract numeric and string components for sorting. |
| 79 | +
|
| 80 | + Args: |
| 81 | + version: Upstream version string |
| 82 | +
|
| 83 | + Returns: |
| 84 | + Tuple of parsed components |
| 85 | + """ |
| 86 | + parts = [] |
| 87 | + |
| 88 | + # Split on common delimiters |
| 89 | + components = re.split(r'[.-]', version) |
| 90 | + |
| 91 | + for component in components: |
| 92 | + # Try to parse as integer |
| 93 | + try: |
| 94 | + parts.append(int(component)) |
| 95 | + except ValueError: |
| 96 | + # If not numeric, use string comparison |
| 97 | + # Convert to tuple of character codes for consistent sorting |
| 98 | + parts.append(component) |
| 99 | + |
| 100 | + return tuple(parts) |
| 101 | + |
| 102 | + def sort_key(self, version: str): |
| 103 | + """Public sort key method. |
| 104 | +
|
| 105 | + Args: |
| 106 | + version: Version string |
| 107 | +
|
| 108 | + Returns: |
| 109 | + Tuple for sorting |
| 110 | + """ |
| 111 | + try: |
| 112 | + return self._sort_key(version) |
| 113 | + except Exception: |
| 114 | + # Fallback to string comparison if parsing fails |
| 115 | + return (version,) |
0 commit comments