Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

17.4 KB
Raw Blame
import java.util.stream.Collectors

/*
 * 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.
 */

// This adds validation of project dependencies:
// 1) license + notice files (only for dependencies shipped in the binary distribution)
// 2) checksum validation/ generation (for all dependencies, including test-only ones).

// WARNING: The tasks in this file share internal state between tasks without using files.
// Because of this all tasks here must always execute together, so they cannot define task outputs.
// TODO: Rewrite the internal state to use state files containing the ext.jarInfos and its referencedFiles

// This should be false only for debugging.
def failOnError = true

// We're using commons-codec for computing checksums.
buildscript {
  apply from: rootProject.file('build-tools/build-infra/declare-repositories.gradle'), to: delegate

  dependencies {
    classpath libs.commonscodec.commonscodec
  }
}

// Configure license checksum folder for top-level projects.
// (The file("licenses") inside the configure scope resolves
// relative to the current project so they're not the same).
configure(project(":solr")) {
  ext.licensesDir = file("licenses")
}

// All known license types. If 'noticeOptional' is true then
// the notice file must accompany the license.
def licenseTypes = [
    "ASL": [name: "Apache Software License 2.0"],
    "BSD": [name: "Berkeley Software Distribution"],
    //BSD like just means someone has taken the BSD license and put in their name, copyright, or it's a very similar license.
    "BSD_LIKE": [name: "BSD like license"],
    "CDDL": [name: "Common Development and Distribution License", noticeOptional: true],
    "CPL": [name: "Common Public License"],
    "EPL": [name: "Eclipse Public License Version 1.0", noticeOptional: true],
    "MIT": [name: "Massachusetts Institute of Tech. License", noticeOptional: true],
    "MPL": [name: "Mozilla Public License", noticeOptional: true /* NOT SURE on the required notice */],
    "PD": [name: "Public Domain", noticeOptional: true],
    "SUN": [name: "Sun Open Source License", noticeOptional: true],
    "COMPOUND": [name: "Compound license (details in NOTICE file)."],
]

// Projects whose runtime dependencies ship in the binary distribution.
// Keep in sync with gradle/solr/packaging.gradle and solr/packaging/build.gradle.
rootProject.ext.isDistShippingProject = { Project p ->
  p.path == ":solr:server" ||
      p.path.startsWith(":solr:modules:") ||
      p.path == ":solr:cross-dc-manager"
}

allprojects {
  task licenses() {
    group = 'Dependency validation'
    description = "Apply all dependency/ license checks."
  }
  check.dependsOn(licenses)
}

subprojects {
  // Configure jarValidation configuration for all projects. Any dependency
  // declared on this configuration (or any configuration it extends from) will
  // be verified.
  project.configurations {
    jarValidation
    // Dependencies shipped in the binary distribution; LICENSE/NOTICE required only for these.
    distJarValidation {
      canBeConsumed = false
      canBeResolved = false // parents are resolved individually; also keeps it out of dependency locking
    }
  }

  // For Java projects, add all dependencies from the following configurations
  // to jar validation
  plugins.withType(JavaPlugin) {
    project.configurations {
      jarValidation {
        extendsFrom runtimeClasspath
        extendsFrom compileClasspath
        extendsFrom testRuntimeClasspath
        extendsFrom testCompileClasspath
      }
    }

    // Align jarValidation with tested runtimeClasspath versions
    // This ensures validation uses the same versions that were tested
    alignWithRuntimeClasspath(configurations.jarValidation)

    if (rootProject.ext.isDistShippingProject(project)) {
      project.configurations.distJarValidation.extendsFrom project.configurations.runtimeClasspath
    }
  }

  // Collects dependency JAR information for a project and saves it in
  // project.ext.jarInfos. Each dependency has a map of attributes
  // which make it easier to process it later on (name, hash, origin module,
  // see the code below for details).
  task collectJarInfos() {
    dependsOn configurations.jarValidation

    doFirst {
      // When gradle resolves a configuration it applies exclude rules from inherited configurations
      // globally (this seems like a bug to me). So we process each inherited configuration independently
      // but make sure there are no other dependencies on the validation configuration itself.
      def resolvableParents = { Configuration validationConf ->
        if (!validationConf.dependencies.isEmpty()) {
          throw new GradleException(
              "${validationConf.name} must only inherit from other configurations (can't have its own dependencies).")
        }

        def excludeRules = validationConf.excludeRules
        List<Map<String, String>> excludeRuleMaps;
        if (excludeRules && excludeRules.size() > 0) {
          excludeRuleMaps = excludeRules.stream().map {rule ->
            if (rule.module != null) {
              Map.of("group", rule.group, "module", rule.module)
            } else {
              Map.of("group", rule.group)
            }
          }.collect(Collectors.toList())
        }

        return validationConf.extendsFrom.collect {conf ->
          if (excludeRules && excludeRules.size() > 0) {
            def source = conf
            conf = source.copyRecursive()
            conf.canBeResolved = true
            pinCopyToSource(conf, source)
            excludeRuleMaps.forEach {conf.exclude(it)}
          }
          conf
        }.findAll {conf -> conf.canBeResolved}
      }

      ArrayDeque<ResolvedDependency> queue = new ArrayDeque<>()
      resolvableParents(configurations.jarValidation).each {conf ->
        queue.addAll(conf.resolvedConfiguration.firstLevelModuleDependencies)
      }

      def visited = new HashSet<>()
      def seenDeps = new HashSet<>()
      def infos = []

      while (!queue.isEmpty()) {
        def dep = queue.removeFirst()
        seenDeps.add(dep)

        // Skip any artifacts from other Solr Modules (they will be resolved there).
        if (dep.moduleGroup == "org.apache.solr") {
          continue
        }

        // Make sure we don't keep visiting the same children over and over again
        dep.children.each {child ->
          if (!seenDeps.contains(child)) {
            queue.add(child)
          }
        }
        dep.moduleArtifacts.each {resolvedArtifact ->
          def file = resolvedArtifact.file
          if (visited.add(file)) {
            infos.add([
                name: resolvedArtifact.name,
                jarName: file.toPath().getFileName().toString(),
                path: file,
                module: resolvedArtifact.moduleVersion,
                checksum: provider {buildinfra.sha1Digest().digestAsHex(file).trim()},
                // We keep track of the files referenced by this dependency (sha, license, notice, etc.)
                // so that we can determine unused dangling files later on.
                referencedFiles: []
            ])
          }
        }
      }

      project.ext.jarInfos = infos.sort {a, b -> "${a.module}".compareTo("${b.module}")}
      // jarInfos.each { info -> println "${info.module}" }

      // Full transitive set of jars this project contributes to the binary distribution.
      def distFiles = new HashSet<File>()
      resolvableParents(configurations.distJarValidation).each {conf ->
        conf.resolvedConfiguration.resolvedArtifacts.each {artifact -> distFiles.add(artifact.file)}
      }
      project.ext.distJarFiles = distFiles
    }
  }

  // Verifies that each JAR has a corresponding checksum and that it matches actual JAR available for this dependency.
  task validateJarChecksums() {
    group = 'Dependency validation'
    description = "Validate checksums of dependencies"
    dependsOn collectJarInfos

    doLast {
      def errors = []
      jarInfos.each {dep ->
        def expectedChecksumFile = file("${licensesDir}/${dep.jarName}.sha1")
        def forceLuceneVersion = project.properties['lucene.dev.version']
        if (dep.module.id.group == 'org.apache.lucene' && forceLuceneVersion != null &&
            forceLuceneVersion.endsWith('-SNAPSHOT')) {
          logger.log(LogLevel.INFO,
              "Skipping jar checksum validation for local SNAPSHOT dependency ('${dep.module}')")
        } else if (!expectedChecksumFile.exists()) {
          errors <<
              "Dependency checksum missing ('${dep.module}'), expected it at: ${expectedChecksumFile}"
        } else {
          dep.referencedFiles += expectedChecksumFile
          def expected = expectedChecksumFile.getText("UTF-8").trim()
          def actual = dep.checksum.get()
          if (expected.compareToIgnoreCase(actual) != 0) {
            errors <<
                "Dependency checksum mismatch ('${dep.module}'), expected it to be: ${expected}, but was: ${actual}"
          } else {
            logger.log(LogLevel.INFO, "Dependency checksum OK ('${dep.module}')")
          }
        }
      }

      if (errors) {
        def msg = "Dependency checksum validation failed:\n  - " + errors.join("\n  - ") +
            "\n\nThese missing checksums can be generated using:\n  ./gradlew updateLicenses"
        if (failOnError) {
          throw new GradleException(msg)
        } else {
          logger.log(LogLevel.WARN, "WARNING: ${msg}")
        }
      }
    }
  }

  // Locate the set of license file candidates for this dependency. We
  // search for [jar-or-prefix]-LICENSE-[type].txt
  // where 'jar-or-prefix' can be any '-'-delimited prefix of the dependency JAR's name.
  // So for 'commons-io' it can be 'commons-io-LICENSE-foo.txt' or
  // 'commons-LICENSE.txt'
  tasks.register('validateJarLicenses') {
    group = 'Dependency validation'
    description = "Validate license and notice files of dependencies shipped in the binary distribution"
    dependsOn collectJarInfos
    // The shipped set is global: a jar reachable only through another Solr project
    // (e.g. lucene-core via :solr:core) is validated here but ships via :solr:server.
    dependsOn rootProject.subprojects
        .findAll {p -> rootProject.ext.isDistShippingProject(p)}
        .collect {p -> "${p.path}:collectJarInfos"}

    doLast {
      def distJarFiles = rootProject.subprojects
          .findAll {p -> rootProject.ext.isDistShippingProject(p)}
          .collectMany {p -> p.ext.has('distJarFiles') ? p.ext.distJarFiles : []} as Set
      def errors = []
      jarInfos.findAll {dep -> distJarFiles.contains(dep.path)}.each {dep ->
        def baseName = dep.name
        def found = []
        def candidates = []
        while (true) {
          candidates += file("${licensesDir}/${baseName}-LICENSE-[type].txt")
          found += fileTree(dir: licensesDir, include: "${baseName}-LICENSE-*.txt").files
          def prefix = baseName.replaceAll(/[\-][^-]+$/, "")
          if (found || prefix == baseName) {
            break
          }
          baseName = prefix
        }

        if (found.size() == 0) {
          errors <<
              "License file missing ('${dep.module}'), expected it at: ${candidates.join(" or ")}," +
              " where [type] can be any of ${licenseTypes.keySet()}."
        } else if (found.size() > 1) {
          errors << "Multiple license files matching for ('${dep.module}'): ${found.join(", ")}"
        } else {
          def licenseFile = found.get(0)
          dep.referencedFiles += licenseFile
          def m = (licenseFile.name =~ /LICENSE-(.+)\.txt$/)
          if (!m) throw new GradleException("License file name doesn't contain license type?: ${licenseFile.name}")

          def licenseName = m[0][1]
          def licenseType = licenseTypes[licenseName]
          if (!licenseType) {
            errors <<
                "Unknown license type suffix for ('${dep.module}'): ${licenseFile} (must be one of ${licenseTypes.keySet()})"
          } else {
            logger
                .log(LogLevel.INFO, "Dependency license file OK ('${dep.module}'): " + licenseName)

            // Look for sibling NOTICE file.
            def noticeFile = file(licenseFile.path.replaceAll(/\-LICENSE-.+/, "-NOTICE.txt"))
            if (noticeFile.exists()) {
              dep.referencedFiles += noticeFile
              logger
                  .log(LogLevel.INFO, "Dependency notice file OK ('${dep.module}'): " + noticeFile)
            } else if (!licenseType.noticeOptional) {
              errors << "Notice file missing for ('${dep.module}'), expected it at: ${noticeFile}"
            }
          }
        }
      }

      if (errors) {
        def msg = "Certain license/ notice files are missing:\n  - " + errors.join("\n  - ")
        if (failOnError) {
          throw new GradleException(msg)
        } else {
          logger.log(LogLevel.WARN, "WARNING: ${msg}")
        }
      }
    }
  }

  licenses.dependsOn validateJarChecksums, validateJarLicenses
}

// Add top-project level tasks validating dangling files
// and regenerating dependency checksums.
def plist = []
plist.add(project(":solr"))

configure(plist) {
  def validationTasks = subprojects.collectMany {it.tasks.matching {it.name == "licenses"}}
  def jarInfoTasks = subprojects.collectMany {it.tasks.matching {it.name == "collectJarInfos"}}

  // Update dependency checksums.
  task updateLicenses() {
    group = 'Dependency validation'
    description =
        "Write or update checksums of dependencies. May need to run gradlew --write-locks first if changing dependency versions"
    dependsOn jarInfoTasks

    doLast {
      licensesDir.mkdirs()

      // Clean any previous checksums. In theory we wouldn't have to do it --
      // dangling files from any previous JARs would be reported;
      // it automates the process of updating versions and makes it easier though so
      // why not.
      project.delete fileTree(licensesDir, {
        include "*.sha1"
        exclude checkDanglingLicenseFiles.ext.exclude
      })

      def updated = []
      jarInfoTasks.collectMany {task -> task.project.jarInfos}.each {dep ->
        def expectedChecksumFile = file("${licensesDir}/${dep.jarName}.sha1")
        def actual = dep.checksum.get()
        if (expectedChecksumFile.exists()) {
          def expected = expectedChecksumFile.getText("UTF-8").trim()
          if (expected.compareToIgnoreCase(actual) == 0) {
            return;
          }
        }

        updated += "Updated checksum ('${dep.module}'): ${expectedChecksumFile}"
        expectedChecksumFile.write(actual + "\n", "UTF-8")
      }

      updated.sort().each {line -> logger.log(LogLevel.LIFECYCLE, line)}
    }
  }

  // Any validation task must run after all updates have been applied.
  // We add an ordering constraint that any validation task (or its dependency subgraph)
  // must run after updateLicenses
  validationTasks
      .collectMany {task -> [task, task.dependsOn]}
      .flatten()
      .each {task ->
        task.mustRunAfter updateLicenses
      }

  // Check for dangling files in the licenses folder.
  task checkDanglingLicenseFiles() {
    dependsOn validationTasks

    ext {
      exclude = []
    }

    doFirst {
      // Only collect for enabled tasks: https://issues.apache.org/jira/browse/LUCENE-9780
      def enabledValidationTasks = validationTasks.findAll {it.enabled}
      def allReferenced = enabledValidationTasks
          .collectMany {task ->
            task.project.jarInfos.collectMany {it.referencedFiles}
          }
          .collect {it.toString()}

      def patterns = ext.exclude
      def allExisting = fileTree(licensesDir, {
        exclude patterns
      }).files.collect {it.toString()}

      def dangling = (allExisting - allReferenced).sort()

      if (dangling) {
        def msg = "Unreferenced files present in the licenses folder. Delete stale LICENSE/NOTICE " +
            "files manually, or run 'gradlew updateLicenses' to regenerate .sha1 files:\n  - ${dangling.join("\n  - ")}"
        // Only fail when all validation tasks ran, otherwise the reference list is incomplete.
        if (failOnError && enabledValidationTasks.size() == validationTasks.size()) {
          throw new GradleException(msg)
        } else {
          logger.warn("WARNING: ${msg}")
        }
      }
    }
  }

  licenses.dependsOn checkDanglingLicenseFiles
}

// Exclude files that are not a result of direct dependencies but have to be there.
// It would be probably better to move non-dependency licenses into the actual project
// where they're used and only assemble them for the distribution package.

configure(project(":solr")) {
  checkDanglingLicenseFiles {
    exclude += [
        "README.committers.txt"
    ]
  }
}

// solr-ref-guide doesn't contribute any JARs to dependency checks.
configure(project(":solr:solr-ref-guide")) {
  configurations {
    jarValidation {
      exclude group: "*"
    }
  }
}