|
| 1 | +/* |
| 2 | + * This file is part of CycloneDX Maven Plugin. |
| 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 | + * http://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 | + * |
| 16 | + * SPDX-License-Identifier: Apache-2.0 |
| 17 | + * Copyright (c) OWASP Foundation. All Rights Reserved. |
| 18 | + */ |
| 19 | +package org.cyclonedx.maven; |
| 20 | + |
| 21 | +import java.util.Arrays; |
| 22 | +import java.util.HashMap; |
| 23 | +import java.util.HashSet; |
| 24 | +import java.util.List; |
| 25 | +import java.util.Map; |
| 26 | +import java.util.Set; |
| 27 | + |
| 28 | +import org.apache.commons.lang3.ArrayUtils; |
| 29 | +import org.apache.commons.lang3.StringUtils; |
| 30 | +import org.apache.commons.lang3.tuple.Pair; |
| 31 | +import org.apache.maven.plugin.MojoExecutionException; |
| 32 | +import org.apache.maven.plugin.MojoFailureException; |
| 33 | +import org.apache.maven.plugins.annotations.LifecyclePhase; |
| 34 | +import org.apache.maven.plugins.annotations.Mojo; |
| 35 | +import org.apache.maven.plugins.annotations.ResolutionScope; |
| 36 | +import org.apache.maven.project.MavenProject; |
| 37 | +import org.cyclonedx.maven.utils.SpdxLicenseUtil; |
| 38 | +import org.cyclonedx.model.Bom; |
| 39 | +import org.cyclonedx.model.Component; |
| 40 | +import org.cyclonedx.model.License; |
| 41 | +import org.cyclonedx.model.LicenseChoice; |
| 42 | +import org.jetbrains.annotations.NotNull; |
| 43 | +import org.spdx.library.InvalidSPDXAnalysisException; |
| 44 | +import org.spdx.library.model.license.AnyLicenseInfo; |
| 45 | +import org.spdx.library.model.license.LicenseInfoFactory; |
| 46 | + |
| 47 | +@Mojo( |
| 48 | + name = "enforceAggregateBom", |
| 49 | + defaultPhase = LifecyclePhase.PACKAGE, |
| 50 | + aggregator = true, |
| 51 | + requiresOnline = true, |
| 52 | + requiresDependencyCollection = ResolutionScope.TEST, |
| 53 | + requiresDependencyResolution = ResolutionScope.TEST |
| 54 | +) |
| 55 | +public class CycloneDxAggregateEnforceMojo extends CycloneDxAggregateMojo { |
| 56 | + |
| 57 | + @Override |
| 58 | + protected boolean shouldExclude(@NotNull MavenProject mavenProject) { |
| 59 | + if (super.shouldExclude(mavenProject)) { |
| 60 | + return true; |
| 61 | + } |
| 62 | + if (enforceExcludeArtifactId != null && enforceExcludeArtifactId.length > 0) { |
| 63 | + if (Arrays.asList(enforceExcludeArtifactId).contains(mavenProject.getArtifactId())) { |
| 64 | + return true; |
| 65 | + } |
| 66 | + } |
| 67 | + return false; |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + @NotNull |
| 72 | + protected Bom postProcessingBom(@NotNull Bom bom) throws MojoExecutionException { |
| 73 | + bom = super.postProcessingBom(bom); |
| 74 | + doEnforceComponentsSameVersion(bom); |
| 75 | + doEnforceLicensesBlackListAndWhiteList(bom); |
| 76 | + return bom; |
| 77 | + } |
| 78 | + |
| 79 | + private void doEnforceComponentsSameVersion(@NotNull Bom bom) throws MojoExecutionException { |
| 80 | + if (this.enforceComponentsSameVersion) { |
| 81 | + List<Component> components = bom.getComponents(); |
| 82 | + if (components != null) { |
| 83 | + Map<Pair<String, String>, Set<String>> componentMap = |
| 84 | + new HashMap<>((int) Math.ceil(components.size() / 0.75)); |
| 85 | + for (Component component : components) { |
| 86 | + if (component == null) { |
| 87 | + continue; |
| 88 | + } |
| 89 | + String group = component.getGroup(); |
| 90 | + String name = component.getName(); |
| 91 | + String version = component.getVersion(); |
| 92 | + Pair<String, String> key = Pair.of(group, name); |
| 93 | + Set<String> versions = componentMap.computeIfAbsent( |
| 94 | + key, |
| 95 | + stringStringPair -> new HashSet<>() |
| 96 | + ); |
| 97 | + versions.add(version); |
| 98 | + } |
| 99 | + StringBuilder stringBuilder = new StringBuilder(); |
| 100 | + for (Map.Entry<Pair<String, String>, Set<String>> entry : componentMap.entrySet()) { |
| 101 | + Pair<String, String> key = entry.getKey(); |
| 102 | + Set<String> versions = entry.getValue(); |
| 103 | + if (versions.size() > 1) { |
| 104 | + stringBuilder |
| 105 | + .append("[ERROR]Duplicated versions for ") |
| 106 | + .append(key.getLeft()) |
| 107 | + .append(":") |
| 108 | + .append(key.getRight()) |
| 109 | + .append(" , versions : ") |
| 110 | + .append(StringUtils.join(versions.iterator(), ",")) |
| 111 | + .append("\n"); |
| 112 | + } |
| 113 | + } |
| 114 | + if (stringBuilder.length() > 0) { |
| 115 | + throw new MojoExecutionException(stringBuilder.toString()); |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + private void doEnforceLicensesBlackListAndWhiteList(@NotNull Bom bom) throws MojoExecutionException { |
| 122 | + List<Component> components = bom.getComponents(); |
| 123 | + if (components != null) { |
| 124 | + StringBuilder stringBuilder = new StringBuilder(); |
| 125 | + for (Component component : components) { |
| 126 | + if (component == null) { |
| 127 | + continue; |
| 128 | + } |
| 129 | + String group = component.getGroup(); |
| 130 | + String name = component.getName(); |
| 131 | + LicenseChoice licenseChoice = component.getLicenseChoice(); |
| 132 | + if (licenseChoice == null) { |
| 133 | + continue; |
| 134 | + } |
| 135 | + if (StringUtils.isNotBlank(licenseChoice.getExpression())) { |
| 136 | + try { |
| 137 | + AnyLicenseInfo anyLicenseInfo = LicenseInfoFactory.parseSPDXLicenseString(licenseChoice.getExpression()); |
| 138 | + if (!ArrayUtils.isEmpty(this.enforceLicensesBlackList)) { |
| 139 | + if (!SpdxLicenseUtil.isLicensePassBlackList(anyLicenseInfo, this.enforceLicensesBlackList)) { |
| 140 | + stringBuilder |
| 141 | + .append("[ERROR]License in blackList for ") |
| 142 | + .append(group) |
| 143 | + .append(":") |
| 144 | + .append(name) |
| 145 | + .append(" , license : ") |
| 146 | + .append(licenseChoice.getExpression()) |
| 147 | + .append("\n"); |
| 148 | + } |
| 149 | + } |
| 150 | + if (!ArrayUtils.isEmpty(this.enforceLicensesWhiteList)) { |
| 151 | + if (!SpdxLicenseUtil.isLicensePassWhiteList(anyLicenseInfo, this.enforceLicensesWhiteList)) { |
| 152 | + stringBuilder |
| 153 | + .append("[ERROR]License not in whiteList for ") |
| 154 | + .append(group) |
| 155 | + .append(":") |
| 156 | + .append(name) |
| 157 | + .append(" , license : ") |
| 158 | + .append(licenseChoice.getExpression()) |
| 159 | + .append("\n"); |
| 160 | + } |
| 161 | + } |
| 162 | + } catch (InvalidSPDXAnalysisException e) { |
| 163 | + getLog().warn(e); |
| 164 | + } |
| 165 | + } else if (licenseChoice.getLicenses() != null) { |
| 166 | + for (License license : licenseChoice.getLicenses()) { |
| 167 | + if (!ArrayUtils.isEmpty(this.enforceLicensesBlackList)) { |
| 168 | + if ( |
| 169 | + ArrayUtils.contains(this.enforceLicensesBlackList, license.getId()) |
| 170 | + || ArrayUtils.contains(this.enforceLicensesBlackList, license.getName()) |
| 171 | + ) { |
| 172 | + stringBuilder |
| 173 | + .append("[ERROR]License in blackList for ") |
| 174 | + .append(group) |
| 175 | + .append(":") |
| 176 | + .append(name) |
| 177 | + .append(" , license : ") |
| 178 | + .append(license.getId()) |
| 179 | + .append("\n"); |
| 180 | + } |
| 181 | + } |
| 182 | + if (!ArrayUtils.isEmpty(this.enforceLicensesWhiteList)) { |
| 183 | + if ( |
| 184 | + !( |
| 185 | + ArrayUtils.contains(this.enforceLicensesBlackList, license.getId()) |
| 186 | + || ArrayUtils.contains(this.enforceLicensesBlackList, license.getName()) |
| 187 | + ) |
| 188 | + ) { |
| 189 | + stringBuilder |
| 190 | + .append("[ERROR]License not in whiteList for ") |
| 191 | + .append(group) |
| 192 | + .append(":") |
| 193 | + .append(name) |
| 194 | + .append(" , license : ") |
| 195 | + .append(license.getId()) |
| 196 | + .append("\n"); |
| 197 | + } |
| 198 | + } |
| 199 | + } |
| 200 | + } |
| 201 | + } |
| 202 | + if (stringBuilder.length() > 0) { |
| 203 | + throw new MojoExecutionException(stringBuilder.toString()); |
| 204 | + } |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | +} |
0 commit comments