mirror of
https://github.com/scm-manager/scm-manager.git
synced 2026-03-06 04:10:52 +01:00
Decrypt import only if password is given
This commit is contained in:
@@ -328,7 +328,9 @@ public class RepositoryImportResource {
|
||||
Map<String, List<InputPart>> formParts = input.getFormDataMap();
|
||||
InputStream inputStream = extractInputStream(formParts);
|
||||
RepositoryImportFromFileDto repositoryDto = extractRepositoryDto(formParts);
|
||||
inputStream = decryptInputStream(inputStream, repositoryDto.getPassword());
|
||||
if (!Strings.isNullOrEmpty(repositoryDto.getPassword())) {
|
||||
inputStream = decryptInputStream(inputStream, repositoryDto.getPassword());
|
||||
}
|
||||
|
||||
Type t = type(manager, type);
|
||||
checkSupport(t, Command.UNBUNDLE);
|
||||
|
||||
@@ -24,6 +24,7 @@
|
||||
|
||||
package sonia.scm.importexport;
|
||||
|
||||
import com.google.common.base.Strings;
|
||||
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
|
||||
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
|
||||
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
|
||||
@@ -68,7 +69,7 @@ public class FullScmRepositoryImporter {
|
||||
if (inputStream.available() > 0) {
|
||||
try (
|
||||
BufferedInputStream bif = new BufferedInputStream(inputStream);
|
||||
InputStream cif = repositoryImportExportEncryption.decrypt(bif, password);
|
||||
InputStream cif = decryptIfPasswordSet(bif, password);
|
||||
GzipCompressorInputStream gcis = new GzipCompressorInputStream(cif);
|
||||
TarArchiveInputStream tais = createTarInputStream(gcis)
|
||||
) {
|
||||
@@ -89,6 +90,14 @@ public class FullScmRepositoryImporter {
|
||||
}
|
||||
}
|
||||
|
||||
private InputStream decryptIfPasswordSet(InputStream potentiallyEncryptedStream, String password) throws IOException {
|
||||
if (Strings.isNullOrEmpty(password)) {
|
||||
return potentiallyEncryptedStream;
|
||||
} else {
|
||||
return repositoryImportExportEncryption.decrypt(potentiallyEncryptedStream, password);
|
||||
}
|
||||
}
|
||||
|
||||
private Repository run(Repository repository, TarArchiveInputStream tais) throws IOException {
|
||||
ImportState state = new ImportState(repositoryManager.create(repository));
|
||||
try {
|
||||
|
||||
@@ -112,10 +112,9 @@ class FullScmRepositoryImporterTest {
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void initRepositoryService() throws IOException {
|
||||
void initRepositoryService() {
|
||||
lenient().when(serviceFactory.create(REPOSITORY)).thenReturn(service);
|
||||
lenient().when(service.getUnbundleCommand()).thenReturn(unbundleCommandBuilder);
|
||||
lenient().when(repositoryImportExportEncryption.decrypt(any(), any())).thenAnswer(invocation -> invocation.getArgument(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -196,5 +195,15 @@ class FullScmRepositoryImporterTest {
|
||||
verify(unbundleCommandBuilder).unbundle((InputStream) argThat(argument -> argument.getClass().equals(NoneClosingInputStream.class)));
|
||||
verify(workdirProvider, never()).createNewWorkdir(REPOSITORY.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldDecryptStreamWhenPasswordSet() throws IOException {
|
||||
InputStream stream = Resources.getResource("sonia/scm/repository/import/scm-import.tar.gz").openStream();
|
||||
when(repositoryImportExportEncryption.decrypt(any(), eq("hg2tg"))).thenAnswer(invocation -> invocation.getArgument(0));
|
||||
|
||||
fullImporter.importFromStream(REPOSITORY, stream, "hg2tg");
|
||||
|
||||
verify(updateEngine).update(REPOSITORY.getId());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user