Files
SCM-Manager/scm-webapp/src/main/java/sonia/scm/plugin/PluginInstaller.java

137 lines
4.9 KiB
Java
Raw Normal View History

/*
* MIT License
*
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
2020-03-25 15:31:20 +01:00
2019-08-20 14:43:48 +02:00
package sonia.scm.plugin;
import com.google.common.hash.HashCode;
2019-08-20 14:43:48 +02:00
import com.google.common.hash.Hashing;
import com.google.common.hash.HashingInputStream;
2019-08-20 14:43:48 +02:00
import sonia.scm.SCMContextProvider;
import sonia.scm.net.ahc.AdvancedHttpClient;
import javax.inject.Inject;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
2019-08-20 14:43:48 +02:00
import java.util.Optional;
@SuppressWarnings("UnstableApiUsage") // guava hash is marked as unstable
2019-08-20 14:43:48 +02:00
class PluginInstaller {
private final SCMContextProvider scmContext;
2019-08-20 14:43:48 +02:00
private final AdvancedHttpClient client;
2020-01-24 12:01:21 +01:00
private final SmpDescriptorExtractor smpDescriptorExtractor;
2019-08-20 14:43:48 +02:00
@Inject
public PluginInstaller(SCMContextProvider scmContext, AdvancedHttpClient client, SmpDescriptorExtractor smpDescriptorExtractor) {
this.scmContext = scmContext;
2019-08-20 14:43:48 +02:00
this.client = client;
2020-01-24 12:01:21 +01:00
this.smpDescriptorExtractor = smpDescriptorExtractor;
2019-08-20 14:43:48 +02:00
}
@SuppressWarnings("squid:S4790") // hashing should be safe
public PendingPluginInstallation install(PluginInstallationContext context, AvailablePlugin plugin) {
2019-08-21 07:56:52 +02:00
Path file = null;
try (HashingInputStream input = new HashingInputStream(Hashing.sha256(), download(plugin))) {
2019-08-21 07:56:52 +02:00
file = createFile(plugin);
Files.copy(input, file);
2019-08-20 14:43:48 +02:00
2019-08-21 07:56:52 +02:00
verifyChecksum(plugin, input.hash(), file);
InstalledPluginDescriptor descriptor = smpDescriptorExtractor.extractPluginDescriptor(file);
PluginInstallationVerifier.verify(context, descriptor);
verifyInformation(plugin.getDescriptor(), descriptor);
2019-08-21 12:49:15 +02:00
return new PendingPluginInstallation(plugin.install(), file);
} catch (PluginException ex) {
cleanup(file);
throw ex;
2019-08-20 14:43:48 +02:00
} catch (IOException ex) {
2019-08-21 07:56:52 +02:00
cleanup(file);
2020-03-25 15:31:20 +01:00
throw new PluginDownloadException(plugin, ex);
2019-08-20 14:43:48 +02:00
}
}
private void verifyInformation(AvailablePluginDescriptor api, InstalledPluginDescriptor downloaded) {
verifyInformation(api.getInformation(), downloaded.getInformation());
}
private void verifyInformation(PluginInformation api, PluginInformation downloaded) {
if (!api.getName().equals(downloaded.getName())) {
throw new PluginInformationMismatchException(
api, downloaded,
String.format(
"downloaded plugin name \"%s\" does not match the expected name \"%s\" from plugin-center",
downloaded.getName(),
api.getName()
)
);
}
if (!api.getVersion().equals(downloaded.getVersion())) {
throw new PluginInformationMismatchException(
api, downloaded,
String.format(
"downloaded plugin version \"%s\" does not match the expected version \"%s\" from plugin-center",
downloaded.getVersion(),
api.getVersion()
)
);
}
}
2019-08-21 07:56:52 +02:00
private void cleanup(Path file) {
try {
if (file != null) {
Files.deleteIfExists(file);
}
} catch (IOException e) {
2020-03-25 15:31:20 +01:00
throw new PluginCleanupException(file);
2019-08-21 07:56:52 +02:00
}
}
private void verifyChecksum(AvailablePlugin plugin, HashCode hash, Path file) {
2019-08-20 14:43:48 +02:00
Optional<String> checksum = plugin.getDescriptor().getChecksum();
if (checksum.isPresent()) {
String calculatedChecksum = hash.toString();
2019-08-20 14:43:48 +02:00
if (!checksum.get().equalsIgnoreCase(calculatedChecksum)) {
2019-08-21 07:56:52 +02:00
cleanup(file);
2020-03-25 15:31:20 +01:00
throw new PluginChecksumMismatchException(plugin, calculatedChecksum, checksum.get());
2019-08-20 14:43:48 +02:00
}
}
}
private InputStream download(AvailablePlugin plugin) throws IOException {
return client.get(plugin.getDescriptor().getUrl()).request().contentAsStream();
}
private Path createFile(AvailablePlugin plugin) throws IOException {
Path directory = scmContext.resolve(Paths.get("plugins"));
Files.createDirectories(directory);
return directory.resolve(plugin.getDescriptor().getInformation().getName() + ".smp");
2019-08-20 14:43:48 +02:00
}
}