|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +import groovy.json.JsonOutput |
| 21 | +import groovy.json.JsonSlurper |
| 22 | +import org.cyclonedx.gradle.CycloneDxTask |
| 23 | +import org.cyclonedx.model.ExternalReference |
| 24 | +import org.cyclonedx.model.LicenseChoice |
| 25 | +import org.cyclonedx.model.License |
| 26 | +import org.cyclonedx.model.OrganizationalContact |
| 27 | +import org.cyclonedx.model.OrganizationalEntity |
| 28 | +import org.cyclonedx.model.Component |
| 29 | + |
| 30 | +import java.nio.charset.StandardCharsets |
| 31 | +import java.time.format.DateTimeFormatter |
| 32 | +import java.time.temporal.ChronoUnit |
| 33 | + |
| 34 | +apply plugin: 'org.cyclonedx.bom' |
| 35 | + |
| 36 | +project.ext.setProperty('sbomOutputLocation', project.layout.buildDirectory.file("${findProperty('pomArtifactId') ?: project.name}-${projectVersion}-sbom.json")) |
| 37 | + |
| 38 | +def sbomTask = tasks.named('cyclonedxBom', CycloneDxTask) |
| 39 | +sbomTask.configure { CycloneDxTask it -> |
| 40 | + // the 2.x version of Cyclonedx uses a legacy syntax & helpers for setting inputs so the syntax below |
| 41 | + // is required until the 3.x version is GA |
| 42 | + it.setProjectType(findProperty('sbomProjectType')?.toString() ?: Component.Type.FRAMEWORK.name()) |
| 43 | + it.@componentName.set(findProperty('pomArtifactId')?.toString() ?: project.name) |
| 44 | + it.@organizationalEntity.set(new OrganizationalEntity().tap { |
| 45 | + name = 'Apache Software Foundation' |
| 46 | + urls = ['https://www.apache.org/', 'https://security.apache.org/'] |
| 47 | + addContact(new OrganizationalContact().tap { |
| 48 | + name = 'Apache Grails Development Team' |
| 49 | + |
| 50 | + }) |
| 51 | + }) |
| 52 | + it.@licenseChoice.set(new LicenseChoice().tap { |
| 53 | + addLicense(new License().tap { |
| 54 | + name = 'Apache-2.0' |
| 55 | + url = 'https://www.apache.org/licenses/LICENSE-2.0.txt' |
| 56 | + }) |
| 57 | + }) |
| 58 | + |
| 59 | + it.@externalReferences.set([ |
| 60 | + new ExternalReference().tap { |
| 61 | + url = 'https://grails.apache.org/' |
| 62 | + type = ExternalReference.Type.WEBSITE |
| 63 | + } |
| 64 | + ]) |
| 65 | + |
| 66 | + // sboms are published for the purposes of vulnerability analysis so only include the runtime classpath |
| 67 | + it.@includeConfigs.set(['runtimeClasspath']) |
| 68 | + it.@skipConfigs.set(['compileClasspath', 'testRuntimeClasspath']) |
| 69 | + |
| 70 | + // turn off license text since it's base64 encoded & will inflate the jar sizes |
| 71 | + it.@includeLicenseText.set(false) |
| 72 | + |
| 73 | + // disable xml output |
| 74 | + it.xmlOutput.unsetConvention() |
| 75 | + |
| 76 | + def sbomOutputLocation = findProperty('sbomOutputLocation') |
| 77 | + it.jsonOutput.set(sbomOutputLocation.get()) |
| 78 | + it.outputs.file(sbomOutputLocation) |
| 79 | + |
| 80 | + // cyclonedx does not support "choosing" the license placed in the sbom |
| 81 | + // see: https://github.com/CycloneDX/cyclonedx-gradle-plugin/issues/16 |
| 82 | + it.doLast { |
| 83 | + // ordered so that first value is the most preferred, this list is from https://www.apache.org/legal/resolved.html |
| 84 | + def preferredLicenses = ['Apache-2.0', 'EPL-1.0', 'BSD-3-Clause', 'EPL-2.0', 'MIT', 'MIT-0', '0BSD', 'UPL-1.0', |
| 85 | + 'CC0-1.0', 'ICU', 'Xnet', 'NCSA', 'W3C', 'Zlib', 'AFL-3.0', 'MS-PL', 'PSF-2.0', 'APAFML', |
| 86 | + 'BSL-1.0', 'WTFPL', 'Unlicense', 'HPND', 'EPICS', 'TCL'] |
| 87 | + |
| 88 | + // licenses are standardized @ https://spdx.org/licenses/ |
| 89 | + def licenses = [ |
| 90 | + 'Apache-2.0' : [ |
| 91 | + id : 'Apache-2.0', |
| 92 | + url : 'https://www.apache.org/licenses/LICENSE-2.0' |
| 93 | + ], |
| 94 | + 'BSD-2-Clause': [ |
| 95 | + id : 'BSD-2-Clause', |
| 96 | + url : 'https://opensource.org/license/bsd-3-clause/' |
| 97 | + ], |
| 98 | + 'BSD-3-Clause': [ |
| 99 | + id : 'BSD-3-Clause', |
| 100 | + url : 'https://opensource.org/license/bsd-3-clause/' |
| 101 | + ], |
| 102 | + // Variant of Apache 1.1 license. Approved by legal LEGAL-707 |
| 103 | + 'OpenSymphony' : [ |
| 104 | + // id is optional and the opensymphony license doesn't have an SPDX id |
| 105 | + name: 'The OpenSymphony Software License, Version 1.1', |
| 106 | + url: 'https://raw.githubusercontent.com/sitemesh/sitemesh2/refs/heads/master/LICENSE.txt' |
| 107 | + ], |
| 108 | + 'UPL-1.0' : [ |
| 109 | + id: 'UPL-1.0', |
| 110 | + url: 'https://oss.oracle.com/licenses/upl/' |
| 111 | + ], |
| 112 | + |
| 113 | + ] |
| 114 | + |
| 115 | + def licenseMapping = [ |
| 116 | + 'pkg:maven/org.antlr/[email protected]?type=jar' : 'BSD-3-Clause', // maps incorrectly because of https://github.com/CycloneDX/cyclonedx-core-java/issues/205 |
| 117 | + 'pkg:maven/jline/[email protected]?type=jar' : 'BSD-2-Clause', // maps incorrectly because of https://github.com/CycloneDX/cyclonedx-core-java/issues/205 |
| 118 | + 'pkg:maven/org.jline/[email protected]?type=jar' : 'BSD-2-Clause', // maps incorrectly because of https://github.com/CycloneDX/cyclonedx-core-java/issues/205 |
| 119 | + 'pkg:maven/org.liquibase.ext/[email protected]?type=jar': 'Apache-2.0', // maps incorrectly because of https://github.com/liquibase/liquibase/issues/2445 & the base pom does not define a license |
| 120 | + 'pkg:maven/com.oracle.coherence.ce/[email protected]?type=pom': 'UPL-1.0', // does not have map based on license id |
| 121 | + 'pkg:maven/com.oracle.coherence.ce/[email protected]?type=pom': 'UPL-1.0', // does not have map based on license id |
| 122 | + 'pkg:maven/opensymphony/[email protected]?type=jar' : 'OpenSymphony', // custom license approved by legal LEGAL-707 |
| 123 | + 'pkg:maven/org.jruby/[email protected]?type=jar' : 'BSD-3-Clause'// https://web.archive.org/web/20240822213507/http://www.jcraft.com/jzlib/LICENSE.txt shows it's a 3 clause |
| 124 | + ] |
| 125 | + |
| 126 | + // we don't distribute these so these licenses are considered acceptable, but we still prefer ASF licenses. |
| 127 | + // Require a whitelist of any case of category X licenses to prevent accidental inclusion in a distributed artifact |
| 128 | + // this list will need to be updated anytime we change versions so we can revise the licenses |
| 129 | + def licenseExceptions = [ |
| 130 | + 'grails-data-hibernate5-core' : [ |
| 131 | + 'pkg:maven/org.hibernate.common/[email protected]?type=jar': 'LGPL-2.1-only', // hibernate 5 is LGPL, we are migrating to ASF license in hibernate 7 |
| 132 | + 'pkg:maven/org.hibernate/[email protected]?type=jar': 'LGPL-2.1-only', // hibernate 5 is LGPL, we are migrating to ASF license in hibernate 7 |
| 133 | + ], |
| 134 | + 'grails-data-hibernate5' : [ |
| 135 | + 'pkg:maven/org.hibernate.common/[email protected]?type=jar': 'LGPL-2.1-only', // hibernate 5 is LGPL, we are migrating to ASF license in hibernate 7 |
| 136 | + 'pkg:maven/org.hibernate/[email protected]?type=jar': 'LGPL-2.1-only', // hibernate 5 is LGPL, we are migrating to ASF license in hibernate 7 |
| 137 | + ], |
| 138 | + 'grails-data-hibernate5-spring-boot': [ |
| 139 | + 'pkg:maven/org.hibernate.common/[email protected]?type=jar': 'LGPL-2.1-only', // hibernate 5 is LGPL, we are migrating to ASF license in hibernate 7 |
| 140 | + 'pkg:maven/org.hibernate/[email protected]?type=jar': 'LGPL-2.1-only', // hibernate 5 is LGPL, we are migrating to ASF license in hibernate 7 |
| 141 | + ], |
| 142 | + 'grails-data-hibernate5-dbmigration': [ |
| 143 | + 'pkg:maven/javax.xml.bind/[email protected]?type=jar': 'CDDL-1.1', // api export |
| 144 | + ], |
| 145 | + ] |
| 146 | + |
| 147 | + def pickLicense = { String bomRef, List licenseChoices -> |
| 148 | + if (!bomRef) { |
| 149 | + throw new GradleException("No bomRef found for a dependency of ${project.name}, cannot pick license") |
| 150 | + } |
| 151 | + |
| 152 | + logger.info('Picking license for {} from {} choices', bomRef, licenseChoices.size()) |
| 153 | + if (licenseMapping.containsKey(bomRef)) { |
| 154 | + // There are several reasons that cyclone will get the license wrong, usually due to upstream not publishing information or publishing it incorrectly |
| 155 | + // see the licenseMapping map above for details |
| 156 | + def licenseId = licenseMapping[bomRef] |
| 157 | + logger.lifecycle('Forcing license for {} to {}', bomRef, licenseId) |
| 158 | + |
| 159 | + def licenseBlock = licenses[licenseId] |
| 160 | + if (!licenseBlock) { |
| 161 | + throw new GradleException("Cannot find license information for id ${licenseId} to use for bomRef ${bomRef} in project ${project.name}") |
| 162 | + } |
| 163 | + |
| 164 | + return licenseBlock |
| 165 | + } |
| 166 | + |
| 167 | + if (!(licenseChoices instanceof List) || licenseChoices.isEmpty()) { |
| 168 | + throw new GradleException("No License was found for dependency: ${bomRef} in project ${project.name}") |
| 169 | + } |
| 170 | + |
| 171 | + def licenseIds = licenseChoices.findAll { it instanceof Map && it.license instanceof Map && it.license.id } |
| 172 | + def foundLicense = preferredLicenses.find { p -> licenseIds.any { it.license.id == p } } |
| 173 | + if (foundLicense) { |
| 174 | + return licenseIds.find { it.license.id == foundLicense } |
| 175 | + } |
| 176 | + |
| 177 | + def defaultLicense = licenseChoices[0] // pick the first one found |
| 178 | + def defaultLicenseId = defaultLicense.license.id as String |
| 179 | + if (defaultLicenseId == null) { |
| 180 | + throw new GradleException("Could not determine License id for dependency: ${bomRef} in project ${project.name} for value ${defaultLicense}") |
| 181 | + } |
| 182 | + if (!(defaultLicenseId in preferredLicenses)) { |
| 183 | + def projectLicenseExemptions = licenseExceptions[project.name] ?: [:] |
| 184 | + def permittedLicense = projectLicenseExemptions.get(bomRef) == defaultLicenseId |
| 185 | + if (!permittedLicense) { |
| 186 | + throw new GradleException("Unpermitted License found for bom dependency: ${bomRef} in project ${project.name} : ${defaultLicenseId}") |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + return defaultLicense |
| 191 | + } |
| 192 | + |
| 193 | + // json schema is documented here: https://cyclonedx.org/docs/1.6/json/ |
| 194 | + def rewriteSbom = { File f -> |
| 195 | + def bom = new JsonSlurper().parse(f) |
| 196 | + |
| 197 | + // timestamp is not reproducible: https://github.com/CycloneDX/cyclonedx-gradle-plugin/issues/292 |
| 198 | + bom.metadata.timestamp = DateTimeFormatter.ISO_INSTANT.format(buildDate.truncatedTo(ChronoUnit.SECONDS)) |
| 199 | + |
| 200 | + // components[*].licenses |
| 201 | + def comps = (bom instanceof Map && bom.components instanceof List) ? bom.components : [] |
| 202 | + comps.each { c -> |
| 203 | + if (c instanceof Map && c.licenses instanceof List && !c.licenses.isEmpty()) { |
| 204 | + def chosen = pickLicense(c['bom-ref'] as String, c.licenses as List) |
| 205 | + if (chosen != null) { |
| 206 | + c.licenses = [chosen] |
| 207 | + } |
| 208 | + } |
| 209 | + } |
| 210 | + |
| 211 | + // force the serialNumber to be reproducible by removing it & recalculating |
| 212 | + bom.serialNumber = '' |
| 213 | + String withOutSerial = JsonOutput.prettyPrint(JsonOutput.toJson(bom)) |
| 214 | + def uuid = UUID.nameUUIDFromBytes(withOutSerial.getBytes(StandardCharsets.UTF_8.name())) |
| 215 | + def urn = "urn:uuid:${uuid.toString()}" as String |
| 216 | + bom.serialNumber = urn |
| 217 | + |
| 218 | + f.setText(JsonOutput.prettyPrint(JsonOutput.toJson(bom)), StandardCharsets.UTF_8.name()) |
| 219 | + |
| 220 | + logger.info('Rewrote JSON SBOM ({}) to pick preferred license', project.relativePath(f)) |
| 221 | + } |
| 222 | + |
| 223 | + sbomOutputLocation.get().with { rewriteSbom(it.asFile) } |
| 224 | + } |
| 225 | +} |
| 226 | + |
| 227 | +// for now, we only publish the sbom inside of the binary jar (our bom projects won't have a jar file) |
| 228 | +pluginManager.withPlugin('java') { |
| 229 | + tasks.named('assemble').configure { |
| 230 | + dependsOn('cyclonedxBom') |
| 231 | + } |
| 232 | + |
| 233 | + if (!project.findProperty('skipJavaComponent')) { |
| 234 | + tasks.named('jar').configure { Jar jar -> |
| 235 | + jar.dependsOn('cyclonedxBom') |
| 236 | + |
| 237 | + jar.from(findProperty('sbomOutputLocation')) { |
| 238 | + into('META-INF') |
| 239 | + rename { |
| 240 | + 'sbom.json' |
| 241 | + } |
| 242 | + } |
| 243 | + |
| 244 | + jar.manifest { |
| 245 | + attributes('Sbom-Location': 'META-INF/sbom.json') |
| 246 | + attributes('Sbom-Format': 'CycloneDX') |
| 247 | + } |
| 248 | + } |
| 249 | + } |
| 250 | +} |
0 commit comments