diff --git a/src/main/java/com/epam/ta/reportportal/binary/impl/AttachmentBinaryDataServiceImpl.java b/src/main/java/com/epam/ta/reportportal/binary/impl/AttachmentBinaryDataServiceImpl.java index fce4c7b96..b49b1a407 100644 --- a/src/main/java/com/epam/ta/reportportal/binary/impl/AttachmentBinaryDataServiceImpl.java +++ b/src/main/java/com/epam/ta/reportportal/binary/impl/AttachmentBinaryDataServiceImpl.java @@ -184,7 +184,8 @@ public BinaryData load(Long fileId, ReportPortalUser.ProjectDetails projectDetai ErrorType.ACCESS_DENIED, formattedSupplier("You are not assigned to project '{}'", projectDetails.getProjectName()) ); - return new BinaryData(attachment.getFileName(), attachment.getContentType(), (long) data.available(), data); + return new BinaryData( + attachment.getFileName(), attachment.getContentType(), (long) data.available(), data); } catch (IOException e) { LOGGER.error("Unable to load binary data", e); throw new ReportPortalException( diff --git a/src/main/java/com/epam/ta/reportportal/config/EncryptConfiguration.java b/src/main/java/com/epam/ta/reportportal/config/EncryptConfiguration.java index 36ff5207c..b865b6add 100644 --- a/src/main/java/com/epam/ta/reportportal/config/EncryptConfiguration.java +++ b/src/main/java/com/epam/ta/reportportal/config/EncryptConfiguration.java @@ -22,6 +22,7 @@ import com.epam.ta.reportportal.exception.ReportPortalException; import com.epam.ta.reportportal.filesystem.DataStore; import com.epam.ta.reportportal.util.FeatureFlagHandler; +import com.epam.ta.reportportal.ws.model.ErrorType; import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; @@ -30,6 +31,7 @@ import java.nio.file.Paths; import java.security.SecureRandom; import java.util.Base64; +import java.util.Optional; import org.apache.commons.io.IOUtils; import org.jasypt.encryption.pbe.StandardPBEStringEncryptor; import org.jasypt.util.text.BasicTextEncryptor; @@ -51,13 +53,17 @@ public class EncryptConfiguration implements InitializingBean { private static final Logger LOGGER = LoggerFactory.getLogger(EncryptConfiguration.class); + @Value("${rp.encryptor.password:#{null}}") + private String password; + @Value("${rp.integration.salt.path:keystore}") - private String integrationSaltPath; + private String passwordFilePath; @Value("${rp.integration.salt.file:secret-integration-salt}") - private String integrationSaltFile; + private String passwordFile; private String secretFilePath; + private final DataStore dataStore; private final FeatureFlagHandler featureFlagHandler; @@ -74,9 +80,9 @@ public EncryptConfiguration(DataStore dataStore, FeatureFlagHandler featureFlagH * @return {@link BasicTextEncryptor} instance */ @Bean(name = "basicEncryptor") - public BasicTextEncryptor getBasicEncrypt() throws IOException { + public BasicTextEncryptor getBasicEncrypt() { BasicTextEncryptor basic = new BasicTextEncryptor(); - basic.setPassword(IOUtils.toString(dataStore.load(secretFilePath), StandardCharsets.UTF_8)); + basic.setPassword(getPassword()); return basic; } @@ -86,27 +92,39 @@ public BasicTextEncryptor getBasicEncrypt() throws IOException { * @return {@link StandardPBEStringEncryptor} instance */ @Bean(name = "strongEncryptor") - public StandardPBEStringEncryptor getStrongEncryptor() throws IOException { + public StandardPBEStringEncryptor getStrongEncryptor() { StandardPBEStringEncryptor strong = new StandardPBEStringEncryptor(); - strong.setPassword(IOUtils.toString(dataStore.load(secretFilePath), StandardCharsets.UTF_8)); + strong.setPassword(getPassword()); strong.setAlgorithm("PBEWithMD5AndTripleDES"); return strong; } @Override - public void afterPropertiesSet() throws Exception { + public void afterPropertiesSet() { if (featureFlagHandler.isEnabled(FeatureFlag.SINGLE_BUCKET)) { - secretFilePath = Paths.get(INTEGRATION_SECRETS_PATH, integrationSaltFile).toString(); + secretFilePath = Paths.get(INTEGRATION_SECRETS_PATH, passwordFile).toString(); } else { - secretFilePath = integrationSaltPath + File.separator + integrationSaltFile; + secretFilePath = passwordFilePath + File.separator + passwordFile; } - loadOrGenerateIntegrationSalt(dataStore); + if (password == null) { + loadOrGenerateEncryptorPassword(); + } + } + + private String getPassword() { + return Optional.ofNullable(password).orElseGet(this::loadFromDataStore); } - private void loadOrGenerateIntegrationSalt(DataStore dataStore) { + private String loadFromDataStore() { try { - dataStore.load(secretFilePath); - } catch (ReportPortalException ex) { + return IOUtils.toString(dataStore.load(secretFilePath), StandardCharsets.UTF_8); + } catch (IOException e) { + throw new ReportPortalException(ErrorType.UNABLE_TO_LOAD_BINARY_DATA, e.getMessage()); + } + } + + private void loadOrGenerateEncryptorPassword() { + if (!dataStore.exists(secretFilePath)) { byte[] bytes = new byte[20]; new SecureRandom().nextBytes(bytes); try (InputStream secret = new ByteArrayInputStream( diff --git a/src/main/java/com/epam/ta/reportportal/filesystem/DataStore.java b/src/main/java/com/epam/ta/reportportal/filesystem/DataStore.java index 3d4b88ee0..2dbb43ef4 100644 --- a/src/main/java/com/epam/ta/reportportal/filesystem/DataStore.java +++ b/src/main/java/com/epam/ta/reportportal/filesystem/DataStore.java @@ -28,6 +28,8 @@ public interface DataStore { InputStream load(String filePath); + boolean exists(String filePath); + void delete(String filePath); void deleteAll(List filePaths, String bucketName); diff --git a/src/main/java/com/epam/ta/reportportal/filesystem/LocalDataStore.java b/src/main/java/com/epam/ta/reportportal/filesystem/LocalDataStore.java index 88e2b70a0..34bb83299 100644 --- a/src/main/java/com/epam/ta/reportportal/filesystem/LocalDataStore.java +++ b/src/main/java/com/epam/ta/reportportal/filesystem/LocalDataStore.java @@ -81,6 +81,11 @@ public InputStream load(String filePath) { } } + @Override + public boolean exists(String filePath) { + return Files.exists(Paths.get(storageRootPath, filePath)); + } + @Override public void delete(String filePath) { diff --git a/src/main/java/com/epam/ta/reportportal/filesystem/distributed/s3/S3DataStore.java b/src/main/java/com/epam/ta/reportportal/filesystem/distributed/s3/S3DataStore.java index 1ac0d896f..313e1102b 100644 --- a/src/main/java/com/epam/ta/reportportal/filesystem/distributed/s3/S3DataStore.java +++ b/src/main/java/com/epam/ta/reportportal/filesystem/distributed/s3/S3DataStore.java @@ -27,6 +27,7 @@ import java.nio.file.Paths; import java.util.List; import java.util.Objects; +import java.util.Optional; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; import org.jclouds.blobstore.BlobStore; @@ -103,17 +104,22 @@ public String save(String filePath, InputStream inputStream) { @Override public InputStream load(String filePath) { S3File s3File = getS3File(filePath); - try { - Blob fileBlob = blobStore.getBlob(s3File.getBucket(), s3File.getFilePath()); - if (fileBlob != null) { - return fileBlob.getPayload().openStream(); - } else { - throw new Exception(); + Blob fileBlob = blobStore.getBlob(s3File.getBucket(), s3File.getFilePath()); + if (fileBlob != null) { + try (InputStream inputStream = fileBlob.getPayload().openStream()) { + return inputStream; + } catch (IOException e) { + throw new ReportPortalException(ErrorType.UNABLE_TO_LOAD_BINARY_DATA, e.getMessage()); } - } catch (Exception e) { - LOGGER.error("Unable to find file '{}'", filePath, e); - throw new ReportPortalException(ErrorType.UNABLE_TO_LOAD_BINARY_DATA, "Unable to find file"); } + LOGGER.error("Unable to find file '{}'", filePath); + throw new ReportPortalException(ErrorType.UNABLE_TO_LOAD_BINARY_DATA, "Unable to find file"); + } + + @Override + public boolean exists(String filePath) { + S3File s3File = getS3File(filePath); + return blobStore.blobExists(s3File.getBucket(), s3File.getFilePath()); } @Override