mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-02 11:35:57 +01:00
remove downloaded artifact on error
This commit is contained in:
@@ -27,25 +27,35 @@ class PluginInstaller {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public PendingPluginInstallation install(AvailablePlugin plugin) {
|
public PendingPluginInstallation install(AvailablePlugin plugin) {
|
||||||
|
Path file = null;
|
||||||
try (HashingInputStream input = new HashingInputStream(Hashing.sha256(), download(plugin))) {
|
try (HashingInputStream input = new HashingInputStream(Hashing.sha256(), download(plugin))) {
|
||||||
Path file = createFile(plugin);
|
file = createFile(plugin);
|
||||||
Files.copy(input, file);
|
Files.copy(input, file);
|
||||||
|
|
||||||
verifyChecksum(plugin, input.hash());
|
verifyChecksum(plugin, input.hash(), file);
|
||||||
|
|
||||||
// TODO clean up in case of error
|
|
||||||
|
|
||||||
return new PendingPluginInstallation(plugin, file);
|
return new PendingPluginInstallation(plugin, file);
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
|
cleanup(file);
|
||||||
throw new PluginDownloadException("failed to download plugin", ex);
|
throw new PluginDownloadException("failed to download plugin", ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void verifyChecksum(AvailablePlugin plugin, HashCode hash) {
|
private void cleanup(Path file) {
|
||||||
|
try {
|
||||||
|
if (file != null) {
|
||||||
|
Files.deleteIfExists(file);
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new PluginInstallException("failed to cleanup, after broken installation");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void verifyChecksum(AvailablePlugin plugin, HashCode hash, Path file) {
|
||||||
Optional<String> checksum = plugin.getDescriptor().getChecksum();
|
Optional<String> checksum = plugin.getDescriptor().getChecksum();
|
||||||
if (checksum.isPresent()) {
|
if (checksum.isPresent()) {
|
||||||
String calculatedChecksum = hash.toString();
|
String calculatedChecksum = hash.toString();
|
||||||
if (!checksum.get().equalsIgnoreCase(calculatedChecksum)) {
|
if (!checksum.get().equalsIgnoreCase(calculatedChecksum)) {
|
||||||
|
cleanup(file);
|
||||||
throw new PluginChecksumMismatchException(
|
throw new PluginChecksumMismatchException(
|
||||||
String.format("downloaded plugin checksum %s does not match expected %s", calculatedChecksum, checksum.get())
|
String.format("downloaded plugin checksum %s does not match expected %s", calculatedChecksum, checksum.get())
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -13,16 +13,15 @@ import sonia.scm.net.ahc.AdvancedHttpClient;
|
|||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.assertj.core.api.Assertions.in;
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
|
||||||
import static org.mockito.ArgumentMatchers.any;
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
import static org.mockito.Mockito.lenient;
|
import static org.mockito.Mockito.*;
|
||||||
import static org.mockito.Mockito.when;
|
|
||||||
|
|
||||||
@ExtendWith({MockitoExtension.class, TempDirectory.class})
|
@ExtendWith({MockitoExtension.class, TempDirectory.class})
|
||||||
class PluginInstallerTest {
|
class PluginInstallerTest {
|
||||||
@@ -92,6 +91,17 @@ class PluginInstallerTest {
|
|||||||
mockContent("21");
|
mockContent("21");
|
||||||
|
|
||||||
assertThrows(PluginChecksumMismatchException.class, () -> installer.install(createGitPlugin()));
|
assertThrows(PluginChecksumMismatchException.class, () -> installer.install(createGitPlugin()));
|
||||||
|
assertThat(directory.resolve("plugins").resolve("scm-git-plugin.smp")).doesNotExist();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldThrowPluginDownloadExceptionAndCleanup() throws IOException {
|
||||||
|
InputStream stream = mock(InputStream.class);
|
||||||
|
when(stream.read(any(), anyInt(), anyInt())).thenThrow(new IOException("failed to read"));
|
||||||
|
when(client.get("https://download.hitchhiker.com").request().contentAsStream()).thenReturn(stream);
|
||||||
|
|
||||||
|
assertThrows(PluginDownloadException.class, () -> installer.install(createGitPlugin()));
|
||||||
|
assertThat(directory.resolve("plugins").resolve("scm-git-plugin.smp")).doesNotExist();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user