448 changed files with 14771 additions and 6313 deletions
@ -0,0 +1,46 @@ |
|||
/** |
|||
* Copyright © 2016-2022 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.service.edge.rpc.constructor; |
|||
|
|||
import org.springframework.stereotype.Component; |
|||
import org.thingsboard.common.util.JacksonUtil; |
|||
import org.thingsboard.server.common.data.edge.Edge; |
|||
import org.thingsboard.server.gen.edge.v1.EdgeConfiguration; |
|||
import org.thingsboard.server.queue.util.TbCoreComponent; |
|||
|
|||
@Component |
|||
@TbCoreComponent |
|||
public class EdgeMsgConstructor { |
|||
|
|||
public EdgeConfiguration constructEdgeConfiguration(Edge edge) { |
|||
EdgeConfiguration.Builder builder = EdgeConfiguration.newBuilder() |
|||
.setEdgeIdMSB(edge.getId().getId().getMostSignificantBits()) |
|||
.setEdgeIdLSB(edge.getId().getId().getLeastSignificantBits()) |
|||
.setTenantIdMSB(edge.getTenantId().getId().getMostSignificantBits()) |
|||
.setTenantIdLSB(edge.getTenantId().getId().getLeastSignificantBits()) |
|||
.setName(edge.getName()) |
|||
.setType(edge.getType()) |
|||
.setRoutingKey(edge.getRoutingKey()) |
|||
.setSecret(edge.getSecret()) |
|||
.setAdditionalInfo(JacksonUtil.toString(edge.getAdditionalInfo())) |
|||
.setCloudType("CE"); |
|||
if (edge.getCustomerId() != null) { |
|||
builder.setCustomerIdMSB(edge.getCustomerId().getId().getMostSignificantBits()) |
|||
.setCustomerIdLSB(edge.getCustomerId().getId().getLeastSignificantBits()); |
|||
} |
|||
return builder.build(); |
|||
} |
|||
} |
|||
@ -0,0 +1,47 @@ |
|||
/** |
|||
* Copyright © 2016-2022 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.service.edge.rpc.fetch; |
|||
|
|||
import lombok.AllArgsConstructor; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.thingsboard.server.common.data.Device; |
|||
import org.thingsboard.server.common.data.EdgeUtils; |
|||
import org.thingsboard.server.common.data.edge.Edge; |
|||
import org.thingsboard.server.common.data.edge.EdgeEvent; |
|||
import org.thingsboard.server.common.data.edge.EdgeEventActionType; |
|||
import org.thingsboard.server.common.data.edge.EdgeEventType; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.common.data.page.PageData; |
|||
import org.thingsboard.server.common.data.page.PageLink; |
|||
import org.thingsboard.server.dao.device.DeviceService; |
|||
|
|||
@AllArgsConstructor |
|||
@Slf4j |
|||
public class DevicesEdgeEventFetcher extends BasePageableEdgeEventFetcher<Device> { |
|||
|
|||
private final DeviceService deviceService; |
|||
|
|||
@Override |
|||
PageData<Device> fetchPageData(TenantId tenantId, Edge edge, PageLink pageLink) { |
|||
return deviceService.findDevicesByTenantIdAndEdgeId(tenantId, edge.getId(), pageLink); |
|||
} |
|||
|
|||
@Override |
|||
EdgeEvent constructEdgeEvent(TenantId tenantId, Edge edge, Device device) { |
|||
return EdgeUtils.constructEdgeEvent(tenantId, edge.getId(), EdgeEventType.DEVICE, |
|||
EdgeEventActionType.ADDED, device.getId(), null); |
|||
} |
|||
} |
|||
@ -0,0 +1,47 @@ |
|||
/** |
|||
* Copyright © 2016-2022 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.service.edge.rpc.fetch; |
|||
|
|||
import lombok.AllArgsConstructor; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.thingsboard.server.common.data.EdgeUtils; |
|||
import org.thingsboard.server.common.data.EntityView; |
|||
import org.thingsboard.server.common.data.edge.Edge; |
|||
import org.thingsboard.server.common.data.edge.EdgeEvent; |
|||
import org.thingsboard.server.common.data.edge.EdgeEventActionType; |
|||
import org.thingsboard.server.common.data.edge.EdgeEventType; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.common.data.page.PageData; |
|||
import org.thingsboard.server.common.data.page.PageLink; |
|||
import org.thingsboard.server.dao.entityview.EntityViewService; |
|||
|
|||
@AllArgsConstructor |
|||
@Slf4j |
|||
public class EntityViewsEdgeEventFetcher extends BasePageableEdgeEventFetcher<EntityView> { |
|||
|
|||
private final EntityViewService entityViewService; |
|||
|
|||
@Override |
|||
PageData<EntityView> fetchPageData(TenantId tenantId, Edge edge, PageLink pageLink) { |
|||
return entityViewService.findEntityViewsByTenantIdAndEdgeId(tenantId, edge.getId(), pageLink); |
|||
} |
|||
|
|||
@Override |
|||
EdgeEvent constructEdgeEvent(TenantId tenantId, Edge edge, EntityView entityView) { |
|||
return EdgeUtils.constructEdgeEvent(tenantId, edge.getId(), EdgeEventType.ENTITY_VIEW, |
|||
EdgeEventActionType.ADDED, entityView.getId(), null); |
|||
} |
|||
} |
|||
@ -1,235 +0,0 @@ |
|||
/** |
|||
* Copyright © 2016-2022 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.service.edge.rpc.processor; |
|||
|
|||
import com.google.common.util.concurrent.Futures; |
|||
import com.google.common.util.concurrent.ListenableFuture; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.stereotype.Component; |
|||
import org.thingsboard.server.common.data.Device; |
|||
import org.thingsboard.server.common.data.EdgeUtils; |
|||
import org.thingsboard.server.common.data.edge.Edge; |
|||
import org.thingsboard.server.common.data.edge.EdgeEvent; |
|||
import org.thingsboard.server.common.data.edge.EdgeEventActionType; |
|||
import org.thingsboard.server.common.data.edge.EdgeEventType; |
|||
import org.thingsboard.server.common.data.id.CustomerId; |
|||
import org.thingsboard.server.common.data.id.DeviceId; |
|||
import org.thingsboard.server.common.data.id.EdgeId; |
|||
import org.thingsboard.server.common.data.id.EntityId; |
|||
import org.thingsboard.server.common.data.id.EntityIdFactory; |
|||
import org.thingsboard.server.common.data.id.RuleChainId; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.common.data.page.PageData; |
|||
import org.thingsboard.server.common.data.page.PageLink; |
|||
import org.thingsboard.server.common.data.rule.RuleChain; |
|||
import org.thingsboard.server.common.data.rule.RuleChainConnectionInfo; |
|||
import org.thingsboard.server.gen.edge.v1.DeviceCredentialsRequestMsg; |
|||
import org.thingsboard.server.gen.edge.v1.DeviceUpdateMsg; |
|||
import org.thingsboard.server.gen.edge.v1.DownlinkMsg; |
|||
import org.thingsboard.server.gen.edge.v1.UpdateMsgType; |
|||
import org.thingsboard.server.gen.transport.TransportProtos; |
|||
import org.thingsboard.server.queue.util.TbCoreComponent; |
|||
|
|||
import java.util.ArrayList; |
|||
import java.util.List; |
|||
import java.util.UUID; |
|||
|
|||
@Component |
|||
@Slf4j |
|||
@TbCoreComponent |
|||
public class EntityEdgeProcessor extends BaseEdgeProcessor { |
|||
|
|||
public DownlinkMsg processEntityMergeRequestMessageToEdge(Edge edge, EdgeEvent edgeEvent) { |
|||
DownlinkMsg downlinkMsg = null; |
|||
if (EdgeEventType.DEVICE.equals(edgeEvent.getType())) { |
|||
DeviceId deviceId = new DeviceId(edgeEvent.getEntityId()); |
|||
Device device = deviceService.findDeviceById(edge.getTenantId(), deviceId); |
|||
CustomerId customerId = getCustomerIdIfEdgeAssignedToCustomer(device, edge); |
|||
String conflictName = null; |
|||
if(edgeEvent.getBody() != null) { |
|||
conflictName = edgeEvent.getBody().get("conflictName").asText(); |
|||
} |
|||
DeviceUpdateMsg deviceUpdateMsg = deviceMsgConstructor |
|||
.constructDeviceUpdatedMsg(UpdateMsgType.ENTITY_MERGE_RPC_MESSAGE, device, customerId, conflictName); |
|||
downlinkMsg = DownlinkMsg.newBuilder() |
|||
.setDownlinkMsgId(EdgeUtils.nextPositiveInt()) |
|||
.addDeviceUpdateMsg(deviceUpdateMsg) |
|||
.build(); |
|||
} |
|||
return downlinkMsg; |
|||
} |
|||
|
|||
public DownlinkMsg processCredentialsRequestMessageToEdge(EdgeEvent edgeEvent) { |
|||
DownlinkMsg downlinkMsg = null; |
|||
if (EdgeEventType.DEVICE.equals(edgeEvent.getType())) { |
|||
DeviceId deviceId = new DeviceId(edgeEvent.getEntityId()); |
|||
DeviceCredentialsRequestMsg deviceCredentialsRequestMsg = DeviceCredentialsRequestMsg.newBuilder() |
|||
.setDeviceIdMSB(deviceId.getId().getMostSignificantBits()) |
|||
.setDeviceIdLSB(deviceId.getId().getLeastSignificantBits()) |
|||
.build(); |
|||
DownlinkMsg.Builder builder = DownlinkMsg.newBuilder() |
|||
.setDownlinkMsgId(EdgeUtils.nextPositiveInt()) |
|||
.addDeviceCredentialsRequestMsg(deviceCredentialsRequestMsg); |
|||
downlinkMsg = builder.build(); |
|||
} |
|||
return downlinkMsg; |
|||
} |
|||
|
|||
public ListenableFuture<Void> processEntityNotification(TenantId tenantId, TransportProtos.EdgeNotificationMsgProto edgeNotificationMsg) { |
|||
EdgeEventActionType actionType = EdgeEventActionType.valueOf(edgeNotificationMsg.getAction()); |
|||
EdgeEventType type = EdgeEventType.valueOf(edgeNotificationMsg.getType()); |
|||
EntityId entityId = EntityIdFactory.getByEdgeEventTypeAndUuid(type, |
|||
new UUID(edgeNotificationMsg.getEntityIdMSB(), edgeNotificationMsg.getEntityIdLSB())); |
|||
EdgeId edgeId = safeGetEdgeId(edgeNotificationMsg); |
|||
switch (actionType) { |
|||
case ADDED: // used only for USER entity
|
|||
case UPDATED: |
|||
case CREDENTIALS_UPDATED: |
|||
return pushNotificationToAllRelatedEdges(tenantId, entityId, type, actionType); |
|||
case ASSIGNED_TO_CUSTOMER: |
|||
case UNASSIGNED_FROM_CUSTOMER: |
|||
return pushNotificationToAllRelatedCustomerEdges(tenantId, edgeNotificationMsg, entityId, actionType, type); |
|||
case DELETED: |
|||
if (edgeId != null) { |
|||
return saveEdgeEvent(tenantId, edgeId, type, actionType, entityId, null); |
|||
} else { |
|||
return pushNotificationToAllRelatedEdges(tenantId, entityId, type, actionType); |
|||
} |
|||
case ASSIGNED_TO_EDGE: |
|||
case UNASSIGNED_FROM_EDGE: |
|||
ListenableFuture<Void> future = saveEdgeEvent(tenantId, edgeId, type, actionType, entityId, null); |
|||
return Futures.transformAsync(future, unused -> { |
|||
if (type.equals(EdgeEventType.RULE_CHAIN)) { |
|||
return updateDependentRuleChains(tenantId, new RuleChainId(entityId.getId()), edgeId); |
|||
} else { |
|||
return Futures.immediateFuture(null); |
|||
} |
|||
}, dbCallbackExecutorService); |
|||
default: |
|||
return Futures.immediateFuture(null); |
|||
} |
|||
} |
|||
|
|||
private ListenableFuture<Void> pushNotificationToAllRelatedCustomerEdges(TenantId tenantId, |
|||
TransportProtos.EdgeNotificationMsgProto edgeNotificationMsg, |
|||
EntityId entityId, |
|||
EdgeEventActionType actionType, |
|||
EdgeEventType type) { |
|||
PageLink pageLink = new PageLink(DEFAULT_PAGE_SIZE); |
|||
PageData<EdgeId> pageData; |
|||
List<ListenableFuture<Void>> futures = new ArrayList<>(); |
|||
do { |
|||
pageData = edgeService.findRelatedEdgeIdsByEntityId(tenantId, entityId, pageLink); |
|||
if (pageData != null && pageData.getData() != null && !pageData.getData().isEmpty()) { |
|||
for (EdgeId relatedEdgeId : pageData.getData()) { |
|||
try { |
|||
CustomerId customerId = mapper.readValue(edgeNotificationMsg.getBody(), CustomerId.class); |
|||
ListenableFuture<Edge> future = edgeService.findEdgeByIdAsync(tenantId, relatedEdgeId); |
|||
futures.add(Futures.transformAsync(future, edge -> { |
|||
if (edge != null && edge.getCustomerId() != null && |
|||
!edge.getCustomerId().isNullUid() && edge.getCustomerId().equals(customerId)) { |
|||
return saveEdgeEvent(tenantId, relatedEdgeId, type, actionType, entityId, null); |
|||
} else { |
|||
return Futures.immediateFuture(null); |
|||
} |
|||
}, dbCallbackExecutorService)); |
|||
} catch (Exception e) { |
|||
log.error("Can't parse customer id from entity body [{}]", edgeNotificationMsg, e); |
|||
return Futures.immediateFailedFuture(e); |
|||
} |
|||
} |
|||
if (pageData.hasNext()) { |
|||
pageLink = pageLink.nextPageLink(); |
|||
} |
|||
} |
|||
} while (pageData != null && pageData.hasNext()); |
|||
return Futures.transform(Futures.allAsList(futures), voids -> null, dbCallbackExecutorService); |
|||
} |
|||
|
|||
private EdgeId safeGetEdgeId(TransportProtos.EdgeNotificationMsgProto edgeNotificationMsg) { |
|||
if (edgeNotificationMsg.getEdgeIdMSB() != 0 && edgeNotificationMsg.getEdgeIdLSB() != 0) { |
|||
return new EdgeId(new UUID(edgeNotificationMsg.getEdgeIdMSB(), edgeNotificationMsg.getEdgeIdLSB())); |
|||
} else { |
|||
return null; |
|||
} |
|||
} |
|||
|
|||
private ListenableFuture<Void> pushNotificationToAllRelatedEdges(TenantId tenantId, EntityId entityId, EdgeEventType type, EdgeEventActionType actionType) { |
|||
PageLink pageLink = new PageLink(DEFAULT_PAGE_SIZE); |
|||
PageData<EdgeId> pageData; |
|||
List<ListenableFuture<Void>> futures = new ArrayList<>(); |
|||
do { |
|||
pageData = edgeService.findRelatedEdgeIdsByEntityId(tenantId, entityId, pageLink); |
|||
if (pageData != null && pageData.getData() != null && !pageData.getData().isEmpty()) { |
|||
for (EdgeId relatedEdgeId : pageData.getData()) { |
|||
futures.add(saveEdgeEvent(tenantId, relatedEdgeId, type, actionType, entityId, null)); |
|||
} |
|||
if (pageData.hasNext()) { |
|||
pageLink = pageLink.nextPageLink(); |
|||
} |
|||
} |
|||
} while (pageData != null && pageData.hasNext()); |
|||
return Futures.transform(Futures.allAsList(futures), voids -> null, dbCallbackExecutorService); |
|||
} |
|||
|
|||
private ListenableFuture<Void> updateDependentRuleChains(TenantId tenantId, RuleChainId processingRuleChainId, EdgeId edgeId) { |
|||
PageLink pageLink = new PageLink(DEFAULT_PAGE_SIZE); |
|||
PageData<RuleChain> pageData; |
|||
List<ListenableFuture<Void>> futures = new ArrayList<>(); |
|||
do { |
|||
pageData = ruleChainService.findRuleChainsByTenantIdAndEdgeId(tenantId, edgeId, pageLink); |
|||
if (pageData != null && pageData.getData() != null && !pageData.getData().isEmpty()) { |
|||
for (RuleChain ruleChain : pageData.getData()) { |
|||
if (!ruleChain.getId().equals(processingRuleChainId)) { |
|||
List<RuleChainConnectionInfo> connectionInfos = |
|||
ruleChainService.loadRuleChainMetaData(ruleChain.getTenantId(), ruleChain.getId()).getRuleChainConnections(); |
|||
if (connectionInfos != null && !connectionInfos.isEmpty()) { |
|||
for (RuleChainConnectionInfo connectionInfo : connectionInfos) { |
|||
if (connectionInfo.getTargetRuleChainId().equals(processingRuleChainId)) { |
|||
futures.add(saveEdgeEvent(tenantId, |
|||
edgeId, |
|||
EdgeEventType.RULE_CHAIN_METADATA, |
|||
EdgeEventActionType.UPDATED, |
|||
ruleChain.getId(), |
|||
null)); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
if (pageData.hasNext()) { |
|||
pageLink = pageLink.nextPageLink(); |
|||
} |
|||
} |
|||
} while (pageData != null && pageData.hasNext()); |
|||
return Futures.transform(Futures.allAsList(futures), voids -> null, dbCallbackExecutorService); |
|||
} |
|||
|
|||
public ListenableFuture<Void> processEntityNotificationForAllEdges(TenantId tenantId, TransportProtos.EdgeNotificationMsgProto edgeNotificationMsg) { |
|||
EdgeEventActionType actionType = EdgeEventActionType.valueOf(edgeNotificationMsg.getAction()); |
|||
EdgeEventType type = EdgeEventType.valueOf(edgeNotificationMsg.getType()); |
|||
EntityId entityId = EntityIdFactory.getByEdgeEventTypeAndUuid(type, new UUID(edgeNotificationMsg.getEntityIdMSB(), edgeNotificationMsg.getEntityIdLSB())); |
|||
switch (actionType) { |
|||
case ADDED: |
|||
case UPDATED: |
|||
case DELETED: |
|||
return processActionForAllEdges(tenantId, type, actionType, entityId); |
|||
default: |
|||
return Futures.immediateFuture(null); |
|||
} |
|||
} |
|||
} |
|||
|
|||
@ -0,0 +1,37 @@ |
|||
/** |
|||
* Copyright © 2016-2022 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.service.install; |
|||
|
|||
import org.springframework.context.annotation.Profile; |
|||
import org.springframework.stereotype.Service; |
|||
import org.thingsboard.server.dao.util.NoSqlAnyDaoNonCloud; |
|||
|
|||
/* |
|||
* Create keyspace for Cassandra NoSQL database for non-cloud deployment. |
|||
* For cloud service like Astra DBaas admin have to create keyspace manually on cloud UI. |
|||
* Then create tokens with database admin role and put it on Thingsboard parameters. |
|||
* Without this service cloud DB will end up with exception like |
|||
* UnauthorizedException: Missing correct permission on thingsboard |
|||
* */ |
|||
@Service |
|||
@NoSqlAnyDaoNonCloud |
|||
@Profile("install") |
|||
public class CassandraKeyspaceService extends CassandraAbstractDatabaseSchemaService |
|||
implements NoSqlKeyspaceService { |
|||
public CassandraKeyspaceService() { |
|||
super("schema-keyspace.cql"); |
|||
} |
|||
} |
|||
@ -0,0 +1,26 @@ |
|||
/** |
|||
* Copyright © 2016-2022 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.service.install; |
|||
|
|||
import org.springframework.context.annotation.Profile; |
|||
import org.springframework.stereotype.Component; |
|||
import org.thingsboard.server.service.executors.DbCallbackExecutorService; |
|||
|
|||
@Component |
|||
@Profile("install") |
|||
public class DbUpgradeExecutorService extends DbCallbackExecutorService { |
|||
|
|||
} |
|||
@ -0,0 +1,19 @@ |
|||
/** |
|||
* Copyright © 2016-2022 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.service.install; |
|||
|
|||
public interface NoSqlKeyspaceService extends DatabaseSchemaService { |
|||
} |
|||
@ -1,249 +0,0 @@ |
|||
/** |
|||
* Copyright © 2016-2022 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.service.script; |
|||
|
|||
import com.google.common.hash.Hashing; |
|||
import com.google.common.util.concurrent.Futures; |
|||
import com.google.common.util.concurrent.ListenableFuture; |
|||
import com.google.common.util.concurrent.MoreExecutors; |
|||
import lombok.Getter; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.beans.factory.annotation.Value; |
|||
import org.springframework.data.util.Pair; |
|||
import org.thingsboard.common.util.ThingsBoardThreadFactory; |
|||
import org.thingsboard.server.common.data.ApiUsageRecordKey; |
|||
import org.thingsboard.server.common.data.id.CustomerId; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.queue.usagestats.TbApiUsageClient; |
|||
import org.thingsboard.server.service.apiusage.TbApiUsageStateService; |
|||
|
|||
import java.util.Map; |
|||
import java.util.UUID; |
|||
import java.util.concurrent.ConcurrentHashMap; |
|||
import java.util.concurrent.Executors; |
|||
import java.util.concurrent.ScheduledExecutorService; |
|||
import java.util.concurrent.atomic.AtomicInteger; |
|||
|
|||
import static java.lang.String.format; |
|||
|
|||
/** |
|||
* Created by ashvayka on 26.09.18. |
|||
*/ |
|||
@Slf4j |
|||
@SuppressWarnings("UnstableApiUsage") |
|||
public abstract class AbstractJsInvokeService implements JsInvokeService { |
|||
|
|||
private final TbApiUsageStateService apiUsageStateService; |
|||
private final TbApiUsageClient apiUsageClient; |
|||
protected ScheduledExecutorService timeoutExecutorService; |
|||
|
|||
protected final Map<UUID, Pair<String, String>> scriptIdToNameAndHashMap = new ConcurrentHashMap<>(); |
|||
protected final Map<UUID, DisableListInfo> disabledFunctions = new ConcurrentHashMap<>(); |
|||
|
|||
@Getter |
|||
@Value("${js.max_total_args_size:100000}") |
|||
private long maxTotalArgsSize; |
|||
@Getter |
|||
@Value("${js.max_result_size:300000}") |
|||
private long maxResultSize; |
|||
@Getter |
|||
@Value("${js.max_script_body_size:50000}") |
|||
private long maxScriptBodySize; |
|||
|
|||
protected AbstractJsInvokeService(TbApiUsageStateService apiUsageStateService, TbApiUsageClient apiUsageClient) { |
|||
this.apiUsageStateService = apiUsageStateService; |
|||
this.apiUsageClient = apiUsageClient; |
|||
} |
|||
|
|||
public void init(long maxRequestsTimeout) { |
|||
if (maxRequestsTimeout > 0) { |
|||
timeoutExecutorService = Executors.newSingleThreadScheduledExecutor(ThingsBoardThreadFactory.forName("nashorn-js-timeout")); |
|||
} |
|||
} |
|||
|
|||
public void stop() { |
|||
if (timeoutExecutorService != null) { |
|||
timeoutExecutorService.shutdownNow(); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public ListenableFuture<UUID> eval(TenantId tenantId, JsScriptType scriptType, String scriptBody, String... argNames) { |
|||
if (apiUsageStateService.getApiUsageState(tenantId).isJsExecEnabled()) { |
|||
if (scriptBodySizeExceeded(scriptBody)) { |
|||
return error(format("Script body exceeds maximum allowed size of %s symbols", getMaxScriptBodySize())); |
|||
} |
|||
UUID scriptId = UUID.randomUUID(); |
|||
String scriptHash = hash(tenantId, scriptBody); |
|||
String functionName = constructFunctionName(scriptId, scriptHash); |
|||
String jsScript = generateJsScript(scriptType, functionName, scriptBody, argNames); |
|||
return doEval(scriptId, scriptHash, functionName, jsScript); |
|||
} else { |
|||
return error("JS Execution is disabled due to API limits!"); |
|||
} |
|||
} |
|||
|
|||
protected String constructFunctionName(UUID scriptId, String scriptHash) { |
|||
return "invokeInternal_" + scriptId.toString().replace('-', '_'); |
|||
} |
|||
|
|||
@Override |
|||
public ListenableFuture<String> invokeFunction(TenantId tenantId, CustomerId customerId, UUID scriptId, Object... args) { |
|||
if (apiUsageStateService.getApiUsageState(tenantId).isJsExecEnabled()) { |
|||
Pair<String, String> nameAndHash = scriptIdToNameAndHashMap.get(scriptId); |
|||
if (nameAndHash == null) { |
|||
return error("No compiled script found for scriptId: [" + scriptId + "]!"); |
|||
} |
|||
String functionName = nameAndHash.getFirst(); |
|||
String scriptHash = nameAndHash.getSecond(); |
|||
if (!isDisabled(scriptId)) { |
|||
if (argsSizeExceeded(args)) { |
|||
return scriptExecutionError(scriptId, format("Script input arguments exceed maximum allowed total args size of %s symbols", getMaxTotalArgsSize())); |
|||
} |
|||
apiUsageClient.report(tenantId, customerId, ApiUsageRecordKey.JS_EXEC_COUNT, 1); |
|||
return Futures.transformAsync(doInvokeFunction(scriptId, scriptHash, functionName, args), output -> { |
|||
String result = output.toString(); |
|||
if (resultSizeExceeded(result)) { |
|||
return scriptExecutionError(scriptId, format("Script invocation result exceeds maximum allowed size of %s symbols", getMaxResultSize())); |
|||
} |
|||
return Futures.immediateFuture(result); |
|||
}, MoreExecutors.directExecutor()); |
|||
} else { |
|||
String message = "Script invocation is blocked due to maximum error count " |
|||
+ getMaxErrors() + ", scriptId " + scriptId + "!"; |
|||
log.warn(message); |
|||
return error(message); |
|||
} |
|||
} else { |
|||
return error("JS Execution is disabled due to API limits!"); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public ListenableFuture<Void> release(UUID scriptId) { |
|||
Pair<String, String> nameAndHash = scriptIdToNameAndHashMap.get(scriptId); |
|||
if (nameAndHash != null) { |
|||
try { |
|||
scriptIdToNameAndHashMap.remove(scriptId); |
|||
disabledFunctions.remove(scriptId); |
|||
doRelease(scriptId, nameAndHash.getSecond(), nameAndHash.getFirst()); |
|||
} catch (Exception e) { |
|||
return Futures.immediateFailedFuture(e); |
|||
} |
|||
} |
|||
return Futures.immediateFuture(null); |
|||
} |
|||
|
|||
protected abstract ListenableFuture<UUID> doEval(UUID scriptId, String scriptHash, String functionName, String scriptBody); |
|||
|
|||
protected abstract ListenableFuture<Object> doInvokeFunction(UUID scriptId, String scriptHash, String functionName, Object[] args); |
|||
|
|||
protected abstract void doRelease(UUID scriptId, String scriptHash, String functionName) throws Exception; |
|||
|
|||
protected abstract int getMaxErrors(); |
|||
|
|||
protected abstract long getMaxBlacklistDuration(); |
|||
|
|||
protected String hash(TenantId tenantId, String scriptBody) { |
|||
return Hashing.murmur3_128().newHasher() |
|||
.putLong(tenantId.getId().getMostSignificantBits()) |
|||
.putLong(tenantId.getId().getLeastSignificantBits()) |
|||
.putUnencodedChars(scriptBody) |
|||
.hash().toString(); |
|||
} |
|||
|
|||
protected void onScriptExecutionError(UUID scriptId, Throwable t, String scriptBody) { |
|||
DisableListInfo disableListInfo = disabledFunctions.computeIfAbsent(scriptId, key -> new DisableListInfo()); |
|||
log.warn("Script has exception and will increment counter {} on disabledFunctions for id {}, exception {}, cause {}, scriptBody {}", |
|||
disableListInfo.get(), scriptId, t, t.getCause(), scriptBody); |
|||
disableListInfo.incrementAndGet(); |
|||
} |
|||
|
|||
private boolean scriptBodySizeExceeded(String scriptBody) { |
|||
if (getMaxScriptBodySize() <= 0) return false; |
|||
return scriptBody.length() > getMaxScriptBodySize(); |
|||
} |
|||
|
|||
private boolean argsSizeExceeded(Object[] args) { |
|||
if (getMaxTotalArgsSize() <= 0) return false; |
|||
long totalArgsSize = 0; |
|||
for (Object arg : args) { |
|||
if (arg instanceof CharSequence) { |
|||
totalArgsSize += ((CharSequence) arg).length(); |
|||
} |
|||
} |
|||
return totalArgsSize > getMaxTotalArgsSize(); |
|||
} |
|||
|
|||
private boolean resultSizeExceeded(String result) { |
|||
if (getMaxResultSize() <= 0) return false; |
|||
return result.length() > getMaxResultSize(); |
|||
} |
|||
|
|||
private String generateJsScript(JsScriptType scriptType, String functionName, String scriptBody, String... argNames) { |
|||
if (scriptType == JsScriptType.RULE_NODE_SCRIPT) { |
|||
return RuleNodeScriptFactory.generateRuleNodeScript(functionName, scriptBody, argNames); |
|||
} |
|||
throw new RuntimeException("No script factory implemented for scriptType: " + scriptType); |
|||
} |
|||
|
|||
private boolean isDisabled(UUID scriptId) { |
|||
DisableListInfo errorCount = disabledFunctions.get(scriptId); |
|||
if (errorCount != null) { |
|||
if (errorCount.getExpirationTime() <= System.currentTimeMillis()) { |
|||
disabledFunctions.remove(scriptId); |
|||
return false; |
|||
} else { |
|||
return errorCount.get() >= getMaxErrors(); |
|||
} |
|||
} else { |
|||
return false; |
|||
} |
|||
} |
|||
|
|||
private <T> ListenableFuture<T> error(String message) { |
|||
return Futures.immediateFailedFuture(new RuntimeException(message)); |
|||
} |
|||
|
|||
private <T> ListenableFuture<T> scriptExecutionError(UUID scriptId, String errorMsg) { |
|||
RuntimeException error = new RuntimeException(errorMsg); |
|||
onScriptExecutionError(scriptId, error, null); |
|||
return Futures.immediateFailedFuture(error); |
|||
} |
|||
|
|||
private class DisableListInfo { |
|||
private final AtomicInteger counter; |
|||
private long expirationTime; |
|||
|
|||
private DisableListInfo() { |
|||
this.counter = new AtomicInteger(0); |
|||
} |
|||
|
|||
public int get() { |
|||
return counter.get(); |
|||
} |
|||
|
|||
public int incrementAndGet() { |
|||
int result = counter.incrementAndGet(); |
|||
expirationTime = System.currentTimeMillis() + getMaxBlacklistDuration(); |
|||
return result; |
|||
} |
|||
|
|||
public long getExpirationTime() { |
|||
return expirationTime; |
|||
} |
|||
} |
|||
} |
|||
@ -1,185 +0,0 @@ |
|||
/** |
|||
* Copyright © 2016-2022 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.service.script; |
|||
|
|||
import com.google.common.util.concurrent.FutureCallback; |
|||
import com.google.common.util.concurrent.Futures; |
|||
import com.google.common.util.concurrent.ListenableFuture; |
|||
import com.google.common.util.concurrent.MoreExecutors; |
|||
import delight.nashornsandbox.NashornSandbox; |
|||
import delight.nashornsandbox.NashornSandboxes; |
|||
import lombok.Getter; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.beans.factory.annotation.Value; |
|||
import org.springframework.data.util.Pair; |
|||
import org.springframework.scheduling.annotation.Scheduled; |
|||
import org.thingsboard.common.util.ThingsBoardExecutors; |
|||
import org.thingsboard.server.queue.usagestats.TbApiUsageClient; |
|||
import org.thingsboard.server.service.apiusage.TbApiUsageStateService; |
|||
|
|||
import javax.annotation.PostConstruct; |
|||
import javax.annotation.PreDestroy; |
|||
import javax.script.Invocable; |
|||
import javax.script.ScriptEngine; |
|||
import javax.script.ScriptEngineManager; |
|||
import javax.script.ScriptException; |
|||
import java.util.UUID; |
|||
import java.util.concurrent.ExecutionException; |
|||
import java.util.concurrent.ExecutorService; |
|||
import java.util.concurrent.TimeUnit; |
|||
import java.util.concurrent.atomic.AtomicInteger; |
|||
import java.util.concurrent.locks.ReentrantLock; |
|||
|
|||
@Slf4j |
|||
public abstract class AbstractNashornJsInvokeService extends AbstractJsInvokeService { |
|||
|
|||
private NashornSandbox sandbox; |
|||
private ScriptEngine engine; |
|||
private ExecutorService monitorExecutorService; |
|||
|
|||
private final AtomicInteger jsPushedMsgs = new AtomicInteger(0); |
|||
private final AtomicInteger jsInvokeMsgs = new AtomicInteger(0); |
|||
private final AtomicInteger jsEvalMsgs = new AtomicInteger(0); |
|||
private final AtomicInteger jsFailedMsgs = new AtomicInteger(0); |
|||
private final AtomicInteger jsTimeoutMsgs = new AtomicInteger(0); |
|||
private final FutureCallback<UUID> evalCallback = new JsStatCallback<>(jsEvalMsgs, jsTimeoutMsgs, jsFailedMsgs); |
|||
private final FutureCallback<Object> invokeCallback = new JsStatCallback<>(jsInvokeMsgs, jsTimeoutMsgs, jsFailedMsgs); |
|||
|
|||
private final ReentrantLock evalLock = new ReentrantLock(); |
|||
|
|||
@Getter |
|||
private final JsExecutorService jsExecutor; |
|||
|
|||
@Value("${js.local.max_requests_timeout:0}") |
|||
private long maxRequestsTimeout; |
|||
|
|||
@Value("${js.local.stats.enabled:false}") |
|||
private boolean statsEnabled; |
|||
|
|||
public AbstractNashornJsInvokeService(TbApiUsageStateService apiUsageStateService, TbApiUsageClient apiUsageClient, JsExecutorService jsExecutor) { |
|||
super(apiUsageStateService, apiUsageClient); |
|||
this.jsExecutor = jsExecutor; |
|||
} |
|||
|
|||
@Scheduled(fixedDelayString = "${js.local.stats.print_interval_ms:10000}") |
|||
public void printStats() { |
|||
if (statsEnabled) { |
|||
int pushedMsgs = jsPushedMsgs.getAndSet(0); |
|||
int invokeMsgs = jsInvokeMsgs.getAndSet(0); |
|||
int evalMsgs = jsEvalMsgs.getAndSet(0); |
|||
int failed = jsFailedMsgs.getAndSet(0); |
|||
int timedOut = jsTimeoutMsgs.getAndSet(0); |
|||
if (pushedMsgs > 0 || invokeMsgs > 0 || evalMsgs > 0 || failed > 0 || timedOut > 0) { |
|||
log.info("Nashorn JS Invoke Stats: pushed [{}] received [{}] invoke [{}] eval [{}] failed [{}] timedOut [{}]", |
|||
pushedMsgs, invokeMsgs + evalMsgs, invokeMsgs, evalMsgs, failed, timedOut); |
|||
} |
|||
} |
|||
} |
|||
|
|||
@PostConstruct |
|||
public void init() { |
|||
super.init(maxRequestsTimeout); |
|||
if (useJsSandbox()) { |
|||
sandbox = NashornSandboxes.create(); |
|||
monitorExecutorService = ThingsBoardExecutors.newWorkStealingPool(getMonitorThreadPoolSize(), "nashorn-js-monitor"); |
|||
sandbox.setExecutor(monitorExecutorService); |
|||
sandbox.setMaxCPUTime(getMaxCpuTime()); |
|||
sandbox.allowNoBraces(false); |
|||
sandbox.allowLoadFunctions(true); |
|||
sandbox.setMaxPreparedStatements(30); |
|||
} else { |
|||
ScriptEngineManager factory = new ScriptEngineManager(); |
|||
engine = factory.getEngineByName("nashorn"); |
|||
} |
|||
} |
|||
|
|||
@PreDestroy |
|||
public void stop() { |
|||
super.stop(); |
|||
if (monitorExecutorService != null) { |
|||
monitorExecutorService.shutdownNow(); |
|||
} |
|||
} |
|||
|
|||
protected abstract boolean useJsSandbox(); |
|||
|
|||
protected abstract int getMonitorThreadPoolSize(); |
|||
|
|||
protected abstract long getMaxCpuTime(); |
|||
|
|||
@Override |
|||
protected ListenableFuture<UUID> doEval(UUID scriptId, String scriptHash, String functionName, String jsScript) { |
|||
jsPushedMsgs.incrementAndGet(); |
|||
ListenableFuture<UUID> result = jsExecutor.executeAsync(() -> { |
|||
try { |
|||
evalLock.lock(); |
|||
try { |
|||
if (useJsSandbox()) { |
|||
sandbox.eval(jsScript); |
|||
} else { |
|||
engine.eval(jsScript); |
|||
} |
|||
} finally { |
|||
evalLock.unlock(); |
|||
} |
|||
scriptIdToNameAndHashMap.put(scriptId, Pair.of(functionName, scriptHash)); |
|||
return scriptId; |
|||
} catch (Exception e) { |
|||
log.debug("Failed to compile JS script: {}", e.getMessage(), e); |
|||
throw new ExecutionException(e); |
|||
} |
|||
}); |
|||
if (maxRequestsTimeout > 0) { |
|||
result = Futures.withTimeout(result, maxRequestsTimeout, TimeUnit.MILLISECONDS, timeoutExecutorService); |
|||
} |
|||
Futures.addCallback(result, evalCallback, MoreExecutors.directExecutor()); |
|||
return result; |
|||
} |
|||
|
|||
@Override |
|||
protected ListenableFuture<Object> doInvokeFunction(UUID scriptId, String scriptHash, String functionName, Object[] args) { |
|||
jsPushedMsgs.incrementAndGet(); |
|||
ListenableFuture<Object> result = jsExecutor.executeAsync(() -> { |
|||
try { |
|||
if (useJsSandbox()) { |
|||
return sandbox.getSandboxedInvocable().invokeFunction(functionName, args); |
|||
} else { |
|||
return ((Invocable) engine).invokeFunction(functionName, args); |
|||
} |
|||
} catch (ScriptException e) { |
|||
throw new ExecutionException(e); |
|||
} catch (Exception e) { |
|||
onScriptExecutionError(scriptId, e, functionName); |
|||
throw new ExecutionException(e); |
|||
} |
|||
}); |
|||
|
|||
if (maxRequestsTimeout > 0) { |
|||
result = Futures.withTimeout(result, maxRequestsTimeout, TimeUnit.MILLISECONDS, timeoutExecutorService); |
|||
} |
|||
Futures.addCallback(result, invokeCallback, MoreExecutors.directExecutor()); |
|||
return result; |
|||
} |
|||
|
|||
protected void doRelease(UUID scriptId, String scriptHash, String functionName) throws ScriptException { |
|||
if (useJsSandbox()) { |
|||
sandbox.eval(functionName + " = undefined;"); |
|||
} else { |
|||
engine.eval(functionName + " = undefined;"); |
|||
} |
|||
} |
|||
|
|||
} |
|||
@ -1,64 +0,0 @@ |
|||
/** |
|||
* Copyright © 2016-2022 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.service.script; |
|||
|
|||
import lombok.Getter; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.beans.factory.annotation.Value; |
|||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
|||
import org.springframework.stereotype.Service; |
|||
import org.thingsboard.server.queue.usagestats.TbApiUsageClient; |
|||
import org.thingsboard.server.service.apiusage.TbApiUsageStateService; |
|||
|
|||
import java.util.concurrent.TimeUnit; |
|||
|
|||
@Slf4j |
|||
@ConditionalOnProperty(prefix = "js", value = "evaluator", havingValue = "local", matchIfMissing = true) |
|||
@Service |
|||
public class NashornJsInvokeService extends AbstractNashornJsInvokeService { |
|||
|
|||
@Value("${js.local.use_js_sandbox}") |
|||
private boolean useJsSandbox; |
|||
|
|||
@Getter |
|||
@Value("${js.local.monitor_thread_pool_size}") |
|||
private int monitorThreadPoolSize; |
|||
|
|||
@Getter |
|||
@Value("${js.local.max_cpu_time}") |
|||
private long maxCpuTime; |
|||
|
|||
@Getter |
|||
@Value("${js.local.max_errors}") |
|||
private int maxErrors; |
|||
|
|||
@Value("${js.local.max_black_list_duration_sec:60}") |
|||
private int maxBlackListDurationSec; |
|||
|
|||
public NashornJsInvokeService(TbApiUsageStateService apiUsageStateService, TbApiUsageClient apiUsageClient, JsExecutorService jsExecutor) { |
|||
super(apiUsageStateService, apiUsageClient, jsExecutor); |
|||
} |
|||
|
|||
@Override |
|||
protected boolean useJsSandbox() { |
|||
return useJsSandbox; |
|||
} |
|||
|
|||
@Override |
|||
protected long getMaxBlacklistDuration() { |
|||
return TimeUnit.SECONDS.toMillis(maxBlackListDurationSec); |
|||
} |
|||
} |
|||
@ -0,0 +1,171 @@ |
|||
/** |
|||
* Copyright © 2016-2022 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.service.script; |
|||
|
|||
import com.fasterxml.jackson.core.type.TypeReference; |
|||
import com.fasterxml.jackson.databind.JsonNode; |
|||
import com.google.common.util.concurrent.Futures; |
|||
import com.google.common.util.concurrent.ListenableFuture; |
|||
import com.google.common.util.concurrent.MoreExecutors; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.thingsboard.common.util.JacksonUtil; |
|||
import org.thingsboard.script.api.RuleNodeScriptFactory; |
|||
import org.thingsboard.script.api.mvel.MvelInvokeService; |
|||
import org.thingsboard.server.common.data.StringUtils; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.common.msg.TbMsg; |
|||
import org.thingsboard.server.common.msg.TbMsgMetaData; |
|||
|
|||
import javax.script.ScriptException; |
|||
import java.util.ArrayList; |
|||
import java.util.Collection; |
|||
import java.util.Collections; |
|||
import java.util.HashMap; |
|||
import java.util.HashSet; |
|||
import java.util.List; |
|||
import java.util.Map; |
|||
import java.util.Set; |
|||
import java.util.stream.Collectors; |
|||
|
|||
|
|||
@Slf4j |
|||
public class RuleNodeMvelScriptEngine extends RuleNodeScriptEngine<MvelInvokeService, Object> { |
|||
|
|||
public RuleNodeMvelScriptEngine(TenantId tenantId, MvelInvokeService scriptInvokeService, String script, String... argNames) { |
|||
super(tenantId, scriptInvokeService, script, argNames); |
|||
} |
|||
|
|||
@Override |
|||
protected ListenableFuture<Boolean> executeFilterTransform(Object result) { |
|||
if (result instanceof Boolean) { |
|||
return Futures.immediateFuture((Boolean) result); |
|||
} |
|||
return wrongResultType(result); |
|||
} |
|||
|
|||
@Override |
|||
protected ListenableFuture<List<TbMsg>> executeUpdateTransform(TbMsg msg, Object result) { |
|||
if (result instanceof Map) { |
|||
return Futures.immediateFuture(Collections.singletonList(unbindMsg((Map) result, msg))); |
|||
} else if (result instanceof Collection) { |
|||
List<TbMsg> res = new ArrayList<>(); |
|||
for (Object resObject : (Collection) result) { |
|||
if (resObject instanceof Map) { |
|||
res.add(unbindMsg((Map) result, msg)); |
|||
} else { |
|||
return wrongResultType(resObject); |
|||
} |
|||
} |
|||
return Futures.immediateFuture(res); |
|||
} |
|||
return wrongResultType(result); |
|||
} |
|||
|
|||
@Override |
|||
protected ListenableFuture<TbMsg> executeGenerateTransform(TbMsg prevMsg, Object result) { |
|||
if (result instanceof Map) { |
|||
return Futures.immediateFuture(unbindMsg((Map) result, prevMsg)); |
|||
} |
|||
return wrongResultType(result); |
|||
} |
|||
|
|||
@Override |
|||
protected ListenableFuture<String> executeToStringTransform(Object result) { |
|||
if (result instanceof String) { |
|||
return Futures.immediateFuture((String) result); |
|||
} else { |
|||
return Futures.immediateFuture(JacksonUtil.toString(result)); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
protected ListenableFuture<Set<String>> executeSwitchTransform(Object result) { |
|||
if (result instanceof String) { |
|||
return Futures.immediateFuture(Collections.singleton((String) result)); |
|||
} else if (result instanceof Collection) { |
|||
Set<String> res = new HashSet<>(); |
|||
for (Object resObject : (Collection) result) { |
|||
if (resObject instanceof String) { |
|||
res.add((String) resObject); |
|||
} else { |
|||
return wrongResultType(resObject); |
|||
} |
|||
} |
|||
return Futures.immediateFuture(res); |
|||
} |
|||
return wrongResultType(result); |
|||
} |
|||
|
|||
@Override |
|||
public ListenableFuture<JsonNode> executeJsonAsync(TbMsg msg) { |
|||
return Futures.transform(executeScriptAsync(msg), JacksonUtil::valueToTree, MoreExecutors.directExecutor()); |
|||
|
|||
} |
|||
|
|||
@Override |
|||
protected Object convertResult(Object result) { |
|||
return result; |
|||
} |
|||
|
|||
@Override |
|||
protected Object[] prepareArgs(TbMsg msg) { |
|||
Object[] args = new Object[3]; |
|||
if (msg.getData() != null) { |
|||
args[0] = JacksonUtil.fromString(msg.getData(), Map.class); |
|||
} else { |
|||
args[0] = new HashMap<>(); |
|||
} |
|||
args[1] = new HashMap<>(msg.getMetaData().getData()); |
|||
args[2] = msg.getType(); |
|||
return args; |
|||
} |
|||
|
|||
private static TbMsg unbindMsg(Map msgData, TbMsg msg) { |
|||
String data = null; |
|||
Map<String, String> metadata = null; |
|||
String messageType = null; |
|||
if (msgData.containsKey(RuleNodeScriptFactory.MSG)) { |
|||
data = JacksonUtil.toString(msgData.get(RuleNodeScriptFactory.MSG)); |
|||
} |
|||
if (msgData.containsKey(RuleNodeScriptFactory.METADATA)) { |
|||
Object msgMetadataObj = msgData.get(RuleNodeScriptFactory.METADATA); |
|||
if (msgMetadataObj instanceof Map) { |
|||
metadata = ((Map<?, ?>) msgMetadataObj).entrySet().stream().filter(e -> e.getValue() != null) |
|||
.collect(Collectors.toMap(e -> e.getKey().toString(), e -> e.getValue().toString())); |
|||
} else { |
|||
metadata = JacksonUtil.convertValue(msgMetadataObj, new TypeReference<>() { |
|||
}); |
|||
} |
|||
} |
|||
if (msgData.containsKey(RuleNodeScriptFactory.MSG_TYPE)) { |
|||
messageType = msgData.get(RuleNodeScriptFactory.MSG_TYPE).toString(); |
|||
} |
|||
String newData = data != null ? data : msg.getData(); |
|||
TbMsgMetaData newMetadata = metadata != null ? new TbMsgMetaData(metadata) : msg.getMetaData().copy(); |
|||
String newMessageType = !StringUtils.isEmpty(messageType) ? messageType : msg.getType(); |
|||
return TbMsg.transformMsg(msg, newMessageType, msg.getOriginator(), newMetadata, newData); |
|||
} |
|||
|
|||
private static <T> ListenableFuture<T> wrongResultType(Object result) { |
|||
String className = toClassName(result); |
|||
log.warn("Wrong result type: {}", className); |
|||
return Futures.immediateFailedFuture(new ScriptException("Wrong result type: " + className)); |
|||
} |
|||
|
|||
private static String toClassName(Object result) { |
|||
return result != null ? result.getClass().getSimpleName() : "null"; |
|||
} |
|||
} |
|||
@ -0,0 +1,133 @@ |
|||
/** |
|||
* Copyright © 2016-2022 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.service.script; |
|||
|
|||
import com.google.common.util.concurrent.Futures; |
|||
import com.google.common.util.concurrent.ListenableFuture; |
|||
import com.google.common.util.concurrent.MoreExecutors; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.thingsboard.rule.engine.api.ScriptEngine; |
|||
import org.thingsboard.script.api.ScriptInvokeService; |
|||
import org.thingsboard.script.api.ScriptType; |
|||
import org.thingsboard.server.common.data.id.CustomerId; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.common.msg.TbMsg; |
|||
|
|||
import javax.script.ScriptException; |
|||
import java.util.List; |
|||
import java.util.Set; |
|||
import java.util.UUID; |
|||
import java.util.concurrent.ExecutionException; |
|||
|
|||
|
|||
@Slf4j |
|||
public abstract class RuleNodeScriptEngine<T extends ScriptInvokeService, R> implements ScriptEngine { |
|||
|
|||
private final T scriptInvokeService; |
|||
|
|||
private final UUID scriptId; |
|||
private final TenantId tenantId; |
|||
|
|||
public RuleNodeScriptEngine(TenantId tenantId, T scriptInvokeService, String script, String... argNames) { |
|||
this.tenantId = tenantId; |
|||
this.scriptInvokeService = scriptInvokeService; |
|||
try { |
|||
this.scriptId = this.scriptInvokeService.eval(tenantId, ScriptType.RULE_NODE_SCRIPT, script, argNames).get(); |
|||
} catch (Exception e) { |
|||
Throwable t = e; |
|||
if (e instanceof ExecutionException) { |
|||
t = e.getCause(); |
|||
} |
|||
throw new IllegalArgumentException("Can't compile script: " + t.getMessage(), t); |
|||
} |
|||
} |
|||
|
|||
protected abstract Object[] prepareArgs(TbMsg msg); |
|||
|
|||
@Override |
|||
public ListenableFuture<List<TbMsg>> executeUpdateAsync(TbMsg msg) { |
|||
ListenableFuture<R> result = executeScriptAsync(msg); |
|||
return Futures.transformAsync(result, |
|||
json -> executeUpdateTransform(msg, json), |
|||
MoreExecutors.directExecutor()); |
|||
} |
|||
|
|||
protected abstract ListenableFuture<List<TbMsg>> executeUpdateTransform(TbMsg msg, R result); |
|||
|
|||
@Override |
|||
public ListenableFuture<TbMsg> executeGenerateAsync(TbMsg prevMsg) { |
|||
return Futures.transformAsync(executeScriptAsync(prevMsg), |
|||
result -> executeGenerateTransform(prevMsg, result), |
|||
MoreExecutors.directExecutor()); |
|||
} |
|||
|
|||
protected abstract ListenableFuture<TbMsg> executeGenerateTransform(TbMsg prevMsg, R result); |
|||
|
|||
@Override |
|||
public ListenableFuture<String> executeToStringAsync(TbMsg msg) { |
|||
return Futures.transformAsync(executeScriptAsync(msg), this::executeToStringTransform, MoreExecutors.directExecutor()); |
|||
} |
|||
|
|||
|
|||
@Override |
|||
public ListenableFuture<Boolean> executeFilterAsync(TbMsg msg) { |
|||
return Futures.transformAsync(executeScriptAsync(msg), |
|||
this::executeFilterTransform, |
|||
MoreExecutors.directExecutor()); |
|||
} |
|||
|
|||
protected abstract ListenableFuture<String> executeToStringTransform(R result); |
|||
|
|||
protected abstract ListenableFuture<Boolean> executeFilterTransform(R result); |
|||
|
|||
protected abstract ListenableFuture<Set<String>> executeSwitchTransform(R result); |
|||
|
|||
@Override |
|||
public ListenableFuture<Set<String>> executeSwitchAsync(TbMsg msg) { |
|||
return Futures.transformAsync(executeScriptAsync(msg), |
|||
this::executeSwitchTransform, |
|||
MoreExecutors.directExecutor()); //usually runs in a callbackExecutor
|
|||
} |
|||
|
|||
ListenableFuture<R> executeScriptAsync(TbMsg msg) { |
|||
log.trace("execute script async, msg {}", msg); |
|||
Object[] inArgs = prepareArgs(msg); |
|||
return executeScriptAsync(msg.getCustomerId(), inArgs[0], inArgs[1], inArgs[2]); |
|||
} |
|||
|
|||
ListenableFuture<R> executeScriptAsync(CustomerId customerId, Object... args) { |
|||
return Futures.transformAsync(scriptInvokeService.invokeScript(tenantId, customerId, this.scriptId, args), |
|||
o -> { |
|||
try { |
|||
return Futures.immediateFuture(convertResult(o)); |
|||
} catch (Exception e) { |
|||
if (e.getCause() instanceof ScriptException) { |
|||
return Futures.immediateFailedFuture(e.getCause()); |
|||
} else if (e.getCause() instanceof RuntimeException) { |
|||
return Futures.immediateFailedFuture(new ScriptException(e.getCause().getMessage())); |
|||
} else { |
|||
return Futures.immediateFailedFuture(new ScriptException(e)); |
|||
} |
|||
} |
|||
}, MoreExecutors.directExecutor()); |
|||
} |
|||
|
|||
public void destroy() { |
|||
scriptInvokeService.release(this.scriptId); |
|||
} |
|||
|
|||
protected abstract R convertResult(Object result); |
|||
} |
|||
@ -0,0 +1,71 @@ |
|||
/** |
|||
* Copyright © 2016-2022 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.service.security.auth; |
|||
|
|||
import io.jsonwebtoken.Claims; |
|||
import lombok.RequiredArgsConstructor; |
|||
import org.springframework.beans.factory.annotation.Qualifier; |
|||
import org.springframework.context.event.EventListener; |
|||
import org.springframework.stereotype.Service; |
|||
import org.thingsboard.server.cache.TbTransactionalCache; |
|||
import org.thingsboard.server.common.data.StringUtils; |
|||
import org.thingsboard.server.common.data.id.UserId; |
|||
import org.thingsboard.server.common.data.security.event.UserAuthDataChangedEvent; |
|||
import org.thingsboard.server.common.data.security.model.JwtToken; |
|||
import org.thingsboard.server.service.security.model.token.JwtTokenFactory; |
|||
|
|||
import java.util.Optional; |
|||
|
|||
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
|||
|
|||
@Service |
|||
public class DefaultTokenOutdatingService implements TokenOutdatingService { |
|||
|
|||
private final TbTransactionalCache<String, Long> cache; |
|||
private final JwtTokenFactory tokenFactory; |
|||
|
|||
public DefaultTokenOutdatingService(@Qualifier("UsersSessionInvalidation") TbTransactionalCache<String, Long> cache, JwtTokenFactory tokenFactory) { |
|||
this.cache = cache; |
|||
this.tokenFactory = tokenFactory; |
|||
} |
|||
|
|||
@EventListener(classes = UserAuthDataChangedEvent.class) |
|||
public void onUserAuthDataChanged(UserAuthDataChangedEvent event) { |
|||
if (StringUtils.hasText(event.getId())) { |
|||
cache.put(event.getId(), event.getTs()); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public boolean isOutdated(JwtToken token, UserId userId) { |
|||
Claims claims = tokenFactory.parseTokenClaims(token).getBody(); |
|||
long issueTime = claims.getIssuedAt().getTime(); |
|||
String sessionId = claims.get("sessionId", String.class); |
|||
if (isTokenOutdated(issueTime, userId.toString())){ |
|||
return true; |
|||
} else { |
|||
return sessionId != null && isTokenOutdated(issueTime, sessionId); |
|||
} |
|||
} |
|||
|
|||
private Boolean isTokenOutdated(long issueTime, String sessionId) { |
|||
return Optional.ofNullable(cache.get(sessionId)).map(outdatageTime -> isTokenOutdated(issueTime, outdatageTime.get())).orElse(false); |
|||
} |
|||
|
|||
private boolean isTokenOutdated(long issueTime, Long outdatageTime) { |
|||
return MILLISECONDS.toSeconds(issueTime) < MILLISECONDS.toSeconds(outdatageTime); |
|||
} |
|||
} |
|||
@ -1,38 +0,0 @@ |
|||
/** |
|||
* Copyright © 2016-2022 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.service.security.auth.jwt; |
|||
|
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.stereotype.Component; |
|||
import org.thingsboard.server.common.data.security.model.JwtToken; |
|||
import org.thingsboard.server.service.security.model.SecurityUser; |
|||
import org.thingsboard.server.service.security.model.token.JwtTokenFactory; |
|||
|
|||
@Component |
|||
public class RefreshTokenRepository { |
|||
|
|||
private final JwtTokenFactory tokenFactory; |
|||
|
|||
@Autowired |
|||
public RefreshTokenRepository(final JwtTokenFactory tokenFactory) { |
|||
this.tokenFactory = tokenFactory; |
|||
} |
|||
|
|||
public JwtToken requestRefreshToken(SecurityUser user) { |
|||
return tokenFactory.createRefreshToken(user); |
|||
} |
|||
|
|||
} |
|||
Some files were not shown because too many files changed in this diff
Loading…
Reference in new issue