From ff4d9224f94dbef3892412b538476003e70f170e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Pfeuffer?= Date: Thu, 25 Feb 2021 16:26:49 +0100 Subject: [PATCH] Decrypt import only if password is given --- .../api/v2/resources/RepositoryImportResource.java | 4 +++- .../scm/importexport/FullScmRepositoryImporter.java | 11 ++++++++++- .../importexport/FullScmRepositoryImporterTest.java | 13 +++++++++++-- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/scm-webapp/src/main/java/sonia/scm/api/v2/resources/RepositoryImportResource.java b/scm-webapp/src/main/java/sonia/scm/api/v2/resources/RepositoryImportResource.java index e46f2b8b47..b87142c3a7 100644 --- a/scm-webapp/src/main/java/sonia/scm/api/v2/resources/RepositoryImportResource.java +++ b/scm-webapp/src/main/java/sonia/scm/api/v2/resources/RepositoryImportResource.java @@ -328,7 +328,9 @@ public class RepositoryImportResource { Map> 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); diff --git a/scm-webapp/src/main/java/sonia/scm/importexport/FullScmRepositoryImporter.java b/scm-webapp/src/main/java/sonia/scm/importexport/FullScmRepositoryImporter.java index 4a19dd22f3..550fd7cedb 100644 --- a/scm-webapp/src/main/java/sonia/scm/importexport/FullScmRepositoryImporter.java +++ b/scm-webapp/src/main/java/sonia/scm/importexport/FullScmRepositoryImporter.java @@ -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 { diff --git a/scm-webapp/src/test/java/sonia/scm/importexport/FullScmRepositoryImporterTest.java b/scm-webapp/src/test/java/sonia/scm/importexport/FullScmRepositoryImporterTest.java index a9c70ec799..ee915d7856 100644 --- a/scm-webapp/src/test/java/sonia/scm/importexport/FullScmRepositoryImporterTest.java +++ b/scm-webapp/src/test/java/sonia/scm/importexport/FullScmRepositoryImporterTest.java @@ -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()); + } } }