Browse Source

Merge pull request #48153 from TerryTaoYY

* pr/48153:
  Hande SSL metrics for dynamically registered bundles

Closes gh-48153
pull/48169/head
Stéphane Nicoll 1 month ago
parent
commit
bb247e82aa
  1. 5
      spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/ssl/SslMeterBinder.java
  2. 41
      spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/ssl/SslMeterBinderTests.java

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

@ -65,7 +65,10 @@ class SslMeterBinder implements MeterBinder { @@ -65,7 +65,10 @@ class SslMeterBinder implements MeterBinder {
SslMeterBinder(SslInfo sslInfo, SslBundles sslBundles, Clock clock) {
this.clock = clock;
this.sslInfo = sslInfo;
sslBundles.addBundleRegisterHandler((bundleName, ignored) -> onBundleChange(bundleName));
sslBundles.addBundleRegisterHandler((bundleName, ignored) -> {
onBundleChange(bundleName);
sslBundles.addBundleUpdateHandler(bundleName, (ignoredBundle) -> onBundleChange(bundleName));
});
for (String bundleName : sslBundles.getBundleNames()) {
sslBundles.addBundleUpdateHandler(bundleName, (ignored) -> onBundleChange(bundleName));
}

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

@ -20,6 +20,8 @@ import java.time.Clock; @@ -20,6 +20,8 @@ import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.time.ZoneId;
import java.util.Collections;
import java.util.List;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
@ -34,6 +36,10 @@ import org.springframework.boot.ssl.jks.JksSslStoreBundle; @@ -34,6 +36,10 @@ import org.springframework.boot.ssl.jks.JksSslStoreBundle;
import org.springframework.boot.ssl.jks.JksSslStoreDetails;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.then;
import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.mock;
/**
* Tests for {@link SslMeterBinder}.
@ -63,6 +69,41 @@ class SslMeterBinderTests { @@ -63,6 +69,41 @@ class SslMeterBinderTests {
.hasDays(36889);
}
@Test
void shouldWatchUpdatesForBundlesRegisteredAfterConstruction() {
DefaultSslBundleRegistry sslBundleRegistry = new DefaultSslBundleRegistry();
SslInfo sslInfo = mock(SslInfo.class);
given(sslInfo.getBundles()).willReturn(Collections.emptyList());
SslInfo.BundleInfo bundleInfo = mock(SslInfo.BundleInfo.class);
SslInfo.CertificateChainInfo chainInfo = mock(SslInfo.CertificateChainInfo.class);
SslInfo.CertificateInfo certificateInfo = mock(SslInfo.CertificateInfo.class);
SslInfo.CertificateValidityInfo validityInfo = mock(SslInfo.CertificateValidityInfo.class);
given(sslInfo.getBundle("dynamic")).willReturn(bundleInfo);
given(bundleInfo.getName()).willReturn("dynamic");
given(bundleInfo.getCertificateChains()).willReturn(List.of(chainInfo));
given(chainInfo.getAlias()).willReturn("server");
given(chainInfo.getCertificates()).willReturn(List.of(certificateInfo));
given(certificateInfo.getSerialNumber()).willReturn("serial");
Instant expiry = CLOCK.instant().plus(Duration.ofDays(365));
given(certificateInfo.getValidityEnds()).willReturn(expiry);
given(certificateInfo.getValidity()).willReturn(validityInfo);
given(validityInfo.getStatus()).willReturn(SslInfo.CertificateValidityInfo.Status.VALID);
given(validityInfo.getMessage()).willReturn(null);
SslMeterBinder binder = new SslMeterBinder(sslInfo, sslBundleRegistry, CLOCK);
SimpleMeterRegistry meterRegistry = new SimpleMeterRegistry();
binder.bindTo(meterRegistry);
SslBundle bundle = mock(SslBundle.class);
sslBundleRegistry.registerBundle("dynamic", bundle);
sslBundleRegistry.updateBundle("dynamic", bundle);
then(sslInfo).should(atLeast(2)).getBundle("dynamic");
}
private static long findExpiryGauge(MeterRegistry meterRegistry, String chain, String certificateSerialNumber) {
return (long) meterRegistry.get("ssl.chain.expiry")
.tag("bundle", "test-0")

Loading…
Cancel
Save