Skip to content

Commit 70e61bd

Browse files
committed
Allow sha512 checksum without filename for maven plugins
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 elastic#52413
1 parent 01a6dae commit 70e61bd

File tree

2 files changed

+44
-12
lines changed

2 files changed

+44
-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
@@ -545,21 +545,24 @@ private Path downloadAndValidate(
545545
final BufferedReader checksumReader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8));
546546
final String checksumLine = checksumReader.readLine();
547547
final String[] fields = checksumLine.split(" {2}");
548-
if (fields.length != 2) {
548+
if (officialPlugin && fields.length != 2 || officialPlugin == false && fields.length > 2) {
549549
throw new UserException(ExitCodes.IO_ERROR, "Invalid checksum file at " + checksumUrl);
550550
}
551551
expectedChecksum = fields[0];
552-
final String[] segments = URI.create(urlString).getPath().split("/");
553-
final String expectedFile = segments[segments.length - 1];
554-
if (fields[1].equals(expectedFile) == false) {
555-
final String message = String.format(
556-
Locale.ROOT,
557-
"checksum file at [%s] is not for this plugin, expected [%s] but was [%s]",
558-
checksumUrl,
559-
expectedFile,
560-
fields[1]
561-
);
562-
throw new UserException(ExitCodes.IO_ERROR, message);
552+
if (fields.length == 2) {
553+
// checksum line contains filename as well
554+
final String[] segments = URI.create(urlString).getPath().split("/");
555+
final String expectedFile = segments[segments.length - 1];
556+
if (fields[1].equals(expectedFile) == false) {
557+
final String message = String.format(
558+
Locale.ROOT,
559+
"checksum file at [%s] is not for this plugin, expected [%s] but was [%s]",
560+
checksumUrl,
561+
expectedFile,
562+
fields[1]
563+
);
564+
throw new UserException(ExitCodes.IO_ERROR, message);
565+
}
563566
}
564567
if (checksumReader.readLine() != null) {
565568
throw new UserException(ExitCodes.IO_ERROR, "Invalid checksum file at " + checksumUrl);

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

+29
Original file line numberDiff line numberDiff line change
@@ -1109,6 +1109,35 @@ public void testMavenSha1Backcompat() throws Exception {
11091109
assertTrue(terminal.getOutput(), terminal.getOutput().contains("sha512 not found, falling back to sha1"));
11101110
}
11111111

1112+
public void testMavenChecksumWithoutFilename() throws Exception {
1113+
String url = "https://repo1.maven.org/maven2/mygroup/myplugin/1.0.0/myplugin-1.0.0.zip";
1114+
MessageDigest digest = MessageDigest.getInstance("SHA-512");
1115+
assertInstallPluginFromUrl("mygroup:myplugin:1.0.0", "myplugin", url, null, false, ".sha512", checksum(digest), null, (b, p) -> null);
1116+
}
1117+
1118+
public void testOfficialChecksumWithoutFilename() throws Exception {
1119+
String url = "https://artifacts.elastic.co/downloads/elasticsearch-plugins/analysis-icu/analysis-icu-"
1120+
+ Build.CURRENT.getQualifiedVersion()
1121+
+ ".zip";
1122+
MessageDigest digest = MessageDigest.getInstance("SHA-512");
1123+
UserException e = expectThrows(
1124+
UserException.class,
1125+
() -> assertInstallPluginFromUrl(
1126+
"analysis-icu",
1127+
"analysis-icu",
1128+
url,
1129+
null,
1130+
false,
1131+
".sha512",
1132+
checksum(digest),
1133+
null,
1134+
(b, p) -> null
1135+
)
1136+
);
1137+
assertEquals(ExitCodes.IO_ERROR, e.exitCode);
1138+
assertTrue(e.getMessage(), e.getMessage().startsWith("Invalid checksum file"));
1139+
}
1140+
11121141
public void testOfficialShaMissing() throws Exception {
11131142
String url = "https://artifacts.elastic.co/downloads/elasticsearch-plugins/analysis-icu/analysis-icu-"
11141143
+ Build.CURRENT.getQualifiedVersion()

0 commit comments

Comments
 (0)