Skip to content

Commit cd54dc1

Browse files
authored
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 437273f commit cd54dc1

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

Lines changed: 15 additions & 12 deletions
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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@
114114
import static org.hamcrest.Matchers.endsWith;
115115
import static org.hamcrest.Matchers.hasToString;
116116
import static org.hamcrest.Matchers.not;
117+
import static org.hamcrest.Matchers.startsWith;
117118

118119
@LuceneTestCase.SuppressFileSystems("*")
119120
public class InstallPluginCommandTests extends ESTestCase {
@@ -1109,6 +1110,45 @@ public void testMavenSha1Backcompat() throws Exception {
11091110
assertTrue(terminal.getOutput(), terminal.getOutput().contains("sha512 not found, falling back to sha1"));
11101111
}
11111112

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

0 commit comments

Comments
 (0)