Search in sources :

Example 21 with LwM2MDeviceCredentials

use of org.thingsboard.server.common.data.device.credentials.lwm2m.LwM2MDeviceCredentials in project thingsboard by thingsboard.

the class AbstractRpcLwM2MIntegrationTest method initRpc.

private void initRpc() throws Exception {
    String endpoint = DEVICE_ENDPOINT_RPC_PREF + endpointSequence.incrementAndGet();
    createNewClient(SECURITY_NO_SEC, COAP_CONFIG, true, endpoint, false, null);
    expectedObjects = ConcurrentHashMap.newKeySet();
    expectedObjectIdVers = ConcurrentHashMap.newKeySet();
    expectedInstances = ConcurrentHashMap.newKeySet();
    expectedObjectIdVerInstances = ConcurrentHashMap.newKeySet();
    lwM2MTestClient.getLeshanClient().getObjectTree().getObjectEnablers().forEach((key, val) -> {
        if (key > 0) {
            String objectVerId = "/" + key;
            objectVerId += ("_" + val.getObjectModel().version);
            expectedObjects.add("/" + key);
            expectedObjectIdVers.add(objectVerId);
            String finalObjectVerId = objectVerId;
            val.getAvailableInstanceIds().forEach(inststanceId -> {
                expectedInstances.add("/" + key + "/" + inststanceId);
                expectedObjectIdVerInstances.add(finalObjectVerId + "/" + inststanceId);
            });
        }
    });
    String ver_Id_0 = lwM2MTestClient.getLeshanClient().getObjectTree().getModel().getObjectModel(OBJECT_ID_0).version;
    objectIdVer_0 = "/" + OBJECT_ID_0 + "_" + ver_Id_0;
    objectIdVer_2 = (String) expectedObjectIdVers.stream().filter(path -> ((String) path).startsWith("/" + ACCESS_CONTROL)).findFirst().get();
    objectIdVer_3 = (String) expectedObjectIdVers.stream().filter(PREDICATE_3).findFirst().get();
    objectIdVer_19 = (String) expectedObjectIdVers.stream().filter(path -> ((String) path).startsWith("/" + BINARY_APP_DATA_CONTAINER)).findFirst().get();
    objectIdVer_3303 = (String) expectedObjectIdVers.stream().filter(path -> ((String) path).startsWith("/" + TEMPERATURE_SENSOR)).findFirst().get();
    objectInstanceIdVer_1 = (String) expectedObjectIdVerInstances.stream().filter(path -> (!((String) path).startsWith("/" + BINARY_APP_DATA_CONTAINER) && ((String) path).startsWith("/" + SERVER))).findFirst().get();
    objectInstanceIdVer_3 = (String) expectedObjectIdVerInstances.stream().filter(PREDICATE_3).findFirst().get();
    objectInstanceIdVer_5 = (String) expectedObjectIdVerInstances.stream().filter(path -> ((String) path).startsWith("/" + FIRMWARE)).findFirst().get();
    objectInstanceIdVer_9 = (String) expectedObjectIdVerInstances.stream().filter(path -> ((String) path).startsWith("/" + SOFTWARE_MANAGEMENT)).findFirst().get();
    idVer_3_0_9 = objectIdVer_3 + "/" + OBJECT_INSTANCE_ID_0 + "/" + RESOURCE_ID_9;
    idVer_19_0_0 = objectIdVer_19 + "/" + OBJECT_INSTANCE_ID_0 + "/" + RESOURCE_ID_0;
    OBSERVE_ATTRIBUTES_WITH_PARAMS_RPC = "    {\n" + "    \"keyName\": {\n" + "      \"" + idVer_3_0_9 + "\": \"" + RESOURCE_ID_NAME_3_9 + "\",\n" + "      \"" + objectIdVer_3 + "/" + OBJECT_INSTANCE_ID_0 + "/" + RESOURCE_ID_14 + "\": \"" + RESOURCE_ID_NAME_3_14 + "\",\n" + "      \"" + idVer_19_0_0 + "\": \"" + RESOURCE_ID_NAME_19_0_0 + "\",\n" + "      \"" + objectIdVer_19 + "/" + OBJECT_INSTANCE_ID_1 + "/" + RESOURCE_ID_0 + "\": \"" + RESOURCE_ID_NAME_19_1_0 + "\"\n" + "    },\n" + "    \"observe\": [\n" + "      \"" + idVer_3_0_9 + "\",\n" + "      \"" + idVer_19_0_0 + "\"\n" + "    ],\n" + "    \"attribute\": [\n" + "      \"" + objectIdVer_3 + "/" + OBJECT_INSTANCE_ID_0 + "/" + RESOURCE_ID_14 + "\"\n" + "    ],\n" + "    \"telemetry\": [\n" + "      \"" + idVer_3_0_9 + "\",\n" + "      \"" + idVer_19_0_0 + "\",\n" + "      \"" + objectIdVer_19 + "/" + OBJECT_INSTANCE_ID_1 + "/" + RESOURCE_ID_0 + "\"\n" + "    ],\n" + "    \"attributeLwm2m\": {}\n" + "  }";
    Lwm2mDeviceProfileTransportConfiguration transportConfiguration = getTransportConfiguration(OBSERVE_ATTRIBUTES_WITH_PARAMS_RPC, getBootstrapServerCredentialsNoSec(NONE));
    createDeviceProfile(transportConfiguration);
    LwM2MDeviceCredentials deviceCredentials = getDeviceCredentialsNoSec(createNoSecClientCredentials(endpoint));
    final Device device = createDevice(deviceCredentials, endpoint);
    deviceId = device.getId().getId().toString();
    lwM2MTestClient.start(true);
}
Also used : LwM2MDeviceCredentials(org.thingsboard.server.common.data.device.credentials.lwm2m.LwM2MDeviceCredentials) Device(org.thingsboard.server.common.data.Device) Lwm2mDeviceProfileTransportConfiguration(org.thingsboard.server.common.data.device.profile.Lwm2mDeviceProfileTransportConfiguration)

Example 22 with LwM2MDeviceCredentials

use of org.thingsboard.server.common.data.device.credentials.lwm2m.LwM2MDeviceCredentials in project thingsboard by thingsboard.

the class AbstractLwM2MIntegrationTest method createDevice.

protected Device createDevice(LwM2MDeviceCredentials credentials, String endpoint) throws Exception {
    Device device = new Device();
    device.setName(endpoint);
    device.setDeviceProfileId(deviceProfile.getId());
    device.setTenantId(tenantId);
    device = doPost("/api/device", device, Device.class);
    Assert.assertNotNull(device);
    DeviceCredentials deviceCredentials = doGet("/api/device/" + device.getId().getId().toString() + "/credentials", DeviceCredentials.class);
    Assert.assertEquals(device.getId(), deviceCredentials.getDeviceId());
    deviceCredentials.setCredentialsType(DeviceCredentialsType.LWM2M_CREDENTIALS);
    deviceCredentials.setCredentialsValue(JacksonUtil.toString(credentials));
    doPost("/api/device/credentials", deviceCredentials).andExpect(status().isOk());
    return device;
}
Also used : Device(org.thingsboard.server.common.data.Device) LwM2MDeviceCredentials(org.thingsboard.server.common.data.device.credentials.lwm2m.LwM2MDeviceCredentials) DeviceCredentials(org.thingsboard.server.common.data.security.DeviceCredentials)

Example 23 with LwM2MDeviceCredentials

use of org.thingsboard.server.common.data.device.credentials.lwm2m.LwM2MDeviceCredentials in project thingsboard by thingsboard.

the class AbstractLwM2MIntegrationTest method getDeviceCredentialsNoSec.

protected LwM2MDeviceCredentials getDeviceCredentialsNoSec(LwM2MClientCredential clientCredentials) {
    LwM2MDeviceCredentials credentials = new LwM2MDeviceCredentials();
    credentials.setClient(clientCredentials);
    LwM2MBootstrapClientCredentials bootstrapCredentials = new LwM2MBootstrapClientCredentials();
    NoSecBootstrapClientCredential serverCredentials = new NoSecBootstrapClientCredential();
    bootstrapCredentials.setBootstrapServer(serverCredentials);
    bootstrapCredentials.setLwm2mServer(serverCredentials);
    credentials.setBootstrap(bootstrapCredentials);
    return credentials;
}
Also used : LwM2MDeviceCredentials(org.thingsboard.server.common.data.device.credentials.lwm2m.LwM2MDeviceCredentials) LwM2MBootstrapClientCredentials(org.thingsboard.server.common.data.device.credentials.lwm2m.LwM2MBootstrapClientCredentials) NoSecBootstrapClientCredential(org.thingsboard.server.common.data.device.credentials.lwm2m.NoSecBootstrapClientCredential)

Example 24 with LwM2MDeviceCredentials

use of org.thingsboard.server.common.data.device.credentials.lwm2m.LwM2MDeviceCredentials in project thingsboard by thingsboard.

the class NoSecLwM2MIntegrationTest method testWithNoSecConnectLwm2mSuccessAndObserveTelemetry.

// Lwm2m only
@Test
public void testWithNoSecConnectLwm2mSuccessAndObserveTelemetry() throws Exception {
    String clientEndpoint = CLIENT_ENDPOINT_NO_SEC;
    LwM2MDeviceCredentials clientCredentials = getDeviceCredentialsNoSec(createNoSecClientCredentials(clientEndpoint));
    super.basicTestConnectionObserveTelemetry(SECURITY_NO_SEC, clientCredentials, COAP_CONFIG, clientEndpoint);
}
Also used : LwM2MDeviceCredentials(org.thingsboard.server.common.data.device.credentials.lwm2m.LwM2MDeviceCredentials) Test(org.junit.Test) AbstractSecurityLwM2MIntegrationTest(org.thingsboard.server.transport.lwm2m.security.AbstractSecurityLwM2MIntegrationTest)

Example 25 with LwM2MDeviceCredentials

use of org.thingsboard.server.common.data.device.credentials.lwm2m.LwM2MDeviceCredentials in project thingsboard by thingsboard.

the class DeviceCredentialsServiceImpl method formatAndValidateSimpleLwm2mCredentials.

private void formatAndValidateSimpleLwm2mCredentials(DeviceCredentials deviceCredentials) {
    LwM2MDeviceCredentials lwM2MCredentials;
    try {
        lwM2MCredentials = JacksonUtil.fromString(deviceCredentials.getCredentialsValue(), LwM2MDeviceCredentials.class);
        validateLwM2MDeviceCredentials(lwM2MCredentials);
    } catch (IllegalArgumentException e) {
        throw new DeviceCredentialsValidationException("Invalid credentials body for LwM2M credentials!");
    }
    String credentialsId = null;
    LwM2MClientCredential clientCredentials = lwM2MCredentials.getClient();
    switch(clientCredentials.getSecurityConfigClientMode()) {
        case NO_SEC:
        case RPK:
            deviceCredentials.setCredentialsValue(JacksonUtil.toString(lwM2MCredentials));
            credentialsId = clientCredentials.getEndpoint();
            break;
        case PSK:
            credentialsId = ((PSKClientCredential) clientCredentials).getIdentity();
            break;
        case X509:
            deviceCredentials.setCredentialsValue(JacksonUtil.toString(lwM2MCredentials));
            X509ClientCredential x509ClientConfig = (X509ClientCredential) clientCredentials;
            if ((StringUtils.isNotBlank(x509ClientConfig.getCert()))) {
                String sha3Hash = EncryptionUtil.getSha3Hash(x509ClientConfig.getCert());
                credentialsId = sha3Hash;
            } else {
                credentialsId = x509ClientConfig.getEndpoint();
            }
            break;
    }
    if (credentialsId == null) {
        throw new DeviceCredentialsValidationException("Invalid credentials body for LwM2M credentials!");
    }
    deviceCredentials.setCredentialsId(credentialsId);
}
Also used : LwM2MDeviceCredentials(org.thingsboard.server.common.data.device.credentials.lwm2m.LwM2MDeviceCredentials) LwM2MClientCredential(org.thingsboard.server.common.data.device.credentials.lwm2m.LwM2MClientCredential) Validator.validateString(org.thingsboard.server.dao.service.Validator.validateString) DeviceCredentialsValidationException(org.thingsboard.server.dao.exception.DeviceCredentialsValidationException) X509ClientCredential(org.thingsboard.server.common.data.device.credentials.lwm2m.X509ClientCredential)

Aggregations

LwM2MDeviceCredentials (org.thingsboard.server.common.data.device.credentials.lwm2m.LwM2MDeviceCredentials)25 Lwm2mDeviceProfileTransportConfiguration (org.thingsboard.server.common.data.device.profile.Lwm2mDeviceProfileTransportConfiguration)19 Test (org.junit.Test)17 AbstractSecurityLwM2MIntegrationTest (org.thingsboard.server.transport.lwm2m.security.AbstractSecurityLwM2MIntegrationTest)14 PrivateKey (java.security.PrivateKey)10 X509Certificate (java.security.cert.X509Certificate)10 Security (org.eclipse.leshan.client.object.Security)8 X509ClientCredential (org.thingsboard.server.common.data.device.credentials.lwm2m.X509ClientCredential)7 Device (org.thingsboard.server.common.data.Device)6 MvcResult (org.springframework.test.web.servlet.MvcResult)5 RPKClientCredential (org.thingsboard.server.common.data.device.credentials.lwm2m.RPKClientCredential)4 PSKClientCredential (org.thingsboard.server.common.data.device.credentials.lwm2m.PSKClientCredential)3 KvEntry (org.thingsboard.server.common.data.kv.KvEntry)3 TsKvEntry (org.thingsboard.server.common.data.kv.TsKvEntry)3 OtaPackageUpdateStatus (org.thingsboard.server.common.data.ota.OtaPackageUpdateStatus)3 AbstractOtaLwM2MIntegrationTest (org.thingsboard.server.transport.lwm2m.ota.AbstractOtaLwM2MIntegrationTest)3 LwM2MBootstrapClientCredentials (org.thingsboard.server.common.data.device.credentials.lwm2m.LwM2MBootstrapClientCredentials)2 DeviceCredentials (org.thingsboard.server.common.data.security.DeviceCredentials)2 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 LwM2MClientCredential (org.thingsboard.server.common.data.device.credentials.lwm2m.LwM2MClientCredential)1