thingsboard-aplcache

Fix cassandra audit log dao.

2/22/2018 6:28:24 AM

Details

diff --git a/application/src/main/java/org/thingsboard/server/install/ThingsboardInstallConfiguration.java b/application/src/main/java/org/thingsboard/server/install/ThingsboardInstallConfiguration.java
new file mode 100644
index 0000000..d0132f8
--- /dev/null
+++ b/application/src/main/java/org/thingsboard/server/install/ThingsboardInstallConfiguration.java
@@ -0,0 +1,34 @@
+/**
+ * Copyright © 2016-2017 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.install;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.Profile;
+import org.thingsboard.server.dao.audit.AuditLogLevelFilter;
+
+import java.util.HashMap;
+
+@Configuration
+@Profile("install")
+public class ThingsboardInstallConfiguration {
+
+    @Bean
+    public AuditLogLevelFilter emptyAuditLogLevelFilter() {
+        return new AuditLogLevelFilter(new HashMap<>());
+    }
+}
diff --git a/application/src/main/resources/thingsboard.yml b/application/src/main/resources/thingsboard.yml
index 7ea68c7..f7a3a80 100644
--- a/application/src/main/resources/thingsboard.yml
+++ b/application/src/main/resources/thingsboard.yml
@@ -310,10 +310,10 @@ audit_log:
   # Allowed values: OFF (disable), W (log write operations), RW (log read and write operations)
   logging_level:
     mask:
-      "device": "W"
-      "asset": "W"
-      "dashboard": "W"
-      "customer": "W"
-      "user": "W"
-      "rule": "W"
-      "plugin": "W"
+      "device": "${AUDIT_LOG_MASK_DEVICE:W}"
+      "asset": "${AUDIT_LOG_MASK_ASSET:W}"
+      "dashboard": "${AUDIT_LOG_MASK_DASHBOARD:W}"
+      "customer": "${AUDIT_LOG_MASK_CUSTOMER:W}"
+      "user": "${AUDIT_LOG_MASK_USER:W}"
+      "rule": "${AUDIT_LOG_MASK_RULE:W}"
+      "plugin": "${AUDIT_LOG_MASK_PLUGIN:W}"
diff --git a/dao/src/main/java/org/thingsboard/server/dao/audit/CassandraAuditLogDao.java b/dao/src/main/java/org/thingsboard/server/dao/audit/CassandraAuditLogDao.java
index 085d798..fa32b5d 100644
--- a/dao/src/main/java/org/thingsboard/server/dao/audit/CassandraAuditLogDao.java
+++ b/dao/src/main/java/org/thingsboard/server/dao/audit/CassandraAuditLogDao.java
@@ -48,10 +48,7 @@ import java.time.Instant;
 import java.time.LocalDate;
 import java.time.LocalDateTime;
 import java.time.ZoneOffset;
-import java.util.Arrays;
-import java.util.List;
-import java.util.Optional;
-import java.util.UUID;
+import java.util.*;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 import java.util.stream.Collectors;
@@ -135,12 +132,7 @@ public class CassandraAuditLogDao extends CassandraAbstractSearchTimeDao<AuditLo
 
         long partition = toPartitionTs(LocalDate.now().atStartOfDay().toInstant(ZoneOffset.UTC).toEpochMilli());
         BoundStatement stmt = getSaveByTenantStmt().bind();
-        stmt = stmt.setUUID(0, auditLog.getId().getId())
-                .setUUID(1, auditLog.getTenantId().getId())
-                .setUUID(2, auditLog.getEntityId().getId())
-                .setString(3, auditLog.getEntityId().getEntityType().name())
-                .setString(4, auditLog.getActionType().name())
-                .setLong(5, partition);
+        stmt = setSaveStmtVariables(stmt, auditLog, partition);
         return getFuture(executeAsyncWrite(stmt), rs -> null);
     }
 
@@ -149,7 +141,7 @@ public class CassandraAuditLogDao extends CassandraAbstractSearchTimeDao<AuditLo
         log.debug("Save saveByTenantIdAndEntityId [{}] ", auditLog);
 
         BoundStatement stmt = getSaveByTenantIdAndEntityIdStmt().bind();
-        stmt = setSaveStmtVariables(stmt, auditLog);
+        stmt = setSaveStmtVariables(stmt, auditLog, -1);
         return getFuture(executeAsyncWrite(stmt), rs -> null);
     }
 
@@ -158,7 +150,7 @@ public class CassandraAuditLogDao extends CassandraAbstractSearchTimeDao<AuditLo
         log.debug("Save saveByTenantIdAndCustomerId [{}] ", auditLog);
 
         BoundStatement stmt = getSaveByTenantIdAndCustomerIdStmt().bind();
-        stmt = setSaveStmtVariables(stmt, auditLog);
+        stmt = setSaveStmtVariables(stmt, auditLog, -1);
         return getFuture(executeAsyncWrite(stmt), rs -> null);
     }
 
@@ -167,12 +159,12 @@ public class CassandraAuditLogDao extends CassandraAbstractSearchTimeDao<AuditLo
         log.debug("Save saveByTenantIdAndUserId [{}] ", auditLog);
 
         BoundStatement stmt = getSaveByTenantIdAndUserIdStmt().bind();
-        stmt = setSaveStmtVariables(stmt, auditLog);
+        stmt = setSaveStmtVariables(stmt, auditLog, -1);
         return getFuture(executeAsyncWrite(stmt), rs -> null);
     }
 
-    private BoundStatement setSaveStmtVariables(BoundStatement stmt, AuditLog auditLog) {
-        return stmt.setUUID(0, auditLog.getId().getId())
+    private BoundStatement setSaveStmtVariables(BoundStatement stmt, AuditLog auditLog, long partition) {
+         stmt.setUUID(0, auditLog.getId().getId())
                 .setUUID(1, auditLog.getTenantId().getId())
                 .setUUID(2, auditLog.getCustomerId().getId())
                 .setUUID(3, auditLog.getEntityId().getId())
@@ -184,6 +176,10 @@ public class CassandraAuditLogDao extends CassandraAbstractSearchTimeDao<AuditLo
                 .setString(9, auditLog.getActionData() != null ? auditLog.getActionData().toString() : null)
                 .setString(10, auditLog.getActionStatus().name())
                 .setString(11, auditLog.getActionFailureDetails());
+        if (partition > -1) {
+            stmt.setLong(12, partition);
+        }
+        return stmt;
     }
 
     @Override
@@ -198,42 +194,57 @@ public class CassandraAuditLogDao extends CassandraAbstractSearchTimeDao<AuditLo
         return getFuture(executeAsyncWrite(stmt), rs -> null);
     }
 
+    private PreparedStatement getSaveByTenantStmt() {
+        if (saveByTenantStmt == null) {
+            saveByTenantStmt = getSaveByTenantIdAndCFName(ModelConstants.AUDIT_LOG_BY_TENANT_ID_CF, true);
+        }
+        return saveByTenantStmt;
+    }
+
     private PreparedStatement getSaveByTenantIdAndEntityIdStmt() {
         if (saveByTenantIdAndEntityIdStmt == null) {
-            saveByTenantIdAndEntityIdStmt = getSaveByTenantIdAndCFName(ModelConstants.AUDIT_LOG_BY_ENTITY_ID_CF);
+            saveByTenantIdAndEntityIdStmt = getSaveByTenantIdAndCFName(ModelConstants.AUDIT_LOG_BY_ENTITY_ID_CF, false);
         }
         return saveByTenantIdAndEntityIdStmt;
     }
 
     private PreparedStatement getSaveByTenantIdAndCustomerIdStmt() {
         if (saveByTenantIdAndCustomerIdStmt == null) {
-            saveByTenantIdAndCustomerIdStmt = getSaveByTenantIdAndCFName(ModelConstants.AUDIT_LOG_BY_CUSTOMER_ID_CF);
+            saveByTenantIdAndCustomerIdStmt = getSaveByTenantIdAndCFName(ModelConstants.AUDIT_LOG_BY_CUSTOMER_ID_CF, false);
         }
         return saveByTenantIdAndCustomerIdStmt;
     }
 
     private PreparedStatement getSaveByTenantIdAndUserIdStmt() {
         if (saveByTenantIdAndUserIdStmt == null) {
-            saveByTenantIdAndUserIdStmt = getSaveByTenantIdAndCFName(ModelConstants.AUDIT_LOG_BY_USER_ID_CF);
+            saveByTenantIdAndUserIdStmt = getSaveByTenantIdAndCFName(ModelConstants.AUDIT_LOG_BY_USER_ID_CF, false);
         }
         return saveByTenantIdAndUserIdStmt;
     }
 
-    private PreparedStatement getSaveByTenantIdAndCFName(String cfName) {
-        return getSession().prepare(INSERT_INTO + cfName +
-                "(" + ModelConstants.AUDIT_LOG_ID_PROPERTY +
-                "," + ModelConstants.AUDIT_LOG_TENANT_ID_PROPERTY +
-                "," + ModelConstants.AUDIT_LOG_CUSTOMER_ID_PROPERTY +
-                "," + ModelConstants.AUDIT_LOG_ENTITY_ID_PROPERTY +
-                "," + ModelConstants.AUDIT_LOG_ENTITY_TYPE_PROPERTY +
-                "," + ModelConstants.AUDIT_LOG_ENTITY_NAME_PROPERTY +
-                "," + ModelConstants.AUDIT_LOG_USER_ID_PROPERTY +
-                "," + ModelConstants.AUDIT_LOG_USER_NAME_PROPERTY +
-                "," + ModelConstants.AUDIT_LOG_ACTION_TYPE_PROPERTY +
-                "," + ModelConstants.AUDIT_LOG_ACTION_DATA_PROPERTY +
-                "," + ModelConstants.AUDIT_LOG_ACTION_STATUS_PROPERTY +
-                "," + ModelConstants.AUDIT_LOG_ACTION_FAILURE_DETAILS_PROPERTY + ")" +
-                " VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
+    private PreparedStatement getSaveByTenantIdAndCFName(String cfName, boolean hasPartition) {
+        List columnsList = new ArrayList();
+        columnsList.add(ModelConstants.AUDIT_LOG_ID_PROPERTY);
+        columnsList.add(ModelConstants.AUDIT_LOG_TENANT_ID_PROPERTY);
+        columnsList.add(ModelConstants.AUDIT_LOG_CUSTOMER_ID_PROPERTY);
+        columnsList.add(ModelConstants.AUDIT_LOG_ENTITY_ID_PROPERTY);
+        columnsList.add(ModelConstants.AUDIT_LOG_ENTITY_TYPE_PROPERTY);
+        columnsList.add(ModelConstants.AUDIT_LOG_ENTITY_NAME_PROPERTY);
+        columnsList.add(ModelConstants.AUDIT_LOG_USER_ID_PROPERTY);
+        columnsList.add(ModelConstants.AUDIT_LOG_USER_NAME_PROPERTY);
+        columnsList.add(ModelConstants.AUDIT_LOG_ACTION_TYPE_PROPERTY);
+        columnsList.add(ModelConstants.AUDIT_LOG_ACTION_DATA_PROPERTY);
+        columnsList.add(ModelConstants.AUDIT_LOG_ACTION_STATUS_PROPERTY);
+        columnsList.add(ModelConstants.AUDIT_LOG_ACTION_FAILURE_DETAILS_PROPERTY);
+        if (hasPartition) {
+            columnsList.add(ModelConstants.AUDIT_LOG_PARTITION_PROPERTY);
+        }
+        StringJoiner values = new StringJoiner(",");
+        for (int i=0;i<columnsList.size();i++) {
+            values.add("?");
+        }
+        String statementString = INSERT_INTO + cfName + " (" + String.join(",", columnsList) + ") VALUES (" + values.toString() + ")";
+        return getSession().prepare(statementString);
     }
 
     private PreparedStatement getPartitionInsertStmt() {
@@ -246,20 +257,6 @@ public class CassandraAuditLogDao extends CassandraAbstractSearchTimeDao<AuditLo
         return partitionInsertStmt;
     }
 
-    private PreparedStatement getSaveByTenantStmt() {
-        if (saveByTenantStmt == null) {
-            saveByTenantStmt = getSession().prepare(INSERT_INTO + ModelConstants.AUDIT_LOG_BY_TENANT_ID_CF +
-                    "(" + ModelConstants.AUDIT_LOG_ID_PROPERTY +
-                    "," + ModelConstants.AUDIT_LOG_TENANT_ID_PROPERTY +
-                    "," + ModelConstants.AUDIT_LOG_ENTITY_ID_PROPERTY +
-                    "," + ModelConstants.AUDIT_LOG_ENTITY_TYPE_PROPERTY +
-                    "," + ModelConstants.AUDIT_LOG_ACTION_TYPE_PROPERTY +
-                    "," + ModelConstants.AUDIT_LOG_PARTITION_PROPERTY + ")" +
-                    " VALUES(?, ?, ?, ?, ?, ?)");
-        }
-        return saveByTenantStmt;
-    }
-
     private long toPartitionTs(long ts) {
         LocalDateTime time = LocalDateTime.ofInstant(Instant.ofEpochMilli(ts), ZoneOffset.UTC);
         return tsFormat.truncatedTo(time).toInstant(ZoneOffset.UTC).toEpochMilli();
diff --git a/dao/src/main/java/org/thingsboard/server/dao/cassandra/AbstractCassandraCluster.java b/dao/src/main/java/org/thingsboard/server/dao/cassandra/AbstractCassandraCluster.java
index f37fa93..5d6c8e6 100644
--- a/dao/src/main/java/org/thingsboard/server/dao/cassandra/AbstractCassandraCluster.java
+++ b/dao/src/main/java/org/thingsboard/server/dao/cassandra/AbstractCassandraCluster.java
@@ -20,9 +20,6 @@ import com.datastax.driver.core.*;
 import com.datastax.driver.core.ProtocolOptions.Compression;
 import com.datastax.driver.mapping.Mapper;
 import com.datastax.driver.mapping.MappingManager;
-import lombok.AccessLevel;
-import lombok.Data;
-import lombok.Getter;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -36,7 +33,6 @@ import java.util.Collections;
 import java.util.List;
 
 @Slf4j
-@Data
 public abstract class AbstractCassandraCluster {
 
     private static final String COMMA = ",";
@@ -77,7 +73,7 @@ public abstract class AbstractCassandraCluster {
     private Cluster cluster;
     private Cluster.Builder clusterBuilder;
 
-    @Getter(AccessLevel.NONE) private Session session;
+    private Session session;
 
     private MappingManager mappingManager;
 
@@ -115,6 +111,10 @@ public abstract class AbstractCassandraCluster {
         }
     }
 
+    public Cluster getCluster() {
+        return cluster;
+    }
+
     public Session getSession() {
         if (!isInstall()) {
             return session;
diff --git a/dao/src/main/java/org/thingsboard/server/dao/model/nosql/AuditLogEntity.java b/dao/src/main/java/org/thingsboard/server/dao/model/nosql/AuditLogEntity.java
index 25b6a21..4ad54f5 100644
--- a/dao/src/main/java/org/thingsboard/server/dao/model/nosql/AuditLogEntity.java
+++ b/dao/src/main/java/org/thingsboard/server/dao/model/nosql/AuditLogEntity.java
@@ -15,6 +15,7 @@
  */
 package org.thingsboard.server.dao.model.nosql;
 
+import com.datastax.driver.core.utils.UUIDs;
 import com.datastax.driver.mapping.annotations.Column;
 import com.datastax.driver.mapping.annotations.Table;
 import com.fasterxml.jackson.databind.JsonNode;
@@ -114,10 +115,11 @@ public class AuditLogEntity implements BaseEntity<AuditLog> {
     @Override
     public AuditLog toData() {
         AuditLog auditLog = new AuditLog(new AuditLogId(id));
+        auditLog.setCreatedTime(UUIDs.unixTimestamp(id));
         if (tenantId != null) {
             auditLog.setTenantId(new TenantId(tenantId));
         }
-        if (entityId != null & entityType != null) {
+        if (entityId != null && entityType != null) {
             auditLog.setEntityId(EntityIdFactory.getByTypeAndUuid(entityType, entityId));
         }
         if (customerId != null) {