|
20 | 20 | import java.time.Duration; |
21 | 21 | import java.time.Instant; |
22 | 22 | import java.time.ZoneId; |
| 23 | +import java.util.Collections; |
| 24 | +import java.util.List; |
23 | 25 |
|
24 | 26 | import io.micrometer.core.instrument.MeterRegistry; |
25 | 27 | import io.micrometer.core.instrument.simple.SimpleMeterRegistry; |
|
34 | 36 | import org.springframework.boot.ssl.jks.JksSslStoreDetails; |
35 | 37 |
|
36 | 38 | 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; |
37 | 43 |
|
38 | 44 | /** |
39 | 45 | * Tests for {@link SslMeterBinder}. |
@@ -63,6 +69,41 @@ void shouldRegisterChainExpiryMetrics() { |
63 | 69 | .hasDays(36889); |
64 | 70 | } |
65 | 71 |
|
| 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 | + |
66 | 107 | private static long findExpiryGauge(MeterRegistry meterRegistry, String chain, String certificateSerialNumber) { |
67 | 108 | return (long) meterRegistry.get("ssl.chain.expiry") |
68 | 109 | .tag("bundle", "test-0") |
|
0 commit comments