Skip to content
Closed
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 @@ -67,7 +67,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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
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;
Expand All @@ -34,6 +36,10 @@
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}.
Expand Down Expand Up @@ -63,6 +69,41 @@ void shouldRegisterChainExpiryMetrics() {
.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")
Expand Down