|
1 | 1 | package org.zstack.core.encrypt;
|
2 | 2 |
|
3 | 3 | import org.springframework.beans.factory.annotation.Autowired;
|
| 4 | +import org.springframework.transaction.annotation.Transactional; |
4 | 5 | import org.zstack.core.Platform;
|
5 | 6 | import org.zstack.core.cloudbus.CloudBus;
|
6 | 7 | import org.zstack.core.componentloader.PluginRegistry;
|
7 |
| -import org.zstack.core.config.GlobalConfig; |
8 |
| -import org.zstack.core.config.GlobalConfigBeforeUpdateExtensionPoint; |
9 |
| -import org.zstack.core.config.GlobalConfigUpdateExtensionPoint; |
| 8 | +import org.zstack.core.config.*; |
10 | 9 | import org.zstack.core.convert.PasswordConverter;
|
11 | 10 | import org.zstack.core.db.DatabaseFacade;
|
12 | 11 | import org.zstack.core.db.SQLBatch;
|
@@ -273,6 +272,123 @@ public void updateGlobalConfig(GlobalConfig oldConfig, GlobalConfig newConfig) {
|
273 | 272 | }
|
274 | 273 | }
|
275 | 274 | });
|
| 275 | + } |
| 276 | + |
| 277 | + protected void handleNewAddedEncryptEntity() { |
| 278 | + if (PasswordEncryptType.None.toString().equals(EncryptGlobalConfig.ENABLE_PASSWORD_ENCRYPT.value())) { |
| 279 | + return; |
| 280 | + } |
| 281 | + |
| 282 | + List<EncryptEntityMetadataVO> metadataVOList = Q.New(EncryptEntityMetadataVO.class) |
| 283 | + .eq(EncryptEntityMetadataVO_.state, EncryptEntityState.NewAdded) |
| 284 | + .list(); |
| 285 | + |
| 286 | + new SQLBatch() { |
| 287 | + @Override |
| 288 | + protected void scripts() { |
| 289 | + for (EncryptEntityMetadataVO metadata : metadataVOList) { |
| 290 | + // do encrypt |
| 291 | + long count = SQL.New(String.format("select count(1) from %s", metadata.getEntityName()), Long.class).find(); |
| 292 | + metadata.setState(EncryptEntityState.Encrypting); |
| 293 | + metadata = dbf.updateAndRefresh(metadata); |
| 294 | + String className = metadata.getEntityName(); |
| 295 | + String fieldName = metadata.getColumnName(); |
| 296 | + sql(String.format("select uuid from %s", metadata.getEntityName()), String.class) |
| 297 | + .limit(1000) |
| 298 | + .paginate(count, (List<String> uuids) -> { |
| 299 | + for (String uuid : uuids) { |
| 300 | + String value = sql(String.format("select %s from %s where uuid = '%s'", fieldName, className, uuid)).find(); |
| 301 | + |
| 302 | + try { |
| 303 | + // If part of the data has been encrypted, first decrypt all the data before encrypting |
| 304 | + String decryptedString = decrypt(value); |
| 305 | + String encryptedString = encrypt(decryptedString); |
| 306 | + |
| 307 | + String sql = String.format("update %s set %s = :encrypted where uuid = :uuid", className, fieldName); |
| 308 | + |
| 309 | + Query query = dbf.getEntityManager().createQuery(sql); |
| 310 | + query.setParameter("encrypted", encryptedString); |
| 311 | + query.setParameter("uuid", uuid); |
| 312 | + query.executeUpdate(); |
| 313 | + } catch (Exception e) { |
| 314 | + logger.debug(String.format("encrypt error because : %s", e.getMessage())); |
| 315 | + } |
| 316 | + } |
| 317 | + |
| 318 | + }); |
| 319 | + metadata.setState(EncryptEntityState.Encrypted); |
| 320 | + dbf.updateAndRefresh(metadata); |
| 321 | + } |
| 322 | + } |
| 323 | + }.execute(); |
| 324 | + } |
| 325 | + |
| 326 | + private void collectEncryptEntityMetadata() { |
| 327 | + for (Field field : encryptedFields) { |
| 328 | + List<String> classNames = new ArrayList<>(); |
| 329 | + |
| 330 | + if (field.getDeclaringClass().getAnnotation(Entity.class) != null && field.getDeclaringClass().getAnnotation(Table.class) != null) { |
| 331 | + classNames.add(field.getDeclaringClass().getSimpleName()); |
| 332 | + } else { |
| 333 | + classNames.addAll(BeanUtils.reflections.getSubTypesOf(field.getDeclaringClass()).stream() |
| 334 | + .filter(aClass -> aClass.getAnnotation(Entity.class) != null && aClass.getAnnotation(Table.class) != null) |
| 335 | + .map(Class::getSimpleName) |
| 336 | + .collect(Collectors.toList())); |
| 337 | + } |
| 338 | + |
| 339 | + for (String className : classNames) { |
| 340 | + createIfNotExists(className, field.getName()); |
| 341 | + } |
| 342 | + } |
| 343 | + } |
| 344 | + |
| 345 | + private void createIfNotExists(String entity, String column) { |
| 346 | + if (Q.New(EncryptEntityMetadataVO.class) |
| 347 | + .eq(EncryptEntityMetadataVO_.entityName, entity) |
| 348 | + .eq(EncryptEntityMetadataVO_.columnName, column) |
| 349 | + .isExists()) { |
| 350 | + return; |
| 351 | + } |
| 352 | + |
| 353 | + EncryptEntityMetadataVO metadataVO = new EncryptEntityMetadataVO(); |
| 354 | + metadataVO.setColumnName(column); |
| 355 | + metadataVO.setEntityName(entity); |
| 356 | + metadataVO.setState(EncryptEntityState.NewAdded); |
| 357 | + dbf.persist(metadataVO); |
| 358 | + } |
| 359 | + |
| 360 | + public void updateEncryptDataStateIfExists(String entity, String column, EncryptEntityState state) { |
| 361 | + String sql = String.format("update EncryptEntityMetadataVO set state = :state where columnName = :columnName and entityName = :entityName"); |
| 362 | + Query query = dbf.getEntityManager().createQuery(sql); |
| 363 | + query.setParameter("state", state); |
| 364 | + query.setParameter("entityName", entity); |
| 365 | + query.setParameter("columnName", column); |
| 366 | + query.executeUpdate(); |
| 367 | + } |
| 368 | + |
| 369 | + @Transactional |
| 370 | + public void removeConvertRecoverData() { |
| 371 | + if (Q.New(EncryptEntityMetadataVO.class) |
| 372 | + .isExists()) { |
| 373 | + return; |
| 374 | + } |
| 375 | + |
| 376 | + if (PasswordEncryptType.None.toString().equals(EncryptGlobalConfig.ENABLE_PASSWORD_ENCRYPT.value())) { |
| 377 | + return; |
| 378 | + } |
| 379 | + |
| 380 | + decryptAllPassword(); |
| 381 | + encryptAllPassword(); |
| 382 | + } |
| 383 | + |
| 384 | + @Override |
| 385 | + public boolean start() { |
| 386 | + initEncryptDriver(); |
| 387 | + collectAllEncryptPassword(); |
| 388 | + installGlobalConfigUpdateHooks(); |
| 389 | + removeConvertRecoverData(); |
| 390 | + collectEncryptEntityMetadata(); |
| 391 | + handleNewAddedEncryptEntity(); |
276 | 392 |
|
277 | 393 | return true;
|
278 | 394 | }
|
|
0 commit comments