Skip to content

Commit a1e7429

Browse files
committed
Allow sha512 checksum without filename for maven plugins (#52668)
When installing plugins from remote sources, either the Elastic download service, or maven, a checksum file is downloaded and checked against the downloaded zip. The current format for official plugins is to use a sha512 checksum which includes the zip filename. This format matches that from sha512sum, and allows using the --check argument there to verify the checksum manually. However, when generating checksum files with maven and gradle, the filename is not included. This commit relaxes the requirement the filename existing within the sha512 checksum file for maven plugins. We continue to strictly enforce official plugins have the existing format of the file. closes #52413
1 parent 4cae4de commit a1e7429

File tree

2 files changed

+55
-12
lines changed

2 files changed

+55
-12
lines changed

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

+15-12
Original file line numberDiff line numberDiff line change
@@ -561,21 +561,24 @@ private Path downloadAndValidate(
561561
final BufferedReader checksumReader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8));
562562
final String checksumLine = checksumReader.readLine();
563563
final String[] fields = checksumLine.split(" {2}");
564-
if (fields.length != 2) {
564+
if (officialPlugin && fields.length != 2 || officialPlugin == false && fields.length > 2) {
565565
throw new UserException(ExitCodes.IO_ERROR, "Invalid checksum file at " + checksumUrl);
566566
}
567567
expectedChecksum = fields[0];
568-
final String[] segments = URI.create(urlString).getPath().split("/");
569-
final String expectedFile = segments[segments.length - 1];
570-
if (fields[1].equals(expectedFile) == false) {
571-
final String message = String.format(
572-
Locale.ROOT,
573-
"checksum file at [%s] is not for this plugin, expected [%s] but was [%s]",
574-
checksumUrl,
575-
expectedFile,
576-
fields[1]
577-
);
578-
throw new UserException(ExitCodes.IO_ERROR, message);
568+
if (fields.length == 2) {
569+
// checksum line contains filename as well
570+
final String[] segments = URI.create(urlString).getPath().split("/");
571+
final String expectedFile = segments[segments.length - 1];
572+
if (fields[1].equals(expectedFile) == false) {
573+
final String message = String.format(
574+
Locale.ROOT,
575+
"checksum file at [%s] is not for this plugin, expected [%s] but was [%s]",
576+
checksumUrl,
577+
expectedFile,
578+
fields[1]
579+
);
580+
throw new UserException(ExitCodes.IO_ERROR, message);
581+
}
579582
}
580583
if (checksumReader.readLine() != null) {
581584
throw new UserException(ExitCodes.IO_ERROR, "Invalid checksum file at " + checksumUrl);

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

+40
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@
115115
import static org.hamcrest.Matchers.endsWith;
116116
import static org.hamcrest.Matchers.hasToString;
117117
import static org.hamcrest.Matchers.not;
118+
import static org.hamcrest.Matchers.startsWith;
118119

119120
@LuceneTestCase.SuppressFileSystems("*")
120121
public class InstallPluginCommandTests extends ESTestCase {
@@ -1113,6 +1114,45 @@ public void testMavenSha1Backcompat() throws Exception {
11131114
assertTrue(terminal.getOutput(), terminal.getOutput().contains("sha512 not found, falling back to sha1"));
11141115
}
11151116

1117+
public void testMavenChecksumWithoutFilename() throws Exception {
1118+
String url = "https://repo1.maven.org/maven2/mygroup/myplugin/1.0.0/myplugin-1.0.0.zip";
1119+
MessageDigest digest = MessageDigest.getInstance("SHA-512");
1120+
assertInstallPluginFromUrl(
1121+
"mygroup:myplugin:1.0.0",
1122+
"myplugin",
1123+
url,
1124+
null,
1125+
false,
1126+
".sha512",
1127+
checksum(digest),
1128+
null,
1129+
(b, p) -> null
1130+
);
1131+
}
1132+
1133+
public void testOfficialChecksumWithoutFilename() throws Exception {
1134+
String url = "https://artifacts.elastic.co/downloads/elasticsearch-plugins/analysis-icu/analysis-icu-"
1135+
+ Build.CURRENT.getQualifiedVersion()
1136+
+ ".zip";
1137+
MessageDigest digest = MessageDigest.getInstance("SHA-512");
1138+
UserException e = expectThrows(
1139+
UserException.class,
1140+
() -> assertInstallPluginFromUrl(
1141+
"analysis-icu",
1142+
"analysis-icu",
1143+
url,
1144+
null,
1145+
false,
1146+
".sha512",
1147+
checksum(digest),
1148+
null,
1149+
(b, p) -> null
1150+
)
1151+
);
1152+
assertEquals(ExitCodes.IO_ERROR, e.exitCode);
1153+
assertThat(e.getMessage(), startsWith("Invalid checksum file"));
1154+
}
1155+
11161156
public void testOfficialShaMissing() throws Exception {
11171157
String url = "https://artifacts.elastic.co/downloads/elasticsearch-plugins/analysis-icu/analysis-icu-"
11181158
+ Build.CURRENT.getQualifiedVersion()

0 commit comments

Comments
 (0)