3737import com .github .tomakehurst .wiremock .jetty9 .JettyHttpServer ;
3838import com .github .tomakehurst .wiremock .junit .WireMockRule ;
3939import com .google .common .base .Joiner ;
40+ import java .io .ByteArrayOutputStream ;
4041import java .io .FileNotFoundException ;
4142import java .io .IOException ;
43+ import java .io .InputStream ;
4244import java .net .InetSocketAddress ;
4345import java .net .URISyntaxException ;
4446import java .net .URL ;
4547import java .nio .charset .StandardCharsets ;
4648import java .nio .file .Files ;
4749import java .nio .file .Path ;
4850import java .nio .file .Paths ;
51+ import java .util .zip .ZipEntry ;
52+ import java .util .zip .ZipInputStream ;
53+ import java .util .zip .ZipOutputStream ;
4954import org .eclipse .jetty .io .NetworkTrafficListener ;
5055import org .eclipse .jetty .server .ConnectionFactory ;
5156import org .eclipse .jetty .server .ServerConnector ;
5257import org .eclipse .jetty .server .SslConnectionFactory ;
5358import org .eclipse .jetty .util .ssl .SslContextFactory ;
59+ import org .junit .After ;
60+ import org .junit .Before ;
5461import org .junit .Rule ;
5562import org .junit .Test ;
5663import org .junit .runner .RunWith ;
@@ -65,21 +72,39 @@ public class CloudConfigFactoryTest {
6572 public WireMockRule wireMockRule =
6673 new WireMockRule (
6774 wireMockConfig ()
68- .httpsPort (30443 )
6975 .dynamicPort ()
76+ .dynamicHttpsPort ()
7077 .httpServerFactory (new HttpsServerFactory ())
7178 .needClientAuth (true )
7279 .keystorePath (path ("/config/cloud/identity.jks" ).toString ())
7380 .keystorePassword ("fakePasswordForTests" )
81+ .keyManagerPassword ("fakePasswordForTests" )
82+ .keystoreType ("JKS" )
7483 .trustStorePath (path ("/config/cloud/trustStore.jks" ).toString ())
75- .trustStorePassword ("fakePasswordForTests2" ));
84+ .trustStorePassword ("fakePasswordForTests2" )
85+ .trustStoreType ("JKS" ));
86+
87+ private Path tempBundlePath ;
88+
89+ @ Before
90+ public void createBundle () throws Exception {
91+ tempBundlePath = Files .createTempFile ("secure-connect" , ".zip" );
92+ Files .write (tempBundlePath , secureBundle ());
93+ }
94+
95+ @ After
96+ public void cleanupBundle () throws IOException {
97+ if (tempBundlePath != null ) {
98+ Files .deleteIfExists (tempBundlePath );
99+ }
100+ }
76101
77102 public CloudConfigFactoryTest () throws URISyntaxException {}
78103
79104 @ Test
80105 public void should_load_config_from_local_filesystem () throws Exception {
81106 // given
82- URL configFile = getClass ().getResource ( BUNDLE_PATH );
107+ URL configFile = tempBundlePath . toUri ().toURL ( );
83108 mockProxyMetadataService (jsonMetadata ());
84109 // when
85110 CloudConfigFactory cloudConfigFactory = new CloudConfigFactory ();
@@ -91,7 +116,7 @@ public void should_load_config_from_local_filesystem() throws Exception {
91116 @ Test
92117 public void should_load_config_from_external_location () throws Exception {
93118 // given
94- mockHttpSecureBundle (secureBundle ( ));
119+ mockHttpSecureBundle (Files . readAllBytes ( tempBundlePath ));
95120 mockProxyMetadataService (jsonMetadata ());
96121 // when
97122 URL configFile = new URL ("http" , "localhost" , wireMockRule .port (), BUNDLE_PATH );
@@ -130,7 +155,7 @@ public void should_throw_when_bundle_not_readable() throws Exception {
130155 @ Test
131156 public void should_throw_when_metadata_not_found () throws Exception {
132157 // given
133- mockHttpSecureBundle (secureBundle ( ));
158+ mockHttpSecureBundle (Files . readAllBytes ( tempBundlePath ));
134159 stubFor (any (urlPathEqualTo ("/metadata" )).willReturn (aResponse ().withStatus (404 )));
135160 // when
136161 URL configFile = new URL ("http" , "localhost" , wireMockRule .port (), BUNDLE_PATH );
@@ -142,7 +167,7 @@ public void should_throw_when_metadata_not_found() throws Exception {
142167 @ Test
143168 public void should_throw_when_metadata_not_readable () throws Exception {
144169 // given
145- mockHttpSecureBundle (secureBundle ( ));
170+ mockHttpSecureBundle (Files . readAllBytes ( tempBundlePath ));
146171 mockProxyMetadataService ("not a valid json payload" );
147172 // when
148173 URL configFile = new URL ("http" , "localhost" , wireMockRule .port (), BUNDLE_PATH );
@@ -171,8 +196,37 @@ private void mockProxyMetadataService(String jsonMetadata) {
171196 .withBody (jsonMetadata )));
172197 }
173198
174- private byte [] secureBundle () throws IOException , URISyntaxException {
175- return Files .readAllBytes (path (BUNDLE_PATH ));
199+ private byte [] secureBundle () throws IOException {
200+ try (InputStream in = getClass ().getResourceAsStream (BUNDLE_PATH );
201+ ZipInputStream zipIn = new ZipInputStream (in );
202+ ByteArrayOutputStream out = new ByteArrayOutputStream ();
203+ ZipOutputStream zipOut = new ZipOutputStream (out )) {
204+ ZipEntry entry ;
205+ byte [] buffer = new byte [8192 ];
206+ while ((entry = zipIn .getNextEntry ()) != null ) {
207+ zipOut .putNextEntry (new ZipEntry (entry .getName ()));
208+ if ("config.json" .equals (entry .getName ())) {
209+ ByteArrayOutputStream configBuffer = new ByteArrayOutputStream ();
210+ int len ;
211+ while ((len = zipIn .read (buffer )) != -1 ) {
212+ configBuffer .write (buffer , 0 , len );
213+ }
214+ String config =
215+ new String (configBuffer .toByteArray (), StandardCharsets .UTF_8 )
216+ .replace ("\" port\" : 30443" , "\" port\" : " + wireMockRule .httpsPort ());
217+ zipOut .write (config .getBytes (StandardCharsets .UTF_8 ));
218+ } else {
219+ int len ;
220+ while ((len = zipIn .read (buffer )) != -1 ) {
221+ zipOut .write (buffer , 0 , len );
222+ }
223+ }
224+ zipOut .closeEntry ();
225+ zipIn .closeEntry ();
226+ }
227+ zipOut .finish ();
228+ return out .toByteArray ();
229+ }
176230 }
177231
178232 private String jsonMetadata () throws IOException , URISyntaxException {
@@ -218,7 +272,8 @@ protected ServerConnector createServerConnector(
218272 int port ,
219273 NetworkTrafficListener listener ,
220274 ConnectionFactory ... connectionFactories ) {
221- if (port == options .httpsSettings ().port ()) {
275+ if (connectionFactories .length > 0
276+ && connectionFactories [0 ] instanceof SslConnectionFactory ) {
222277 SslConnectionFactory sslConnectionFactory =
223278 (SslConnectionFactory ) connectionFactories [0 ];
224279 SslContextFactory sslContextFactory = sslConnectionFactory .getSslContextFactory ();
0 commit comments