|
|
@ -5,18 +5,23 @@ package org.thingsboard.server.controller; |
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.databind.JsonNode; |
|
|
import com.fasterxml.jackson.databind.JsonNode; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
|
|
import org.assertj.core.api.ThrowingConsumer; |
|
|
import org.junit.After; |
|
|
import org.junit.After; |
|
|
import org.junit.Before; |
|
|
import org.junit.Before; |
|
|
import org.junit.Test; |
|
|
import org.junit.Test; |
|
|
import org.thingsboard.common.util.JacksonUtil; |
|
|
import org.thingsboard.common.util.JacksonUtil; |
|
|
|
|
|
import org.thingsboard.server.common.data.kv.BaseAttributeKvEntry; |
|
|
|
|
|
import org.thingsboard.server.common.data.kv.StringDataEntry; |
|
|
import org.thingsboard.server.common.data.pat.ApiKey; |
|
|
import org.thingsboard.server.common.data.pat.ApiKey; |
|
|
import org.thingsboard.server.common.data.pat.ApiKeyInfo; |
|
|
import org.thingsboard.server.common.data.pat.ApiKeyInfo; |
|
|
import org.thingsboard.server.dao.service.DaoSqlTest; |
|
|
import org.thingsboard.server.dao.service.DaoSqlTest; |
|
|
import org.thingsboard.server.service.subscription.SubscriptionErrorCode; |
|
|
import org.thingsboard.server.service.subscription.SubscriptionErrorCode; |
|
|
|
|
|
import org.thingsboard.server.service.subscription.TbAttributeSubscriptionScope; |
|
|
import org.thingsboard.server.service.ws.log.cmd.LogsSubscriptionCmd; |
|
|
import org.thingsboard.server.service.ws.log.cmd.LogsSubscriptionCmd; |
|
|
|
|
|
|
|
|
import java.net.URI; |
|
|
import java.net.URI; |
|
|
import java.net.URISyntaxException; |
|
|
import java.net.URISyntaxException; |
|
|
|
|
|
import java.util.List; |
|
|
import java.util.concurrent.TimeUnit; |
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat; |
|
|
import static org.assertj.core.api.Assertions.assertThat; |
|
|
@ -51,26 +56,27 @@ public class ApiKeyWebSocketApiTest extends WebSocketApiTest { |
|
|
@Test |
|
|
@Test |
|
|
@Override |
|
|
@Override |
|
|
public void testLogsSubscribe_accessDenied() throws Exception { |
|
|
public void testLogsSubscribe_accessDenied() throws Exception { |
|
|
// the API key from setUpApiKey is bound to the tenant admin, who can read the device, so reuse won't deny.
|
|
|
withCustomerWsClient(customerWsClient -> { |
|
|
// authenticate the WS session with an API key bound to the customer user, who has no READ_TELEMETRY permission.
|
|
|
|
|
|
ApiKeyInfo customerApiKeyInfo = new ApiKeyInfo(); |
|
|
|
|
|
customerApiKeyInfo.setDescription("WS test customer API key"); |
|
|
|
|
|
customerApiKeyInfo.setEnabled(true); |
|
|
|
|
|
customerApiKeyInfo.setUserId(customerUserId); |
|
|
|
|
|
ApiKey customerApiKey = doPost("/api/apiKey", customerApiKeyInfo, ApiKey.class); |
|
|
|
|
|
|
|
|
|
|
|
TbTestWebSocketClient customerWsClient = buildAndConnectWebSocketClientWithApiKey(customerApiKey.getValue()); |
|
|
|
|
|
try { |
|
|
|
|
|
customerWsClient.send(new LogsSubscriptionCmd(1, device.getId().getEntityType().name(), |
|
|
customerWsClient.send(new LogsSubscriptionCmd(1, device.getId().getEntityType().name(), |
|
|
device.getId().getId().toString(), 0)); |
|
|
device.getId().getId().toString(), 0)); |
|
|
|
|
|
|
|
|
JsonNode error = JacksonUtil.toJsonNode(customerWsClient.waitForReply(true)); |
|
|
JsonNode error = JacksonUtil.toJsonNode(customerWsClient.waitForReply(true)); |
|
|
assertThat(error.get("errorCode").asInt()).isEqualTo(SubscriptionErrorCode.ACCESS_DENIED.getCode()); |
|
|
assertThat(error.get("errorCode").asInt()).isEqualTo(SubscriptionErrorCode.ACCESS_DENIED.getCode()); |
|
|
} finally { |
|
|
}); |
|
|
customerWsClient.close(); |
|
|
} |
|
|
loginTenantAdmin(); |
|
|
|
|
|
doDelete("/api/apiKey/" + customerApiKey.getId()).andExpect(status().isOk()); |
|
|
@Test |
|
|
} |
|
|
@Override |
|
|
|
|
|
public void testAttributesSubscription_customerUserNotAssignedDevice() throws Exception { |
|
|
|
|
|
sendAttributes(device, TbAttributeSubscriptionScope.SERVER_SCOPE, List.of( |
|
|
|
|
|
new BaseAttributeKvEntry(System.currentTimeMillis(), new StringDataEntry("attr", "secret")) |
|
|
|
|
|
)); |
|
|
|
|
|
|
|
|
|
|
|
withCustomerWsClient(customerWsClient -> { |
|
|
|
|
|
JsonNode update = customerWsClient.subscribeForAttributes(device.getId(), TbAttributeSubscriptionScope.SERVER_SCOPE.name(), List.of("attr")); |
|
|
|
|
|
assertThat(update.get("errorCode").asInt()).isNotEqualTo(SubscriptionErrorCode.NO_ERROR.getCode()); |
|
|
|
|
|
assertThat(update.path("data").isEmpty()).isTrue(); |
|
|
|
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Test |
|
|
@Test |
|
|
@ -85,4 +91,22 @@ public class ApiKeyWebSocketApiTest extends WebSocketApiTest { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// the API key from setUpApiKey is bound to the tenant admin, so access-denied cases need a key bound to the customer user
|
|
|
|
|
|
private void withCustomerWsClient(ThrowingConsumer<TbTestWebSocketClient> action) throws Exception { |
|
|
|
|
|
ApiKeyInfo customerApiKeyInfo = new ApiKeyInfo(); |
|
|
|
|
|
customerApiKeyInfo.setDescription("WS test customer API key"); |
|
|
|
|
|
customerApiKeyInfo.setEnabled(true); |
|
|
|
|
|
customerApiKeyInfo.setUserId(customerUserId); |
|
|
|
|
|
ApiKey customerApiKey = doPost("/api/apiKey", customerApiKeyInfo, ApiKey.class); |
|
|
|
|
|
|
|
|
|
|
|
TbTestWebSocketClient customerWsClient = buildAndConnectWebSocketClientWithApiKey(customerApiKey.getValue()); |
|
|
|
|
|
try { |
|
|
|
|
|
action.accept(customerWsClient); |
|
|
|
|
|
} finally { |
|
|
|
|
|
customerWsClient.close(); |
|
|
|
|
|
loginTenantAdmin(); |
|
|
|
|
|
doDelete("/api/apiKey/" + customerApiKey.getId()).andExpect(status().isOk()); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|