Skip to content

adding the jmx endpoint exposure related files . #473

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
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 @@ -174,7 +174,8 @@ private void verifyPropertyConfigurationUpdate() {
"spring.cassandra.request.timeout=10s\n" +
"spring.cassandra.connection.connect-timeout=10s\n" +
"spring.cassandra.connection.init-query-timeout=10s\n" +
"logging.pattern.dateformat=yyyy-MM-dd HH:mm:ss.SSS\n");
"logging.pattern.dateformat=yyyy-MM-dd HH:mm:ss.SSS\n" +
"management.endpoints.jmx.exposure.include=*\n");
}

private void verifyEhCacheVersionIsUpgraded() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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.actions;

import org.springframework.sbm.boot.properties.finder.SpringBootDefaultPropertiesFinder;
import org.springframework.sbm.engine.context.ProjectContext;
import org.springframework.sbm.engine.recipe.AbstractAction;

import java.util.Optional;

public class Boot_27_30_JmxEndpointExposureAction extends AbstractAction {

public static final String JMX_ENDPOINT = "management.endpoints.jmx.exposure.include";

@Override
public void apply(ProjectContext context) {
SpringBootDefaultPropertiesFinder springBootDefaultPropertiesFinder = new SpringBootDefaultPropertiesFinder();

context.getApplicationModules()
.getTopmostApplicationModules()
.stream()
.map(m -> m.searchMainResources(springBootDefaultPropertiesFinder))
.filter(Optional::isPresent)
.map(Optional::get)
.forEach(p -> {
if (!p.getProperty(JMX_ENDPOINT).isPresent()) {
p.setProperty(JMX_ENDPOINT, "*");
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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.conditions;

import org.springframework.sbm.boot.upgrade_27_30.filter.JmxEndpointExposureFinder;
import org.springframework.sbm.engine.context.ProjectContext;
import org.springframework.sbm.engine.recipe.Condition;

public class JmxEndpointExposureCondition implements Condition {
@Override
public String getDescription() {
return "Check if 'management.endpoints.jmx.exposure.include' is declared.";
}

@Override
public boolean evaluate(ProjectContext context) {
return context.search(new JmxEndpointExposureFinder()).isEmpty();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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.filter;

import org.springframework.sbm.boot.properties.api.SpringBootApplicationProperties;
import org.springframework.sbm.boot.properties.search.SpringBootApplicationPropertiesResourceListFilter;
import org.springframework.sbm.project.resource.ProjectResourceSet;
import org.springframework.sbm.project.resource.filter.ProjectResourceFinder;
import org.springframework.sbm.properties.api.PropertiesSource;

import java.util.List;

public class JmxEndpointExposureFinder implements ProjectResourceFinder<List<? extends PropertiesSource>> {
public static final String JMX_ENDPOINT_KEY = "management.endpoints.jmx.exposure.include";

@Override
public List<? extends PropertiesSource> apply(ProjectResourceSet projectResourceSet) {
List<SpringBootApplicationProperties> springBootApplicationProperties = new SpringBootApplicationPropertiesResourceListFilter().apply(projectResourceSet);
return springBootApplicationProperties.stream()
.filter(find -> find.getProperty(JMX_ENDPOINT_KEY).isPresent())
.toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -693,10 +693,15 @@
condition:
type: org.springframework.sbm.boot.upgrade_27_30.conditions.LoggingDateFormatCondition
addDefaultPropertiesFileToTopModules: true
- type: org.springframework.sbm.boot.upgrade_27_30.actions.Boot_27_30_AddLoggingDateFormat

- type: org.springframework.sbm.boot.upgrade_27_30.actions.Boot_27_30_AddLoggingDateFormat
description: "Sets logging date format to yyyy-MM-dd HH:mm:ss.SSS"
condition:
type: org.springframework.sbm.boot.upgrade_27_30.conditions.LoggingDateFormatCondition

- type: org.springframework.sbm.boot.upgrade_27_30.actions.Boot_27_30_JmxEndpointExposureAction
description: "Sets JMX endpoint exposure include to *"
condition:
type: org.springframework.sbm.boot.upgrade_27_30.conditions.JmxEndpointExposureCondition


Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@
- type: org.springframework.sbm.boot.upgrade_27_30.actions.Boot_27_30_AddLoggingDateFormat
description: "Sets logging date format to yyyy-MM-dd HH:mm:ss.SSS"
condition:
type: org.springframework.sbm.boot.upgrade_27_30.conditions.LoggingDateFormatCondition
type: org.springframework.sbm.boot.upgrade_27_30.conditions.LoggingDateFormatCondition
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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.actions;

import org.junit.jupiter.api.Test;
import org.springframework.sbm.boot.properties.SpringApplicationPropertiesPathMatcher;
import org.springframework.sbm.boot.properties.SpringBootApplicationPropertiesRegistrar;
import org.springframework.sbm.boot.properties.api.SpringBootApplicationProperties;
import org.springframework.sbm.boot.properties.search.SpringBootApplicationPropertiesResourceListFilter;
import org.springframework.sbm.engine.context.ProjectContext;
import org.springframework.sbm.project.resource.TestProjectContext;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

public class Boot_27_30_JmxEndpointExposureActionTest {
private final String DUMMY_PROPERTY_FILE = "foo=bar\n" +
"defaultBasePackage=org.springframework.sbm";

@Test
public void givenAProjectWithoutJmxEndpointExposureOverride_andSpringBootProperties_applyAction_expectPropertyAdded() {
ProjectContext projectContext = TestProjectContext.buildProjectContext()
.addRegistrar(new SpringBootApplicationPropertiesRegistrar(new SpringApplicationPropertiesPathMatcher()))
.addProjectResource("src/main/resources/application.properties", DUMMY_PROPERTY_FILE)
.build();

Boot_27_30_JmxEndpointExposureAction boot_27_30_jmxEndpointExposureAction = new Boot_27_30_JmxEndpointExposureAction();
boot_27_30_jmxEndpointExposureAction.apply(projectContext);

List<SpringBootApplicationProperties> bootApplicationProperties = new SpringBootApplicationPropertiesResourceListFilter().apply(projectContext.getProjectResources());
assertThat(bootApplicationProperties.size()).isEqualTo(1);
assertThat(bootApplicationProperties.get(0).getProperty("management.endpoints.jmx.exposure.include").isPresent()).isTrue();

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* 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.conditions;

import org.junit.jupiter.api.Test;
import org.springframework.sbm.boot.properties.SpringApplicationPropertiesPathMatcher;
import org.springframework.sbm.boot.properties.SpringBootApplicationPropertiesRegistrar;
import org.springframework.sbm.engine.context.ProjectContext;
import org.springframework.sbm.project.resource.TestProjectContext;

import java.nio.file.Path;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

public class JmxEndpointExposureConditionTest {

private static final String APPLICATION_PROPERTIES_WITH_JMX_ENDPOINT_EXPOSED = "foo=bar\n" +
"migrate=true\n" +
"management.endpoints.jmx.exposure.include=*\n";

private static final String APPLICATION_PROPERTIES_WITHOUT_JMX_ENDPOINT_EXPOSED = "foo=bar\n" +
"migrate=true\n";

@Test
public void givenProjectWithJmxEndpointExposureCustomization_evaluateCondition_expectFalse() {
ProjectContext projectContext = TestProjectContext.buildProjectContext()
.addRegistrar(new SpringBootApplicationPropertiesRegistrar(new SpringApplicationPropertiesPathMatcher()))
.addProjectResource(Path.of("src", "main", "resources", "application.properties"), APPLICATION_PROPERTIES_WITH_JMX_ENDPOINT_EXPOSED)
.build();

JmxEndpointExposureCondition jmxEndpointExposureCondition = new JmxEndpointExposureCondition();
assertThat(jmxEndpointExposureCondition.evaluate(projectContext)).isFalse();
}

@Test
public void givenProjectWithJmxEndpointExposureCustomization_evaluateCondition_expectTrue() {
ProjectContext projectContext = TestProjectContext.buildProjectContext()
.addRegistrar(new SpringBootApplicationPropertiesRegistrar(new SpringApplicationPropertiesPathMatcher()))
.addProjectResource(Path.of("src", "main", "resources", "application.properties"), APPLICATION_PROPERTIES_WITHOUT_JMX_ENDPOINT_EXPOSED)
.build();

JmxEndpointExposureCondition jmxEndpointExposureCondition = new JmxEndpointExposureCondition();
assertThat(jmxEndpointExposureCondition.evaluate(projectContext)).isTrue();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* 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.filter;

import org.junit.jupiter.api.Test;
import org.springframework.sbm.boot.properties.SpringApplicationPropertiesPathMatcher;
import org.springframework.sbm.boot.properties.SpringBootApplicationPropertiesRegistrar;
import org.springframework.sbm.engine.context.ProjectContext;
import org.springframework.sbm.project.resource.TestProjectContext;
import org.springframework.sbm.properties.api.PropertiesSource;

import java.nio.file.Path;
import java.util.List;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

public class JmxEndpointExposureFinderTest {
private static final String APPLICATION_PROPERTIES_WITH_JMX_ENDPOINT_EXPOSED = "foo=bar\n" +
"migrate=true\n" +
"management.endpoints.jmx.exposure.include=*\n";

private static final String MULTI_MODULE_POM_XML = "<project xmlns=\"http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " +
"xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\">\n" +
" <modelVersion>4.0.0</modelVersion>\n" +
"\n" +
" <groupId>org.springframework.sbm</groupId>\n" +
" <artifactId>spring-boot-migrator</artifactId>\n" +
" <version>0.11.2-SNAPSHOT</version>\n" +
" <packaging>pom</packaging>\n" +
" <modules>\n" +
" <module>module1</module>\n" +
" <module>module2</module>\n" +
" </modules>\n" +
"</project>";

private static final String SUB_MODULE_POM_XML = "<project xmlns=\"http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " +
"xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\">\n" +
" <parent>\n" +
" <artifactId>spring-boot-migrator</artifactId>\n" +
" <groupId>org.springframework.sbm</groupId>\n" +
" <version>0.11.2-SNAPSHOT</version>\n" +
" <relativePath>../../pom.xml</relativePath>\n" +
" </parent>\n" +
" <modelVersion>4.0.0</modelVersion>\n" +
"\n" +
" <artifactId>{{module}}</artifactId>\n" +
"</project>";
@Test
public void givenProjectWithJmxEndpointExposureCustomization_findResources_returnResource(){
ProjectContext projectContext = TestProjectContext.buildProjectContext()
.addRegistrar(new SpringBootApplicationPropertiesRegistrar(new SpringApplicationPropertiesPathMatcher()))
.addProjectResource(Path.of("src", "main", "resources", "application.properties"), APPLICATION_PROPERTIES_WITH_JMX_ENDPOINT_EXPOSED)
.build();

JmxEndpointExposureFinder jmxEndpointExposureFinder = new JmxEndpointExposureFinder();
List<? extends PropertiesSource> propertiesSources = jmxEndpointExposureFinder.apply(projectContext.getProjectResources());

assertThat(propertiesSources.size()).isEqualTo(1);
assertThat(propertiesSources.get(0).getProperty("management.endpoints.jmx.exposure.include").isPresent()).isTrue();
}

@Test
public void givenMultiModuleProjectWithJmxEndpointExposureCustomization_findResources_returnResource(){
ProjectContext projectContext = TestProjectContext.buildProjectContext()
.withMavenRootBuildFileSource(MULTI_MODULE_POM_XML)
.addProjectResource(Path.of("module1","pom.xml"),SUB_MODULE_POM_XML.replace("{{module}}", "module1"))
.addProjectResource(Path.of("module2","pom.xml"),SUB_MODULE_POM_XML.replace("{{module}}", "module2"))
.addRegistrar(new SpringBootApplicationPropertiesRegistrar(new SpringApplicationPropertiesPathMatcher()))
.addProjectResource(Path.of("module1","src", "main", "resources", "application.properties"), APPLICATION_PROPERTIES_WITH_JMX_ENDPOINT_EXPOSED)
.addProjectResource(Path.of("module2","src", "main", "resources", "application.properties"), APPLICATION_PROPERTIES_WITH_JMX_ENDPOINT_EXPOSED)
.build();

JmxEndpointExposureFinder jmxEndpointExposureFinder = new JmxEndpointExposureFinder();
List<? extends PropertiesSource> propertiesSources = jmxEndpointExposureFinder.apply(projectContext.getProjectResources());

assertThat(propertiesSources.size()).isEqualTo(2);
assertThat(propertiesSources.get(0).getProperty("management.endpoints.jmx.exposure.include").isPresent()).isTrue();
assertThat(propertiesSources.get(1).getProperty("management.endpoints.jmx.exposure.include").isPresent()).isTrue();
}
}