Skip to content

Commit

Permalink
Improve error message for installing plugin
Browse files Browse the repository at this point in the history
Provide more actionable error message when installing an offline plugin
in the plugins directory, and the `plugins` directory for the node
contains plugin distribution.

Closes elastic#27401
  • Loading branch information
mayya-sharipova committed Mar 14, 2018
1 parent bf65cb4 commit 68c91ac
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.StandardCopyOption;
import java.nio.file.attribute.BasicFileAttributes;
Expand Down Expand Up @@ -218,25 +219,25 @@ void execute(Terminal terminal, String pluginId, boolean isBatch, Environment en
throw new UserException(ExitCodes.USAGE, "plugin id is required");
}

Path pluginZip = download(terminal, pluginId, env.tmpFile());
Path pluginZip = download(terminal, pluginId, env.tmpFile(), env.pluginsFile());
Path extractedZip = unzip(pluginZip, env.pluginsFile());
install(terminal, isBatch, extractedZip, env);
}

/** Downloads the plugin and returns the file it was downloaded to. */
private Path download(Terminal terminal, String pluginId, Path tmpDir) throws Exception {
private Path download(Terminal terminal, String pluginId, Path tmpDir, Path pluginsDir) throws Exception {
if (OFFICIAL_PLUGINS.contains(pluginId)) {
final String url = getElasticUrl(terminal, getStagingHash(), Version.CURRENT, pluginId, Platforms.PLATFORM_NAME);
terminal.println("-> Downloading " + pluginId + " from elastic");
return downloadZipAndChecksum(terminal, url, tmpDir, false);
return downloadZipAndChecksum(terminal, url, tmpDir, pluginsDir, false);
}

// now try as maven coordinates, a valid URL would only have a colon and slash
String[] coordinates = pluginId.split(":");
if (coordinates.length == 3 && pluginId.contains("/") == false && pluginId.startsWith("file:") == false) {
String mavenUrl = getMavenUrl(terminal, coordinates, Platforms.PLATFORM_NAME);
terminal.println("-> Downloading " + pluginId + " from maven central");
return downloadZipAndChecksum(terminal, mavenUrl, tmpDir, true);
return downloadZipAndChecksum(terminal, mavenUrl, tmpDir, pluginsDir, true);
}

// fall back to plain old URL
Expand All @@ -250,7 +251,7 @@ private Path download(Terminal terminal, String pluginId, Path tmpDir) throws Ex
throw new UserException(ExitCodes.USAGE, msg);
}
terminal.println("-> Downloading " + URLDecoder.decode(pluginId, "UTF-8"));
return downloadZip(terminal, pluginId, tmpDir);
return downloadZip(terminal, pluginId, tmpDir, pluginsDir);
}

// pkg private so tests can override
Expand Down Expand Up @@ -324,9 +325,17 @@ private List<String> checkMisspelledPlugin(String pluginId) {
/** Downloads a zip from the url, into a temp file under the given temp dir. */
// pkg private for tests
@SuppressForbidden(reason = "We use getInputStream to download plugins")
Path downloadZip(Terminal terminal, String urlString, Path tmpDir) throws IOException {
Path downloadZip(Terminal terminal, String urlString, Path tmpDir, Path pluginsDir) throws IOException {
terminal.println(VERBOSE, "Retrieving zip from " + urlString);
URL url = new URL(urlString);
if (url.getProtocol().equals("file")) {
Path pluginsFile = Paths.get(url.getFile());
if (pluginsFile.startsWith(pluginsDir)) {
throw new IllegalStateException("Installation failed! " +
"Make sure the plugins directory [" + pluginsDir + "] can not contain the plugin distribution [" +
pluginsFile + "]; move the distribution to an alternate location!");
}
}
Path zip = Files.createTempFile(tmpDir, null, ".zip");
URLConnection urlConnection = url.openConnection();
urlConnection.addRequestProperty("User-Agent", "elasticsearch-plugin-installer");
Expand Down Expand Up @@ -375,8 +384,9 @@ public void onProgress(int percent) {
/** Downloads a zip from the url, as well as a SHA512 (or SHA1) checksum, and checks the checksum. */
// pkg private for tests
@SuppressForbidden(reason = "We use openStream to download plugins")
private Path downloadZipAndChecksum(Terminal terminal, String urlString, Path tmpDir, boolean allowSha1) throws Exception {
Path zip = downloadZip(terminal, urlString, tmpDir);
private Path downloadZipAndChecksum(Terminal terminal, String urlString, Path tmpDir, Path pluginsDir, boolean allowSha1)
throws Exception {
Path zip = downloadZip(terminal, urlString, tmpDir, pluginsDir);
pathsToDeleteOnShutdown.add(zip);
String checksumUrlString = urlString + ".sha512";
URL checksumUrl = openUrl(checksumUrlString);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -981,7 +981,7 @@ void assertInstallPluginFromUrl(String pluginId, String name, String url, String
Path pluginZip = createPlugin(name, pluginDir);
InstallPluginCommand command = new InstallPluginCommand() {
@Override
Path downloadZip(Terminal terminal, String urlString, Path tmpDir) throws IOException {
Path downloadZip(Terminal terminal, String urlString, Path tmpDir, Path pluginsDir) throws IOException {
assertEquals(url, urlString);
Path downloadedPath = tmpDir.resolve("downloaded.zip");
Files.copy(pluginZip, downloadedPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ public String name() {
public Collection<Bundle> bundles() {
return bundles;
}

}

/**
Expand Down

0 comments on commit 68c91ac

Please sign in to comment.