|
| 1 | +/* |
| 2 | + * Copyright 2002-2023 the original author or authors. |
| 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 | + * https://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 | + |
| 17 | +package org.springframework.security.convention.versions; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.Collections; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.Set; |
| 24 | +import java.util.function.Supplier; |
| 25 | +import java.util.stream.Collectors; |
| 26 | + |
| 27 | +import org.gradle.api.Plugin; |
| 28 | +import org.gradle.api.Project; |
| 29 | +import org.gradle.api.Task; |
| 30 | +import org.gradle.api.artifacts.Configuration; |
| 31 | +import org.gradle.api.artifacts.ModuleVersionIdentifier; |
| 32 | +import org.gradle.api.tasks.TaskProvider; |
| 33 | + |
| 34 | +public class VerifyDependenciesVersionsPlugin implements Plugin<Project> { |
| 35 | + |
| 36 | + @Override |
| 37 | + public void apply(Project project) { |
| 38 | + TaskProvider<Task> provider = project.getTasks().register("verifyDependenciesVersions", (verifyDependenciesVersionsTask) -> { |
| 39 | + verifyDependenciesVersionsTask.setGroup("Verification"); |
| 40 | + verifyDependenciesVersionsTask.setDescription("Verify that specific dependencies are using the same version"); |
| 41 | + List<Configuration> allConfigurations = new ArrayList<>(); |
| 42 | + allConfigurations.addAll(getConfigurations(project)); |
| 43 | + allConfigurations.addAll(getSubprojectsConfigurations(project.getSubprojects())); |
| 44 | + verifyDependenciesVersionsTask.getInputs().property("dependenciesVersions", new DependencySupplier(allConfigurations)); |
| 45 | + verifyDependenciesVersionsTask.doLast((task) -> { |
| 46 | + DependencySupplier dependencies = (DependencySupplier) task.getInputs().getProperties().get("dependenciesVersions"); |
| 47 | + Map<String, List<Artifact>> artifacts = dependencies.get(); |
| 48 | + List<Artifact> oauth2OidcSdk = artifacts.get("oauth2-oidc-sdk"); |
| 49 | + List<Artifact> nimbusJoseJwt = artifacts.get("nimbus-jose-jwt"); |
| 50 | + if (oauth2OidcSdk.size() > 1) { |
| 51 | + throw new IllegalStateException("Found multiple versions of oauth2-oidc-sdk: " + oauth2OidcSdk); |
| 52 | + } |
| 53 | + Artifact oauth2OidcSdkArtifact = oauth2OidcSdk.get(0); |
| 54 | + String nimbusJoseJwtVersion = TransitiveDependencyLookupUtils.lookupJwtVersion(oauth2OidcSdkArtifact.version()); |
| 55 | + List<Artifact> differentVersions = nimbusJoseJwt.stream() |
| 56 | + .filter((artifact) -> !artifact.version().equals(nimbusJoseJwtVersion)) |
| 57 | + .filter((artifact -> !artifact.configurationName().contains("spring-security-cas"))) // CAS uses a different version |
| 58 | + .collect(Collectors.toList()); |
| 59 | + if (!differentVersions.isEmpty()) { |
| 60 | + String message = "Found transitive nimbus-jose-jwt version [" + nimbusJoseJwtVersion + "] in oauth2-oidc-sdk " + oauth2OidcSdkArtifact |
| 61 | + + ", but the project contains a different version of nimbus-jose-jwt " + differentVersions |
| 62 | + + ". Please align the versions of nimbus-jose-jwt."; |
| 63 | + throw new IllegalStateException(message); |
| 64 | + } |
| 65 | + }); |
| 66 | + }); |
| 67 | + project.getTasks().getByName("build").dependsOn(provider); |
| 68 | + } |
| 69 | + |
| 70 | + private List<Configuration> getConfigurations(Project project) { |
| 71 | + return project.getConfigurations().stream() |
| 72 | + .filter(Configuration::isCanBeResolved) |
| 73 | + .filter((config) -> config.getName().equals("runtimeClasspath")) |
| 74 | + .collect(Collectors.toList()); |
| 75 | + } |
| 76 | + |
| 77 | + private List<Configuration> getSubprojectsConfigurations(Set<Project> subprojects) { |
| 78 | + if (subprojects.isEmpty()) { |
| 79 | + return Collections.emptyList(); |
| 80 | + } |
| 81 | + List<Configuration> subprojectConfigurations = new ArrayList<>(); |
| 82 | + for (Project subproject : subprojects) { |
| 83 | + subprojectConfigurations.addAll(getConfigurations(subproject)); |
| 84 | + subprojectConfigurations.addAll(getSubprojectsConfigurations(subproject.getSubprojects())); |
| 85 | + } |
| 86 | + return subprojectConfigurations; |
| 87 | + } |
| 88 | + |
| 89 | + private static class Artifact { |
| 90 | + |
| 91 | + private final String name; |
| 92 | + private final String version; |
| 93 | + private final String configurationName; |
| 94 | + |
| 95 | + private Artifact(String name, String version, String configurationName) { |
| 96 | + this.name = name; |
| 97 | + this.version = version; |
| 98 | + this.configurationName = configurationName; |
| 99 | + } |
| 100 | + |
| 101 | + public String name() { |
| 102 | + return this.name; |
| 103 | + } |
| 104 | + |
| 105 | + public String version() { |
| 106 | + return this.version; |
| 107 | + } |
| 108 | + |
| 109 | + public String configurationName() { |
| 110 | + return this.configurationName; |
| 111 | + } |
| 112 | + |
| 113 | + } |
| 114 | + |
| 115 | + private static final class DependencySupplier implements Supplier<Map<String, List<Artifact>>> { |
| 116 | + |
| 117 | + private final List<Configuration> configurations; |
| 118 | + |
| 119 | + private DependencySupplier(List<Configuration> configurations) { |
| 120 | + this.configurations = configurations; |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public Map<String, List<Artifact>> get() { |
| 125 | + return getDependencies(this.configurations); |
| 126 | + } |
| 127 | + |
| 128 | + private Map<String, List<Artifact>> getDependencies(List<Configuration> configurations) { |
| 129 | + return configurations.stream().flatMap((configuration) -> { |
| 130 | + return configuration.getResolvedConfiguration().getResolvedArtifacts().stream() |
| 131 | + .map((dep) -> { |
| 132 | + ModuleVersionIdentifier id = dep.getModuleVersion().getId(); |
| 133 | + return new Artifact(id.getName(), id.getVersion(), configuration.toString()); |
| 134 | + }); |
| 135 | + }) |
| 136 | + .distinct() |
| 137 | + .collect(Collectors.groupingBy(Artifact::name)); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | +} |
0 commit comments