2020import java .time .Duration ;
2121import java .time .Instant ;
2222import java .time .ZoneId ;
23- import java .util .Collections ;
24- import java .util .List ;
2523
2624import io .micrometer .core .instrument .MeterRegistry ;
2725import io .micrometer .core .instrument .simple .SimpleMeterRegistry ;
3634import org .springframework .boot .ssl .jks .JksSslStoreDetails ;
3735
3836import 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 ;
4337
4438/**
4539 * Tests for {@link SslMeterBinder}.
@@ -52,7 +46,8 @@ class SslMeterBinderTests {
5246
5347 @ Test
5448 void shouldRegisterChainExpiryMetrics () {
55- MeterRegistry meterRegistry = bindToRegistry ();
49+ DefaultSslBundleRegistry sslBundleRegistry = createSslBundleRegistry ("classpath:certificates/chains.p12" );
50+ MeterRegistry meterRegistry = bindToRegistry (sslBundleRegistry );
5651 assertThat (Duration .ofSeconds (findExpiryGauge (meterRegistry , "ca" , "419224ce190242b2c44069dd3c560192b3b669f3" )))
5752 .hasDays (1095 );
5853 assertThat (Duration
@@ -72,39 +67,30 @@ void shouldRegisterChainExpiryMetrics() {
7267 @ Test
7368 void shouldWatchUpdatesForBundlesRegisteredAfterConstruction () {
7469 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" );
70+ sslBundleRegistry .registerBundle ("dummy" ,
71+ SslBundle .of (createSslStoreBundle ("classpath:certificates/chains2.p12" )));
72+ MeterRegistry meterRegistry = bindToRegistry (sslBundleRegistry );
73+ sslBundleRegistry .registerBundle ("test-0" ,
74+ SslBundle .of (createSslStoreBundle ("classpath:certificates/chains2.p12" )));
75+ sslBundleRegistry .updateBundle ("test-0" ,
76+ SslBundle .of (createSslStoreBundle ("classpath:certificates/chains.p12" )));
77+ assertThat (Duration .ofSeconds (findExpiryGauge (meterRegistry , "ca" , "419224ce190242b2c44069dd3c560192b3b669f3" )))
78+ .hasDays (1095 );
79+ assertThat (Duration
80+ .ofSeconds (findExpiryGauge (meterRegistry , "intermediary" , "60f79365fc46bf69149754d377680192b3b6bcf5" )))
81+ .hasDays (730 );
82+ assertThat (Duration
83+ .ofSeconds (findExpiryGauge (meterRegistry , "server" , "504c45129526ac050abb11459b1f0192b3b70fe9" )))
84+ .hasDays (365 );
85+ assertThat (Duration
86+ .ofSeconds (findExpiryGauge (meterRegistry , "expired" , "562bc5dcf4f26bb179abb13068180192b3bb53dc" )))
87+ .hasDays (-386 );
88+ assertThat (Duration
89+ .ofSeconds (findExpiryGauge (meterRegistry , "not-yet-valid" , "7df79335f274e2cfa7467fd5f9ce0192b3bcf4aa" )))
90+ .hasDays (36889 );
10591 }
10692
107- private static long findExpiryGauge (MeterRegistry meterRegistry , String chain , String certificateSerialNumber ) {
93+ private long findExpiryGauge (MeterRegistry meterRegistry , String chain , String certificateSerialNumber ) {
10894 return (long ) meterRegistry .get ("ssl.chain.expiry" )
10995 .tag ("bundle" , "test-0" )
11096 .tag ("chain" , chain )
@@ -113,27 +99,26 @@ private static long findExpiryGauge(MeterRegistry meterRegistry, String chain, S
11399 .value ();
114100 }
115101
116- private SimpleMeterRegistry bindToRegistry () {
117- SslBundles sslBundles = createSslBundles ("classpath:certificates/chains.p12" );
118- SslInfo sslInfo = createSslInfo (sslBundles );
102+ private SimpleMeterRegistry bindToRegistry (SslBundles sslBundles ) {
103+ SslInfo sslInfo = new SslInfo (sslBundles );
119104 SslMeterBinder binder = new SslMeterBinder (sslInfo , sslBundles , CLOCK );
120105 SimpleMeterRegistry meterRegistry = new SimpleMeterRegistry ();
121106 binder .bindTo (meterRegistry );
122107 return meterRegistry ;
123108 }
124109
125- private SslBundles createSslBundles (String ... locations ) {
110+ private SslStoreBundle createSslStoreBundle (String location ) {
111+ JksSslStoreDetails keyStoreDetails = JksSslStoreDetails .forLocation (location ).withPassword ("secret" );
112+ return new JksSslStoreBundle (keyStoreDetails , null );
113+ }
114+
115+ private DefaultSslBundleRegistry createSslBundleRegistry (String ... locations ) {
126116 DefaultSslBundleRegistry sslBundleRegistry = new DefaultSslBundleRegistry ();
127117 for (int i = 0 ; i < locations .length ; i ++) {
128- JksSslStoreDetails keyStoreDetails = JksSslStoreDetails .forLocation (locations [i ]).withPassword ("secret" );
129- SslStoreBundle sslStoreBundle = new JksSslStoreBundle (keyStoreDetails , null );
118+ SslStoreBundle sslStoreBundle = createSslStoreBundle (locations [i ]);
130119 sslBundleRegistry .registerBundle ("test-%d" .formatted (i ), SslBundle .of (sslStoreBundle ));
131120 }
132121 return sslBundleRegistry ;
133122 }
134123
135- private SslInfo createSslInfo (SslBundles sslBundles ) {
136- return new SslInfo (sslBundles );
137- }
138-
139124}
0 commit comments