|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright 2024-2026 the original author or authors. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +"""Checks that the security patch families of the root build and the published |
| 16 | +ARGI BOM stay aligned (issue #295). |
| 17 | +
|
| 18 | +The root pom.xml and argi-bom/pom.xml both declare the versions of the shared |
| 19 | +security families (BouncyCastle, Tomcat embed, Jackson, jsoup) as properties, |
| 20 | +and the two files are maintained by hand. A root upgrade that forgets the BOM |
| 21 | +ships consumers a different version than the main build resolves. This check |
| 22 | +compares the two source poms directly, so it runs on every CI build without |
| 23 | +requiring a release package or a flattened POM. |
| 24 | +
|
| 25 | +With --verify-consumer, the script additionally builds a minimal out-of-repo |
| 26 | +consumer that imports only the installed argi-bom and resolves the managed |
| 27 | +BouncyCastle and Tomcat artifacts without versions, asserting that the |
| 28 | +effective versions are exactly what the BOM declares. This is the validation |
| 29 | +the issue asks for: effective versions as a consumer sees them, not reactor |
| 30 | +property strings. It requires argi-bom to be installed into the local |
| 31 | +repository first (in CI the build job installs it before this step). |
| 32 | +
|
| 33 | +Exit code 0 means aligned, 1 means drift or verification failure. |
| 34 | +""" |
| 35 | + |
| 36 | +import argparse |
| 37 | +import re |
| 38 | +import shutil |
| 39 | +import subprocess |
| 40 | +import sys |
| 41 | +import tempfile |
| 42 | +import xml.etree.ElementTree as ET |
| 43 | +from pathlib import Path |
| 44 | + |
| 45 | +REPO_ROOT = Path(__file__).resolve().parents[2] |
| 46 | +NS = {"m": "http://maven.apache.org/POM/4.0.0"} |
| 47 | + |
| 48 | +ROOT_POM = REPO_ROOT / "pom.xml" |
| 49 | +BOM_POM = REPO_ROOT / "argi-bom" / "pom.xml" |
| 50 | + |
| 51 | +# Properties the root build and the published BOM must keep aligned. Both sides |
| 52 | +# are hand-maintained; any drift here is what issue #295 reported. |
| 53 | +SHARED_PROPERTIES = ( |
| 54 | + "revision", |
| 55 | + "jackson-2-bom.version", |
| 56 | + "jackson-bom.version", |
| 57 | + "bouncycastle.version", |
| 58 | + "jsoup.version", |
| 59 | + "tomcat.version", |
| 60 | +) |
| 61 | + |
| 62 | +# The artifacts the consumer verification resolves without versions. The |
| 63 | +# BouncyCastle family is managed but unused inside the main build (published for |
| 64 | +# consumers only); the Tomcat embed family is the real transitive pin. |
| 65 | +CONSUMER_ARTIFACTS = ( |
| 66 | + ("org.bouncycastle", "bcprov-jdk18on"), |
| 67 | + ("org.bouncycastle", "bcpkix-jdk18on"), |
| 68 | + ("org.bouncycastle", "bcutil-jdk18on"), |
| 69 | + ("org.apache.tomcat.embed", "tomcat-embed-core"), |
| 70 | + ("org.apache.tomcat.embed", "tomcat-embed-el"), |
| 71 | + ("org.apache.tomcat.embed", "tomcat-embed-jasper"), |
| 72 | + ("org.apache.tomcat.embed", "tomcat-embed-websocket"), |
| 73 | +) |
| 74 | + |
| 75 | + |
| 76 | +def properties(pom_path: Path) -> dict[str, str]: |
| 77 | + root = ET.parse(pom_path).getroot() |
| 78 | + node = root.find("m:properties", NS) |
| 79 | + if node is None: |
| 80 | + raise ValueError(f"{pom_path} has no <properties> section") |
| 81 | + return {child.tag.replace(f"{{{NS['m']}}}", ""): (child.text or "").strip() for child in node} |
| 82 | + |
| 83 | + |
| 84 | +def check_source_consistency() -> bool: |
| 85 | + root_props = properties(ROOT_POM) |
| 86 | + bom_props = properties(BOM_POM) |
| 87 | + aligned = True |
| 88 | + for name in SHARED_PROPERTIES: |
| 89 | + root_value = root_props.get(name) |
| 90 | + bom_value = bom_props.get(name) |
| 91 | + if root_value is None or bom_value is None: |
| 92 | + print(f"MISSING {name}: root={root_value!r} bom={bom_value!r}") |
| 93 | + aligned = False |
| 94 | + elif root_value != bom_value: |
| 95 | + print(f"DRIFT {name}: root pom has {root_value}, argi-bom has {bom_value}") |
| 96 | + aligned = False |
| 97 | + if aligned: |
| 98 | + print(f"Aligned: {len(SHARED_PROPERTIES)} shared properties of root pom and argi-bom agree.") |
| 99 | + return aligned |
| 100 | + |
| 101 | + |
| 102 | +def bom_version(bom_props: dict[str, str], group_id: str, artifact_id: str) -> str | None: |
| 103 | + """Reads the managed version of one artifact from the BOM dependencyManagement.""" |
| 104 | + root = ET.parse(BOM_POM).getroot() |
| 105 | + management = root.find("m:dependencyManagement/m:dependencies", NS) |
| 106 | + if management is None: |
| 107 | + return None |
| 108 | + for dependency in management.findall("m:dependency", NS): |
| 109 | + if (dependency.findtext("m:groupId", namespaces=NS) == group_id |
| 110 | + and dependency.findtext("m:artifactId", namespaces=NS) == artifact_id): |
| 111 | + version = dependency.findtext("m:version", default="", namespaces=NS).strip() |
| 112 | + match = re.fullmatch(r"\$\{([^}]+)\}", version) |
| 113 | + if match: |
| 114 | + return bom_props.get(match.group(1)) |
| 115 | + return version |
| 116 | + return None |
| 117 | + |
| 118 | + |
| 119 | +CONSUMER_POM_TEMPLATE = """<?xml version="1.0" encoding="UTF-8"?> |
| 120 | +<project xmlns="http://maven.apache.org/POM/4.0.0" |
| 121 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 122 | + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> |
| 123 | + <modelVersion>4.0.0</modelVersion> |
| 124 | + <groupId>io.github.agentic-ai-java.check</groupId> |
| 125 | + <artifactId>bom-consumer-verify</artifactId> |
| 126 | + <version>0.0.0-SNAPSHOT</version> |
| 127 | + <packaging>pom</packaging> |
| 128 | + <dependencyManagement> |
| 129 | + <dependencies> |
| 130 | + <dependency> |
| 131 | + <groupId>io.github.agentic-ai-java</groupId> |
| 132 | + <artifactId>argi-bom</artifactId> |
| 133 | + <version>{bom_version}</version> |
| 134 | + <type>pom</type> |
| 135 | + <scope>import</scope> |
| 136 | + </dependency> |
| 137 | + </dependencies> |
| 138 | + </dependencyManagement> |
| 139 | + <dependencies> |
| 140 | +{dependencies} |
| 141 | + </dependencies> |
| 142 | +</project> |
| 143 | +""" |
| 144 | + |
| 145 | +DEPENDENCY_TEMPLATE = """ <dependency> |
| 146 | + <groupId>{group_id}</groupId> |
| 147 | + <artifactId>{artifact_id}</artifactId> |
| 148 | + </dependency>""" |
| 149 | + |
| 150 | + |
| 151 | +def verify_consumer(mvn: str) -> bool: |
| 152 | + bom_props = properties(BOM_POM) |
| 153 | + bom_version_value = bom_props.get("revision") |
| 154 | + if not bom_version_value: |
| 155 | + print("argi-bom has no <revision> property to import") |
| 156 | + return False |
| 157 | + |
| 158 | + with tempfile.TemporaryDirectory(prefix="argi-bom-consumer-") as tmp: |
| 159 | + consumer_dir = Path(tmp) |
| 160 | + dependencies = "\n".join( |
| 161 | + DEPENDENCY_TEMPLATE.format(group_id=group_id, artifact_id=artifact_id) |
| 162 | + for group_id, artifact_id in CONSUMER_ARTIFACTS |
| 163 | + ) |
| 164 | + pom_path = consumer_dir / "pom.xml" |
| 165 | + pom_path.write_text( |
| 166 | + CONSUMER_POM_TEMPLATE.format(bom_version=bom_version_value, dependencies=dependencies), |
| 167 | + encoding="utf-8", |
| 168 | + ) |
| 169 | + output_file = consumer_dir / "dependency-list.txt" |
| 170 | + # Maven is a batch script on Windows; shutil.which resolves mvn.cmd there. |
| 171 | + mvn_executable = shutil.which(mvn) or shutil.which(mvn + ".cmd") or shutil.which(mvn + ".bat") |
| 172 | + if mvn_executable is None: |
| 173 | + print(f"Maven executable {mvn!r} not found on PATH") |
| 174 | + return False |
| 175 | + result = subprocess.run( |
| 176 | + [mvn_executable, "-B", "-q", "-f", str(pom_path), |
| 177 | + "org.apache.maven.plugins:maven-dependency-plugin:3.7.1:list", |
| 178 | + f"-DoutputFile={output_file}"], |
| 179 | + capture_output=True, text=True, |
| 180 | + ) |
| 181 | + if result.returncode != 0: |
| 182 | + print(result.stdout) |
| 183 | + print(result.stderr) |
| 184 | + print("Consumer verification could not resolve the BOM; is argi-bom installed " |
| 185 | + f"at version {bom_version_value}? Run: mvn -pl argi-bom -am install -DskipTests") |
| 186 | + return False |
| 187 | + |
| 188 | + resolved: dict[tuple[str, str], str] = {} |
| 189 | + for line in output_file.read_text(encoding="utf-8").splitlines(): |
| 190 | + # The output file lists "group:artifact:type:version[:classifier]:scope" |
| 191 | + # without the console [INFO] prefix. |
| 192 | + match = re.match(r"\s*([^:\s]+):([^:\s]+):([^:\s]+):([^:\s]+)", line) |
| 193 | + if match: |
| 194 | + resolved[(match.group(1), match.group(2))] = match.group(4) |
| 195 | + |
| 196 | + aligned = True |
| 197 | + for group_id, artifact_id in CONSUMER_ARTIFACTS: |
| 198 | + expected = bom_version(bom_props, group_id, artifact_id) |
| 199 | + effective = resolved.get((group_id, artifact_id)) |
| 200 | + if expected is None: |
| 201 | + print(f"UNMANAGED {group_id}:{artifact_id} is not managed by argi-bom") |
| 202 | + aligned = False |
| 203 | + elif effective is None: |
| 204 | + print(f"MISSING {group_id}:{artifact_id} was not resolved by the consumer") |
| 205 | + aligned = False |
| 206 | + elif effective != expected: |
| 207 | + print(f"DRIFT {group_id}:{artifact_id}: consumer resolves {effective}, BOM manages {expected}") |
| 208 | + aligned = False |
| 209 | + if aligned: |
| 210 | + print(f"Aligned: all {len(CONSUMER_ARTIFACTS)} managed artifacts resolve to the BOM versions " |
| 211 | + f"when imported by an out-of-repo consumer.") |
| 212 | + return aligned |
| 213 | + |
| 214 | + |
| 215 | +def main() -> int: |
| 216 | + parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) |
| 217 | + parser.add_argument("--verify-consumer", action="store_true", |
| 218 | + help="additionally resolve the managed artifacts through an out-of-repo consumer " |
| 219 | + "that imports only the installed argi-bom") |
| 220 | + parser.add_argument("--mvn", default="mvn", help="Maven executable used for the consumer verification") |
| 221 | + args = parser.parse_args() |
| 222 | + |
| 223 | + if not check_source_consistency(): |
| 224 | + return 1 |
| 225 | + if args.verify_consumer and not verify_consumer(args.mvn): |
| 226 | + return 1 |
| 227 | + return 0 |
| 228 | + |
| 229 | + |
| 230 | +if __name__ == "__main__": |
| 231 | + sys.exit(main()) |
0 commit comments