Pull request #555: transactional for RED-4793 vs save

Merge in RED/persistence-service from RED-4793 to master

* commit '0dd9f76659b9da6e73466631344840b62d87e351':
  transactional for RED-4793 vs save
This commit is contained in:
Timo Bejan 2022-10-24 10:05:51 +02:00
commit 3157f3a602
2 changed files with 28 additions and 27 deletions

View File

@ -1,15 +1,14 @@
package com.iqser.red.service.persistence.management.v1.processor.service.persistence;
import javax.transaction.Transactional;
import org.springframework.stereotype.Service;
import com.iqser.red.service.persistence.management.v1.processor.entity.configuration.SMTPConfigurationEntity;
import com.iqser.red.service.persistence.management.v1.processor.exception.NotFoundException;
import com.iqser.red.service.persistence.management.v1.processor.service.EncryptionDecryptionService;
import com.iqser.red.service.persistence.management.v1.processor.service.persistence.repository.SMTPRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.transaction.Transactional;
@Slf4j
@Service
@ -18,6 +17,8 @@ public class SMTPConfigurationService {
private final SMTPRepository smtpRepository;
private final EncryptionDecryptionService encryptionDecryptionService;
@Transactional
public void deleteConfiguration() {
@ -37,4 +38,19 @@ public class SMTPConfigurationService {
return smtpRepository.save(smtpConfiguration);
}
@Transactional
public void encryptPasswordIfNecessary() {
var smtpConfiguration = getConfiguration();
try {
// this makes the migration idempotent, since an exception will be thrown if the password can't be decrypted
// this prevents double encryption if the migration runs more than once
encryptionDecryptionService.decrypt(smtpConfiguration.getPassword());
log.info("SMTP Password for id {} is already encrypted", smtpConfiguration.getId());
} catch (Exception e) {
log.info("Encrypting SMTP Password for id {}", smtpConfiguration.getId());
smtpConfiguration.setPassword(encryptionDecryptionService.encrypt(smtpConfiguration.getPassword()));
}
}
}

View File

@ -1,13 +1,12 @@
package com.iqser.red.service.peristence.v1.server.migration.migrations;
import com.iqser.red.service.peristence.v1.server.migration.Migration;
import com.iqser.red.service.persistence.management.v1.processor.service.persistence.SMTPConfigurationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.iqser.red.service.peristence.v1.server.migration.Migration;
import com.iqser.red.service.persistence.management.v1.processor.service.EncryptionDecryptionService;
import com.iqser.red.service.persistence.management.v1.processor.service.persistence.repository.SMTPRepository;
import lombok.extern.slf4j.Slf4j;
import javax.transaction.Transactional;
@Slf4j
@Service
@ -17,32 +16,18 @@ public class EncryptSMTPPasswordsMigration11 extends Migration {
private static final long VERSION = 11;
@Autowired
private SMTPRepository smtpRepository;
@Autowired
private EncryptionDecryptionService encryptionDecryptionService;
private SMTPConfigurationService smtpConfigurationService;
public EncryptSMTPPasswordsMigration11() {
super(NAME, VERSION);
}
@Override
@Transactional
protected void migrate() {
var smtpConfigurations = smtpRepository.findAll();
for (var smtpConfiguration : smtpConfigurations) {
try {
// this makes the migration idempotent, since an exception will be thrown if the password can't be decrypted
// this prevents double encryption if the migration runs more than once
encryptionDecryptionService.decrypt(smtpConfiguration.getPassword());
} catch (Exception e) {
smtpConfiguration.setPassword(encryptionDecryptionService.encrypt(smtpConfiguration.getPassword()));
smtpRepository.save(smtpConfiguration);
}
}
smtpConfigurationService.encryptPasswordIfNecessary();
}
}