2222import com .fasterxml .jackson .annotation .JsonCreator ;
2323import com .github .tomakehurst .wiremock .common .NetworkAddressRules ;
2424import com .github .tomakehurst .wiremock .common .Notifier ;
25+ import com .github .tomakehurst .wiremock .common .ProhibitedNetworkAddressException ;
2526import com .github .tomakehurst .wiremock .core .Admin ;
2627import com .github .tomakehurst .wiremock .extension .Parameters ;
2728import com .github .tomakehurst .wiremock .extension .PostServeAction ;
2829import com .github .tomakehurst .wiremock .extension .responsetemplating .RequestTemplateModel ;
2930import com .github .tomakehurst .wiremock .extension .responsetemplating .TemplateEngine ;
3031import com .github .tomakehurst .wiremock .http .HttpHeader ;
32+ import com .github .tomakehurst .wiremock .http .NetworkAddressRulesAdheringDnsResolver ;
3133import com .github .tomakehurst .wiremock .stubbing .ServeEvent ;
32- import java .net .InetAddress ;
33- import java .net .URI ;
34- import java .net .UnknownHostException ;
3534import java .util .*;
3635import java .util .concurrent .Executors ;
3736import java .util .concurrent .ScheduledExecutorService ;
@@ -54,24 +53,20 @@ public class Webhooks extends PostServeAction {
5453 private final CloseableHttpClient httpClient ;
5554 private final List <WebhookTransformer > transformers ;
5655 private final TemplateEngine templateEngine ;
57- private final NetworkAddressRules targetAddressRules ;
5856
5957 private Webhooks (
6058 ScheduledExecutorService scheduler ,
6159 CloseableHttpClient httpClient ,
62- List <WebhookTransformer > transformers ,
63- NetworkAddressRules targetAddressRules ) {
60+ List <WebhookTransformer > transformers ) {
6461 this .scheduler = scheduler ;
6562 this .httpClient = httpClient ;
6663 this .transformers = transformers ;
6764
6865 this .templateEngine = TemplateEngine .defaultTemplateEngine ();
69- this .targetAddressRules = targetAddressRules ;
7066 }
7167
7268 private Webhooks (List <WebhookTransformer > transformers , NetworkAddressRules targetAddressRules ) {
73- this (
74- Executors .newScheduledThreadPool (10 ), createHttpClient (), transformers , targetAddressRules );
69+ this (Executors .newScheduledThreadPool (10 ), createHttpClient (targetAddressRules ), transformers );
7570 }
7671
7772 public Webhooks (NetworkAddressRules targetAddressRules ) {
@@ -87,7 +82,7 @@ public Webhooks(WebhookTransformer... transformers) {
8782 this (Arrays .asList (transformers ), NetworkAddressRules .ALLOW_ALL );
8883 }
8984
90- private static CloseableHttpClient createHttpClient () {
85+ private static CloseableHttpClient createHttpClient (NetworkAddressRules targetAddressRules ) {
9186 return HttpClientBuilder .create ()
9287 .disableAuthCaching ()
9388 .disableAutomaticRetries ()
@@ -96,6 +91,7 @@ private static CloseableHttpClient createHttpClient() {
9691 .disableContentCompression ()
9792 .setConnectionManager (
9893 PoolingHttpClientConnectionManagerBuilder .create ()
94+ .setDnsResolver (new NetworkAddressRulesAdheringDnsResolver (targetAddressRules ))
9995 .setDefaultSocketConfig (
10096 SocketConfig .custom ().setSoTimeout (Timeout .ofMilliseconds (30000 )).build ())
10197 .setMaxConnPerRoute (1000 )
@@ -125,10 +121,6 @@ public void doAction(
125121 definition = transformer .transform (serveEvent , definition );
126122 }
127123 definition = applyTemplating (definition , serveEvent );
128- if (targetAddressProhibited (definition .getUrl ())) {
129- notifier ().error ("The target webhook address is denied in WireMock's configuration." );
130- return ;
131- }
132124 request = buildRequest (definition );
133125 } catch (Exception e ) {
134126 notifier ().error ("Exception thrown while configuring webhook" , e );
@@ -146,13 +138,14 @@ public void doAction(
146138 finalDefinition .getUrl (),
147139 response .getCode (),
148140 EntityUtils .toString (response .getEntity ())));
141+ } catch (ProhibitedNetworkAddressException e ) {
142+ notifier .error ("The target webhook address is denied in WireMock's configuration." );
149143 } catch (Exception e ) {
150- notifier ()
151- .error (
152- String .format (
153- "Failed to fire webhook %s %s" ,
154- finalDefinition .getMethod (), finalDefinition .getUrl ()),
155- e );
144+ notifier .error (
145+ String .format (
146+ "Failed to fire webhook %s %s" ,
147+ finalDefinition .getMethod (), finalDefinition .getUrl ()),
148+ e );
156149 }
157150 },
158151 finalDefinition .getDelaySampleMillis (),
@@ -215,19 +208,6 @@ private static ClassicHttpRequest buildRequest(WebhookDefinition definition) {
215208 return requestBuilder .build ();
216209 }
217210
218- // TODO this is duplicated in com.github.tomakehurst.wiremock.http.ProxyResponseRenderer - should
219- // it be on NetworkAddressRules ?
220- private boolean targetAddressProhibited (String url ) {
221- String host = URI .create (url ).getHost ();
222- try {
223- final InetAddress [] resolvedAddresses = InetAddress .getAllByName (host );
224- return !Arrays .stream (resolvedAddresses )
225- .allMatch (address -> targetAddressRules .isAllowed (address .getHostAddress ()));
226- } catch (UnknownHostException e ) {
227- return true ;
228- }
229- }
230-
231211 public static WebhookDefinition webhook () {
232212 return new WebhookDefinition ();
233213 }
0 commit comments