renamed mqtts ca file for device connectivity
This commit is contained in:
		
							parent
							
								
									3ab6b0e2b7
								
							
						
					
					
						commit
						902e153683
					
				@ -50,7 +50,7 @@ import static org.thingsboard.server.controller.ControllerConstants.DEVICE_ID_PA
 | 
			
		||||
import static org.thingsboard.server.controller.ControllerConstants.PROTOCOL;
 | 
			
		||||
import static org.thingsboard.server.controller.ControllerConstants.PROTOCOL_PARAM_DESCRIPTION;
 | 
			
		||||
import static org.thingsboard.server.controller.ControllerConstants.TENANT_OR_CUSTOMER_AUTHORITY_PARAGRAPH;
 | 
			
		||||
import static org.thingsboard.server.dao.util.DeviceConnectivityUtil.PEM_CERT_FILE_NAME;
 | 
			
		||||
import static org.thingsboard.server.dao.util.DeviceConnectivityUtil.CA_FILE_PEM;
 | 
			
		||||
 | 
			
		||||
@RestController
 | 
			
		||||
@TbCoreComponent
 | 
			
		||||
@ -129,8 +129,8 @@ public class DeviceConnectivityController extends BaseController {
 | 
			
		||||
                checkNotNull(deviceConnectivityService.getPemCertFile(protocol), protocol + " pem cert file is not found!");
 | 
			
		||||
 | 
			
		||||
        return ResponseEntity.ok()
 | 
			
		||||
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + PEM_CERT_FILE_NAME)
 | 
			
		||||
                .header("x-filename", PEM_CERT_FILE_NAME)
 | 
			
		||||
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + CA_FILE_PEM)
 | 
			
		||||
                .header("x-filename", CA_FILE_PEM)
 | 
			
		||||
                .contentLength(pemCert.contentLength())
 | 
			
		||||
                .contentType(MediaType.APPLICATION_OCTET_STREAM)
 | 
			
		||||
                .body(pemCert);
 | 
			
		||||
 | 
			
		||||
@ -1180,7 +1180,7 @@ device:
 | 
			
		||||
      # Port of mqtt transport service. If empty default port for mqtts will be used.
 | 
			
		||||
      port: "${DEVICE_CONNECTIVITY_MQTTS_PORT:8883}"
 | 
			
		||||
      # Path to the server certificate file
 | 
			
		||||
      pem_cert_file: "${DEVICE_CONNECTIVITY_MQTT_SSL_PEM_CERT:mqttserver.pem}"
 | 
			
		||||
      pem_cert_file: "${DEVICE_CONNECTIVITY_MQTT_SSL_PEM_CERT:cafile.pem}"
 | 
			
		||||
    coap:
 | 
			
		||||
      # If true coap command will be included to the list of all test commands using DEVICE_CONNECTIVITY_COAP_HOST and DEVICE_CONNECTIVITY_COAP_PORT.
 | 
			
		||||
      enabled: "${DEVICE_CONNECTIVITY_COAP_ENABLED:true}"
 | 
			
		||||
 | 
			
		||||
@ -62,14 +62,14 @@ import static org.thingsboard.server.dao.util.DeviceConnectivityUtil.HTTP;
 | 
			
		||||
import static org.thingsboard.server.dao.util.DeviceConnectivityUtil.HTTPS;
 | 
			
		||||
import static org.thingsboard.server.dao.util.DeviceConnectivityUtil.MQTT;
 | 
			
		||||
import static org.thingsboard.server.dao.util.DeviceConnectivityUtil.MQTTS;
 | 
			
		||||
import static org.thingsboard.server.dao.util.DeviceConnectivityUtil.PEM_CERT_FILE_NAME;
 | 
			
		||||
import static org.thingsboard.server.dao.util.DeviceConnectivityUtil.CA_FILE_PEM;
 | 
			
		||||
 | 
			
		||||
@TestPropertySource(properties = {
 | 
			
		||||
        "device.connectivity.https.enabled=true",
 | 
			
		||||
        "device.connectivity.http.port=8080",
 | 
			
		||||
        "device.connectivity.https.port=444",
 | 
			
		||||
        "device.connectivity.mqtts.enabled=true",
 | 
			
		||||
        "device.connectivity.mqtts.pem_cert_file=/tmp/" + PEM_CERT_FILE_NAME,
 | 
			
		||||
        "device.connectivity.mqtts.pem_cert_file=/tmp/" + CA_FILE_PEM,
 | 
			
		||||
        "device.connectivity.coaps.enabled=true",
 | 
			
		||||
})
 | 
			
		||||
@ContextConfiguration(classes = {DeviceConnectivityControllerTest.Config.class})
 | 
			
		||||
@ -202,8 +202,8 @@ public class DeviceConnectivityControllerTest extends AbstractControllerTest {
 | 
			
		||||
        assertThat(mqttCommands.get(MQTT).asText()).isEqualTo(String.format("mosquitto_pub -d -q 1 -h localhost -p 1883 -t v1/devices/me/telemetry " +
 | 
			
		||||
                        "-u %s -m \"{temperature:25}\"",
 | 
			
		||||
                credentials.getCredentialsId()));
 | 
			
		||||
        assertThat(mqttCommands.get(MQTTS).get(0).asText()).isEqualTo("curl -f -S -o tb-server-chain.pem http://localhost:80/api/device-connectivity/mqtts/certificate/download");
 | 
			
		||||
        assertThat(mqttCommands.get(MQTTS).get(1).asText()).isEqualTo(String.format("mosquitto_pub -d -q 1 --cafile tb-server-chain.pem -h localhost -p 8883 " +
 | 
			
		||||
        assertThat(mqttCommands.get(MQTTS).get(0).asText()).isEqualTo("curl -f -S -o cafile.pem http://localhost:80/api/device-connectivity/mqtts/certificate/download");
 | 
			
		||||
        assertThat(mqttCommands.get(MQTTS).get(1).asText()).isEqualTo(String.format("mosquitto_pub -d -q 1 --cafile cafile.pem -h localhost -p 8883 " +
 | 
			
		||||
                "-t v1/devices/me/telemetry -u %s -m \"{temperature:25}\"", credentials.getCredentialsId()));
 | 
			
		||||
 | 
			
		||||
        JsonNode dockerMqttCommands = commands.get(MQTT).get(DOCKER);
 | 
			
		||||
@ -211,8 +211,8 @@ public class DeviceConnectivityControllerTest extends AbstractControllerTest {
 | 
			
		||||
                        " -p 1883 -t v1/devices/me/telemetry -u %s -m \"{temperature:25}\"",
 | 
			
		||||
                credentials.getCredentialsId()));
 | 
			
		||||
        assertThat(dockerMqttCommands.get(MQTTS).asText()).isEqualTo(String.format("docker run --rm -it thingsboard/mosquitto-clients " +
 | 
			
		||||
                        "/bin/sh -c \"curl -f -S -o tb-server-chain.pem http://localhost:80/api/device-connectivity/mqtts/certificate/download && " +
 | 
			
		||||
                        "mosquitto_pub -d -q 1 --cafile tb-server-chain.pem -h localhost -p 8883 -t v1/devices/me/telemetry -u %s -m \"{temperature:25}\"\"",
 | 
			
		||||
                        "/bin/sh -c \"curl -f -S -o cafile.pem http://localhost:80/api/device-connectivity/mqtts/certificate/download && " +
 | 
			
		||||
                        "mosquitto_pub -d -q 1 --cafile cafile.pem -h localhost -p 8883 -t v1/devices/me/telemetry -u %s -m \"{temperature:25}\"\"",
 | 
			
		||||
                credentials.getCredentialsId()));
 | 
			
		||||
 | 
			
		||||
        JsonNode linuxCoapCommands = commands.get(COAP);
 | 
			
		||||
@ -240,8 +240,8 @@ public class DeviceConnectivityControllerTest extends AbstractControllerTest {
 | 
			
		||||
        JsonNode mqttCommands = commands.get(MQTT);
 | 
			
		||||
        assertThat(mqttCommands.get(MQTT).asText()).isEqualTo(String.format("mosquitto_pub -d -q 1 -h localhost -p 1883 -t %s " +
 | 
			
		||||
                "-u %s -m \"{temperature:25}\"", DEVICE_TELEMETRY_TOPIC, credentials.getCredentialsId()));
 | 
			
		||||
        assertThat(mqttCommands.get(MQTTS).get(0).asText()).isEqualTo("curl -f -S -o tb-server-chain.pem http://localhost:80/api/device-connectivity/mqtts/certificate/download");
 | 
			
		||||
        assertThat(mqttCommands.get(MQTTS).get(1).asText()).isEqualTo(String.format("mosquitto_pub -d -q 1 --cafile tb-server-chain.pem -h localhost -p 8883 " +
 | 
			
		||||
        assertThat(mqttCommands.get(MQTTS).get(0).asText()).isEqualTo("curl -f -S -o cafile.pem http://localhost:80/api/device-connectivity/mqtts/certificate/download");
 | 
			
		||||
        assertThat(mqttCommands.get(MQTTS).get(1).asText()).isEqualTo(String.format("mosquitto_pub -d -q 1 --cafile cafile.pem -h localhost -p 8883 " +
 | 
			
		||||
                "-t %s -u %s -m \"{temperature:25}\"", DEVICE_TELEMETRY_TOPIC, credentials.getCredentialsId()));
 | 
			
		||||
 | 
			
		||||
        JsonNode dockerMqttCommands = commands.get(MQTT).get(DOCKER);
 | 
			
		||||
@ -249,8 +249,8 @@ public class DeviceConnectivityControllerTest extends AbstractControllerTest {
 | 
			
		||||
                        " -p 1883 -t %s -u %s -m \"{temperature:25}\"",
 | 
			
		||||
                DEVICE_TELEMETRY_TOPIC, credentials.getCredentialsId()));
 | 
			
		||||
        assertThat(dockerMqttCommands.get(MQTTS).asText()).isEqualTo(String.format("docker run --rm -it thingsboard/mosquitto-clients " +
 | 
			
		||||
                        "/bin/sh -c \"curl -f -S -o tb-server-chain.pem http://localhost:80/api/device-connectivity/mqtts/certificate/download && " +
 | 
			
		||||
                        "mosquitto_pub -d -q 1 --cafile tb-server-chain.pem -h localhost -p 8883 -t %s -u %s -m \"{temperature:25}\"\"",
 | 
			
		||||
                        "/bin/sh -c \"curl -f -S -o cafile.pem http://localhost:80/api/device-connectivity/mqtts/certificate/download && " +
 | 
			
		||||
                        "mosquitto_pub -d -q 1 --cafile cafile.pem -h localhost -p 8883 -t %s -u %s -m \"{temperature:25}\"\"",
 | 
			
		||||
                DEVICE_TELEMETRY_TOPIC, credentials.getCredentialsId()));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@ -284,8 +284,8 @@ public class DeviceConnectivityControllerTest extends AbstractControllerTest {
 | 
			
		||||
        JsonNode mqttCommands = commands.get(MQTT);
 | 
			
		||||
        assertThat(mqttCommands.get(MQTT).asText()).isEqualTo(String.format("mosquitto_pub -d -q 1 -h localhost -p 1883 -t %s " +
 | 
			
		||||
                "-i %s -u %s -P %s -m \"{temperature:25}\"", DEVICE_TELEMETRY_TOPIC, clientId, userName, password));
 | 
			
		||||
        assertThat(mqttCommands.get(MQTTS).get(0).asText()).isEqualTo("curl -f -S -o tb-server-chain.pem http://localhost:80/api/device-connectivity/mqtts/certificate/download");
 | 
			
		||||
        assertThat(mqttCommands.get(MQTTS).get(1).asText()).isEqualTo(String.format("mosquitto_pub -d -q 1 --cafile tb-server-chain.pem -h localhost -p 8883 " +
 | 
			
		||||
        assertThat(mqttCommands.get(MQTTS).get(0).asText()).isEqualTo("curl -f -S -o cafile.pem http://localhost:80/api/device-connectivity/mqtts/certificate/download");
 | 
			
		||||
        assertThat(mqttCommands.get(MQTTS).get(1).asText()).isEqualTo(String.format("mosquitto_pub -d -q 1 --cafile cafile.pem -h localhost -p 8883 " +
 | 
			
		||||
                "-t %s -i %s -u %s -P %s -m \"{temperature:25}\"", DEVICE_TELEMETRY_TOPIC, clientId, userName, password));
 | 
			
		||||
 | 
			
		||||
        JsonNode dockerMqttCommands = commands.get(MQTT).get(DOCKER);
 | 
			
		||||
@ -293,8 +293,8 @@ public class DeviceConnectivityControllerTest extends AbstractControllerTest {
 | 
			
		||||
                        " -p 1883 -t %s -i %s -u %s -P %s -m \"{temperature:25}\"",
 | 
			
		||||
                DEVICE_TELEMETRY_TOPIC, clientId, userName, password));
 | 
			
		||||
        assertThat(dockerMqttCommands.get(MQTTS).asText()).isEqualTo(String.format("docker run --rm -it thingsboard/mosquitto-clients " +
 | 
			
		||||
                        "/bin/sh -c \"curl -f -S -o tb-server-chain.pem http://localhost:80/api/device-connectivity/mqtts/certificate/download && " +
 | 
			
		||||
                        "mosquitto_pub -d -q 1 --cafile tb-server-chain.pem -h localhost -p 8883 -t %s -i %s -u %s -P %s -m \"{temperature:25}\"\"",
 | 
			
		||||
                        "/bin/sh -c \"curl -f -S -o cafile.pem http://localhost:80/api/device-connectivity/mqtts/certificate/download && " +
 | 
			
		||||
                        "mosquitto_pub -d -q 1 --cafile cafile.pem -h localhost -p 8883 -t %s -i %s -u %s -P %s -m \"{temperature:25}\"\"",
 | 
			
		||||
                DEVICE_TELEMETRY_TOPIC, clientId, userName, password));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@ -368,7 +368,7 @@ public class DeviceConnectivityControllerTest extends AbstractControllerTest {
 | 
			
		||||
    @Test
 | 
			
		||||
    @DirtiesContext
 | 
			
		||||
    public void testDownloadMqttCert() throws Exception {
 | 
			
		||||
        Path path = Files.createFile(Path.of("/tmp/" + PEM_CERT_FILE_NAME));
 | 
			
		||||
        Path path = Files.createFile(Path.of("/tmp/" + CA_FILE_PEM));
 | 
			
		||||
        Files.writeString(path, CERT);
 | 
			
		||||
 | 
			
		||||
        try {
 | 
			
		||||
@ -382,7 +382,7 @@ public class DeviceConnectivityControllerTest extends AbstractControllerTest {
 | 
			
		||||
    @Test
 | 
			
		||||
    @DirtiesContext
 | 
			
		||||
    public void testDownloadMqttCertFromFileWithPrivateKey() throws Exception {
 | 
			
		||||
        Path path = Files.createFile(Path.of("/tmp/" + PEM_CERT_FILE_NAME));
 | 
			
		||||
        Path path = Files.createFile(Path.of("/tmp/" + CA_FILE_PEM));
 | 
			
		||||
        Files.writeString(path, CERT + P_KEY);
 | 
			
		||||
 | 
			
		||||
        try {
 | 
			
		||||
 | 
			
		||||
@ -27,42 +27,21 @@ import org.mockito.AdditionalAnswers;
 | 
			
		||||
import org.mockito.Mockito;
 | 
			
		||||
import org.springframework.context.annotation.Bean;
 | 
			
		||||
import org.springframework.context.annotation.Primary;
 | 
			
		||||
import org.springframework.test.annotation.DirtiesContext;
 | 
			
		||||
import org.springframework.test.context.ContextConfiguration;
 | 
			
		||||
import org.springframework.test.context.TestPropertySource;
 | 
			
		||||
import org.thingsboard.common.util.JacksonUtil;
 | 
			
		||||
import org.thingsboard.common.util.ThingsBoardExecutors;
 | 
			
		||||
import org.thingsboard.server.common.data.Device;
 | 
			
		||||
import org.thingsboard.server.common.data.DeviceProfile;
 | 
			
		||||
import org.thingsboard.server.common.data.DeviceProfileType;
 | 
			
		||||
import org.thingsboard.server.common.data.DeviceTransportType;
 | 
			
		||||
import org.thingsboard.server.common.data.Tenant;
 | 
			
		||||
import org.thingsboard.server.common.data.User;
 | 
			
		||||
import org.thingsboard.server.common.data.device.credentials.BasicMqttCredentials;
 | 
			
		||||
import org.thingsboard.server.common.data.device.profile.CoapDeviceProfileTransportConfiguration;
 | 
			
		||||
import org.thingsboard.server.common.data.device.profile.DefaultDeviceProfileConfiguration;
 | 
			
		||||
import org.thingsboard.server.common.data.device.profile.DeviceProfileData;
 | 
			
		||||
import org.thingsboard.server.common.data.device.profile.MqttDeviceProfileTransportConfiguration;
 | 
			
		||||
import org.thingsboard.server.common.data.id.DeviceProfileId;
 | 
			
		||||
import org.thingsboard.server.common.data.security.Authority;
 | 
			
		||||
import org.thingsboard.server.common.data.security.DeviceCredentials;
 | 
			
		||||
import org.thingsboard.server.common.data.security.DeviceCredentialsType;
 | 
			
		||||
import org.thingsboard.server.dao.device.DeviceDao;
 | 
			
		||||
import org.thingsboard.server.dao.service.DaoSqlTest;
 | 
			
		||||
 | 
			
		||||
import java.nio.file.Files;
 | 
			
		||||
import java.nio.file.Path;
 | 
			
		||||
 | 
			
		||||
import static org.assertj.core.api.Assertions.assertThat;
 | 
			
		||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
 | 
			
		||||
import static org.thingsboard.server.dao.util.DeviceConnectivityUtil.COAP;
 | 
			
		||||
import static org.thingsboard.server.dao.util.DeviceConnectivityUtil.COAPS;
 | 
			
		||||
import static org.thingsboard.server.dao.util.DeviceConnectivityUtil.DOCKER;
 | 
			
		||||
import static org.thingsboard.server.dao.util.DeviceConnectivityUtil.HTTP;
 | 
			
		||||
import static org.thingsboard.server.dao.util.DeviceConnectivityUtil.HTTPS;
 | 
			
		||||
import static org.thingsboard.server.dao.util.DeviceConnectivityUtil.MQTT;
 | 
			
		||||
import static org.thingsboard.server.dao.util.DeviceConnectivityUtil.MQTTS;
 | 
			
		||||
import static org.thingsboard.server.dao.util.DeviceConnectivityUtil.PEM_CERT_FILE_NAME;
 | 
			
		||||
 | 
			
		||||
@TestPropertySource(properties = {
 | 
			
		||||
        "device.connectivity.https.enabled=true",
 | 
			
		||||
 | 
			
		||||
@ -31,7 +31,7 @@ public class DeviceConnectivityUtil {
 | 
			
		||||
    public static final String MQTTS = "mqtts";
 | 
			
		||||
    public static final String COAP = "coap";
 | 
			
		||||
    public static final String COAPS = "coaps";
 | 
			
		||||
    public static final String PEM_CERT_FILE_NAME = "tb-server-chain.pem";
 | 
			
		||||
    public static final String CA_FILE_PEM = "cafile.pem";
 | 
			
		||||
    public static final String CHECK_DOCUMENTATION = "Check documentation";
 | 
			
		||||
    public static final String JSON_EXAMPLE_PAYLOAD = "\"{temperature:25}\"";
 | 
			
		||||
    public static final String DOCKER_RUN = "docker run --rm -it ";
 | 
			
		||||
@ -47,7 +47,7 @@ public class DeviceConnectivityUtil {
 | 
			
		||||
    public static String getMqttPublishCommand(String protocol, String host, String port, String deviceTelemetryTopic, DeviceCredentials deviceCredentials) {
 | 
			
		||||
        StringBuilder command = new StringBuilder("mosquitto_pub -d -q 1");
 | 
			
		||||
        if (MQTTS.equals(protocol)) {
 | 
			
		||||
            command.append(" --cafile ").append(PEM_CERT_FILE_NAME);
 | 
			
		||||
            command.append(" --cafile ").append(CA_FILE_PEM);
 | 
			
		||||
        }
 | 
			
		||||
        command.append(" -h ").append(host).append(port == null ? "" : " -p " + port);
 | 
			
		||||
        command.append(" -t ").append(deviceTelemetryTopic);
 | 
			
		||||
@ -151,7 +151,7 @@ public class DeviceConnectivityUtil {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static String getCurlPemCertCommand(String baseUrl, String protocol) {
 | 
			
		||||
        return String.format("curl -f -S -o %s %s/api/device-connectivity/%s/certificate/download", PEM_CERT_FILE_NAME, baseUrl, protocol);
 | 
			
		||||
        return String.format("curl -f -S -o %s %s/api/device-connectivity/%s/certificate/download", CA_FILE_PEM, baseUrl, protocol);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static String getCoapPublishCommand(String protocol, String host, String port, DeviceCredentials deviceCredentials) {
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user