Skip to content

Commit 791910b

Browse files
committed
GH-503 - Prevent configuration properties from being proxied for observability.
We now avoid proxying configuration properties in the BeanPostProcessor that creates tracing proxies.
1 parent c4c0c2f commit 791910b

File tree

3 files changed

+91
-2
lines changed

3 files changed

+91
-2
lines changed

spring-modulith-observability/src/main/java/org/springframework/modulith/observability/ModuleTracingBeanPostProcessor.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.springframework.beans.factory.config.BeanDefinition;
3131
import org.springframework.beans.factory.config.BeanPostProcessor;
3232
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
33+
import org.springframework.boot.context.properties.ConfigurationProperties;
3334
import org.springframework.modulith.runtime.ApplicationModulesRuntime;
3435
import org.springframework.util.Assert;
3536

@@ -97,8 +98,19 @@ public Object postProcessAfterInitialization(Object bean, String beanName) throw
9798

9899
private boolean isInfrastructureBean(String beanName) {
99100

100-
return factory.containsBean(beanName) &&
101-
factory.getBeanDefinition(beanName).getRole() == BeanDefinition.ROLE_INFRASTRUCTURE;
101+
if (!factory.containsBean(beanName)) {
102+
return false;
103+
}
104+
105+
if (factory.getBeanDefinition(beanName).getRole() == BeanDefinition.ROLE_INFRASTRUCTURE) {
106+
return true;
107+
}
108+
109+
if (factory.findAnnotationOnBean(beanName, ConfigurationProperties.class, false) != null) {
110+
return true;
111+
}
112+
113+
return false;
102114
}
103115

104116
private Advisor getOrBuildAdvisor(ObservedModule module, ObservedModuleType type) {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright 2024 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+
package example.sample;
17+
18+
import org.springframework.boot.context.properties.ConfigurationProperties;
19+
20+
/**
21+
* @author Oliver Drotbohm
22+
*/
23+
@ConfigurationProperties("sample")
24+
public class SampleProperties {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2024 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+
package org.springframework.modulith.observability;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
import static org.mockito.ArgumentMatchers.*;
20+
import static org.mockito.Mockito.*;
21+
22+
import example.sample.SampleProperties;
23+
24+
import org.junit.jupiter.api.Test;
25+
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
26+
import org.springframework.beans.factory.support.RootBeanDefinition;
27+
import org.springframework.modulith.runtime.ApplicationModulesRuntime;
28+
29+
/**
30+
* Unit tests for {@link ModuleTracingBeanPostProcessor}.
31+
*
32+
* @author Oliver Drotbohm
33+
*/
34+
public class ModuleTracingBeanPostProcessorUnitTests {
35+
36+
@Test // GH-503
37+
void doesNotProxyConfiguationProperties() {
38+
39+
var beanFactory = new DefaultListableBeanFactory();
40+
beanFactory.registerBeanDefinition("properties", new RootBeanDefinition(SampleProperties.class));
41+
42+
var mock = mock(ApplicationModulesRuntime.class);
43+
doReturn(SampleProperties.class).when(mock).getUserClass(any(), any());
44+
doReturn(true).when(mock).isApplicationClass(any());
45+
46+
var processor = new ModuleTracingBeanPostProcessor(mock, () -> null, beanFactory);
47+
48+
var bean = new SampleProperties();
49+
var result = processor.postProcessAfterInitialization(bean, "properties");
50+
51+
assertThat(result).isSameAs(bean);
52+
}
53+
}

0 commit comments

Comments
 (0)