Skip to content

Commit b9c2f4b

Browse files
committed
Plugins: Remove intermediate "elasticsearch" directory within plugin zips (#28589)
This commit removes the extra layer of all plugin files existing under "elasticsearch" within plugin zips. This simplifies building plugin zips and removes the need for special logic of modules vs plugins.
1 parent 86dcb18 commit b9c2f4b

File tree

4 files changed

+23
-33
lines changed

4 files changed

+23
-33
lines changed

buildSrc/src/main/groovy/org/elasticsearch/gradle/plugin/MetaPluginBuildPlugin.groovy

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,9 @@ class MetaPluginBuildPlugin implements Plugin<Project> {
5858

5959
// create the actual bundle task, which zips up all the files for the plugin
6060
Zip bundle = project.tasks.create(name: 'bundlePlugin', type: Zip, dependsOn: [buildProperties]) {
61-
into('elasticsearch') {
62-
from(buildProperties.descriptorOutput.parentFile) {
63-
// plugin properties file
64-
include(buildProperties.descriptorOutput.name)
65-
}
61+
from(buildProperties.descriptorOutput.parentFile) {
62+
// plugin properties file
63+
include(buildProperties.descriptorOutput.name)
6664
}
6765
// due to how the renames work for each bundled plugin, we must exclude empty dirs or every subdir
6866
// within bundled plugin zips will show up at the root as an empty dir
@@ -85,10 +83,8 @@ class MetaPluginBuildPlugin implements Plugin<Project> {
8583
dependsOn bundledPluginProject.bundlePlugin
8684
from(project.zipTree(bundledPluginProject.bundlePlugin.outputs.files.singleFile)) {
8785
eachFile { FileCopyDetails details ->
88-
// paths in the individual plugins begin with elasticsearch, and we want to add in the
89-
// bundled plugin name between that and each filename
90-
details.relativePath = new RelativePath(true, 'elasticsearch', bundledPluginProjectName,
91-
details.relativePath.toString().replace('elasticsearch/', ''))
86+
// we want each path to have the plugin name interjected
87+
details.relativePath = new RelativePath(true, bundledPluginProjectName, details.relativePath.toString())
9288
}
9389
}
9490
}

buildSrc/src/main/groovy/org/elasticsearch/gradle/plugin/PluginBuildPlugin.groovy

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,6 @@ public class PluginBuildPlugin extends BuildPlugin {
140140
include 'config/**'
141141
include 'bin/**'
142142
}
143-
if (project.path.startsWith(':modules:') == false) {
144-
into('elasticsearch')
145-
}
146143
}
147144
project.assemble.dependsOn(bundle)
148145

distribution/tools/plugin-cli/src/main/java/org/elasticsearch/plugins/InstallPluginCommand.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -462,17 +462,15 @@ private Path unzip(Path zip, Path pluginsDir) throws IOException, UserException
462462
final Path target = stagingDirectory(pluginsDir);
463463
pathsToDeleteOnShutdown.add(target);
464464

465-
boolean hasEsDir = false;
466465
try (ZipInputStream zipInput = new ZipInputStream(Files.newInputStream(zip))) {
467466
ZipEntry entry;
468467
byte[] buffer = new byte[8192];
469468
while ((entry = zipInput.getNextEntry()) != null) {
470-
if (entry.getName().startsWith("elasticsearch/") == false) {
471-
// only extract the elasticsearch directory
472-
continue;
469+
if (entry.getName().startsWith("elasticsearch/")) {
470+
throw new UserException(PLUGIN_MALFORMED, "This plugin was built with an older plugin structure." +
471+
" Contact the plugin author to remove the intermediate \"elasticsearch\" directory within the plugin zip.");
473472
}
474-
hasEsDir = true;
475-
Path targetFile = target.resolve(entry.getName().substring("elasticsearch/".length()));
473+
Path targetFile = target.resolve(entry.getName());
476474

477475
// Using the entry name as a path can result in an entry outside of the plugin dir,
478476
// either if the name starts with the root of the filesystem, or it is a relative
@@ -499,13 +497,11 @@ private Path unzip(Path zip, Path pluginsDir) throws IOException, UserException
499497
}
500498
zipInput.closeEntry();
501499
}
502-
}
503-
Files.delete(zip);
504-
if (hasEsDir == false) {
500+
} catch (UserException e) {
505501
IOUtils.rm(target);
506-
throw new UserException(PLUGIN_MALFORMED,
507-
"`elasticsearch` directory is missing in the plugin zip");
502+
throw e;
508503
}
504+
Files.delete(zip);
509505
return target;
510506
}
511507

distribution/tools/plugin-cli/src/test/java/org/elasticsearch/plugins/InstallPluginCommandTests.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -259,12 +259,12 @@ static void writePluginSecurityPolicy(Path pluginDir, String... permissions) thr
259259

260260
static Path createPlugin(String name, Path structure, String... additionalProps) throws IOException {
261261
writePlugin(name, structure, additionalProps);
262-
return writeZip(structure, "elasticsearch");
262+
return writeZip(structure, null);
263263
}
264264

265265
static Path createMetaPlugin(String name, Path structure) throws IOException {
266266
writeMetaPlugin(name, structure);
267-
return writeZip(structure, "elasticsearch");
267+
return writeZip(structure, null);
268268
}
269269

270270
void installPlugin(String pluginUrl, Path home) throws Exception {
@@ -811,7 +811,7 @@ public void testMissingDescriptor() throws Exception {
811811
Path pluginDir = metaDir.resolve("fake");
812812
Files.createDirectory(pluginDir);
813813
Files.createFile(pluginDir.resolve("fake.yml"));
814-
String pluginZip = writeZip(pluginDir, "elasticsearch").toUri().toURL().toString();
814+
String pluginZip = writeZip(pluginDir, null).toUri().toURL().toString();
815815
NoSuchFileException e = expectThrows(NoSuchFileException.class, () -> installPlugin(pluginZip, env.v1()));
816816
assertTrue(e.getMessage(), e.getMessage().contains("plugin-descriptor.properties"));
817817
assertInstallCleaned(env.v2());
@@ -822,35 +822,36 @@ public void testMissingDescriptor() throws Exception {
822822
assertInstallCleaned(env.v2());
823823
}
824824

825-
public void testMissingDirectory() throws Exception {
825+
public void testContainsIntermediateDirectory() throws Exception {
826826
Tuple<Path, Environment> env = createEnv(fs, temp);
827827
Path pluginDir = createPluginDir(temp);
828828
Files.createFile(pluginDir.resolve(PluginInfo.ES_PLUGIN_PROPERTIES));
829-
String pluginZip = writeZip(pluginDir, null).toUri().toURL().toString();
829+
String pluginZip = writeZip(pluginDir, "elasticsearch").toUri().toURL().toString();
830830
UserException e = expectThrows(UserException.class, () -> installPlugin(pluginZip, env.v1()));
831-
assertTrue(e.getMessage(), e.getMessage().contains("`elasticsearch` directory is missing in the plugin zip"));
831+
assertThat(e.getMessage(), containsString("This plugin was built with an older plugin structure"));
832832
assertInstallCleaned(env.v2());
833833
}
834834

835-
public void testMissingDirectoryMeta() throws Exception {
835+
public void testContainsIntermediateDirectoryMeta() throws Exception {
836836
Tuple<Path, Environment> env = createEnv(fs, temp);
837837
Path pluginDir = createPluginDir(temp);
838838
Files.createFile(pluginDir.resolve(MetaPluginInfo.ES_META_PLUGIN_PROPERTIES));
839-
String pluginZip = writeZip(pluginDir, null).toUri().toURL().toString();
839+
String pluginZip = writeZip(pluginDir, "elasticsearch").toUri().toURL().toString();
840840
UserException e = expectThrows(UserException.class, () -> installPlugin(pluginZip, env.v1()));
841-
assertTrue(e.getMessage(), e.getMessage().contains("`elasticsearch` directory is missing in the plugin zip"));
841+
assertThat(e.getMessage(), containsString("This plugin was built with an older plugin structure"));
842842
assertInstallCleaned(env.v2());
843843
}
844844

845845
public void testZipRelativeOutsideEntryName() throws Exception {
846846
Tuple<Path, Environment> env = createEnv(fs, temp);
847847
Path zip = createTempDir().resolve("broken.zip");
848848
try (ZipOutputStream stream = new ZipOutputStream(Files.newOutputStream(zip))) {
849-
stream.putNextEntry(new ZipEntry("elasticsearch/../blah"));
849+
stream.putNextEntry(new ZipEntry("../blah"));
850850
}
851851
String pluginZip = zip.toUri().toURL().toString();
852852
UserException e = expectThrows(UserException.class, () -> installPlugin(pluginZip, env.v1()));
853853
assertTrue(e.getMessage(), e.getMessage().contains("resolving outside of plugin directory"));
854+
assertInstallCleaned(env.v2());
854855
}
855856

856857
public void testOfficialPluginsHelpSorted() throws Exception {

0 commit comments

Comments
 (0)