Skip to content

Commit 50f8960

Browse files
committed
[ISSUE #8321] Fail fast when shenyu.httpclient.strategy holds an unsupported value
1 parent d7e8282 commit 50f8960

4 files changed

Lines changed: 235 additions & 4 deletions

File tree

‎RELEASE-NOTES.md‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,15 @@
217217
aggregate and remains an explicit choice. See `shenyu-registry/README.md` for the
218218
provider table and migration steps. (#7578)
219219

220+
- The gateway now refuses to start when `shenyu.httpclient.strategy` holds a value that no http
221+
client plugin is selected for. The property accepts `netty` and `webClient`, matched
222+
case-insensitively the same way the plugin selection conditions match, and keeps defaulting to
223+
`netty` when it is absent. A value that is present but empty is rejected as well, because the
224+
plugin selection conditions cannot match it either. Before this change a mistyped value such as
225+
`web-client` matched neither condition, so the gateway started successfully without any http
226+
client plugin and every proxied request failed without a startup hint. Deployments that
227+
currently run with an unsupported value have to correct it before upgrading. (#8321)
228+
220229
## [v2.7.0]- 2024-12-23
221230

222231
### ✨ New Features

‎shenyu-plugin/shenyu-plugin-httpclient/src/main/java/org/apache/shenyu/plugin/httpclient/config/HttpClientProperties.java‎

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
import io.netty.handler.ssl.SslProvider;
2121
import org.apache.commons.lang3.StringUtils;
2222
import org.apache.shenyu.common.exception.ShenyuException;
23+
import org.springframework.beans.factory.InitializingBean;
24+
import org.springframework.context.EnvironmentAware;
25+
import org.springframework.core.env.Environment;
2326
import org.springframework.util.ResourceUtils;
2427
import reactor.netty.ReactorNetty;
2528
import reactor.netty.resources.ConnectionProvider;
@@ -39,18 +42,43 @@
3942
import java.util.ArrayList;
4043
import java.util.Collection;
4144
import java.util.List;
45+
import java.util.Objects;
4246
import java.util.Optional;
4347

4448
/**
4549
* Configuration properties for the Netty {@link reactor.netty.http.client.HttpClient}.
4650
*/
47-
public class HttpClientProperties {
51+
public class HttpClientProperties implements InitializingBean, EnvironmentAware {
4852

4953
/**
50-
* the http client strategy.
54+
* The netty http client strategy.
55+
*/
56+
public static final String NETTY_STRATEGY = "netty";
57+
58+
/**
59+
* The web client http client strategy.
60+
*/
61+
public static final String WEB_CLIENT_STRATEGY = "webClient";
62+
63+
/**
64+
* The http client strategy property name.
65+
*/
66+
public static final String STRATEGY_PROPERTY = "shenyu.httpclient.strategy";
67+
68+
/**
69+
* The http client strategy.
70+
* Legal values are netty and webClient, matched case-insensitively the same way the plugin
71+
* selection conditions match. When the property is absent the netty strategy is used. Any other
72+
* value is rejected at startup so that a mistyped value cannot silently remove the gateway's
73+
* http forwarding capability.
5174
*/
5275
private String strategy;
5376

77+
/**
78+
* The environment, used to tell an absent strategy apart from a configured but empty one.
79+
*/
80+
private Environment environment;
81+
5482
/**
5583
* The connection timeout in millis, the default is 45s.
5684
*/
@@ -142,6 +170,49 @@ public void setStrategy(final String strategy) {
142170
this.strategy = strategy;
143171
}
144172

173+
/**
174+
* Validate the bound http client strategy.
175+
* The accepted values are the ones the plugin selection conditions match, so a value that would
176+
* leave the gateway without an http client plugin is rejected instead of being ignored.
177+
*
178+
* @throws IllegalStateException if the configured strategy is not a supported value
179+
*/
180+
public void validateStrategy() {
181+
String value = this.strategy;
182+
if (Objects.isNull(value)) {
183+
if (Objects.isNull(this.environment) || !this.environment.containsProperty(STRATEGY_PROPERTY)) {
184+
return;
185+
}
186+
throw new IllegalStateException(String.format(
187+
"Invalid empty value for property '%s', supported values are '%s' and '%s'",
188+
STRATEGY_PROPERTY, NETTY_STRATEGY, WEB_CLIENT_STRATEGY));
189+
}
190+
if (NETTY_STRATEGY.equalsIgnoreCase(value) || WEB_CLIENT_STRATEGY.equalsIgnoreCase(value)) {
191+
return;
192+
}
193+
throw new IllegalStateException(String.format(
194+
"Invalid value '%s' for property '%s', supported values are '%s' and '%s'",
195+
value, STRATEGY_PROPERTY, NETTY_STRATEGY, WEB_CLIENT_STRATEGY));
196+
}
197+
198+
/**
199+
* Validate the strategy once the properties are bound.
200+
*/
201+
@Override
202+
public void afterPropertiesSet() {
203+
validateStrategy();
204+
}
205+
206+
/**
207+
* Set the environment used to detect a configured but empty strategy.
208+
*
209+
* @param environment the environment
210+
*/
211+
@Override
212+
public void setEnvironment(final Environment environment) {
213+
this.environment = environment;
214+
}
215+
145216
/**
146217
* Gets connect timeout.
147218
*

‎shenyu-plugin/shenyu-plugin-httpclient/src/test/java/org/apache/shenyu/plugin/httpclient/HttpClientPropertiesTest.java‎

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import org.junit.jupiter.api.Assertions;
2424
import org.junit.jupiter.api.Test;
2525
import org.mockito.MockedStatic;
26+
import org.springframework.core.env.MapPropertySource;
27+
import org.springframework.core.env.StandardEnvironment;
2628
import org.springframework.util.ResourceUtils;
2729
import reactor.netty.resources.ConnectionProvider;
2830

@@ -32,7 +34,9 @@
3234
import java.security.cert.CertificateFactory;
3335
import java.time.Duration;
3436
import java.util.ArrayList;
37+
import java.util.Arrays;
3538
import java.util.Collections;
39+
import java.util.List;
3640

3741
import static org.mockito.ArgumentMatchers.anyString;
3842
import static org.mockito.Mockito.doThrow;
@@ -171,7 +175,7 @@ public void httpClientPropertiesTest() {
171175
httpClientProperties.setReaderIdleTime(1);
172176
httpClientProperties.setReadTimeout(1);
173177
httpClientProperties.setResponseTimeout(1L);
174-
httpClientProperties.setStrategy("strategy");
178+
httpClientProperties.setStrategy("netty");
175179
httpClientProperties.setWiretap(true);
176180
httpClientProperties.setWriterIdleTime(1);
177181
httpClientProperties.setWriteTimeout(1);
@@ -182,9 +186,66 @@ public void httpClientPropertiesTest() {
182186
Assertions.assertEquals(httpClientProperties.getReaderIdleTime(), 1);
183187
Assertions.assertEquals(httpClientProperties.getReadTimeout(), 1);
184188
Assertions.assertEquals(httpClientProperties.getResponseTimeout(), Duration.ofMillis(1L));
185-
Assertions.assertEquals(httpClientProperties.getStrategy(), "strategy");
189+
Assertions.assertEquals(httpClientProperties.getStrategy(), "netty");
186190
Assertions.assertTrue(httpClientProperties.isWiretap());
187191
Assertions.assertEquals(httpClientProperties.getWriterIdleTime(), 1);
188192
Assertions.assertEquals(httpClientProperties.getWriteTimeout(), 1);
189193
}
194+
195+
@Test
196+
public void validateStrategyRejectsUnsupportedValueTest() {
197+
List<String> unsupported = Arrays.asList("web-client", "web_client", "", " netty", "netty ", "Netty ", "httpclient");
198+
for (String value : unsupported) {
199+
HttpClientProperties httpClientProperties = new HttpClientProperties();
200+
httpClientProperties.setStrategy(value);
201+
IllegalStateException exception = Assertions.assertThrows(IllegalStateException.class, httpClientProperties::validateStrategy);
202+
Assertions.assertTrue(exception.getMessage().contains("shenyu.httpclient.strategy"), exception.getMessage());
203+
Assertions.assertTrue(exception.getMessage().contains("netty"), exception.getMessage());
204+
Assertions.assertTrue(exception.getMessage().contains("webClient"), exception.getMessage());
205+
}
206+
}
207+
208+
@Test
209+
public void validateStrategyAcceptsSupportedValueTest() {
210+
List<String> supported = Arrays.asList("netty", "webClient", "NETTY", "WEBCLIENT", "webclient", "WebClient");
211+
for (String value : supported) {
212+
HttpClientProperties httpClientProperties = new HttpClientProperties();
213+
httpClientProperties.setStrategy(value);
214+
Assertions.assertDoesNotThrow(httpClientProperties::validateStrategy);
215+
}
216+
}
217+
218+
@Test
219+
public void validateStrategyAcceptsAbsentValueTest() {
220+
HttpClientProperties httpClientProperties = new HttpClientProperties();
221+
Assertions.assertNull(httpClientProperties.getStrategy());
222+
Assertions.assertDoesNotThrow(httpClientProperties::validateStrategy);
223+
}
224+
225+
@Test
226+
public void afterPropertiesSetRejectsUnsupportedValueTest() {
227+
HttpClientProperties httpClientProperties = new HttpClientProperties();
228+
httpClientProperties.setStrategy("web-client");
229+
Assertions.assertThrows(IllegalStateException.class, httpClientProperties::afterPropertiesSet);
230+
}
231+
232+
@Test
233+
public void validateStrategyRejectsNullValuedConfiguredPropertyTest() {
234+
HttpClientProperties httpClientProperties = new HttpClientProperties();
235+
StandardEnvironment environment = new StandardEnvironment();
236+
environment.getPropertySources().addFirst(new MapPropertySource("nullValuedStrategy",
237+
Collections.singletonMap(HttpClientProperties.STRATEGY_PROPERTY, null)));
238+
httpClientProperties.setEnvironment(environment);
239+
IllegalStateException exception = Assertions.assertThrows(IllegalStateException.class, httpClientProperties::validateStrategy);
240+
Assertions.assertTrue(exception.getMessage().contains("shenyu.httpclient.strategy"), exception.getMessage());
241+
Assertions.assertTrue(exception.getMessage().contains("netty"), exception.getMessage());
242+
Assertions.assertTrue(exception.getMessage().contains("webClient"), exception.getMessage());
243+
}
244+
245+
@Test
246+
public void validateStrategyAcceptsAbsentPropertyWithEnvironmentTest() {
247+
HttpClientProperties httpClientProperties = new HttpClientProperties();
248+
httpClientProperties.setEnvironment(new StandardEnvironment());
249+
Assertions.assertDoesNotThrow(httpClientProperties::validateStrategy);
250+
}
190251
}

‎shenyu-spring-boot-starter/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-httpclient/src/test/java/org/apache/shenyu/springboot/starter/plugin/httpclient/HttpClientPluginConfigurationTest.java‎

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,17 @@
1919

2020
import static org.hamcrest.MatcherAssert.assertThat;
2121
import static org.hamcrest.Matchers.is;
22+
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
2223
import static org.junit.jupiter.api.Assertions.assertNotNull;
2324
import static org.junit.jupiter.api.Assertions.assertThrows;
25+
import static org.junit.jupiter.api.Assertions.assertTrue;
2426
import static org.mockito.ArgumentMatchers.anyInt;
2527
import static org.mockito.Mockito.mock;
2628
import static org.mockito.Mockito.never;
2729
import static org.mockito.Mockito.verify;
2830

2931
import java.time.Duration;
32+
import java.util.Collections;
3033

3134
import org.apache.shenyu.plugin.api.ShenyuPlugin;
3235
import org.apache.shenyu.plugin.httpclient.config.HttpClientProperties;
@@ -38,6 +41,8 @@
3841
import org.springframework.boot.context.properties.EnableConfigurationProperties;
3942
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
4043
import org.springframework.context.annotation.Configuration;
44+
import org.springframework.core.NestedExceptionUtils;
45+
import org.springframework.core.env.MapPropertySource;
4146

4247
import reactor.netty.http.client.HttpClient;
4348
import reactor.netty.resources.ConnectionProvider;
@@ -189,4 +194,89 @@ public void testNettyHttpClientPlugin() {
189194
assertNotNull(plugin);
190195
});
191196
}
197+
198+
@Test
199+
public void testDefaultStrategyRegistersNettyPlugin() {
200+
applicationContextRunner
201+
.withPropertyValues("debug=true")
202+
.run(context -> {
203+
ShenyuPlugin plugin = context.getBean("nettyHttpClientPlugin", ShenyuPlugin.class);
204+
assertNotNull(plugin);
205+
});
206+
}
207+
208+
@Test
209+
public void testNettyStrategyIsMatchedCaseInsensitively() {
210+
applicationContextRunner
211+
.withPropertyValues(
212+
"debug=true",
213+
"shenyu.httpclient.strategy=NETTY"
214+
)
215+
.run(context -> {
216+
ShenyuPlugin plugin = context.getBean("nettyHttpClientPlugin", ShenyuPlugin.class);
217+
assertNotNull(plugin);
218+
});
219+
}
220+
221+
@Test
222+
public void testWebClientStrategyIsMatchedCaseInsensitively() {
223+
applicationContextRunner
224+
.withPropertyValues(
225+
"debug=true",
226+
"shenyu.httpclient.strategy=webclient"
227+
)
228+
.run(context -> {
229+
ShenyuPlugin plugin = context.getBean("webClientPlugin", ShenyuPlugin.class);
230+
assertNotNull(plugin);
231+
});
232+
}
233+
234+
@Test
235+
public void testInvalidStrategyFailsStartup() {
236+
applicationContextRunner
237+
.withPropertyValues(
238+
"debug=true",
239+
"shenyu.httpclient.strategy=web-client"
240+
)
241+
.run(context -> {
242+
Throwable failure = context.getStartupFailure();
243+
assertNotNull(failure, "the application context must fail to start for an unsupported strategy");
244+
Throwable rootCause = NestedExceptionUtils.getMostSpecificCause(failure);
245+
assertInstanceOf(IllegalStateException.class, rootCause);
246+
assertTrue(rootCause.getMessage().contains("shenyu.httpclient.strategy"), rootCause.getMessage());
247+
assertTrue(rootCause.getMessage().contains("netty"), rootCause.getMessage());
248+
assertTrue(rootCause.getMessage().contains("webClient"), rootCause.getMessage());
249+
});
250+
}
251+
252+
@Test
253+
public void testBlankStrategyFailsStartup() {
254+
applicationContextRunner
255+
.withPropertyValues(
256+
"debug=true",
257+
"shenyu.httpclient.strategy="
258+
)
259+
.run(context -> {
260+
Throwable failure = context.getStartupFailure();
261+
assertNotNull(failure, "the application context must fail to start for a blank strategy");
262+
Throwable rootCause = NestedExceptionUtils.getMostSpecificCause(failure);
263+
assertInstanceOf(IllegalStateException.class, rootCause);
264+
assertTrue(rootCause.getMessage().contains("shenyu.httpclient.strategy"), rootCause.getMessage());
265+
});
266+
}
267+
268+
@Test
269+
public void testNullValuedStrategyFailsStartup() {
270+
applicationContextRunner
271+
.withInitializer(context -> context.getEnvironment().getPropertySources()
272+
.addFirst(new MapPropertySource("nullValuedStrategy",
273+
Collections.singletonMap("shenyu.httpclient.strategy", null))))
274+
.run(context -> {
275+
Throwable failure = context.getStartupFailure();
276+
assertNotNull(failure, "the application context must fail to start for a null valued strategy");
277+
Throwable rootCause = NestedExceptionUtils.getMostSpecificCause(failure);
278+
assertInstanceOf(IllegalStateException.class, rootCause);
279+
assertTrue(rootCause.getMessage().contains("shenyu.httpclient.strategy"), rootCause.getMessage());
280+
});
281+
}
192282
}

0 commit comments

Comments
 (0)