Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

Add config for web server max post size "std" and multi-part (#2532)

Eric Bariaux85c7b46067b8
diff --git a/container/src/main/java/org/openremote/container/web/WebService.java b/container/src/main/java/org/openremote/container/web/WebService.java
index d359c61f5..39cab200f 100644
--- a/container/src/main/java/org/openremote/container/web/WebService.java
+++ b/container/src/main/java/org/openremote/container/web/WebService.java
@@ -87,12 +87,18 @@ public abstract class WebService implements ContainerService {
public static final int OR_WEBSERVER_IO_THREADS_MAX_DEFAULT = Math.max(Runtime.getRuntime().availableProcessors(), 2);
public static final String OR_WEBSERVER_WORKER_THREADS_MAX = "OR_WEBSERVER_WORKER_THREADS_MAX";
public static final int OR_WEBSERVER_WORKER_THREADS_MAX_DEFAULT = Math.max(Runtime.getRuntime().availableProcessors(), 10);
+ public static final String OR_WEBSERVER_MAX_ENTITY_SIZE = "OR_WEBSERVER_MAX_ENTITY_SIZE";
+ public static final long OR_WEBSERVER_MAX_ENTITY_SIZE_DEFAULT = 30_000_000L;
+ public static final String OR_WEBSERVER_MULTIPART_MAX_ENTITY_SIZE = "OR_WEBSERVER_MULTIPART_MAX_ENTITY_SIZE";
+ public static final long OR_WEBSERVER_MULTIPART_MAX_ENTITY_SIZE_DEFAULT = 30_000_000L;
private static final System.Logger LOG = System.getLogger(WebService.class.getName());
protected boolean devMode;
protected String host;
protected IdentityService identityService;
protected int port;
protected Undertow undertow;
+ protected long maxEntitySize = OR_WEBSERVER_MAX_ENTITY_SIZE_DEFAULT;
+ protected long multipartMaxEntitySize = OR_WEBSERVER_MULTIPART_MAX_ENTITY_SIZE_DEFAULT;
protected URI containerHostUri;
protected PathHandler pathHandler = Handlers.path();
@@ -119,8 +125,10 @@ public abstract class WebService implements ContainerService {
public void init(Container container) throws Exception {
identityService = container.getService(IdentityService.class);
devMode = container.isDevMode();
- host = getString(container.getConfig(), OR_WEBSERVER_LISTEN_HOST, OR_WEBSERVER_LISTEN_HOST_DEFAULT);
- port = getInteger(container.getConfig(), OR_WEBSERVER_LISTEN_PORT, OR_WEBSERVER_LISTEN_PORT_DEFAULT);
+ Map<String, String> config = container.getConfig();
+
+ host = getString(config, OR_WEBSERVER_LISTEN_HOST, OR_WEBSERVER_LISTEN_HOST_DEFAULT);
+ port = getInteger(config, OR_WEBSERVER_LISTEN_PORT, OR_WEBSERVER_LISTEN_PORT_DEFAULT);
String containerHost = host.equalsIgnoreCase("localhost") || host.indexOf("127") == 0 || host.indexOf("0.0.0.0") == 0
? getLocalIpAddress()
: host;
@@ -131,17 +139,19 @@ public abstract class WebService implements ContainerService {
.host(containerHost)
.port(port).build();
+ maxEntitySize = getLong(config, OR_WEBSERVER_MAX_ENTITY_SIZE, OR_WEBSERVER_MAX_ENTITY_SIZE_DEFAULT);
+ multipartMaxEntitySize = getLong(config, OR_WEBSERVER_MULTIPART_MAX_ENTITY_SIZE, OR_WEBSERVER_MULTIPART_MAX_ENTITY_SIZE_DEFAULT);
+
undertow = build(
container,
- Undertow.builder()
+ Undertow.builder()
.addHttpListener(port, host)
- .setIoThreads(getInteger(container.getConfig(), OR_WEBSERVER_IO_THREADS_MAX, OR_WEBSERVER_IO_THREADS_MAX_DEFAULT))
- .setWorkerThreads(getInteger(container.getConfig(), OR_WEBSERVER_WORKER_THREADS_MAX, OR_WEBSERVER_WORKER_THREADS_MAX_DEFAULT))
+ .setIoThreads(getInteger(config, OR_WEBSERVER_IO_THREADS_MAX, OR_WEBSERVER_IO_THREADS_MAX_DEFAULT))
+ .setWorkerThreads(getInteger(config, OR_WEBSERVER_WORKER_THREADS_MAX, OR_WEBSERVER_WORKER_THREADS_MAX_DEFAULT))
.setWorkerOption(Options.WORKER_NAME, "WebService")
.setWorkerOption(Options.THREAD_DAEMON, true)
- // TODO: setting entity size to unlimited to reset to Undertow 2.3.20.Final behaviour
- // but should be addressed in a better way: https://github.com/openremote/openremote/issues/2490
- .setServerOption(UndertowOptions.MAX_ENTITY_SIZE, -1L)
+ .setServerOption(UndertowOptions.MULTIPART_MAX_ENTITY_SIZE, multipartMaxEntitySize)
+ .setServerOption(UndertowOptions.MAX_ENTITY_SIZE, maxEntitySize)
).build();
// We have to set system properties for websocket timeouts
diff --git a/container/src/test/java/org/openremote/container/web/WebServiceEntitySizeConfigTest.java b/container/src/test/java/org/openremote/container/web/WebServiceEntitySizeConfigTest.java
new file mode 100644
index 000000000..4d10f1675
--- /dev/null
+++ b/container/src/test/java/org/openremote/container/web/WebServiceEntitySizeConfigTest.java
@@ -0,0 +1,124 @@
+package org.openremote.container.web;
+
+import io.micrometer.core.instrument.MeterRegistry;
+import org.junit.jupiter.api.Test;
+import org.openremote.container.security.IdentityService;
+import org.openremote.model.Container;
+import org.openremote.model.ContainerService;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Map;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.ScheduledExecutorService;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class WebServiceEntitySizeConfigTest {
+
+ @Test
+ public void shouldUseDefaultEntitySizeLimits() throws Exception {
+ TrackingWebService service = new TrackingWebService();
+ service.init(new TestContainer(Map.of(
+ WebService.OR_WEBSERVER_LISTEN_HOST, "127.0.0.1",
+ WebService.OR_WEBSERVER_LISTEN_PORT, "0"
+ )));
+
+ assertEquals(WebService.OR_WEBSERVER_MULTIPART_MAX_ENTITY_SIZE_DEFAULT, service.getMultipartLimit());
+ assertEquals(WebService.OR_WEBSERVER_MAX_ENTITY_SIZE_DEFAULT, service.getMaxLimit());
+ }
+
+ @Test
+ public void shouldUseConfiguredEntitySizeLimits() throws Exception {
+ TrackingWebService service = new TrackingWebService();
+ service.init(new TestContainer(Map.of(
+ WebService.OR_WEBSERVER_LISTEN_HOST, "127.0.0.1",
+ WebService.OR_WEBSERVER_LISTEN_PORT, "0",
+ WebService.OR_WEBSERVER_MULTIPART_MAX_ENTITY_SIZE, "1024",
+ WebService.OR_WEBSERVER_MAX_ENTITY_SIZE, "2048"
+ )));
+
+ assertEquals(1024L, service.getMultipartLimit());
+ assertEquals(2048L, service.getMaxLimit());
+ }
+
+ @Test
+ public void shouldFallbackToDefaultsForInvalidEntitySizeLimits() throws Exception {
+ TrackingWebService service = new TrackingWebService();
+ service.init(new TestContainer(Map.of(
+ WebService.OR_WEBSERVER_LISTEN_HOST, "127.0.0.1",
+ WebService.OR_WEBSERVER_LISTEN_PORT, "0",
+ WebService.OR_WEBSERVER_MULTIPART_MAX_ENTITY_SIZE, "invalid",
+ WebService.OR_WEBSERVER_MAX_ENTITY_SIZE, "also_invalid"
+ )));
+
+ assertEquals(WebService.OR_WEBSERVER_MULTIPART_MAX_ENTITY_SIZE_DEFAULT, service.getMultipartLimit());
+ assertEquals(WebService.OR_WEBSERVER_MAX_ENTITY_SIZE_DEFAULT, service.getMaxLimit());
+ }
+
+ static class TrackingWebService extends WebService {
+ long getMultipartLimit() {
+ return multipartMaxEntitySize;
+ }
+
+ long getMaxLimit() {
+ return maxEntitySize;
+ }
+ }
+
+ static class TestContainer implements Container {
+ private final Map<String, String> config;
+
+ TestContainer(Map<String, String> config) {
+ this.config = config;
+ }
+
+ @Override
+ public Map<String, String> getConfig() {
+ return config;
+ }
+
+ @Override
+ public boolean isDevMode() {
+ return false;
+ }
+
+ @Override
+ public ContainerService[] getServices() {
+ return new ContainerService[0];
+ }
+
+ @Override
+ public ScheduledExecutorService getScheduledExecutor() {
+ return null;
+ }
+
+ @Override
+ public ExecutorService getExecutor() {
+ return null;
+ }
+
+ @Override
+ public <T extends ContainerService> Collection<T> getServices(Class<T> type) {
+ return Collections.emptyList();
+ }
+
+ @Override
+ public <T extends ContainerService> T getService(Class<T> type) throws IllegalStateException {
+ if (type == IdentityService.class) {
+ return null;
+ }
+ throw new IllegalStateException("Service not available in test container: " + type.getName());
+ }
+
+ @Override
+ public <T extends ContainerService> boolean hasService(Class<T> type) {
+ return false;
+ }
+
+ @Override
+ public MeterRegistry getMeterRegistry() {
+ return null;
+ }
+ }
+}
diff --git a/model/src/main/java/org/openremote/model/util/MapAccess.java b/model/src/main/java/org/openremote/model/util/MapAccess.java
index 629bc228b..aea560cdf 100644
--- a/model/src/main/java/org/openremote/model/util/MapAccess.java
+++ b/model/src/main/java/org/openremote/model/util/MapAccess.java
@@ -56,6 +56,17 @@ public class MapAccess {
return defaultValue;
}
+ static public long getLong(Map<String, String> map, String key, long defaultValue) {
+ if (map.containsKey(key)) {
+ try {
+ return Long.valueOf(map.get(key));
+ } catch (Exception ex) {
+ return defaultValue;
+ }
+ }
+ return defaultValue;
+ }
+
static public BigDecimal getDecimal(Map<String, String> map, String key, BigDecimal defaultValue) {
if (map.containsKey(key)) {
try {
diff --git a/profile/deploy.yml b/profile/deploy.yml
index 189f7be78..938cff6bc 100644
--- a/profile/deploy.yml
+++ b/profile/deploy.yml
@@ -233,6 +233,12 @@ services:
# Manager web server maximum IO thread count; default: Processor core count or 2
OR_WEBSERVER_IO_THREADS_MAX:
+ # Manager web server max request entity size in bytes; default: 30000000
+ OR_WEBSERVER_MAX_ENTITY_SIZE:
+
+ # Manager web server max multipart request entity size in bytes; default: 30000000
+ OR_WEBSERVER_MULTIPART_MAX_ENTITY_SIZE:
+
# Database settings
OR_DB_VENDOR: ${OR_DB_VENDOR:-postgres}
OR_DB_HOST: ${OR_DB_HOST:-postgresql}