Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
115 changes: 115 additions & 0 deletions .github/scripts/check-release-artifacts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
#!/usr/bin/env python3
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Check the actual Maven archives, not merely the source legal files."""

import argparse
import hashlib
import json
from pathlib import Path, PurePosixPath
import re
import xml.etree.ElementTree as ET
import zipfile


SHADED_ARTIFACTS = (
"shenyu-admin-listener-apollo",
"shenyu-registry-apollo",
"shenyu-spring-boot-starter-sync-data-apollo",
)


def check_jar(path, notice):
with zipfile.ZipFile(path) as jar:
names = jar.namelist()
for name in ("META-INF/LICENSE", "META-INF/NOTICE"):
if names.count(name) != 1:
raise ValueError(f"{path}: expected exactly one {name}")
license_text = jar.read("META-INF/LICENSE").decode("utf-8")
notice_text = jar.read("META-INF/NOTICE").decode("utf-8")
if not all(term in license_text for term in ("Apache License", "Version 2.0", "TERMS AND CONDITIONS")):
raise ValueError(f"{path}: missing project Apache 2.0 license")
if notice.strip() not in notice_text:
raise ValueError(f"{path}: missing project NOTICE attribution")
if "static/index.html" in names:
for name in ("META-INF/licenses/LICENSE-dashboard.txt", "META-INF/licenses/NOTICE-dashboard.txt"):
if name not in names or not jar.read(name).strip():
raise ValueError(f"{path}: bundled dashboard is missing {name}")
Comment thread
yuluo-yx marked this conversation as resolved.
Resolved
if any(path.name.startswith(artifact + "-") for artifact in SHADED_ARTIFACTS) and not path.name.endswith(("-sources.jar", "-javadoc.jar")):
for name in ("META-INF/LICENSE-shaded-dependencies.txt", "META-INF/NOTICE-shaded-dependencies.txt"):
if name not in names:
raise ValueError(f"{path}: missing shaded dependency terms: {name}")
return {"archive": path.name, "project_license": True, "project_notice": True,
"sha512": hashlib.sha512(path.read_bytes()).hexdigest()}


def check_source(path, root):
with zipfile.ZipFile(path) as archive:
names = archive.namelist()
prefix = names[0].split("/")[0] + "/"
for relative in ("LICENSE", "NOTICE", "RELEASE-NOTES.md"):
if archive.read(prefix + relative) != (root / relative).read_bytes():
raise ValueError(f"{path}: source {relative} differs from the release commit")
static = "shenyu-admin/src/main/resources/static/"
html = archive.read(prefix + static + "index.html").decode("utf-8")
references = re.findall(r'(?:src|href)=[\"\']([^\"\']+\.(?:js|css))(?:\?[^\"\']*)?[\"\']', html)
if not references:
raise ValueError(f"{path}: no dashboard JS/CSS references")
for reference in references:
if "://" in reference or reference.startswith("//"):
raise ValueError(f"{path}: unexpected remote dashboard asset: {reference}")
relative = static + reference.removeprefix("./").lstrip("/")
if archive.read(prefix + relative) != (root / relative).read_bytes():
Comment thread
yuluo-yx marked this conversation as resolved.
Resolved
raise ValueError(f"{path}: dashboard asset differs: {reference}")
for name in names:
relative = name.removeprefix(prefix)
parts = PurePosixPath(relative).parts
if "target" in parts or ".git" in parts or relative.startswith("actions/") or relative.endswith((".jar", ".class", ".war")):
raise ValueError(f"{path}: unintended source archive entry: {relative}")
return {"archive": path.name, "project_legal_files": True, "dashboard_assets": references}


def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--repository", type=Path, required=True)
parser.add_argument("--root", type=Path, default=Path.cwd())
args = parser.parse_args()
root = args.root
pom = ET.parse(root / "pom.xml").getroot()
version = pom.findtext("{http://maven.apache.org/POM/4.0.0}version")
repository = args.repository
notice = (root / "NOTICE").read_text()
jars = sorted(repository.glob(f"*/{version}/*.jar"))
if not jars:
raise ValueError(f"No candidate JARs in {repository}")
results = [check_jar(path, notice) for path in jars]
for path in jars:
if not path.name.endswith(("-sources.jar", "-javadoc.jar")):
for classifier in ("sources", "javadoc"):
if not path.with_name(path.stem + f"-{classifier}.jar").exists():
raise ValueError(f"{path}: missing {classifier} attachment")
sources = list(repository.glob(f"*/{version}/*-src.zip")) + list(repository.glob(f"shenyu/{version}/*-source-release.zip"))
if len(sources) != 2:
raise ValueError(f"Expected both source archives, found {len(sources)}")
report = {"version": version, "jars": results, "sources": [check_source(p, root) for p in sources]}
output = root / "target/release-audit"
output.mkdir(parents=True, exist_ok=True)
(output / "project-artifacts.json").write_text(json.dumps(report, indent=2) + "\n")
print(f"Verified project LICENSE/NOTICE in all {len(jars)} Maven JARs and dashboard resources in both source archives")


if __name__ == "__main__":
main()
144 changes: 144 additions & 0 deletions .github/scripts/collect-release-licenses.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
#!/usr/bin/env python3
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Collect legal resources from the exact assembled distribution JARs."""

import argparse
import hashlib
import io
import json
from pathlib import Path, PurePosixPath
import re
import tarfile
import xml.etree.ElementTree as ET
import zipfile


LEGAL_NAME = re.compile(r"^(license|notice|copyright|copying)(?:[._-].*)?$", re.I)
TEXT_SUFFIXES = {"", ".txt", ".text", ".md", ".html", ".htm", ".rst", ".adoc"}
# Version-specific regressions: names alone cannot establish the applicable license.
LICENSE_DECLARATIONS = {
"jersey-client-3.1.11.jar": ("jersey-client 3.1.11", "EPL 2.0"),
"jersey-common-3.1.11.jar": ("jersey-common 3.1.11", "EPL 2.0"),
"jersey-hk2-3.1.11.jar": ("jersey-hk2 3.1.11", "EPL 2.0"),
"jersey-apache-connector-3.1.11.jar": ("jersey-apache-connector 3.1.11", "EPL 2.0"),
"aspectjweaver-1.9.25.1.jar": ("aspectjweaver 1.9.25.1", "EPL-2.0 AND BSD-3-Clause AND Apache-1.1"),
"osgi-resource-locator-1.0.3.jar": ("osgi-resource-locator 1.0.3", "EPL 2.0 OR GPL 2.0 with Classpath Exception"),
"jakarta.annotation-api-2.1.1.jar": ("jakarta.annotation-api 2.1.1", "EPL 2.0 OR GPL 2.0 with Classpath Exception"),
"lettuce-core-6.6.0.RELEASE.jar": ("lettuce-core 6.6.0.RELEASE", "MIT"),
"jcl-over-slf4j-2.0.17.jar": ("jcl-over-slf4j 2.0.17", "Apache 2.0"),
}


def collect(archive):
jars = []
texts = {}
documents = []
index = ""
with tarfile.open(archive, "r:gz") as distribution:
for member in distribution:
path = PurePosixPath(member.name)
if member.isfile() and ((len(path.parts) == 2 and path.name in {"LICENSE", "NOTICE"})
or (len(path.parts) > 2 and path.parts[1] == "licenses")):
with distribution.extractfile(member) as document:
text = document.read().decode("utf-8-sig", errors="replace").replace("\r\n", "\n")
documents.append(text)
if len(path.parts) == 2 and path.name == "LICENSE":
index = text
if not member.isfile() or path.parent.name != "lib" or path.suffix != ".jar":
continue
if path.name.startswith("shenyu-"):
continue
resources = []
coordinates = []
pom_licenses = []
with distribution.extractfile(member) as jar_stream:
with zipfile.ZipFile(io.BytesIO(jar_stream.read())) as jar:
for entry in sorted(jar.namelist()):
resource = PurePosixPath(entry)
if entry.startswith("META-INF/maven/") and entry.endswith("/pom.properties"):
properties = {}
for line in jar.read(entry).decode("utf-8").splitlines():
key, separator, value = line.partition("=")
if separator and key in {"groupId", "artifactId", "version"}:
properties[key] = value.strip()
if len(properties) == 3:
coordinates.append(properties)
if entry.startswith("META-INF/maven/") and entry.endswith("/pom.xml"):
pom = ET.fromstring(jar.read(entry))
for license_element in pom.findall("{*}licenses/{*}license"):
pom_licenses.append({"pom": entry, "name": license_element.findtext("{*}name"),
"url": license_element.findtext("{*}url")})
legal_path = LEGAL_NAME.fullmatch(resource.name) or re.search(
r"\.(?:license|notice)(?:\.|$)", resource.name, re.I) or any(
part.lower() in {"license", "licenses"} for part in resource.parts[:-1])
if not legal_path or resource.suffix.lower() not in TEXT_SUFFIXES:
continue
# Legal resources are read in place; archive paths are never extracted.
data = jar.read(entry)
try:
text = data.decode("utf-8-sig")
except UnicodeDecodeError:
text = data.decode("latin-1")
text = text.replace("\r\n", "\n").strip()
if not text:
continue
digest = hashlib.sha256(text.encode("utf-8")).hexdigest()
texts[digest] = text
resources.append({
"path": entry,
"kind": "notice" if re.search(r"(^|[._-])notice([._-]|$)", resource.name, re.I) else "license",
"sha256": digest,
})
jars.append({"name": path.name, "coordinates": coordinates, "resources": resources,
"embedded_pom_licenses": pom_licenses})
if not jars:
raise ValueError(f"No third-party library JARs found in {archive}")
legal_documents = "\n".join(documents)
missing = [jar["name"] + " / " + resource["path"] for jar in jars for resource in jar["resources"]
if texts[resource["sha256"]] not in legal_documents]
for jar in jars:
if jar["name"] not in LICENSE_DECLARATIONS:
continue
component, expected = LICENSE_DECLARATIONS[jar["name"]]
declarations = [line for line in index.splitlines() if line.strip().startswith(component + ":")]
if len(declarations) != 1 or expected not in declarations[0]:
missing.append(f"{jar['name']}: expected one component declaration with {expected}")
return {"archive": archive.name, "jars": sorted(jars, key=lambda item: item["name"]), "texts": texts,
"missing_legal_resources": missing}


def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--check", action="store_true", help="Require bundled legal documents to reproduce JAR legal resources")
args = parser.parse_args()
output = Path("target/release-audit")
output.mkdir(parents=True, exist_ok=True)
missing = []
for component in ("admin", "bootstrap"):
archives = list(Path(f"shenyu-dist/shenyu-{component}-dist/target").glob(f"apache-shenyu-*-{component}-bin.tar.gz"))
if len(archives) != 1:
raise ValueError(f"Expected one {component} distribution, found {len(archives)}")
report = collect(archives[0])
(output / f"{component}-legal.json").write_text(json.dumps(report, indent=2) + "\n", encoding="utf-8")
print(f"{component}: {len(report['jars'])} third-party JARs, {len(report['texts'])} distinct legal texts")
missing.extend(component + ": " + resource for resource in report["missing_legal_resources"])
if args.check and missing:
raise ValueError("Legal resources missing from distribution documents:\n" + "\n".join(missing))


if __name__ == "__main__":
main()
Loading
Loading