|
|
|
@ -15,11 +15,9 @@ |
|
|
|
*/ |
|
|
|
package org.thingsboard.server.dao.audit; |
|
|
|
|
|
|
|
import com.datastax.oss.driver.api.core.uuid.Uuids; |
|
|
|
import com.fasterxml.jackson.databind.JsonNode; |
|
|
|
import com.fasterxml.jackson.databind.node.ArrayNode; |
|
|
|
import com.fasterxml.jackson.databind.node.ObjectNode; |
|
|
|
import com.google.common.collect.Lists; |
|
|
|
import com.google.common.util.concurrent.Futures; |
|
|
|
import com.google.common.util.concurrent.ListenableFuture; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
@ -34,7 +32,6 @@ import org.thingsboard.server.common.data.alarm.AlarmComment; |
|
|
|
import org.thingsboard.server.common.data.audit.ActionStatus; |
|
|
|
import org.thingsboard.server.common.data.audit.ActionType; |
|
|
|
import org.thingsboard.server.common.data.audit.AuditLog; |
|
|
|
import org.thingsboard.server.common.data.id.AuditLogId; |
|
|
|
import org.thingsboard.server.common.data.id.CustomerId; |
|
|
|
import org.thingsboard.server.common.data.id.EntityId; |
|
|
|
import org.thingsboard.server.common.data.id.TenantId; |
|
|
|
@ -50,11 +47,11 @@ import org.thingsboard.server.dao.audit.sink.AuditLogSink; |
|
|
|
import org.thingsboard.server.dao.device.provision.ProvisionRequest; |
|
|
|
import org.thingsboard.server.dao.entity.EntityService; |
|
|
|
import org.thingsboard.server.dao.service.DataValidator; |
|
|
|
import org.thingsboard.server.dao.sql.JpaExecutorService; |
|
|
|
|
|
|
|
import java.io.PrintWriter; |
|
|
|
import java.io.StringWriter; |
|
|
|
import java.util.List; |
|
|
|
import java.util.UUID; |
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
import static org.thingsboard.server.dao.service.Validator.validateEntityId; |
|
|
|
@ -66,7 +63,6 @@ import static org.thingsboard.server.dao.service.Validator.validateId; |
|
|
|
public class AuditLogServiceImpl implements AuditLogService { |
|
|
|
|
|
|
|
private static final String INCORRECT_TENANT_ID = "Incorrect tenantId "; |
|
|
|
private static final int INSERTS_PER_ENTRY = 3; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private AuditLogLevelFilter auditLogLevelFilter; |
|
|
|
@ -80,6 +76,9 @@ public class AuditLogServiceImpl implements AuditLogService { |
|
|
|
@Autowired |
|
|
|
private AuditLogSink auditLogSink; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private JpaExecutorService executor; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private DataValidator<AuditLog> auditLogValidator; |
|
|
|
|
|
|
|
@ -115,7 +114,7 @@ public class AuditLogServiceImpl implements AuditLogService { |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public <E extends HasName, I extends EntityId> ListenableFuture<List<Void>> |
|
|
|
public <E extends HasName, I extends EntityId> ListenableFuture<Void> |
|
|
|
logEntityAction(TenantId tenantId, CustomerId customerId, UserId userId, String userName, I entityId, E entity, |
|
|
|
ActionType actionType, Exception e, Object... additionalInfo) { |
|
|
|
if (canLog(entityId.getEntityType(), actionType)) { |
|
|
|
@ -370,9 +369,6 @@ public class AuditLogServiceImpl implements AuditLogService { |
|
|
|
ActionStatus actionStatus, |
|
|
|
String actionFailureDetails) { |
|
|
|
AuditLog result = new AuditLog(); |
|
|
|
UUID id = Uuids.timeBased(); |
|
|
|
result.setId(new AuditLogId(id)); |
|
|
|
result.setCreatedTime(Uuids.unixTimestamp(id)); |
|
|
|
result.setTenantId(tenantId); |
|
|
|
result.setEntityId(entityId); |
|
|
|
result.setEntityName(entityName); |
|
|
|
@ -386,16 +382,16 @@ public class AuditLogServiceImpl implements AuditLogService { |
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
private ListenableFuture<List<Void>> logAction(TenantId tenantId, |
|
|
|
EntityId entityId, |
|
|
|
String entityName, |
|
|
|
CustomerId customerId, |
|
|
|
UserId userId, |
|
|
|
String userName, |
|
|
|
ActionType actionType, |
|
|
|
JsonNode actionData, |
|
|
|
ActionStatus actionStatus, |
|
|
|
String actionFailureDetails) { |
|
|
|
private ListenableFuture<Void> logAction(TenantId tenantId, |
|
|
|
EntityId entityId, |
|
|
|
String entityName, |
|
|
|
CustomerId customerId, |
|
|
|
UserId userId, |
|
|
|
String userName, |
|
|
|
ActionType actionType, |
|
|
|
JsonNode actionData, |
|
|
|
ActionStatus actionStatus, |
|
|
|
String actionFailureDetails) { |
|
|
|
AuditLog auditLogEntry = createAuditLogEntry(tenantId, entityId, entityName, customerId, userId, userName, |
|
|
|
actionType, actionData, actionStatus, actionFailureDetails); |
|
|
|
log.trace("Executing logAction [{}]", auditLogEntry); |
|
|
|
@ -408,12 +404,12 @@ public class AuditLogServiceImpl implements AuditLogService { |
|
|
|
return Futures.immediateFailedFuture(e); |
|
|
|
} |
|
|
|
} |
|
|
|
List<ListenableFuture<Void>> futures = Lists.newArrayListWithExpectedSize(INSERTS_PER_ENTRY); |
|
|
|
futures.add(auditLogDao.saveByTenantId(auditLogEntry)); |
|
|
|
|
|
|
|
auditLogSink.logAction(auditLogEntry); |
|
|
|
|
|
|
|
return Futures.allAsList(futures); |
|
|
|
return executor.submit(() -> { |
|
|
|
AuditLog auditLog = auditLogDao.save(tenantId, auditLogEntry); |
|
|
|
auditLogSink.logAction(auditLog); |
|
|
|
return null; |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|