Skip to content

Commit b33f384

Browse files
authored
Do not checksum all bytes at once in plugin install (#44649)
Today when checksumming a plugin zip during plugin install, we read all of the bytes of the zip into memory at once. When trying to run the plugin installer on a small heap (say, 64 MiB), this can lead to the plugin installer running out of memory when checksumming large plugins. This commit addresses this by reading the plugin bytes in 8 KiB chunks, thus using a constant amount of memory independent of the size of the plugin.
1 parent 5e89cd9 commit b33f384

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

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

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -500,17 +500,26 @@ private Path downloadAndValidate(
500500
}
501501
}
502502

503-
try {
504-
final byte[] zipBytes = Files.readAllBytes(zip);
505-
final String actualChecksum = MessageDigests.toHexString(MessageDigest.getInstance(digestAlgo).digest(zipBytes));
506-
if (expectedChecksum.equals(actualChecksum) == false) {
507-
throw new UserException(
503+
// read the bytes of the plugin zip in chunks to avoid out of memory errors
504+
try (InputStream zis = Files.newInputStream(zip)) {
505+
try {
506+
final MessageDigest digest = MessageDigest.getInstance(digestAlgo);
507+
final byte[] bytes = new byte[8192];
508+
int read;
509+
while ((read = zis.read(bytes)) != -1) {
510+
assert read > 0 : read;
511+
digest.update(bytes, 0, read);
512+
}
513+
final String actualChecksum = MessageDigests.toHexString(digest.digest());
514+
if (expectedChecksum.equals(actualChecksum) == false) {
515+
throw new UserException(
508516
ExitCodes.IO_ERROR,
509517
digestAlgo + " mismatch, expected " + expectedChecksum + " but got " + actualChecksum);
518+
}
519+
} catch (final NoSuchAlgorithmException e) {
520+
// this should never happen as we are using SHA-1 and SHA-512 here
521+
throw new AssertionError(e);
510522
}
511-
} catch (final NoSuchAlgorithmException e) {
512-
// this should never happen as we are using SHA-1 and SHA-512 here
513-
throw new AssertionError(e);
514523
}
515524

516525
if (officialPlugin) {

0 commit comments

Comments
 (0)