diff --git a/buildSrc/src/main/kotlin/datadog/gradle/plugin/muzzle/MuzzleExcludedDependencySupport.kt b/buildSrc/src/main/kotlin/datadog/gradle/plugin/muzzle/MuzzleExcludedDependencySupport.kt new file mode 100644 index 00000000000..b29ce9a729c --- /dev/null +++ b/buildSrc/src/main/kotlin/datadog/gradle/plugin/muzzle/MuzzleExcludedDependencySupport.kt @@ -0,0 +1,190 @@ +package datadog.gradle.plugin.muzzle + +import org.eclipse.aether.artifact.Artifact +import org.gradle.api.Project +import org.gradle.api.artifacts.Configuration +import java.io.ByteArrayInputStream +import java.io.FileNotFoundException +import java.net.URI +import java.nio.file.Files +import java.nio.file.Path +import javax.xml.parsers.DocumentBuilderFactory +import javax.xml.transform.OutputKeys +import javax.xml.transform.TransformerFactory +import javax.xml.transform.dom.DOMSource +import javax.xml.transform.stream.StreamResult +import org.w3c.dom.Document +import org.w3c.dom.Element + +internal object MuzzleExcludedDependencySupport { + fun applyTo( + project: Project, + configuration: Configuration, + directive: MuzzleDirective, + versionArtifact: Artifact? + ) { + if (versionArtifact == null || directive.excludedDependencies.isEmpty()) { + return + } + + val repositoryUris = directive.getRepositories(MuzzleMavenRepoUtils.MUZZLE_REPOS).map { URI.create(it.url) } + val repoDir = project.layout.buildDirectory + .dir("generated/muzzle-excluded-dependencies/${configuration.name}") + .get() + .asFile + .toPath() + Files.createDirectories(repoDir) + + materializeArtifactHierarchy( + repoDir = repoDir, + repositories = repositoryUris, + coordinates = Coordinates(versionArtifact.groupId, versionArtifact.artifactId, versionArtifact.version), + excludedDependencies = directive.excludedDependencies.map { it.split(":", limit = 2) }.map { it[0] to it[1] }.toSet(), + rootArtifact = versionArtifact, + seen = LinkedHashSet() + ) + + val generatedRepo = project.repositories.maven { + name = "${configuration.name}MuzzleExcludedDependencies" + url = repoDir.toUri() + } + project.repositories.remove(generatedRepo) + project.repositories.addFirst(generatedRepo) + } + + private fun materializeArtifactHierarchy( + repoDir: Path, + repositories: List, + coordinates: Coordinates, + excludedDependencies: Set>, + rootArtifact: Artifact?, + seen: MutableSet + ) { + if (!seen.add(coordinates)) { + return + } + + val pomBytes = downloadRequired(repositories, coordinates.pomRelativePath) + val pomDocument = parsePom(pomBytes) + removeExcludedDependencies(pomDocument, excludedDependencies) + writePom(repoDir.resolve(coordinates.pomRelativePath), pomDocument) + + if (rootArtifact != null) { + downloadOptional(repositories, coordinates.artifactRelativePath(rootArtifact.extension, rootArtifact.classifier)) + ?.let { artifactBytes -> + val artifactPath = repoDir.resolve(coordinates.artifactRelativePath(rootArtifact.extension, rootArtifact.classifier)) + Files.createDirectories(artifactPath.parent) + Files.write(artifactPath, artifactBytes) + } + } + + parseParentCoordinates(pomDocument)?.let { parent -> + materializeArtifactHierarchy( + repoDir = repoDir, + repositories = repositories, + coordinates = parent, + excludedDependencies = excludedDependencies, + rootArtifact = null, + seen = seen + ) + } + } + + private fun parsePom(bytes: ByteArray): Document = + DocumentBuilderFactory.newInstance().apply { + isNamespaceAware = false + }.newDocumentBuilder().parse(ByteArrayInputStream(bytes)) + + private fun parseParentCoordinates(document: Document): Coordinates? { + val project = document.documentElement ?: return null + val parent = childElements(project).firstOrNull { it.tagName == "parent" } ?: return null + val groupId = childText(parent, "groupId") ?: return null + val artifactId = childText(parent, "artifactId") ?: return null + val version = childText(parent, "version") ?: return null + return Coordinates(groupId, artifactId, version) + } + + private fun removeExcludedDependencies(document: Document, excludedDependencies: Set>) { + if (excludedDependencies.isEmpty()) { + return + } + + val dependencies = document.getElementsByTagName("dependency") + val toRemove = mutableListOf() + for (index in 0 until dependencies.length) { + val dependency = dependencies.item(index) as? Element ?: continue + val groupId = childText(dependency, "groupId") ?: continue + val artifactId = childText(dependency, "artifactId") ?: continue + if (excludedDependencies.contains(groupId to artifactId)) { + toRemove.add(dependency) + } + } + + toRemove.forEach { dependency -> + dependency.parentNode?.removeChild(dependency) + } + } + + private fun writePom(path: Path, document: Document) { + Files.createDirectories(path.parent) + TransformerFactory.newInstance().newTransformer().apply { + setOutputProperty(OutputKeys.INDENT, "yes") + setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2") + }.transform(DOMSource(document), StreamResult(path.toFile())) + } + + private fun downloadRequired(repositories: List, relativePath: String): ByteArray = + downloadOptional(repositories, relativePath) + ?: error("Could not download $relativePath from repositories ${repositories.joinToString()}") + + private fun downloadOptional(repositories: List, relativePath: String): ByteArray? { + for (repository in repositories) { + try { + val resolvedUri = URI.create("${repository.toString().trimEnd('/')}/$relativePath") + val bytes = when (resolvedUri.scheme) { + "file" -> { + val path = Path.of(resolvedUri) + if (Files.exists(path)) Files.readAllBytes(path) else null + } + "http", "https" -> resolvedUri.toURL().openStream().use { it.readBytes() } + else -> null + } + if (bytes != null) { + return bytes + } + } catch (_: FileNotFoundException) { + // Try the next repository until one has the requested artifact. + } + } + return null + } + + private fun childText(parent: Element, tagName: String): String? = + childElements(parent).firstOrNull { it.tagName == tagName }?.textContent?.trim() + + private fun childElements(parent: Element): List { + val children = mutableListOf() + val nodeList = parent.childNodes + for (index in 0 until nodeList.length) { + val child = nodeList.item(index) + if (child is Element) { + children.add(child) + } + } + return children + } + + private data class Coordinates( + val groupId: String, + val artifactId: String, + val version: String + ) { + val pomRelativePath: String + get() = "${groupId.replace('.', '/')}/$artifactId/$version/$artifactId-$version.pom" + + fun artifactRelativePath(extension: String, classifier: String?): String { + val classifierSuffix = classifier?.takeIf { it.isNotBlank() }?.let { "-$it" } ?: "" + return "${groupId.replace('.', '/')}/$artifactId/$version/$artifactId-$version$classifierSuffix.$extension" + } + } +} diff --git a/buildSrc/src/main/kotlin/datadog/gradle/plugin/muzzle/MuzzleMavenRepoUtils.kt b/buildSrc/src/main/kotlin/datadog/gradle/plugin/muzzle/MuzzleMavenRepoUtils.kt index 998e0357b18..6511198f8b9 100644 --- a/buildSrc/src/main/kotlin/datadog/gradle/plugin/muzzle/MuzzleMavenRepoUtils.kt +++ b/buildSrc/src/main/kotlin/datadog/gradle/plugin/muzzle/MuzzleMavenRepoUtils.kt @@ -110,6 +110,7 @@ internal object MuzzleMavenRepoUtils { module = muzzleDirective.module versions = version.toString() assertPass = !muzzleDirective.assertPass + additionalRepositories = muzzleDirective.additionalRepositories excludedDependencies = muzzleDirective.excludedDependencies includeSnapshots = muzzleDirective.includeSnapshots } diff --git a/buildSrc/src/main/kotlin/datadog/gradle/plugin/muzzle/MuzzlePlugin.kt b/buildSrc/src/main/kotlin/datadog/gradle/plugin/muzzle/MuzzlePlugin.kt index 876c2234218..73e30db2b15 100644 --- a/buildSrc/src/main/kotlin/datadog/gradle/plugin/muzzle/MuzzlePlugin.kt +++ b/buildSrc/src/main/kotlin/datadog/gradle/plugin/muzzle/MuzzlePlugin.kt @@ -185,6 +185,8 @@ class MuzzlePlugin : Plugin { } } instrumentationProject.configurations.register(muzzleTaskName) { + MuzzleExcludedDependencySupport.applyTo(instrumentationProject, this, muzzleDirective, versionArtifact) + if (!muzzleDirective.isCoreJdk && versionArtifact != null) { val depId = buildString { append("${versionArtifact.groupId}:${versionArtifact.artifactId}:${versionArtifact.version}") diff --git a/dd-java-agent/instrumentation/confluent-schema-registry/confluent-schema-registry-4.1/build.gradle b/dd-java-agent/instrumentation/confluent-schema-registry/confluent-schema-registry-4.1/build.gradle index bc8e14b0a1d..a1ad5109771 100644 --- a/dd-java-agent/instrumentation/confluent-schema-registry/confluent-schema-registry-4.1/build.gradle +++ b/dd-java-agent/instrumentation/confluent-schema-registry/confluent-schema-registry-4.1/build.gradle @@ -7,6 +7,9 @@ muzzle { module = "kafka-schema-registry-client" versions = "[4.1.0,)" excludeDependency "org.codehaus.jackson:jackson-mapper-asl" // missing on some releases + + excludeDependency "org.eclipse.jetty:jetty-bom" // broken upstream POM references missing 9.4.59 + assertInverse = true } }