Commit e639dd1fe8f2556ef9e0ec6aa786a18f1ed77073

Authored by Igor Kulikov
1 parent 9f0a8b86

Fix cassandra audit log dao.

  1 +/**
  2 + * Copyright © 2016-2017 The Thingsboard Authors
  3 + *
  4 + * Licensed under the Apache License, Version 2.0 (the "License");
  5 + * you may not use this file except in compliance with the License.
  6 + * You may obtain a copy of the License at
  7 + *
  8 + * http://www.apache.org/licenses/LICENSE-2.0
  9 + *
  10 + * Unless required by applicable law or agreed to in writing, software
  11 + * distributed under the License is distributed on an "AS IS" BASIS,
  12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 + * See the License for the specific language governing permissions and
  14 + * limitations under the License.
  15 + */
  16 +
  17 +package org.thingsboard.server.install;
  18 +
  19 +import org.springframework.context.annotation.Bean;
  20 +import org.springframework.context.annotation.Configuration;
  21 +import org.springframework.context.annotation.Profile;
  22 +import org.thingsboard.server.dao.audit.AuditLogLevelFilter;
  23 +
  24 +import java.util.HashMap;
  25 +
  26 +@Configuration
  27 +@Profile("install")
  28 +public class ThingsboardInstallConfiguration {
  29 +
  30 + @Bean
  31 + public AuditLogLevelFilter emptyAuditLogLevelFilter() {
  32 + return new AuditLogLevelFilter(new HashMap<>());
  33 + }
  34 +}
... ...
... ... @@ -310,10 +310,10 @@ audit_log:
310 310 # Allowed values: OFF (disable), W (log write operations), RW (log read and write operations)
311 311 logging_level:
312 312 mask:
313   - "device": "W"
314   - "asset": "W"
315   - "dashboard": "W"
316   - "customer": "W"
317   - "user": "W"
318   - "rule": "W"
319   - "plugin": "W"
  313 + "device": "${AUDIT_LOG_MASK_DEVICE:W}"
  314 + "asset": "${AUDIT_LOG_MASK_ASSET:W}"
  315 + "dashboard": "${AUDIT_LOG_MASK_DASHBOARD:W}"
  316 + "customer": "${AUDIT_LOG_MASK_CUSTOMER:W}"
  317 + "user": "${AUDIT_LOG_MASK_USER:W}"
  318 + "rule": "${AUDIT_LOG_MASK_RULE:W}"
  319 + "plugin": "${AUDIT_LOG_MASK_PLUGIN:W}"
... ...
... ... @@ -48,10 +48,7 @@ import java.time.Instant;
48 48 import java.time.LocalDate;
49 49 import java.time.LocalDateTime;
50 50 import java.time.ZoneOffset;
51   -import java.util.Arrays;
52   -import java.util.List;
53   -import java.util.Optional;
54   -import java.util.UUID;
  51 +import java.util.*;
55 52 import java.util.concurrent.ExecutorService;
56 53 import java.util.concurrent.Executors;
57 54 import java.util.stream.Collectors;
... ... @@ -135,12 +132,7 @@ public class CassandraAuditLogDao extends CassandraAbstractSearchTimeDao<AuditLo
135 132
136 133 long partition = toPartitionTs(LocalDate.now().atStartOfDay().toInstant(ZoneOffset.UTC).toEpochMilli());
137 134 BoundStatement stmt = getSaveByTenantStmt().bind();
138   - stmt = stmt.setUUID(0, auditLog.getId().getId())
139   - .setUUID(1, auditLog.getTenantId().getId())
140   - .setUUID(2, auditLog.getEntityId().getId())
141   - .setString(3, auditLog.getEntityId().getEntityType().name())
142   - .setString(4, auditLog.getActionType().name())
143   - .setLong(5, partition);
  135 + stmt = setSaveStmtVariables(stmt, auditLog, partition);
144 136 return getFuture(executeAsyncWrite(stmt), rs -> null);
145 137 }
146 138
... ... @@ -149,7 +141,7 @@ public class CassandraAuditLogDao extends CassandraAbstractSearchTimeDao<AuditLo
149 141 log.debug("Save saveByTenantIdAndEntityId [{}] ", auditLog);
150 142
151 143 BoundStatement stmt = getSaveByTenantIdAndEntityIdStmt().bind();
152   - stmt = setSaveStmtVariables(stmt, auditLog);
  144 + stmt = setSaveStmtVariables(stmt, auditLog, -1);
153 145 return getFuture(executeAsyncWrite(stmt), rs -> null);
154 146 }
155 147
... ... @@ -158,7 +150,7 @@ public class CassandraAuditLogDao extends CassandraAbstractSearchTimeDao<AuditLo
158 150 log.debug("Save saveByTenantIdAndCustomerId [{}] ", auditLog);
159 151
160 152 BoundStatement stmt = getSaveByTenantIdAndCustomerIdStmt().bind();
161   - stmt = setSaveStmtVariables(stmt, auditLog);
  153 + stmt = setSaveStmtVariables(stmt, auditLog, -1);
162 154 return getFuture(executeAsyncWrite(stmt), rs -> null);
163 155 }
164 156
... ... @@ -167,12 +159,12 @@ public class CassandraAuditLogDao extends CassandraAbstractSearchTimeDao<AuditLo
167 159 log.debug("Save saveByTenantIdAndUserId [{}] ", auditLog);
168 160
169 161 BoundStatement stmt = getSaveByTenantIdAndUserIdStmt().bind();
170   - stmt = setSaveStmtVariables(stmt, auditLog);
  162 + stmt = setSaveStmtVariables(stmt, auditLog, -1);
171 163 return getFuture(executeAsyncWrite(stmt), rs -> null);
172 164 }
173 165
174   - private BoundStatement setSaveStmtVariables(BoundStatement stmt, AuditLog auditLog) {
175   - return stmt.setUUID(0, auditLog.getId().getId())
  166 + private BoundStatement setSaveStmtVariables(BoundStatement stmt, AuditLog auditLog, long partition) {
  167 + stmt.setUUID(0, auditLog.getId().getId())
176 168 .setUUID(1, auditLog.getTenantId().getId())
177 169 .setUUID(2, auditLog.getCustomerId().getId())
178 170 .setUUID(3, auditLog.getEntityId().getId())
... ... @@ -184,6 +176,10 @@ public class CassandraAuditLogDao extends CassandraAbstractSearchTimeDao<AuditLo
184 176 .setString(9, auditLog.getActionData() != null ? auditLog.getActionData().toString() : null)
185 177 .setString(10, auditLog.getActionStatus().name())
186 178 .setString(11, auditLog.getActionFailureDetails());
  179 + if (partition > -1) {
  180 + stmt.setLong(12, partition);
  181 + }
  182 + return stmt;
187 183 }
188 184
189 185 @Override
... ... @@ -198,42 +194,57 @@ public class CassandraAuditLogDao extends CassandraAbstractSearchTimeDao<AuditLo
198 194 return getFuture(executeAsyncWrite(stmt), rs -> null);
199 195 }
200 196
  197 + private PreparedStatement getSaveByTenantStmt() {
  198 + if (saveByTenantStmt == null) {
  199 + saveByTenantStmt = getSaveByTenantIdAndCFName(ModelConstants.AUDIT_LOG_BY_TENANT_ID_CF, true);
  200 + }
  201 + return saveByTenantStmt;
  202 + }
  203 +
201 204 private PreparedStatement getSaveByTenantIdAndEntityIdStmt() {
202 205 if (saveByTenantIdAndEntityIdStmt == null) {
203   - saveByTenantIdAndEntityIdStmt = getSaveByTenantIdAndCFName(ModelConstants.AUDIT_LOG_BY_ENTITY_ID_CF);
  206 + saveByTenantIdAndEntityIdStmt = getSaveByTenantIdAndCFName(ModelConstants.AUDIT_LOG_BY_ENTITY_ID_CF, false);
204 207 }
205 208 return saveByTenantIdAndEntityIdStmt;
206 209 }
207 210
208 211 private PreparedStatement getSaveByTenantIdAndCustomerIdStmt() {
209 212 if (saveByTenantIdAndCustomerIdStmt == null) {
210   - saveByTenantIdAndCustomerIdStmt = getSaveByTenantIdAndCFName(ModelConstants.AUDIT_LOG_BY_CUSTOMER_ID_CF);
  213 + saveByTenantIdAndCustomerIdStmt = getSaveByTenantIdAndCFName(ModelConstants.AUDIT_LOG_BY_CUSTOMER_ID_CF, false);
211 214 }
212 215 return saveByTenantIdAndCustomerIdStmt;
213 216 }
214 217
215 218 private PreparedStatement getSaveByTenantIdAndUserIdStmt() {
216 219 if (saveByTenantIdAndUserIdStmt == null) {
217   - saveByTenantIdAndUserIdStmt = getSaveByTenantIdAndCFName(ModelConstants.AUDIT_LOG_BY_USER_ID_CF);
  220 + saveByTenantIdAndUserIdStmt = getSaveByTenantIdAndCFName(ModelConstants.AUDIT_LOG_BY_USER_ID_CF, false);
218 221 }
219 222 return saveByTenantIdAndUserIdStmt;
220 223 }
221 224
222   - private PreparedStatement getSaveByTenantIdAndCFName(String cfName) {
223   - return getSession().prepare(INSERT_INTO + cfName +
224   - "(" + ModelConstants.AUDIT_LOG_ID_PROPERTY +
225   - "," + ModelConstants.AUDIT_LOG_TENANT_ID_PROPERTY +
226   - "," + ModelConstants.AUDIT_LOG_CUSTOMER_ID_PROPERTY +
227   - "," + ModelConstants.AUDIT_LOG_ENTITY_ID_PROPERTY +
228   - "," + ModelConstants.AUDIT_LOG_ENTITY_TYPE_PROPERTY +
229   - "," + ModelConstants.AUDIT_LOG_ENTITY_NAME_PROPERTY +
230   - "," + ModelConstants.AUDIT_LOG_USER_ID_PROPERTY +
231   - "," + ModelConstants.AUDIT_LOG_USER_NAME_PROPERTY +
232   - "," + ModelConstants.AUDIT_LOG_ACTION_TYPE_PROPERTY +
233   - "," + ModelConstants.AUDIT_LOG_ACTION_DATA_PROPERTY +
234   - "," + ModelConstants.AUDIT_LOG_ACTION_STATUS_PROPERTY +
235   - "," + ModelConstants.AUDIT_LOG_ACTION_FAILURE_DETAILS_PROPERTY + ")" +
236   - " VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
  225 + private PreparedStatement getSaveByTenantIdAndCFName(String cfName, boolean hasPartition) {
  226 + List columnsList = new ArrayList();
  227 + columnsList.add(ModelConstants.AUDIT_LOG_ID_PROPERTY);
  228 + columnsList.add(ModelConstants.AUDIT_LOG_TENANT_ID_PROPERTY);
  229 + columnsList.add(ModelConstants.AUDIT_LOG_CUSTOMER_ID_PROPERTY);
  230 + columnsList.add(ModelConstants.AUDIT_LOG_ENTITY_ID_PROPERTY);
  231 + columnsList.add(ModelConstants.AUDIT_LOG_ENTITY_TYPE_PROPERTY);
  232 + columnsList.add(ModelConstants.AUDIT_LOG_ENTITY_NAME_PROPERTY);
  233 + columnsList.add(ModelConstants.AUDIT_LOG_USER_ID_PROPERTY);
  234 + columnsList.add(ModelConstants.AUDIT_LOG_USER_NAME_PROPERTY);
  235 + columnsList.add(ModelConstants.AUDIT_LOG_ACTION_TYPE_PROPERTY);
  236 + columnsList.add(ModelConstants.AUDIT_LOG_ACTION_DATA_PROPERTY);
  237 + columnsList.add(ModelConstants.AUDIT_LOG_ACTION_STATUS_PROPERTY);
  238 + columnsList.add(ModelConstants.AUDIT_LOG_ACTION_FAILURE_DETAILS_PROPERTY);
  239 + if (hasPartition) {
  240 + columnsList.add(ModelConstants.AUDIT_LOG_PARTITION_PROPERTY);
  241 + }
  242 + StringJoiner values = new StringJoiner(",");
  243 + for (int i=0;i<columnsList.size();i++) {
  244 + values.add("?");
  245 + }
  246 + String statementString = INSERT_INTO + cfName + " (" + String.join(",", columnsList) + ") VALUES (" + values.toString() + ")";
  247 + return getSession().prepare(statementString);
237 248 }
238 249
239 250 private PreparedStatement getPartitionInsertStmt() {
... ... @@ -246,20 +257,6 @@ public class CassandraAuditLogDao extends CassandraAbstractSearchTimeDao<AuditLo
246 257 return partitionInsertStmt;
247 258 }
248 259
249   - private PreparedStatement getSaveByTenantStmt() {
250   - if (saveByTenantStmt == null) {
251   - saveByTenantStmt = getSession().prepare(INSERT_INTO + ModelConstants.AUDIT_LOG_BY_TENANT_ID_CF +
252   - "(" + ModelConstants.AUDIT_LOG_ID_PROPERTY +
253   - "," + ModelConstants.AUDIT_LOG_TENANT_ID_PROPERTY +
254   - "," + ModelConstants.AUDIT_LOG_ENTITY_ID_PROPERTY +
255   - "," + ModelConstants.AUDIT_LOG_ENTITY_TYPE_PROPERTY +
256   - "," + ModelConstants.AUDIT_LOG_ACTION_TYPE_PROPERTY +
257   - "," + ModelConstants.AUDIT_LOG_PARTITION_PROPERTY + ")" +
258   - " VALUES(?, ?, ?, ?, ?, ?)");
259   - }
260   - return saveByTenantStmt;
261   - }
262   -
263 260 private long toPartitionTs(long ts) {
264 261 LocalDateTime time = LocalDateTime.ofInstant(Instant.ofEpochMilli(ts), ZoneOffset.UTC);
265 262 return tsFormat.truncatedTo(time).toInstant(ZoneOffset.UTC).toEpochMilli();
... ...
... ... @@ -20,9 +20,6 @@ import com.datastax.driver.core.*;
20 20 import com.datastax.driver.core.ProtocolOptions.Compression;
21 21 import com.datastax.driver.mapping.Mapper;
22 22 import com.datastax.driver.mapping.MappingManager;
23   -import lombok.AccessLevel;
24   -import lombok.Data;
25   -import lombok.Getter;
26 23 import lombok.extern.slf4j.Slf4j;
27 24 import org.apache.commons.lang3.StringUtils;
28 25 import org.springframework.beans.factory.annotation.Autowired;
... ... @@ -36,7 +33,6 @@ import java.util.Collections;
36 33 import java.util.List;
37 34
38 35 @Slf4j
39   -@Data
40 36 public abstract class AbstractCassandraCluster {
41 37
42 38 private static final String COMMA = ",";
... ... @@ -77,7 +73,7 @@ public abstract class AbstractCassandraCluster {
77 73 private Cluster cluster;
78 74 private Cluster.Builder clusterBuilder;
79 75
80   - @Getter(AccessLevel.NONE) private Session session;
  76 + private Session session;
81 77
82 78 private MappingManager mappingManager;
83 79
... ... @@ -115,6 +111,10 @@ public abstract class AbstractCassandraCluster {
115 111 }
116 112 }
117 113
  114 + public Cluster getCluster() {
  115 + return cluster;
  116 + }
  117 +
118 118 public Session getSession() {
119 119 if (!isInstall()) {
120 120 return session;
... ...
... ... @@ -15,6 +15,7 @@
15 15 */
16 16 package org.thingsboard.server.dao.model.nosql;
17 17
  18 +import com.datastax.driver.core.utils.UUIDs;
18 19 import com.datastax.driver.mapping.annotations.Column;
19 20 import com.datastax.driver.mapping.annotations.Table;
20 21 import com.fasterxml.jackson.databind.JsonNode;
... ... @@ -114,10 +115,11 @@ public class AuditLogEntity implements BaseEntity<AuditLog> {
114 115 @Override
115 116 public AuditLog toData() {
116 117 AuditLog auditLog = new AuditLog(new AuditLogId(id));
  118 + auditLog.setCreatedTime(UUIDs.unixTimestamp(id));
117 119 if (tenantId != null) {
118 120 auditLog.setTenantId(new TenantId(tenantId));
119 121 }
120   - if (entityId != null & entityType != null) {
  122 + if (entityId != null && entityType != null) {
121 123 auditLog.setEntityId(EntityIdFactory.getByTypeAndUuid(entityType, entityId));
122 124 }
123 125 if (customerId != null) {
... ...