|
| 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.plugins.annotations.LifecyclePhase; |
| 33 | +import org.apache.maven.plugins.annotations.Mojo; |
| 34 | +import org.apache.maven.plugins.annotations.ResolutionScope; |
| 35 | +import org.apache.maven.project.MavenProject; |
| 36 | +import org.cyclonedx.model.Bom; |
| 37 | +import org.cyclonedx.model.Component; |
| 38 | +import org.cyclonedx.model.License; |
| 39 | +import org.cyclonedx.model.LicenseChoice; |
| 40 | +import org.jetbrains.annotations.NotNull; |
| 41 | + |
| 42 | +@Mojo( |
| 43 | + name = "enforceAggregateBom", |
| 44 | + defaultPhase = LifecyclePhase.PACKAGE, |
| 45 | + aggregator = true, |
| 46 | + requiresOnline = true, |
| 47 | + requiresDependencyCollection = ResolutionScope.TEST, |
| 48 | + requiresDependencyResolution = ResolutionScope.TEST |
| 49 | +) |
| 50 | +public class CycloneDxAggregateEnforceMojo extends CycloneDxAggregateMojo { |
| 51 | + |
| 52 | + @Override |
| 53 | + protected boolean shouldExclude(@NotNull MavenProject mavenProject) { |
| 54 | + if (super.shouldExclude(mavenProject)) { |
| 55 | + return true; |
| 56 | + } |
| 57 | + if (enforceExcludeArtifactId != null && enforceExcludeArtifactId.length > 0) { |
| 58 | + if (Arrays.asList(enforceExcludeArtifactId).contains(mavenProject.getArtifactId())) { |
| 59 | + return true; |
| 60 | + } |
| 61 | + } |
| 62 | + return false; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + @NotNull |
| 67 | + protected Bom postProcessingBom(@NotNull Bom bom) throws MojoExecutionException { |
| 68 | + bom = super.postProcessingBom(bom); |
| 69 | + doEnforceComponentsSameVersion(bom); |
| 70 | + doEnforceLicensesBlackListAndWhiteList(bom); |
| 71 | + return bom; |
| 72 | + } |
| 73 | + |
| 74 | + private void doEnforceComponentsSameVersion(@NotNull Bom bom) throws MojoExecutionException { |
| 75 | + if (this.enforceComponentsSameVersion) { |
| 76 | + List<Component> components = bom.getComponents(); |
| 77 | + if (components != null) { |
| 78 | + Map<Pair<String, String>, Set<String>> componentMap = |
| 79 | + new HashMap<>((int) Math.ceil(components.size() / 0.75)); |
| 80 | + for (Component component : components) { |
| 81 | + if (component == null) { |
| 82 | + continue; |
| 83 | + } |
| 84 | + String group = component.getGroup(); |
| 85 | + String name = component.getName(); |
| 86 | + String version = component.getVersion(); |
| 87 | + Pair<String, String> key = Pair.of(group, name); |
| 88 | + Set<String> versions = componentMap.computeIfAbsent( |
| 89 | + key, |
| 90 | + stringStringPair -> new HashSet<>() |
| 91 | + ); |
| 92 | + versions.add(version); |
| 93 | + } |
| 94 | + StringBuilder stringBuilder = new StringBuilder(); |
| 95 | + for (Map.Entry<Pair<String, String>, Set<String>> entry : componentMap.entrySet()) { |
| 96 | + Pair<String, String> key = entry.getKey(); |
| 97 | + Set<String> versions = entry.getValue(); |
| 98 | + if (versions.size() > 1) { |
| 99 | + stringBuilder |
| 100 | + .append("[ERROR]Duplicated versions for ") |
| 101 | + .append(key.getLeft()) |
| 102 | + .append(":") |
| 103 | + .append(key.getRight()) |
| 104 | + .append(" , versions : ") |
| 105 | + .append(StringUtils.join(versions.iterator(), ",")) |
| 106 | + .append("\n"); |
| 107 | + } |
| 108 | + } |
| 109 | + if (stringBuilder.length() > 0) { |
| 110 | + throw new MojoExecutionException(stringBuilder.toString()); |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + private void doEnforceLicensesBlackListAndWhiteList(@NotNull Bom bom) throws MojoExecutionException { |
| 117 | + List<Component> components = bom.getComponents(); |
| 118 | + if (components != null) { |
| 119 | + StringBuilder stringBuilder = new StringBuilder(); |
| 120 | + for (Component component : components) { |
| 121 | + if (component == null) { |
| 122 | + continue; |
| 123 | + } |
| 124 | + String group = component.getGroup(); |
| 125 | + String name = component.getName(); |
| 126 | + LicenseChoice licenseChoice = component.getLicenseChoice(); |
| 127 | + if (licenseChoice == null) { |
| 128 | + continue; |
| 129 | + } |
| 130 | + if (StringUtils.isNotBlank(licenseChoice.getExpression())) { |
| 131 | + getLog().error("[ERROR]Cannot handle spdx license expression for " + group + ":" + name + " , use license id instead"); |
| 132 | + } |
| 133 | + if (licenseChoice.getLicenses() != null) { |
| 134 | + for (License license : licenseChoice.getLicenses()) { |
| 135 | + if (!ArrayUtils.isEmpty(this.enforceLicensesBlackList)) { |
| 136 | + if ( |
| 137 | + ArrayUtils.contains(this.enforceLicensesBlackList, license.getId()) |
| 138 | + || ArrayUtils.contains(this.enforceLicensesBlackList, license.getName()) |
| 139 | + ) { |
| 140 | + stringBuilder |
| 141 | + .append("[ERROR]License in blackList for ") |
| 142 | + .append(group) |
| 143 | + .append(":") |
| 144 | + .append(name) |
| 145 | + .append(" , license : ") |
| 146 | + .append(license.getId()) |
| 147 | + .append("\n"); |
| 148 | + } |
| 149 | + } |
| 150 | + if (!ArrayUtils.isEmpty(this.enforceLicensesWhiteList)) { |
| 151 | + if ( |
| 152 | + !( |
| 153 | + ArrayUtils.contains(this.enforceLicensesBlackList, license.getId()) |
| 154 | + || ArrayUtils.contains(this.enforceLicensesBlackList, license.getName()) |
| 155 | + ) |
| 156 | + ) { |
| 157 | + stringBuilder |
| 158 | + .append("[ERROR]License not in whiteList for ") |
| 159 | + .append(group) |
| 160 | + .append(":") |
| 161 | + .append(name) |
| 162 | + .append(" , license : ") |
| 163 | + .append(license.getId()) |
| 164 | + .append("\n"); |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | + if (stringBuilder.length() > 0) { |
| 171 | + throw new MojoExecutionException(stringBuilder.toString()); |
| 172 | + } |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | +} |
0 commit comments