From 0727ced422df452e426035a37d5cf5082d153fa3 Mon Sep 17 00:00:00 2001 From: Piotr Fus Date: Mon, 26 Feb 2018 17:48:11 +0100 Subject: [PATCH] Add https support --- README.md | 45 +++++- .../xsd/pl/touk/mockserver/api/common.xsd | 11 ++ .../xsd/pl/touk/mockserver/api/request.xsd | 1 + mockserver-tests/pom.xml | 4 + .../tests/MockServerHttpsTest.groovy | 152 ++++++++++++++++++ .../tests/MockServerIntegrationTest.groovy | 12 +- .../src/test/resources/keystore.jks | Bin 0 -> 2257 bytes .../src/test/resources/trusted.jks | Bin 0 -> 2235 bytes .../src/test/resources/truststore.jks | Bin 0 -> 1880 bytes .../src/test/resources/untrusted.jks | Bin 0 -> 2255 bytes .../mockserver/server/ContextExecutor.groovy | 8 +- .../mockserver/server/HttpMockServer.groovy | 32 ++-- .../mockserver/server/HttpServerWraper.groovy | 58 ------- .../server/HttpServerWrapper.groovy | 106 ++++++++++++ .../touk/mockserver/server/HttpsConfig.groovy | 28 ++++ .../pl/touk/mockserver/server/Mock.groovy | 2 + pom.xml | 7 + 17 files changed, 383 insertions(+), 83 deletions(-) create mode 100644 mockserver-tests/src/test/groovy/pl/touk/mockserver/tests/MockServerHttpsTest.groovy create mode 100644 mockserver-tests/src/test/resources/keystore.jks create mode 100644 mockserver-tests/src/test/resources/trusted.jks create mode 100644 mockserver-tests/src/test/resources/truststore.jks create mode 100644 mockserver-tests/src/test/resources/untrusted.jks delete mode 100644 mockserver/src/main/groovy/pl/touk/mockserver/server/HttpServerWraper.groovy create mode 100644 mockserver/src/main/groovy/pl/touk/mockserver/server/HttpServerWrapper.groovy create mode 100644 mockserver/src/main/groovy/pl/touk/mockserver/server/HttpsConfig.groovy diff --git a/README.md b/README.md index c9ee033..2552e18 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,21 @@ testRest { path='testEndpoint' name='testRest' } +testHttps { + soap=false + port=10443 + path='testHttps' + name='testHttps' + method='GET' + https={ + keystorePath='/tmp/keystore.jks' + keystorePassword='keystorePass' + keyPassword='keyPass' + truststorePath='/tmp/truststore.jks' + truststorePassword='truststorePass' + requireClientAuth=true + } +} ``` ### Build with docker @@ -96,7 +111,15 @@ remoteMockServer.addMock(new AddMock( statusCode: ..., method: ..., responseHeaders: ..., - schema: ... + schema: ..., + https: new Https( + keystorePath: '/tmp/keystore.jks', + keystorePassword: 'keystorePass', + keyPassword: 'keyPass', + truststorePath: '/tmp/truststore.jks', + truststorePassword: 'truststorePass', + requireClientAuth: true + ) )) ``` @@ -117,6 +140,14 @@ Send POST request to localhost:/serverControl ... ... + + /tmp/keystore.jks + keystorePass + keyPass + /tmp/truststore.jks + truststorePass + true + ``` @@ -133,6 +164,18 @@ Send POST request to localhost:/serverControl - responseHeaders - groovyClosure as string which must evaluate to Map which will be added to response headers, default { _ -> \[:] } - schema - path to xsd schema file on mockserver classpath; default empty, so no vallidation of request is performed; if validation fails then response has got status 400 and response is raw message from validator - imports - list of imports for closures (each import is separate tag); `alias` is the name of `fullClassName` available in closure; `fullClassName` must be available on classpath of mock server +- https - HTTPS configuration + +#### HTTPS configuration + +- keystorePath - path to keystore in JKS format, keystore should contains only one privateKeyEntry +- keystorePassword - keystore password +- keyPassword - key password +- truststorePath - path to truststore in JKS format +- truststorePassword - truststore password +- requireClientAuth - whether client auth is required (two-way SSL) + +**HTTP** and **HTTPS** should be started on separated ports. ### Closures request properties diff --git a/mockserver-api/src/main/xsd/pl/touk/mockserver/api/common.xsd b/mockserver-api/src/main/xsd/pl/touk/mockserver/api/common.xsd index ac21246..673be7d 100644 --- a/mockserver-api/src/main/xsd/pl/touk/mockserver/api/common.xsd +++ b/mockserver-api/src/main/xsd/pl/touk/mockserver/api/common.xsd @@ -18,5 +18,16 @@ + + + + + + + + + + + diff --git a/mockserver-api/src/main/xsd/pl/touk/mockserver/api/request.xsd b/mockserver-api/src/main/xsd/pl/touk/mockserver/api/request.xsd index 6636b53..23786bc 100644 --- a/mockserver-api/src/main/xsd/pl/touk/mockserver/api/request.xsd +++ b/mockserver-api/src/main/xsd/pl/touk/mockserver/api/request.xsd @@ -20,6 +20,7 @@ + diff --git a/mockserver-tests/pom.xml b/mockserver-tests/pom.xml index f09c8cd..ffc9d6c 100644 --- a/mockserver-tests/pom.xml +++ b/mockserver-tests/pom.xml @@ -44,6 +44,10 @@ mockserver-client ${project.version} + + info.solidsoft.spock + spock-global-unroll + diff --git a/mockserver-tests/src/test/groovy/pl/touk/mockserver/tests/MockServerHttpsTest.groovy b/mockserver-tests/src/test/groovy/pl/touk/mockserver/tests/MockServerHttpsTest.groovy new file mode 100644 index 0000000..8d6eff8 --- /dev/null +++ b/mockserver-tests/src/test/groovy/pl/touk/mockserver/tests/MockServerHttpsTest.groovy @@ -0,0 +1,152 @@ +package pl.touk.mockserver.tests + +import groovy.util.slurpersupport.GPathResult +import org.apache.http.client.methods.CloseableHttpResponse +import org.apache.http.client.methods.HttpPost +import org.apache.http.conn.ssl.SSLConnectionSocketFactory +import org.apache.http.conn.ssl.SSLContexts +import org.apache.http.entity.ContentType +import org.apache.http.entity.StringEntity +import org.apache.http.impl.client.CloseableHttpClient +import org.apache.http.impl.client.HttpClients +import pl.touk.mockserver.api.common.Https +import pl.touk.mockserver.api.request.AddMock +import pl.touk.mockserver.client.RemoteMockServer +import pl.touk.mockserver.client.Util +import pl.touk.mockserver.server.HttpMockServer +import spock.lang.AutoCleanup +import spock.lang.Shared +import spock.lang.Specification + +import javax.net.ssl.SSLContext +import javax.net.ssl.SSLHandshakeException +import java.security.KeyStore + +class MockServerHttpsTest extends Specification { + + RemoteMockServer remoteMockServer = new RemoteMockServer('localhost', 19000) + + @AutoCleanup('stop') + HttpMockServer httpMockServer = new HttpMockServer(19000) + + @Shared + SSLContext noClientAuthSslContext = SSLContexts.custom() + .loadTrustMaterial(trustStore()) + .build() + + @Shared + SSLContext trustedCertificateSslContext = SSLContexts.custom() + .loadKeyMaterial(trustedCertificateKeystore(), 'changeit'.toCharArray()) + .loadTrustMaterial(trustStore()) + .build() + + @Shared + SSLContext untrustedCertificateSslContext = SSLContexts.custom() + .loadKeyMaterial(untrustedCertificateKeystore(), 'changeit'.toCharArray()) + .loadTrustMaterial(trustStore()) + .build() + + def 'should handle HTTPS server' () { + given: + remoteMockServer.addMock(new AddMock( + name: 'testHttps', + path: 'testEndpoint', + port: 10443, + predicate: '''{req -> req.xml.name() == 'request'}''', + response: '''{req -> ""}''', + https: new Https( + keyPassword: 'changeit', + keystorePassword: 'changeit', + keystorePath: MockServerHttpsTest.classLoader.getResource('keystore.jks').path + ), + soap: false + )) + when: + HttpPost restPost = new HttpPost('https://localhost:10443/testEndpoint') + restPost.entity = new StringEntity('', ContentType.create("text/xml", "UTF-8")) + CloseableHttpResponse response = client(noClientAuthSslContext).execute(restPost) + then: + GPathResult restPostResponse = Util.extractXmlResponse(response) + restPostResponse.name() == 'goodResponse-request' + } + + def 'should handle HTTPS server with client auth' () { + given: + remoteMockServer.addMock(new AddMock( + name: 'testHttps', + path: 'testEndpoint', + port: 10443, + predicate: '''{req -> req.xml.name() == 'request'}''', + response: '''{req -> ""}''', + https: new Https( + keyPassword: 'changeit', + keystorePassword: 'changeit', + keystorePath: MockServerHttpsTest.classLoader.getResource('keystore.jks').path, + truststorePath: MockServerHttpsTest.classLoader.getResource('truststore.jks').path, + truststorePassword: 'changeit', + requireClientAuth: true + ), + soap: false + )) + when: + HttpPost restPost = new HttpPost('https://localhost:10443/testEndpoint') + restPost.entity = new StringEntity('', ContentType.create("text/xml", "UTF-8")) + CloseableHttpResponse response = client(trustedCertificateSslContext).execute(restPost) + then: + GPathResult restPostResponse = Util.extractXmlResponse(response) + restPostResponse.name() == 'goodResponse-request' + } + + def 'should handle HTTPS server with wrong client auth' () { + given: + remoteMockServer.addMock(new AddMock( + name: 'testHttps', + path: 'testEndpoint', + port: 10443, + predicate: '''{req -> req.xml.name() == 'request'}''', + response: '''{req -> ""}''', + https: new Https( + keyPassword: 'changeit', + keystorePassword: 'changeit', + keystorePath: MockServerHttpsTest.classLoader.getResource('keystore.jks').path, + truststorePath: MockServerHttpsTest.classLoader.getResource('truststore.jks').path, + truststorePassword: 'changeit', + requireClientAuth: true + ), + soap: false + )) + when: + HttpPost restPost = new HttpPost('https://localhost:10443/testEndpoint') + restPost.entity = new StringEntity('', ContentType.create("text/xml", "UTF-8")) + client(sslContext).execute(restPost) + then: + thrown(SSLHandshakeException) + where: + sslContext << [noClientAuthSslContext, untrustedCertificateSslContext] + } + + private CloseableHttpClient client(SSLContext sslContext) { + return HttpClients.custom() + .setHostnameVerifier(SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER) + .setSslcontext(sslContext) + .build() + } + + private KeyStore trustedCertificateKeystore() { + return loadKeystore('trusted.jks') + } + + private KeyStore untrustedCertificateKeystore() { + return loadKeystore('untrusted.jks') + } + + private KeyStore trustStore() { + return loadKeystore('truststore.jks') + } + + private KeyStore loadKeystore(String fileName) { + KeyStore truststore = KeyStore.getInstance(KeyStore.defaultType) + truststore.load(new FileInputStream(MockServerHttpsTest.classLoader.getResource(fileName).path), "changeit".toCharArray()); + return truststore + } +} diff --git a/mockserver-tests/src/test/groovy/pl/touk/mockserver/tests/MockServerIntegrationTest.groovy b/mockserver-tests/src/test/groovy/pl/touk/mockserver/tests/MockServerIntegrationTest.groovy index ab16248..e9f2d42 100644 --- a/mockserver-tests/src/test/groovy/pl/touk/mockserver/tests/MockServerIntegrationTest.groovy +++ b/mockserver-tests/src/test/groovy/pl/touk/mockserver/tests/MockServerIntegrationTest.groovy @@ -27,14 +27,15 @@ import pl.touk.mockserver.client.MockDoesNotExist import pl.touk.mockserver.client.RemoteMockServer import pl.touk.mockserver.client.Util import pl.touk.mockserver.server.HttpMockServer +import spock.lang.AutoCleanup import spock.lang.Shared import spock.lang.Specification -import spock.lang.Unroll class MockServerIntegrationTest extends Specification { RemoteMockServer remoteMockServer + @AutoCleanup('stop') HttpMockServer httpMockServer @Shared @@ -45,10 +46,6 @@ class MockServerIntegrationTest extends Specification { remoteMockServer = new RemoteMockServer('localhost', 9000) } - def cleanup() { - httpMockServer.stop() - } - def "should add working rest mock on endpoint"() { expect: remoteMockServer.addMock(new AddMock( @@ -244,7 +241,6 @@ class MockServerIntegrationTest extends Specification { soapPostResponse.Body.'goodResponseSoap-request'.size() == 1 } - @Unroll def "should dispatch rest mocks when second on #name"() { given: remoteMockServer.addMock(new AddMock( @@ -283,7 +279,6 @@ class MockServerIntegrationTest extends Specification { 9998 | 'test2' | 'another port and path' } - @Unroll def "should dispatch rest mock with response code"() { given: remoteMockServer.addMock(new AddMock( @@ -857,7 +852,6 @@ class MockServerIntegrationTest extends Specification { mockEvents2[0].response.statusCode == 202 } - @Unroll def "should return mock report with #mockEvents events when deleting mock with flag skip mock = #skipReport"() { expect: remoteMockServer.addMock(new AddMock( @@ -885,7 +879,6 @@ class MockServerIntegrationTest extends Specification { true | 0 } - @Unroll def "should reject mock when it has System.exit in closure"() { when: remoteMockServer.addMock(new AddMock( @@ -1133,7 +1126,6 @@ class MockServerIntegrationTest extends Specification { remoteMockServer.removeMock('testRest')?.size() == 1 } - @Unroll def 'should handle leading slash'() { given: String name = "testRest-${UUID.randomUUID().toString()}" diff --git a/mockserver-tests/src/test/resources/keystore.jks b/mockserver-tests/src/test/resources/keystore.jks new file mode 100644 index 0000000000000000000000000000000000000000..d5e35d1551194e1ced94cb1f249b8decfa66aa0a GIT binary patch literal 2257 zcmc(gX*kr29>?cD3o-U3O9qi*I>XG^mpFr%GWLCMY>mbesZJ;iMp5i8xBaZLn*Pa&yxKlP>n>7CAy@#LoIJ^s?%3^}d*dwO4oQeB zBfPWB*Iy4*Bs_h?*e=idB>zQCreCGnUlPX8$xQ=8%OP{(uVv?o#H$44Sv(e)lf2ey zJ7inI{d5(b!85)w@Xf<~no6QW%zpEjitAk{wiPm~xxhC+rJD3MWCot+AvmKrJ}281 zAqVL1uo~T$Z?{#iKIV(W#*B^_EkOB_?c+Iwl9@F1PDG2aYU%c9c;?!Al9?fmDLX{I zMK7%7E84GCp3>60C(aH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+4Nmqa!-JfR6w~41B^Q*ZpMOoIKRqJRjNesC6^}}At5Gm&K^K!4T zOtg9V#DB^Tz0bFWIFs}~9-nshsknEF_tAwXA2ofM5O<+;6-)WO`zn@gbFWN2`Xuh; c-UT~myb*gL`u>sUT7xSMcXgI*(%Ya10Jt*6!~g&Q literal 0 HcmV?d00001 diff --git a/mockserver-tests/src/test/resources/untrusted.jks b/mockserver-tests/src/test/resources/untrusted.jks new file mode 100644 index 0000000000000000000000000000000000000000..ca94b454c5d413ac28dbcf921a5b678eb20896a5 GIT binary patch literal 2255 zcmc(gX*kr29>?cz#xNNBQWQd6hL{<<7(&QSS(6T8h-(?k8D$-eP-Mx3v9?)eLLn^} zB8@HTpzJ3p3{qySgX5g%-skqB_xHv3#rN~-_k5q<^Zo8E?=6EsAdUk8{|FWt9D$1r zkMO;GK)g)SSI&Y!oFF6t!bb8#d6c051VC^I0RSfmLV(OGZ&JRCH8R4@^J^DW8`_3_ z6rWi>v{S9NoI!bfsxbh%!D-lhck{xHgzm4F`ofr$A%t{j5 zdc0=sU3`1phd6T`3$b&HimkERIZfVAy3#GxALgM%6)?5r;%jNw6xJ8k_DcBBwPaVv zQ?7%r)s=P3^yE8?#e;h>1(c5Tq6j6XS=N%6rP})QE)# z--?D8&4V3+ZFN8_TdqDUz?0qv%rcRhb<^$r9vi z_dDnEWWa7GcA{ZDZ$wmm#uePdT#MUl-uhyzx)czlPJ@haBC=CxBH?`7{h`RYCUgn+ zB-Vck7mi6;PrMg1FUd9~{n@E`gAkPPCCY-wW7nK``3B~+OU)I7--4Lqzu&K%=KR5; zR@t5XF#LIZeEMr{i=S-}Z~xQ@TCL%Vs_G&2m->=XH%^FJL91(L4IVjU@v6SX!}e?yC76w~E|)0H8s3&r zNYd5JE8Z)}UY+jP;=QMc@!%X3c}I&WSw4l6a8&ndR4XA@{;O_5aM`rKH-BiPQeTH~ z!FQ``?DLusjeFY9rcGew`Ki@p94B_SxT^pgQaA*vx!LdK^vng3 ztP!#z=*|^jN`_|I?`rKEzR-jzxc+!xN%wY?D_@a|-si$;oGfb@|9nX~J&cNH8?%^u zu%q}lZYbW{$1iPvOv~vJW=TXH?bYMFc-BwtzKqwx9i%*cl$yM__~ClQsC+&h?>WpY zKGr?W(5Gpw)LsZ%JyVNKxr^}Sz8O^B-gibulwGu+%PNg{>w!l(LP}@iDXaUWxO}HQDl}GF6R&nF(y3aDf4CyM5d&e=A03)sBDree>QlQP1WwYlZvIXcB#!Y z`t9%dS*=@-MV#wnwL}WAP8|FxemO6clgw(iaP;3+`#<*oaS{QL`#G3{)c_L! zfrB{#csK|E0HR*a@y3nEJSY^;9)a2fPB+sEbrp5nzoX|`^Qa#+wXjtpyv zf6r6k6!Ttm6YLoFO1Gq8uw}>ogt1|4g)w%SNDf1~O?}owG1BG-e z0jO3(_A3VEiE%t1$btTQ*#h%Xo)qQcxe7Hl2B0CoB7G_bD*6W>pRVHaa|<$SnWWOG z8o636{^c&c&4?E}qt91=lrE)F`#-29AE$h+&zYrK^{GLkE8q$afqn0TPq?%=e9KQbX|(gsBXRbvB;F4Lp=DdgAf6b z8)ItfjFEyO5j*Q{CvzNwpy!Ez1wGPD`0i(3LSQmve z6h-!@kDm-vl7lk2ge$!w^X@)nRkv}=b@I5elZ>EM{tI1n%*@B9R#hEpZH`%CdmHN-9^-;H_Artw!Fcc;HOhxwt?)s||HqqyZYN(KMPJDq#mZzvJ! z6RK-A0_wMsUa3A0&FVv5G80c!ys=TW)_*95(`|27pf8$eNqO8vu5RAv`;yvJAqxuu WR1{UN&PgWK`+(=#3qkv-?7slilILmw literal 0 HcmV?d00001 diff --git a/mockserver/src/main/groovy/pl/touk/mockserver/server/ContextExecutor.groovy b/mockserver/src/main/groovy/pl/touk/mockserver/server/ContextExecutor.groovy index b26eb5a..ce11c4c 100644 --- a/mockserver/src/main/groovy/pl/touk/mockserver/server/ContextExecutor.groovy +++ b/mockserver/src/main/groovy/pl/touk/mockserver/server/ContextExecutor.groovy @@ -10,15 +10,15 @@ import java.util.concurrent.CopyOnWriteArrayList @Slf4j @PackageScope class ContextExecutor { - private final HttpServerWraper httpServerWraper + private final HttpServerWrapper httpServerWrapper final String path private final List mocks - ContextExecutor(HttpServerWraper httpServerWraper, Mock initialMock) { - this.httpServerWraper = httpServerWraper + ContextExecutor(HttpServerWrapper httpServerWrapper, Mock initialMock) { + this.httpServerWrapper = httpServerWrapper this.path = "/${initialMock.path}" this.mocks = new CopyOnWriteArrayList<>([initialMock]) - httpServerWraper.createContext(path) { + httpServerWrapper.createContext(path) { HttpExchange ex -> try { applyMocks(ex) diff --git a/mockserver/src/main/groovy/pl/touk/mockserver/server/HttpMockServer.groovy b/mockserver/src/main/groovy/pl/touk/mockserver/server/HttpMockServer.groovy index 7d2f86b..6a57f12 100644 --- a/mockserver/src/main/groovy/pl/touk/mockserver/server/HttpMockServer.groovy +++ b/mockserver/src/main/groovy/pl/touk/mockserver/server/HttpMockServer.groovy @@ -2,6 +2,7 @@ package pl.touk.mockserver.server import com.sun.net.httpserver.HttpExchange import groovy.util.logging.Slf4j +import pl.touk.mockserver.api.common.Https import pl.touk.mockserver.api.common.ImportAlias import pl.touk.mockserver.api.common.Method import pl.touk.mockserver.api.request.AddMock @@ -30,8 +31,8 @@ import static pl.touk.mockserver.server.Util.createResponse @Slf4j class HttpMockServer { - private final HttpServerWraper httpServerWraper - private final Map childServers = new ConcurrentHashMap<>() + private final HttpServerWrapper httpServerWrapper + private final Map childServers = new ConcurrentHashMap<>() private final Set mockNames = new CopyOnWriteArraySet<>() private final ConfigObject configuration = new ConfigObject() private final Executor executor @@ -41,13 +42,13 @@ class HttpMockServer { HttpMockServer(int port = 9999, ConfigObject initialConfiguration = new ConfigObject(), int threads = 10) { executor = Executors.newFixedThreadPool(threads) - httpServerWraper = new HttpServerWraper(port, executor) + httpServerWrapper = new HttpServerWrapper(port, executor) initialConfiguration.values()?.each { ConfigObject co -> addMock(co) } - httpServerWraper.createContext('/serverControl', { + httpServerWrapper.createContext('/serverControl', { HttpExchange ex -> try { if (ex.requestMethod == 'GET') { @@ -108,7 +109,7 @@ class HttpMockServer { throw new RuntimeException('mock already registered') } Mock mock = mockFromRequest(request) - HttpServerWraper child = getOrCreateChildServer(mock.port) + HttpServerWrapper child = getOrCreateChildServer(mock.port, mock.https) child.addMock(mock) saveConfiguration(request) mockNames << name @@ -121,7 +122,7 @@ class HttpMockServer { throw new RuntimeException('mock already registered') } Mock mock = mockFromConfig(co) - HttpServerWraper child = getOrCreateChildServer(mock.port) + HttpServerWrapper child = getOrCreateChildServer(mock.port, mock.https) child.addMock(mock) configuration.put(name, co) mockNames << name @@ -156,6 +157,7 @@ class HttpMockServer { mock.responseHeaders = request.responseHeaders mock.schema = request.schema mock.preserveHistory = request.preserveHistory != false + mock.https = request.https return mock } @@ -170,13 +172,23 @@ class HttpMockServer { mock.responseHeaders = co.responseHeaders ?: null mock.schema = co.schema ?: null mock.preserveHistory = co.preserveHistory != false + if (co.https) { + mock.https = new Https( + keystorePath: co.https.keystorePath ?: null, + keystorePassword: co.https.keystorePassword, + keyPassword: co.https.keyPassword, + truststorePath: co.https.truststorePath, + truststorePassword: co.https.truststorePassword, + requireClientAuth: co.https?.requireClientAuth?.asBoolean() ?: false + ) + } return mock } - private HttpServerWraper getOrCreateChildServer(int mockPort) { - HttpServerWraper child = childServers[mockPort] + private HttpServerWrapper getOrCreateChildServer(int mockPort, Https https) { + HttpServerWrapper child = childServers[mockPort] if (!child) { - child = new HttpServerWraper(mockPort, executor) + child = new HttpServerWrapper(mockPort, executor, https) childServers.put(mockPort, child) } return child @@ -244,6 +256,6 @@ class HttpMockServer { void stop() { childServers.values().each { it.stop() } - httpServerWraper.stop() + httpServerWrapper.stop() } } diff --git a/mockserver/src/main/groovy/pl/touk/mockserver/server/HttpServerWraper.groovy b/mockserver/src/main/groovy/pl/touk/mockserver/server/HttpServerWraper.groovy deleted file mode 100644 index e1ef2f8..0000000 --- a/mockserver/src/main/groovy/pl/touk/mockserver/server/HttpServerWraper.groovy +++ /dev/null @@ -1,58 +0,0 @@ -package pl.touk.mockserver.server - -import com.sun.net.httpserver.HttpHandler -import com.sun.net.httpserver.HttpServer -import groovy.transform.PackageScope -import groovy.util.logging.Slf4j - -import java.util.concurrent.Executor - -@Slf4j -@PackageScope -class HttpServerWraper { - private final HttpServer httpServer - final int port - - private List executors = [] - - HttpServerWraper(int port, Executor executor) { - this.port = port - InetSocketAddress addr = new InetSocketAddress(Inet4Address.getByName("0.0.0.0"), port) - httpServer = HttpServer.create(addr, 0) - httpServer.executor = executor - log.info("Http server starting on port $port...") - httpServer.start() - log.info('Http server is started') - } - - void createContext(String context, HttpHandler handler) { - httpServer.createContext(context, handler) - } - - void addMock(Mock mock) { - ContextExecutor executor = executors.find { it.path == mock.path } - if (executor) { - executor.addMock(mock) - } else { - executors << new ContextExecutor(this, mock) - } - log.info("Added mock ${mock.name}") - } - - void stop() { - executors.each { httpServer.removeContext(it.contextPath) } - httpServer.stop(0) - } - - List removeMock(String name) { - return executors.collect { it.removeMock(name) }.flatten() as List - } - - List peekMock(String name) { - return executors.collect { it.peekMock(name) }.flatten() as List - } - - List getMocks() { - return executors.collect { it.mocks }.flatten() as List - } -} diff --git a/mockserver/src/main/groovy/pl/touk/mockserver/server/HttpServerWrapper.groovy b/mockserver/src/main/groovy/pl/touk/mockserver/server/HttpServerWrapper.groovy new file mode 100644 index 0000000..c9e63e9 --- /dev/null +++ b/mockserver/src/main/groovy/pl/touk/mockserver/server/HttpServerWrapper.groovy @@ -0,0 +1,106 @@ +package pl.touk.mockserver.server + +import com.sun.net.httpserver.HttpHandler +import com.sun.net.httpserver.HttpServer +import com.sun.net.httpserver.HttpsServer +import groovy.transform.PackageScope +import groovy.util.logging.Slf4j +import pl.touk.mockserver.api.common.Https + +import javax.net.ssl.KeyManager +import javax.net.ssl.KeyManagerFactory +import javax.net.ssl.SSLContext +import javax.net.ssl.TrustManager +import javax.net.ssl.TrustManagerFactory +import java.security.KeyStore +import java.security.SecureRandom +import java.util.concurrent.Executor + +@Slf4j +@PackageScope +class HttpServerWrapper { + private final HttpServer httpServer + final int port + + private List executors = [] + + HttpServerWrapper(int port, Executor executor, Https https = null) { + this.port = port + InetSocketAddress addr = new InetSocketAddress(Inet4Address.getByName("0.0.0.0"), port) + httpServer = buildServer(addr, https) + httpServer.executor = executor + log.info("Http server starting on port $port...") + httpServer.start() + log.info('Http server is started') + } + + private HttpServer buildServer(InetSocketAddress addr, Https https) { + if (https) { + HttpsServer httpsServer = HttpsServer.create(addr, 0) + httpsServer.httpsConfigurator = new HttpsConfig(buildSslContext(https), https) + return httpsServer + } else { + return HttpServer.create(addr, 0) + } + } + + private SSLContext buildSslContext(Https https) { + KeyManager[] keyManagers = buildKeyManager(https) + TrustManager[] trustManagers = buildTrustManager(https) + + SSLContext ssl = SSLContext.getInstance('TLSv1') + ssl.init(keyManagers, trustManagers, new SecureRandom()) + return ssl + } + + private KeyManager[] buildKeyManager(Https https) { + KeyStore keyStore = KeyStore.getInstance('jks') + keyStore.load(new FileInputStream(https.keystorePath), https.keystorePassword.toCharArray()) + KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.defaultAlgorithm) + kmf.init(keyStore, https.keyPassword.toCharArray()) + return kmf.keyManagers + } + + private TrustManager[] buildTrustManager(Https https) { + if (https.requireClientAuth) { + KeyStore trustStore = KeyStore.getInstance('jks') + trustStore.load(new FileInputStream(https.truststorePath), https.truststorePassword.toCharArray()) + TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.defaultAlgorithm) + tmf.init(trustStore) + return tmf.trustManagers + } else { + return [] + } + } + + void createContext(String context, HttpHandler handler) { + httpServer.createContext(context, handler) + } + + void addMock(Mock mock) { + ContextExecutor executor = executors.find { it.path == mock.path } + if (executor) { + executor.addMock(mock) + } else { + executors << new ContextExecutor(this, mock) + } + log.info("Added mock ${mock.name}") + } + + void stop() { + executors.each { httpServer.removeContext(it.contextPath) } + httpServer.stop(0) + } + + List removeMock(String name) { + return executors.collect { it.removeMock(name) }.flatten() as List + } + + List peekMock(String name) { + return executors.collect { it.peekMock(name) }.flatten() as List + } + + List getMocks() { + return executors.collect { it.mocks }.flatten() as List + } +} diff --git a/mockserver/src/main/groovy/pl/touk/mockserver/server/HttpsConfig.groovy b/mockserver/src/main/groovy/pl/touk/mockserver/server/HttpsConfig.groovy new file mode 100644 index 0000000..68b5550 --- /dev/null +++ b/mockserver/src/main/groovy/pl/touk/mockserver/server/HttpsConfig.groovy @@ -0,0 +1,28 @@ +package pl.touk.mockserver.server + +import com.sun.net.httpserver.HttpsConfigurator +import com.sun.net.httpserver.HttpsParameters +import groovy.transform.CompileStatic +import pl.touk.mockserver.api.common.Https + +import javax.net.ssl.SSLContext +import javax.net.ssl.SSLParameters + +@CompileStatic +class HttpsConfig extends HttpsConfigurator { + private final Https https + + HttpsConfig(SSLContext sslContext, Https https) { + super(sslContext) + this.https = https + } + + @Override + void configure(HttpsParameters httpsParameters) { + SSLContext sslContext = getSSLContext() + SSLParameters sslParameters = sslContext.defaultSSLParameters + sslParameters.needClientAuth = https.requireClientAuth + httpsParameters.needClientAuth = https.requireClientAuth + httpsParameters.SSLParameters = sslParameters + } +} diff --git a/mockserver/src/main/groovy/pl/touk/mockserver/server/Mock.groovy b/mockserver/src/main/groovy/pl/touk/mockserver/server/Mock.groovy index ed37f89..e132ac9 100644 --- a/mockserver/src/main/groovy/pl/touk/mockserver/server/Mock.groovy +++ b/mockserver/src/main/groovy/pl/touk/mockserver/server/Mock.groovy @@ -5,6 +5,7 @@ import groovy.transform.PackageScope import groovy.util.logging.Slf4j import org.codehaus.groovy.control.CompilerConfiguration import org.codehaus.groovy.control.customizers.ImportCustomizer +import pl.touk.mockserver.api.common.Https import pl.touk.mockserver.api.common.Method import javax.xml.XMLConstants @@ -35,6 +36,7 @@ class Mock implements Comparable { private Validator validator Map imports = [:] boolean preserveHistory = true + Https https Mock(String name, String path, int port) { if (!(name)) { diff --git a/pom.xml b/pom.xml index 43ce7cd..c99b3f7 100644 --- a/pom.xml +++ b/pom.xml @@ -36,6 +36,7 @@ 1.11.2 2.5.2 1.4 + 0.5.1 @@ -99,6 +100,12 @@ ${jmh.version} test + + info.solidsoft.spock + spock-global-unroll + ${spock-global-unroll.version} + test +