Skip to content

Commit

Permalink
Fix network address rules breaking change (#2478)
Browse files Browse the repository at this point in the history
* Moved constants and static methods from DefaultNetworkAddressRules back to NetworkAddressRules to revert breaking change
  • Loading branch information
tomakehurst committed Nov 3, 2023
1 parent 6b5323c commit 029c803
Show file tree
Hide file tree
Showing 17 changed files with 77 additions and 81 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ allprojects {
mavenCentral()
}

version = '3.3.0'
version = '3.3.1'


sourceCompatibility = 11
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,17 @@

import static com.github.tomakehurst.wiremock.common.NetworkAddressRange.ALL;
import static com.github.tomakehurst.wiremock.common.NetworkAddressUtils.isValidInet4Address;
import static java.util.Collections.emptySet;
import static java.util.stream.Collectors.toSet;

import java.util.HashSet;
import java.util.Set;

public class DefaultNetworkAddressRules implements NetworkAddressRules {

public static Builder builder() {
return new Builder();
}

private final Set<NetworkAddressRange> allowed;
private final Set<NetworkAddressRange> allowedHostPatterns;
private final Set<NetworkAddressRange> denied;
private final Set<NetworkAddressRange> deniedHostPatterns;

public static final NetworkAddressRules ALLOW_ALL =
new DefaultNetworkAddressRules(Set.of(ALL), emptySet());

public DefaultNetworkAddressRules(
Set<NetworkAddressRange> allowed, Set<NetworkAddressRange> denied) {
this.allowed =
Expand Down Expand Up @@ -89,27 +80,4 @@ public boolean isAllowed(String testValue) {
&& deniedHostPatterns.stream().noneMatch(rule -> rule.isIncluded(testValue));
}
}

public static class Builder {
private final Set<NetworkAddressRange> allowed = new HashSet<>();
private final Set<NetworkAddressRange> denied = new HashSet<>();

public Builder allow(String expression) {
allowed.add(NetworkAddressRange.of(expression));
return this;
}

public Builder deny(String expression) {
denied.add(NetworkAddressRange.of(expression));
return this;
}

public NetworkAddressRules build() {
Set<NetworkAddressRange> allowedRanges = allowed;
if (allowedRanges.isEmpty()) {
allowedRanges = Set.of(ALL);
}
return new DefaultNetworkAddressRules(Set.copyOf(allowedRanges), Set.copyOf(denied));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,41 @@
*/
package com.github.tomakehurst.wiremock.common;

import static com.github.tomakehurst.wiremock.common.NetworkAddressRange.ALL;
import static java.util.Collections.emptySet;

import java.util.HashSet;
import java.util.Set;

public interface NetworkAddressRules {
NetworkAddressRules ALLOW_ALL = new DefaultNetworkAddressRules(Set.of(ALL), emptySet());

static Builder builder() {
return new Builder();
}

boolean isAllowed(String testValue);

public static class Builder {
private final Set<NetworkAddressRange> allowed = new HashSet<>();
private final Set<NetworkAddressRange> denied = new HashSet<>();

public Builder allow(String expression) {
allowed.add(NetworkAddressRange.of(expression));
return this;
}

public Builder deny(String expression) {
denied.add(NetworkAddressRange.of(expression));
return this;
}

public NetworkAddressRules build() {
Set<NetworkAddressRange> allowedRanges = allowed;
if (allowedRanges.isEmpty()) {
allowedRanges = Set.of(ALL);
}
return new DefaultNetworkAddressRules(Set.copyOf(allowedRanges), Set.copyOf(denied));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public class WireMockConfiguration implements Options {

private Limit responseBodySizeLimit = UNLIMITED;

private NetworkAddressRules proxyTargetRules = DefaultNetworkAddressRules.ALLOW_ALL;
private NetworkAddressRules proxyTargetRules = NetworkAddressRules.ALLOW_ALL;

private int proxyTimeout = DEFAULT_TIMEOUT;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.apache.commons.lang3.StringUtils.isEmpty;

import com.github.tomakehurst.wiremock.common.DefaultNetworkAddressRules;
import com.github.tomakehurst.wiremock.common.NetworkAddressRules;
import com.github.tomakehurst.wiremock.common.ProxySettings;
import com.github.tomakehurst.wiremock.common.ssl.KeyStoreSettings;
Expand Down Expand Up @@ -241,7 +240,7 @@ public static CloseableHttpClient createClient(int maxConnections, int timeoutMi
NO_PROXY,
NO_STORE,
true,
DefaultNetworkAddressRules.ALLOW_ALL);
NetworkAddressRules.ALLOW_ALL);
}

public static CloseableHttpClient createClient(int timeoutMilliseconds) {
Expand All @@ -255,7 +254,7 @@ public static CloseableHttpClient createClient(ProxySettings proxySettings) {
proxySettings,
NO_STORE,
true,
DefaultNetworkAddressRules.ALLOW_ALL);
NetworkAddressRules.ALLOW_ALL);
}

public static CloseableHttpClient createClient() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ public DataTruncationSettings getDataTruncationSettings() {

@Override
public NetworkAddressRules getProxyTargetRules() {
return DefaultNetworkAddressRules.ALLOW_ALL;
return NetworkAddressRules.ALLOW_ALL;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -904,7 +904,7 @@ public DataTruncationSettings getDataTruncationSettings() {

@Override
public NetworkAddressRules getProxyTargetRules() {
DefaultNetworkAddressRules.Builder builder = DefaultNetworkAddressRules.builder();
DefaultNetworkAddressRules.Builder builder = NetworkAddressRules.builder();
if (optionSet.has(ALLOW_PROXY_TARGETS)) {
Arrays.stream(((String) optionSet.valueOf(ALLOW_PROXY_TARGETS)).split(","))
.forEach(builder::allow);
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/swagger/wiremock-admin-api.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"openapi": "3.0.0",
"info": {
"title": "WireMock",
"version": "3.3.0"
"version": "3.3.1"
},
"externalDocs": {
"description": "WireMock user documentation",
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/swagger/wiremock-admin-api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ openapi: 3.0.0

info:
title: WireMock
version: 3.3.0
version: 3.3.1

externalDocs:
description: WireMock user documentation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import static org.junit.jupiter.api.Assertions.assertFalse;

import com.github.tomakehurst.wiremock.client.WireMock;
import com.github.tomakehurst.wiremock.common.DefaultNetworkAddressRules;
import com.github.tomakehurst.wiremock.common.NetworkAddressRules;
import com.github.tomakehurst.wiremock.common.ProxySettings;
import com.github.tomakehurst.wiremock.core.Options;
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
Expand Down Expand Up @@ -637,7 +637,7 @@ void preventsProxyingToExcludedIpAddress() {
init(
wireMockConfig()
.limitProxyTargets(
DefaultNetworkAddressRules.builder()
NetworkAddressRules.builder()
.deny("10.1.2.3")
.deny("192.168.10.1-192.168.11.254")
.build()));
Expand All @@ -656,8 +656,7 @@ void preventsProxyingToExcludedIpAddress() {
void preventsProxyingToExcludedHostnames() {
init(
wireMockConfig()
.limitProxyTargets(
DefaultNetworkAddressRules.builder().deny("*.wiremock.org").build()));
.limitProxyTargets(NetworkAddressRules.builder().deny("*.wiremock.org").build()));

proxy.register(proxyAllTo("http://noway.wiremock.org"));
assertThat(
Expand All @@ -669,7 +668,7 @@ void preventsProxyingToExcludedHostnames() {
void preventsProxyingToNonIncludedHostnames() {
init(
wireMockConfig()
.limitProxyTargets(DefaultNetworkAddressRules.builder().allow("wiremock.org").build()));
.limitProxyTargets(NetworkAddressRules.builder().allow("wiremock.org").build()));

proxy.register(proxyAllTo("http://wiremock.io"));
assertThat(
Expand All @@ -681,7 +680,7 @@ void preventsProxyingToNonIncludedHostnames() {
void preventsProxyingToIpResolvedFromHostname() {
init(
wireMockConfig()
.limitProxyTargets(DefaultNetworkAddressRules.builder().deny("127.0.0.1").build()));
.limitProxyTargets(NetworkAddressRules.builder().deny("127.0.0.1").build()));

proxy.register(proxyAllTo("http://localhost"));
assertThat(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

import com.github.tomakehurst.wiremock.client.WireMock;
import com.github.tomakehurst.wiremock.common.ConsoleNotifier;
import com.github.tomakehurst.wiremock.common.DefaultNetworkAddressRules;
import com.github.tomakehurst.wiremock.common.NetworkAddressRules;
import com.github.tomakehurst.wiremock.core.Admin;
import com.github.tomakehurst.wiremock.extension.PostServeAction;
import com.github.tomakehurst.wiremock.http.RequestMethod;
Expand Down Expand Up @@ -92,9 +92,7 @@ public String getName() {
.dynamicPort()
.notifier(notifier)
.limitProxyTargets(
DefaultNetworkAddressRules.builder()
.deny("169.254.0.0-169.254.255.255")
.build()))
NetworkAddressRules.builder().deny("169.254.0.0-169.254.255.255").build()))
.configureStaticDsl(true)
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

import com.github.tomakehurst.wiremock.client.WireMock;
import com.github.tomakehurst.wiremock.common.ConsoleNotifier;
import com.github.tomakehurst.wiremock.common.DefaultNetworkAddressRules;
import com.github.tomakehurst.wiremock.common.NetworkAddressRules;
import com.github.tomakehurst.wiremock.core.Admin;
import com.github.tomakehurst.wiremock.extension.PostServeAction;
import com.github.tomakehurst.wiremock.http.RequestMethod;
Expand Down Expand Up @@ -92,9 +92,7 @@ public String getName() {
.dynamicPort()
.notifier(notifier)
.limitProxyTargets(
DefaultNetworkAddressRules.builder()
.deny("169.254.0.0-169.254.255.255")
.build()))
NetworkAddressRules.builder().deny("169.254.0.0-169.254.255.255").build()))
.configureStaticDsl(true)
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class NetworkAddressRulesTest {
@Test
void allowsAddressIncludedAndNotExcluded() {
NetworkAddressRules rules =
DefaultNetworkAddressRules.builder()
NetworkAddressRules.builder()
.allow("10.1.1.1-10.2.1.1")
.allow("192.168.1.1-192.168.2.1")
.deny("10.1.2.3")
Expand All @@ -41,7 +41,7 @@ void allowsAddressIncludedAndNotExcluded() {

@Test
void onlyAllowSingleIp() {
NetworkAddressRules rules = DefaultNetworkAddressRules.builder().allow("10.1.1.1").build();
NetworkAddressRules rules = NetworkAddressRules.builder().allow("10.1.1.1").build();

assertThat(rules.isAllowed("10.1.1.1"), is(true));
assertThat(rules.isAllowed("10.1.1.0"), is(false));
Expand All @@ -50,7 +50,7 @@ void onlyAllowSingleIp() {

@Test
void onlyDenySingleIp() {
NetworkAddressRules rules = DefaultNetworkAddressRules.builder().deny("10.1.1.1").build();
NetworkAddressRules rules = NetworkAddressRules.builder().deny("10.1.1.1").build();

assertThat(rules.isAllowed("10.1.1.1"), is(false));
assertThat(rules.isAllowed("10.1.1.0"), is(true));
Expand All @@ -60,7 +60,7 @@ void onlyDenySingleIp() {
@Test
void allowAndDenySingleIps() {
NetworkAddressRules rules =
DefaultNetworkAddressRules.builder().deny("10.1.1.1").allow("10.1.1.3").build();
NetworkAddressRules.builder().deny("10.1.1.1").allow("10.1.1.3").build();

assertThat(rules.isAllowed("10.1.1.0"), is(false));
assertThat(rules.isAllowed("10.1.1.1"), is(false));
Expand All @@ -72,7 +72,7 @@ void allowAndDenySingleIps() {
@Test
void allowRangeAndDenySingleIp() {
NetworkAddressRules rules =
DefaultNetworkAddressRules.builder().allow("10.1.1.1-10.1.1.3").deny("10.1.1.2").build();
NetworkAddressRules.builder().allow("10.1.1.1-10.1.1.3").deny("10.1.1.2").build();

assertThat(rules.isAllowed("10.1.1.0"), is(false));
assertThat(rules.isAllowed("10.1.1.1"), is(true));
Expand All @@ -84,7 +84,7 @@ void allowRangeAndDenySingleIp() {
@Test
void denyRangeAndAllowSingleIp() {
NetworkAddressRules rules =
DefaultNetworkAddressRules.builder().deny("10.1.1.1-10.1.1.3").allow("10.1.1.2").build();
NetworkAddressRules.builder().deny("10.1.1.1-10.1.1.3").allow("10.1.1.2").build();

assertThat(rules.isAllowed("10.1.1.0"), is(false));
assertThat(rules.isAllowed("10.1.1.1"), is(false));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import static java.util.Collections.emptyList;

import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.common.DefaultNetworkAddressRules;
import com.github.tomakehurst.wiremock.common.NetworkAddressRules;
import com.github.tomakehurst.wiremock.common.ssl.KeyStoreSettings;
import com.github.tomakehurst.wiremock.crypto.CertificateSpecification;
import com.github.tomakehurst.wiremock.crypto.InMemoryKeyStore;
Expand Down Expand Up @@ -94,7 +94,7 @@ public void startServerAndBuildClient(
/* trustSelfSignedCertificates= */ false,
trustedHosts,
false,
DefaultNetworkAddressRules.ALLOW_ALL);
NetworkAddressRules.ALLOW_ALL);
}

@AfterEach
Expand Down

0 comments on commit 029c803

Please sign in to comment.