Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EPMRPP-80574 #939

Merged
merged 11 commits into from
Oct 16, 2023
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,17 @@
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;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
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;
Expand All @@ -51,13 +54,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;
Expand All @@ -74,9 +81,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;
}

Expand All @@ -86,27 +93,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::loadFormDataStore);
}

private void loadOrGenerateIntegrationSalt(DataStore dataStore) {
private String loadFormDataStore() {
pbortnik marked this conversation as resolved.
Show resolved Hide resolved
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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public interface DataStore {

InputStream load(String filePath);

boolean exists(String filePath);

void delete(String filePath);

void deleteAll(List<String> filePaths, String bucketName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down