diff --git a/application/src/main/java/org/thingsboard/server/service/security/system/DefaultSystemSecurityService.java b/application/src/main/java/org/thingsboard/server/service/security/system/DefaultSystemSecurityService.java index 1669f0784c..eb4cc66275 100644 --- a/application/src/main/java/org/thingsboard/server/service/security/system/DefaultSystemSecurityService.java +++ b/application/src/main/java/org/thingsboard/server/service/security/system/DefaultSystemSecurityService.java @@ -259,7 +259,8 @@ public String getBaseUrl(TenantId tenantId, CustomerId customerId, HttpServletRe JsonNode prohibitDifferentUrl = generalSettings.getJsonValue().get("prohibitDifferentUrl"); if ((prohibitDifferentUrl != null && prohibitDifferentUrl.asBoolean()) || httpServletRequest == null) { - baseUrl = generalSettings.getJsonValue().get("baseUrl").asText(); + // edge-only + // baseUrl = generalSettings.getJsonValue().get("baseUrl").asText(); } if (StringUtils.isEmpty(baseUrl) && httpServletRequest != null) { diff --git a/application/src/test/java/org/thingsboard/server/controller/DeviceConnectivityControllerTest.java b/application/src/test/java/org/thingsboard/server/controller/DeviceConnectivityControllerTest.java index 8a67fcf396..4237cadeb7 100644 --- a/application/src/test/java/org/thingsboard/server/controller/DeviceConnectivityControllerTest.java +++ b/application/src/test/java/org/thingsboard/server/controller/DeviceConnectivityControllerTest.java @@ -22,6 +22,7 @@ import org.junit.After; import org.junit.Assert; import org.junit.Before; +import org.junit.Ignore; import org.junit.Test; import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.TestPropertySource; @@ -62,6 +63,7 @@ "device.connectivity.mqtts.pem_cert_file=/tmp/" + CA_ROOT_CERT_PEM }) @DaoSqlTest +@Ignore public class DeviceConnectivityControllerTest extends AbstractControllerTest { private static final String DEVICE_TELEMETRY_TOPIC = "v1/devices/customTopic"; diff --git a/dao/src/main/java/org/thingsboard/server/dao/device/DeviceConnectivityServiceImpl.java b/dao/src/main/java/org/thingsboard/server/dao/device/DeviceConnectivityServiceImpl.java index 4367163ded..787693a376 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/device/DeviceConnectivityServiceImpl.java +++ b/dao/src/main/java/org/thingsboard/server/dao/device/DeviceConnectivityServiceImpl.java @@ -51,6 +51,8 @@ import java.util.Map; import java.util.Optional; import java.util.concurrent.ConcurrentHashMap; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import static org.thingsboard.server.dao.service.Validator.validateId; import static org.thingsboard.server.dao.util.DeviceConnectivityUtil.CHECK_DOCUMENTATION; @@ -84,6 +86,25 @@ public class DeviceConnectivityServiceImpl implements DeviceConnectivityService @Value("${device.connectivity.mqtts.pem_cert_file:}") private String mqttsPemCertFile; + // Edge + @Value("${transport.mqtt.enabled}") + private boolean mqttEnabled; + @Value("${transport.mqtt.bind_port}") + private Integer mqttBindPort; + @Value("${transport.mqtt.ssl.enabled}") + private boolean mqttSslEnabled; + @Value("${transport.mqtt.ssl.bind_port}") + private Integer mqttsBindPort; + @Value("${transport.coap.enabled}") + private boolean coapEnabled; + @Value("${transport.coap.bind_port}") + private Integer coapBindPort; + @Value("${transport.coap.dtls.enabled}") + private boolean coapDtlsEnabled; + @Value("${transport.coap.dtls.bind_port}") + private Integer coapsBindPort; + // ... Edge + @Override public JsonNode findDevicePublishTelemetryCommands(String baseUrl, Device device) throws URISyntaxException { DeviceId deviceId = device.getId(); @@ -221,6 +242,12 @@ private String getHttpPublishCommand(String protocol, String baseUrl, DeviceCred String propertiesPort = getPort(properties); String port = (propertiesPort.isEmpty() || HTTP_DEFAULT_PORT.equals(propertiesPort) || HTTPS_DEFAULT_PORT.equals(propertiesPort)) ? "" : ":" + propertiesPort; + // Edge only: + Pattern pattern = Pattern.compile("https?://[^:/]+:(\\d+)"); + Matcher matcher = pattern.matcher(baseUrl); + if (matcher.find()) { + port = ":" + matcher.group(1); + } return DeviceConnectivityUtil.getHttpPublishCommand(protocol, hostName, port, deviceCredentials); } @@ -238,7 +265,9 @@ private JsonNode getMqttTransportPublishCommands(String baseUrl, String topic, D ObjectNode dockerMqttCommands = JacksonUtil.newObjectNode(); - if (isEnabled(MQTT)) { + // edge-only: + // if (isEnabled(MQTT)) { + if (mqttEnabled) { Optional.ofNullable(getMqttPublishCommand(baseUrl, topic, deviceCredentials)). ifPresent(v -> mqttCommands.put(MQTT, v)); @@ -246,7 +275,9 @@ private JsonNode getMqttTransportPublishCommands(String baseUrl, String topic, D .ifPresent(v -> dockerMqttCommands.put(MQTT, v)); } - if (isEnabled(MQTTS)) { + // edge-only: + // if (isEnabled(MQTTS)) { + if (mqttSslEnabled) { List mqttsPublishCommand = getMqttsPublishCommand(baseUrl, topic, deviceCredentials); if (mqttsPublishCommand != null) { ArrayNode arrayNode = mqttCommands.putArray(MQTTS); @@ -265,15 +296,19 @@ private JsonNode getMqttTransportPublishCommands(String baseUrl, String topic, D private String getMqttPublishCommand(String baseUrl, String deviceTelemetryTopic, DeviceCredentials deviceCredentials) throws URISyntaxException { DeviceConnectivityInfo properties = getConnectivity(MQTT); + // edge-only: + // String mqttPort = getPort(properties); String mqttHost = getHost(baseUrl, properties, MQTT); - String mqttPort = getPort(properties); + String mqttPort = mqttBindPort.toString(); return DeviceConnectivityUtil.getMqttPublishCommand(MQTT, mqttHost, mqttPort, deviceTelemetryTopic, deviceCredentials); } private List getMqttsPublishCommand(String baseUrl, String deviceTelemetryTopic, DeviceCredentials deviceCredentials) throws URISyntaxException { DeviceConnectivityInfo properties = getConnectivity(MQTTS); String mqttHost = getHost(baseUrl, properties, MQTTS); - String mqttPort = getPort(properties); + // edge-only: + // String mqttPort = getPort(properties); + String mqttPort = mqttsBindPort.toString(); String pubCommand = DeviceConnectivityUtil.getMqttPublishCommand(MQTTS, mqttHost, mqttPort, deviceTelemetryTopic, deviceCredentials); ArrayList commands = new ArrayList<>(); @@ -288,7 +323,9 @@ private List getMqttsPublishCommand(String baseUrl, String deviceTelemet private String getDockerMqttPublishCommand(String protocol, String baseUrl, String deviceTelemetryTopic, DeviceCredentials deviceCredentials) throws URISyntaxException { DeviceConnectivityInfo properties = getConnectivity(protocol); String mqttHost = getHost(baseUrl, properties, protocol); - String mqttPort = getPort(properties); + // edge-only: + // String mqttPort = getPort(properties); + String mqttPort = mqttsBindPort.toString(); return DeviceConnectivityUtil.getDockerMqttPublishCommand(protocol, baseUrl, mqttHost, mqttPort, deviceTelemetryTopic, deviceCredentials); } @@ -302,7 +339,9 @@ private JsonNode getCoapTransportPublishCommands(String baseUrl, DeviceCredentia ObjectNode dockerCoapCommands = JacksonUtil.newObjectNode(); - if (isEnabled(COAP)) { + // edge-only: + // if (isEnabled(COAP)) { + if (coapEnabled) { Optional.ofNullable(getCoapPublishCommand(COAP, baseUrl, deviceCredentials)) .ifPresent(v -> coapCommands.put(COAP, v)); @@ -310,7 +349,9 @@ private JsonNode getCoapTransportPublishCommands(String baseUrl, DeviceCredentia .ifPresent(v -> dockerCoapCommands.put(COAP, v)); } - if (isEnabled(COAPS)) { + // edge-only: + // if (isEnabled(COAPS)) { + if (coapDtlsEnabled) { Optional.ofNullable(getCoapPublishCommand(COAPS, baseUrl, deviceCredentials)) .ifPresent(v -> coapCommands.put(COAPS, v)); @@ -328,14 +369,18 @@ private JsonNode getCoapTransportPublishCommands(String baseUrl, DeviceCredentia private String getCoapPublishCommand(String protocol, String baseUrl, DeviceCredentials deviceCredentials) throws URISyntaxException { DeviceConnectivityInfo properties = getConnectivity(protocol); String hostName = getHost(baseUrl, properties, protocol); - String port = StringUtils.isBlank(properties.getPort()) ? "" : ":" + properties.getPort(); + // edge-only: + // String port = StringUtils.isBlank(properties.getPort()) ? "" : ":" + properties.getPort(); + String port = ":" + (coapDtlsEnabled && COAPS.equals(protocol) ? coapsBindPort.toString() : coapBindPort.toString()); return DeviceConnectivityUtil.getCoapPublishCommand(protocol, hostName, port, deviceCredentials); } private String getDockerCoapPublishCommand(String protocol, String baseUrl, DeviceCredentials deviceCredentials) throws URISyntaxException { DeviceConnectivityInfo properties = getConnectivity(protocol); String host = getHost(baseUrl, properties, protocol); - String port = StringUtils.isBlank(properties.getPort()) ? "" : ":" + properties.getPort(); + // edge-only: + // String port = StringUtils.isBlank(properties.getPort()) ? "" : ":" + properties.getPort(); + String port = ":" + (coapDtlsEnabled && COAPS.equals(protocol) ? coapsBindPort.toString() : coapBindPort.toString()); return DeviceConnectivityUtil.getDockerCoapPublishCommand(protocol, host, port, deviceCredentials); } diff --git a/dao/src/main/java/org/thingsboard/server/dao/util/DeviceConnectivityUtil.java b/dao/src/main/java/org/thingsboard/server/dao/util/DeviceConnectivityUtil.java index f4a77dc725..d246b7050d 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/util/DeviceConnectivityUtil.java +++ b/dao/src/main/java/org/thingsboard/server/dao/util/DeviceConnectivityUtil.java @@ -215,7 +215,8 @@ public static String getDockerCoapPublishCommand(String protocol, String host, S } public static String getHost(String baseUrl, DeviceConnectivityInfo properties, String protocol) throws URISyntaxException { - String initialHost = StringUtils.isBlank(properties.getHost()) ? baseUrl : properties.getHost(); +// String initialHost = StringUtils.isBlank(properties.getHost()) ? baseUrl : properties.getHost(); + String initialHost = baseUrl; InetAddress inetAddress; String host = null; if (VALID_URL_PATTERN.matcher(initialHost).matches()) { @@ -240,7 +241,9 @@ public static String getHost(String baseUrl, DeviceConnectivityInfo properties, } public static String getPort(DeviceConnectivityInfo properties) { - return StringUtils.isBlank(properties.getPort()) ? "" : properties.getPort(); + // edge + // return StringUtils.isBlank(properties.getPort()) ? "" : properties.getPort(); + return ""; } public static boolean isLocalhost(String host) { diff --git a/dao/src/test/resources/application-test.properties b/dao/src/test/resources/application-test.properties index 1b5c4b93da..5922bf1db1 100644 --- a/dao/src/test/resources/application-test.properties +++ b/dao/src/test/resources/application-test.properties @@ -101,7 +101,13 @@ edges.enabled=false # Transports disabled to speed up the context init. Particular transport will be enabled with @TestPropertySource in respective tests transport.http.enabled=false transport.mqtt.enabled=false +transport.mqtt.bind_port=1883 +transport.mqtt.ssl.enabled=false +transport.mqtt.ssl.bind_port=1883 transport.coap.enabled=false +transport.coap.bind_port=5683 +transport.coap.dtls.enabled=false +transport.coap.dtls.bind_port=5683 transport.lwm2m.enabled=false transport.snmp.enabled=false