Skip to content

Report section for Actuator Endpoints Sanitization #570

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Module_contains_Test {
@Test
void singleModuleProject() {
String rootPom = PomBuilder
.buiildPom("com.example:parent:1.0")
.buildPom("com.example:parent:1.0")
.type("jar")
.withModules("module1", "module2")
.build();
Expand Down Expand Up @@ -66,19 +66,19 @@ public class SomeClassTest {}
@Test
void multiModuleProject() {
String rootPom = PomBuilder
.buiildPom("com.example:parent:1.0")
.buildPom("com.example:parent:1.0")
.type("pom")
.withModules("module1", "module2")
.build();

String module1Pom = PomBuilder
.buiildPom("com.example:parent:1.0", "module1")
.buildPom("com.example:parent:1.0", "module1")
.unscopedDependencies("com.example:module2:1.0")
.build();

String module2Pom = PomBuilder.buiildPom("com.example:parent:1.0", "module2").build();
String module2Pom = PomBuilder.buildPom("com.example:parent:1.0", "module2").build();

String moduleInModule1Pom = PomBuilder.buiildPom("com.example:parent:1.0", "module-in-module1").build();
String moduleInModule1Pom = PomBuilder.buildPom("com.example:parent:1.0", "module-in-module1").build();


String javaClass = """
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void noExecutionContextGiven() {

@Test
void customExecutionContextGiven() {
String pom = PomBuilder.buiildPom("com.example:project:1.0").build();
String pom = PomBuilder.buildPom("com.example:project:1.0").build();
ExecutionContext ctx = new RewriteExecutionContext();
sut.parse(ctx, pom);
// first time when initializing the parser
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package org.springframework.sbm.build.util;

import org.openrewrite.maven.tree.Scope;
import org.springframework.sbm.build.api.Dependency;
import org.springframework.sbm.project.parser.DependencyHelper;

import java.util.*;
Expand All @@ -34,20 +33,39 @@ public class PomBuilder {
private Map<Scope, org.openrewrite.maven.tree.Dependency> dependencies = new LinkedHashMap<Scope, org.openrewrite.maven.tree.Dependency>();

private DependencyHelper dependencyHelper = new DependencyHelper();
private String parentPom;

public static PomBuilder buiildPom(String coordinate) {
public static PomBuilder buildPom(String coordinate) {
PomBuilder pomBuilder = new PomBuilder();
pomBuilder.coordinate = coordinate;
return pomBuilder;
}

public static PomBuilder buiildPom(String parent, String artifactId) {
public static PomBuilder buildPom(String parentCoordinate, String artifactId) {
PomBuilder pomBuilder = new PomBuilder();
pomBuilder.parent = parent;
pomBuilder.parent = parentCoordinate;
pomBuilder.artifactId = artifactId;
return pomBuilder;
}

/**
* Build a parent pom file with a parent, e.g. spring-boot-starter-parent
*
* @param parentCoordinate
* @param coordinate
*/
public static PomBuilder buildParentPom(String parentCoordinate, String coordinate) {
PomBuilder pomBuilder = new PomBuilder();
pomBuilder.parentPom = parentCoordinate;
pomBuilder.coordinate = coordinate;
return pomBuilder;
}

/**
* Add modules to a pom.
*
* @param moduleArtifactNames one or more module artifactIds
*/
public PomBuilder withModules(String... moduleArtifactNames) {
this.modules = Arrays.asList(moduleArtifactNames);
if(this.modules.stream().anyMatch(m -> m.contains(":"))) throw new RuntimeException("Found ':' in artifact name but artifact names of modules must not be provided as coordinate.");
Expand All @@ -62,6 +80,10 @@ public String build() {
<modelVersion>4.0.0</modelVersion>
""");

if(parentPom != null && parent != null) {
throw new IllegalStateException("parentPom and parent were set.");
}

if (parent != null) {
String[] coord = parent.split(":");
sb.append(" <parent>").append("\n");
Expand All @@ -70,7 +92,14 @@ public String build() {
sb.append(" <version>").append(coord[2]).append("</version>").append("\n");
sb.append(" </parent>").append("\n");
sb.append(" <artifactId>").append(artifactId).append("</artifactId>").append("\n");
} else {
} else if (parentPom != null) {
String[] coord = parentPom.split(":");
sb.append(" <parent>").append("\n");
sb.append(" <groupId>").append(coord[0]).append("</groupId>").append("\n");
sb.append(" <artifactId>").append(coord[1]).append("</artifactId>").append("\n");
sb.append(" <version>").append(coord[2]).append("</version>").append("\n");
sb.append(" </parent>").append("\n");
} if (parent == null){
String[] coord = coordinate.split(":");
sb.append(" <groupId>").append(coord[0]).append("</groupId>").append("\n");
sb.append(" <artifactId>").append(coord[1]).append("</artifactId>").append("\n");
Expand Down Expand Up @@ -139,14 +168,16 @@ private void renderDependency(StringBuilder dependenciesSection, Scope scope, or
.append(dependency.getArtifactId())
.append("</artifactId>")
.append("\n");
dependenciesSection
.append(" ")
.append(" ")
.append(" ")
.append("<version>")
.append(dependency.getVersion())
.append("</version>")
.append("\n");
if(dependency.getVersion() != null) {
dependenciesSection
.append(" ")
.append(" ")
.append(" ")
.append("<version>")
.append(dependency.getVersion())
.append("</version>")
.append("\n");
}
if(scope != Scope.None) {
dependenciesSection
.append(" ")
Expand Down Expand Up @@ -176,6 +207,13 @@ public PomBuilder unscopedDependencies(String... coordinates) {
return this;
}

public PomBuilder compileScopeDependencies(String... coordinates) {
dependencyHelper.mapCoordinatesToDependencies(Arrays.asList(coordinates))
.stream()
.forEach(c -> this.dependencies.put(Scope.Compile, c));
return this;
}

public PomBuilder testScopeDependencies(String... coordinates) {
dependencyHelper.mapCoordinatesToDependencies(Arrays.asList(coordinates))
.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,12 @@
import org.intellij.lang.annotations.Language;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.io.TempDir;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.sbm.build.api.BuildFile;
import org.springframework.sbm.build.api.DependenciesChangedEvent;
import org.springframework.sbm.build.api.Dependency;
import org.springframework.sbm.build.api.Module;
import org.springframework.sbm.build.api.Plugin;
import org.springframework.sbm.build.util.PomBuilder;
import org.springframework.sbm.engine.context.ProjectContext;
Expand Down Expand Up @@ -907,7 +905,7 @@ private BuildFile getBuildFileByPackagingType(ProjectContext context, String ear
@Nested
class GetDependenciesMultiModuleTest {
String parentPom = PomBuilder
.buiildPom("com.example:parent:1.0")
.buildPom("com.example:parent:1.0")
.withProperties(Map.of(
"jakarta.version", "3.0.2",
"validation.groupId", "jakarta.validation",
Expand All @@ -918,13 +916,13 @@ class GetDependenciesMultiModuleTest {
.build();

String module1Pom = PomBuilder
.buiildPom("com.example:parent:1.0", "module1")
.buildPom("com.example:parent:1.0", "module1")
.unscopedDependencies("com.example:module2:${project.version}")
.testScopeDependencies("javax.annotation:${annotationApi.artifactId}:1.3.2")
.build();

String module2Pom = PomBuilder
.buiildPom("com.example:parent:1.0", "module2")
.buildPom("com.example:parent:1.0", "module2")
.unscopedDependencies("${validation.groupId}:jakarta.validation-api:${jakarta.version}")
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public Map<String, Object> getData(ProjectContext context, @Valid List<SpringBoo
data.put("projectName", context.getBuildFile().getName().get());
}

// FIXME: results in all conditons for all sections being evaluated twice
data.put("numberOfChanges", sections.stream().filter(s -> s.shouldRender(context)).count());

// FIXME: Retrieve Boot version from Finder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
import lombok.Setter;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.sbm.build.api.BuildFile;
import org.springframework.sbm.engine.context.ProjectContext;
import org.springframework.sbm.engine.recipe.Condition;
import org.stringtemplate.v4.ST;

import javax.validation.constraints.NotEmpty;
import java.io.IOException;
Expand All @@ -51,14 +51,24 @@ public class SpringBootUpgradeReportSection {

/**
* Helper acting as {@link Condition} and data provide for a {@link SpringBootUpgradeReportSection}.
* @deprecated Use {@link AbstractHelper} instead
*/
@Deprecated(forRemoval = true)
public interface Helper<T> extends Condition {
/**
* @return {@code Map<String, T>} the model data for the template.
*/
Map<String, T> getData();
}

public static abstract class AbstractHelper<T> implements Helper<T> {

@Override
public String getDescription() {
return "";
}
}

public static final String CHANGE_HEADER = "What Changed";
public static final String AFFECTED = "Why is the application affected";
public static final String REMEDIATION = "Remediation";
Expand Down Expand Up @@ -99,7 +109,7 @@ public boolean shouldRender(ProjectContext context) {
private Set<String> contributors;

@JsonIgnore
private Helper helper;
private Helper<Object> helper;
@JsonIgnore
@Autowired
private SpringBootUpgradeReportFreemarkerSupport freemarkerSupport;
Expand Down Expand Up @@ -199,7 +209,7 @@ private void renderLineBreak(StringBuilder sb) {

private void renderGitHubInfo(StringBuilder sb) {
if(gitHubIssue != null) {
sb.append("Issue: https://github.com/spring-projects-experimental/spring-boot-migrator/issues/").append(gitHubIssue).append("[#").append(gitHubIssue).append("]");
sb.append("Issue: https://github.com/spring-projects-experimental/spring-boot-migrator/issues/").append(gitHubIssue).append("[#").append(gitHubIssue).append("^, role=\"ext-link\"]");
}
if(contributors != null && gitHubIssue != null) {
sb.append(", ");
Expand Down Expand Up @@ -235,7 +245,10 @@ public List<Author> getAuthors() {

private String renderRemediation() {
StringBuilder sb = new StringBuilder();
sb.append(remediation.getDescription()).append(ls).append(ls);
if(remediation.getDescription() != null) {
sb.append(remediation.getDescription()).append(ls);
}
sb.append(ls);
if(remediation.getPossibilities().isEmpty()) {
renderResourcesList(sb, remediation);
renderRecipeButton(sb, remediation.getRecipe());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 2021 - 2022 the original author or authors.
*
* Licensed 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
*
* https://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.
*/
package org.springframework.sbm.boot.upgrade_27_30.report.helper;
import org.springframework.sbm.boot.common.conditions.IsSpringBootProject;
import org.springframework.sbm.boot.upgrade_27_30.report.SpringBootUpgradeReportSection;
import org.springframework.sbm.build.api.BuildFile;
import org.springframework.sbm.build.api.Module;
import org.springframework.sbm.engine.context.ProjectContext;
import org.springframework.sbm.project.resource.ProjectResource;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author Fabian Krüger
*/
public class ActuatorEndpointsSanitizationHelper extends SpringBootUpgradeReportSection.AbstractHelper<List<BuildFile>> {

private static final String ACTUATOR_GROUP_ID = "org.springframework.boot";
private static final String ACTUATOR_ARTIFACT_ID = "spring-boot-actuator";
public static final String VERSION_PATTERN = "(2\\.7\\..*)|(3\\.0\\..*)";
private List<BuildFile> buildFilesWithActuatorOnClasspath;

@Override
public boolean evaluate(ProjectContext context) {
IsSpringBootProject isSpringBootProjectCondition = new IsSpringBootProject();
isSpringBootProjectCondition.setVersionPattern(VERSION_PATTERN);
boolean isSpringBoot3Application = isSpringBootProjectCondition.evaluate(context);
if(! isSpringBoot3Application) {
return false;
}
buildFilesWithActuatorOnClasspath = getActuatorDependency(context);
return ! buildFilesWithActuatorOnClasspath.isEmpty();
}

private List<BuildFile> getActuatorDependency(ProjectContext context) {
return context.getApplicationModules().stream()
.map(Module::getBuildFile)
.filter(b -> b.getEffectiveDependencies().stream().anyMatch(d -> d.getGroupId().equals(ACTUATOR_GROUP_ID) && d.getArtifactId().equals(ACTUATOR_ARTIFACT_ID)))
.sorted(Comparator.comparing(ProjectResource::getSourcePath))
.collect(Collectors.toList());
}

@Override
public Map<String, List<BuildFile>> getData() {
return Map.of("matchingBuildFiles", buildFilesWithActuatorOnClasspath);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
condition:
type: org.springframework.sbm.boot.common.conditions.IsSpringBootProject
versionPattern: "2\\.7\\..*"
description: Bump Spring Boot to 3.0.0-RC2
description: Bump Spring Boot to 3.0.0
openRewriteRecipe: |-
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.java.spring.boot3.data.UpgradeSpringData30
Expand All @@ -17,12 +17,12 @@
recipeList:
- org.openrewrite.maven.spring.UpgradeUnmanagedSpringProject:
versionPattern: "2\\.7\\..*"
newVersion: 3.0.0-RC2
newVersion: 3.0.0
- org.openrewrite.maven.UpgradeParentVersion:
groupId: org.springframework.boot
artifactId: spring-boot-starter-parent
newVersion: 3.0.0-RC2
newVersion: 3.0.0
- org.openrewrite.maven.UpgradeDependencyVersion:
groupId: org.springframework.boot
artifactId: spring-boot-dependencies
newVersion: 3.0.0-RC2
newVersion: 3.0.0
Loading