Search in sources :

Example 1 with DeviceCredentialsFilter

use of org.thingsboard.server.common.data.security.DeviceCredentialsFilter in project thingsboard by thingsboard.

the class DefaultActorServiceTest method testBasicPostWithSyncSession.

@Test
public void testBasicPostWithSyncSession() throws Exception {
    SessionContext ssnCtx = mock(SessionContext.class);
    KvEntry entry1 = new StringDataEntry("key1", "value1");
    KvEntry entry2 = new StringDataEntry("key2", "value2");
    BasicTelemetryUploadRequest telemetry = new BasicTelemetryUploadRequest();
    long ts = 42;
    telemetry.add(ts, entry1);
    telemetry.add(ts, entry2);
    BasicAdaptorToSessionActorMsg msg = new BasicAdaptorToSessionActorMsg(ssnCtx, telemetry);
    DeviceId deviceId = new DeviceId(UUID.randomUUID());
    DeviceCredentialsFilter filter = new DeviceTokenCredentials("token1");
    Device device = mock(Device.class);
    when(device.getId()).thenReturn(deviceId);
    when(device.getTenantId()).thenReturn(tenantId);
    when(ssnCtx.getSessionId()).thenReturn(new DummySessionID("session1"));
    when(ssnCtx.getSessionType()).thenReturn(SessionType.SYNC);
    when(deviceAuthService.process(filter)).thenReturn(DeviceAuthResult.of(deviceId));
    when(deviceService.findDeviceById(deviceId)).thenReturn(device);
    ObjectMapper ruleMapper = new ObjectMapper();
    when(ruleMock.getFilters()).thenReturn(ruleMapper.readTree(FILTERS_CONFIGURATION));
    when(ruleMock.getAction()).thenReturn(ruleMapper.readTree(ACTION_CONFIGURATION));
    ComponentDescriptor filterComp = new ComponentDescriptor();
    filterComp.setClazz("org.thingsboard.server.extensions.core.filter.MsgTypeFilter");
    filterComp.setType(ComponentType.FILTER);
    when(componentService.getComponent("org.thingsboard.server.extensions.core.filter.MsgTypeFilter")).thenReturn(Optional.of(filterComp));
    ComponentDescriptor actionComp = new ComponentDescriptor();
    actionComp.setClazz("org.thingsboard.server.extensions.core.action.telemetry.TelemetryPluginAction");
    actionComp.setType(ComponentType.ACTION);
    when(componentService.getComponent("org.thingsboard.server.extensions.core.action.telemetry.TelemetryPluginAction")).thenReturn(Optional.of(actionComp));
    ObjectMapper pluginMapper = new ObjectMapper();
    JsonNode pluginAdditionalInfo = pluginMapper.readTree(PLUGIN_CONFIGURATION);
    when(pluginMock.getConfiguration()).thenReturn(pluginAdditionalInfo);
    when(pluginMock.getClazz()).thenReturn(TelemetryStoragePlugin.class.getName());
    when(attributesService.findAll(deviceId, DataConstants.CLIENT_SCOPE)).thenReturn(Futures.immediateFuture(Collections.emptyList()));
    when(attributesService.findAll(deviceId, DataConstants.SHARED_SCOPE)).thenReturn(Futures.immediateFuture(Collections.emptyList()));
    when(attributesService.findAll(deviceId, DataConstants.SERVER_SCOPE)).thenReturn(Futures.immediateFuture(Collections.emptyList()));
    initActorSystem();
    Thread.sleep(1000);
    actorService.process(new BasicToDeviceActorSessionMsg(device, msg));
    // Check that device data was saved to DB;
    List<TsKvEntry> expected = new ArrayList<>();
    expected.add(new BasicTsKvEntry(ts, entry1));
    expected.add(new BasicTsKvEntry(ts, entry2));
    verify(tsService, Mockito.timeout(5000)).save(deviceId, expected, 0L);
}
Also used : BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) Device(org.thingsboard.server.common.data.Device) ComponentDescriptor(org.thingsboard.server.common.data.plugin.ComponentDescriptor) BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) KvEntry(org.thingsboard.server.common.data.kv.KvEntry) TelemetryStoragePlugin(org.thingsboard.server.extensions.core.plugin.telemetry.TelemetryStoragePlugin) JsonNode(com.fasterxml.jackson.databind.JsonNode) DeviceCredentialsFilter(org.thingsboard.server.common.data.security.DeviceCredentialsFilter) StringDataEntry(org.thingsboard.server.common.data.kv.StringDataEntry) BasicTelemetryUploadRequest(org.thingsboard.server.common.msg.core.BasicTelemetryUploadRequest) DeviceTokenCredentials(org.thingsboard.server.common.data.security.DeviceTokenCredentials) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 2 with DeviceCredentialsFilter

use of org.thingsboard.server.common.data.security.DeviceCredentialsFilter in project thingsboard by thingsboard.

the class CoapTransportResource method decodeCredentials.

private Optional<DeviceCredentialsFilter> decodeCredentials(Request request) {
    List<String> uriPath = request.getOptions().getUriPath();
    DeviceCredentialsFilter credentials = null;
    if (uriPath.size() >= ACCESS_TOKEN_POSITION) {
        credentials = new DeviceTokenCredentials(uriPath.get(ACCESS_TOKEN_POSITION - 1));
    }
    return Optional.ofNullable(credentials);
}
Also used : DeviceCredentialsFilter(org.thingsboard.server.common.data.security.DeviceCredentialsFilter) DeviceTokenCredentials(org.thingsboard.server.common.data.security.DeviceTokenCredentials)

Example 3 with DeviceCredentialsFilter

use of org.thingsboard.server.common.data.security.DeviceCredentialsFilter in project thingsboard by thingsboard.

the class CoapTransportResource method processRequest.

private Optional<SessionId> processRequest(CoapExchange exchange, MsgType type) {
    log.trace("Processing {}", exchange.advanced().getRequest());
    exchange.accept();
    Exchange advanced = exchange.advanced();
    Request request = advanced.getRequest();
    Optional<DeviceCredentialsFilter> credentials = decodeCredentials(request);
    if (!credentials.isPresent()) {
        exchange.respond(ResponseCode.BAD_REQUEST);
        return Optional.empty();
    }
    CoapSessionCtx ctx = new CoapSessionCtx(exchange, adaptor, processor, authService, timeout);
    if (!ctx.login(credentials.get())) {
        exchange.respond(ResponseCode.UNAUTHORIZED);
        return Optional.empty();
    }
    AdaptorToSessionActorMsg msg;
    try {
        switch(type) {
            case GET_ATTRIBUTES_REQUEST:
            case POST_ATTRIBUTES_REQUEST:
            case POST_TELEMETRY_REQUEST:
            case TO_DEVICE_RPC_RESPONSE:
            case TO_SERVER_RPC_REQUEST:
                ctx.setSessionType(SessionType.SYNC);
                msg = adaptor.convertToActorMsg(ctx, type, request);
                break;
            case SUBSCRIBE_ATTRIBUTES_REQUEST:
            case SUBSCRIBE_RPC_COMMANDS_REQUEST:
                ExchangeObserver systemObserver = (ExchangeObserver) observerField.get(advanced);
                advanced.setObserver(new CoapExchangeObserverProxy(systemObserver, ctx));
                ctx.setSessionType(SessionType.ASYNC);
                msg = adaptor.convertToActorMsg(ctx, type, request);
                break;
            case UNSUBSCRIBE_ATTRIBUTES_REQUEST:
            case UNSUBSCRIBE_RPC_COMMANDS_REQUEST:
                ctx.setSessionType(SessionType.ASYNC);
                msg = adaptor.convertToActorMsg(ctx, type, request);
                break;
            default:
                log.trace("[{}] Unsupported msg type: {}", ctx.getSessionId(), type);
                throw new IllegalArgumentException("Unsupported msg type: " + type);
        }
        log.trace("Processing msg: {}", msg);
        processor.process(new BasicToDeviceActorSessionMsg(ctx.getDevice(), msg));
    } catch (AdaptorException e) {
        log.debug("Failed to decode payload {}", e);
        exchange.respond(ResponseCode.BAD_REQUEST, e.getMessage());
        return Optional.empty();
    } catch (IllegalArgumentException | IllegalAccessException e) {
        log.debug("Failed to process payload {}", e);
        exchange.respond(ResponseCode.INTERNAL_SERVER_ERROR, e.getMessage());
    }
    return Optional.of(ctx.getSessionId());
}
Also used : Request(org.eclipse.californium.core.coap.Request) CoapExchangeObserverProxy(org.thingsboard.server.transport.coap.session.CoapExchangeObserverProxy) CoapExchange(org.eclipse.californium.core.server.resources.CoapExchange) Exchange(org.eclipse.californium.core.network.Exchange) DeviceCredentialsFilter(org.thingsboard.server.common.data.security.DeviceCredentialsFilter) AdaptorException(org.thingsboard.server.common.transport.adaptor.AdaptorException) CoapSessionCtx(org.thingsboard.server.transport.coap.session.CoapSessionCtx) ExchangeObserver(org.eclipse.californium.core.network.ExchangeObserver)

Aggregations

DeviceCredentialsFilter (org.thingsboard.server.common.data.security.DeviceCredentialsFilter)3 DeviceTokenCredentials (org.thingsboard.server.common.data.security.DeviceTokenCredentials)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Request (org.eclipse.californium.core.coap.Request)1 Exchange (org.eclipse.californium.core.network.Exchange)1 ExchangeObserver (org.eclipse.californium.core.network.ExchangeObserver)1 CoapExchange (org.eclipse.californium.core.server.resources.CoapExchange)1 Test (org.junit.Test)1 Device (org.thingsboard.server.common.data.Device)1 BasicTsKvEntry (org.thingsboard.server.common.data.kv.BasicTsKvEntry)1 KvEntry (org.thingsboard.server.common.data.kv.KvEntry)1 StringDataEntry (org.thingsboard.server.common.data.kv.StringDataEntry)1 TsKvEntry (org.thingsboard.server.common.data.kv.TsKvEntry)1 ComponentDescriptor (org.thingsboard.server.common.data.plugin.ComponentDescriptor)1 BasicTelemetryUploadRequest (org.thingsboard.server.common.msg.core.BasicTelemetryUploadRequest)1 AdaptorException (org.thingsboard.server.common.transport.adaptor.AdaptorException)1 TelemetryStoragePlugin (org.thingsboard.server.extensions.core.plugin.telemetry.TelemetryStoragePlugin)1 CoapExchangeObserverProxy (org.thingsboard.server.transport.coap.session.CoapExchangeObserverProxy)1 CoapSessionCtx (org.thingsboard.server.transport.coap.session.CoapSessionCtx)1