Skip to content

Commit bb247e8

Browse files
committed
Merge pull request #48153 from TerryTaoYY
* pr/48153: Hande SSL metrics for dynamically registered bundles Closes gh-48153
2 parents 7a235ca + 8214f3c commit bb247e8

File tree

2 files changed

+45
-1
lines changed
  • spring-boot-project/spring-boot-actuator-autoconfigure/src

2 files changed

+45
-1
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/ssl/SslMeterBinder.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,10 @@ class SslMeterBinder implements MeterBinder {
6565
SslMeterBinder(SslInfo sslInfo, SslBundles sslBundles, Clock clock) {
6666
this.clock = clock;
6767
this.sslInfo = sslInfo;
68-
sslBundles.addBundleRegisterHandler((bundleName, ignored) -> onBundleChange(bundleName));
68+
sslBundles.addBundleRegisterHandler((bundleName, ignored) -> {
69+
onBundleChange(bundleName);
70+
sslBundles.addBundleUpdateHandler(bundleName, (ignoredBundle) -> onBundleChange(bundleName));
71+
});
6972
for (String bundleName : sslBundles.getBundleNames()) {
7073
sslBundles.addBundleUpdateHandler(bundleName, (ignored) -> onBundleChange(bundleName));
7174
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/ssl/SslMeterBinderTests.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import java.time.Duration;
2121
import java.time.Instant;
2222
import java.time.ZoneId;
23+
import java.util.Collections;
24+
import java.util.List;
2325

2426
import io.micrometer.core.instrument.MeterRegistry;
2527
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
@@ -34,6 +36,10 @@
3436
import org.springframework.boot.ssl.jks.JksSslStoreDetails;
3537

3638
import static org.assertj.core.api.Assertions.assertThat;
39+
import static org.mockito.BDDMockito.given;
40+
import static org.mockito.BDDMockito.then;
41+
import static org.mockito.Mockito.atLeast;
42+
import static org.mockito.Mockito.mock;
3743

3844
/**
3945
* Tests for {@link SslMeterBinder}.
@@ -63,6 +69,41 @@ void shouldRegisterChainExpiryMetrics() {
6369
.hasDays(36889);
6470
}
6571

72+
@Test
73+
void shouldWatchUpdatesForBundlesRegisteredAfterConstruction() {
74+
DefaultSslBundleRegistry sslBundleRegistry = new DefaultSslBundleRegistry();
75+
SslInfo sslInfo = mock(SslInfo.class);
76+
given(sslInfo.getBundles()).willReturn(Collections.emptyList());
77+
78+
SslInfo.BundleInfo bundleInfo = mock(SslInfo.BundleInfo.class);
79+
SslInfo.CertificateChainInfo chainInfo = mock(SslInfo.CertificateChainInfo.class);
80+
SslInfo.CertificateInfo certificateInfo = mock(SslInfo.CertificateInfo.class);
81+
SslInfo.CertificateValidityInfo validityInfo = mock(SslInfo.CertificateValidityInfo.class);
82+
83+
given(sslInfo.getBundle("dynamic")).willReturn(bundleInfo);
84+
given(bundleInfo.getName()).willReturn("dynamic");
85+
given(bundleInfo.getCertificateChains()).willReturn(List.of(chainInfo));
86+
given(chainInfo.getAlias()).willReturn("server");
87+
given(chainInfo.getCertificates()).willReturn(List.of(certificateInfo));
88+
given(certificateInfo.getSerialNumber()).willReturn("serial");
89+
90+
Instant expiry = CLOCK.instant().plus(Duration.ofDays(365));
91+
given(certificateInfo.getValidityEnds()).willReturn(expiry);
92+
given(certificateInfo.getValidity()).willReturn(validityInfo);
93+
given(validityInfo.getStatus()).willReturn(SslInfo.CertificateValidityInfo.Status.VALID);
94+
given(validityInfo.getMessage()).willReturn(null);
95+
96+
SslMeterBinder binder = new SslMeterBinder(sslInfo, sslBundleRegistry, CLOCK);
97+
SimpleMeterRegistry meterRegistry = new SimpleMeterRegistry();
98+
binder.bindTo(meterRegistry);
99+
100+
SslBundle bundle = mock(SslBundle.class);
101+
sslBundleRegistry.registerBundle("dynamic", bundle);
102+
sslBundleRegistry.updateBundle("dynamic", bundle);
103+
104+
then(sslInfo).should(atLeast(2)).getBundle("dynamic");
105+
}
106+
66107
private static long findExpiryGauge(MeterRegistry meterRegistry, String chain, String certificateSerialNumber) {
67108
return (long) meterRegistry.get("ssl.chain.expiry")
68109
.tag("bundle", "test-0")

0 commit comments

Comments
 (0)