mirror of
https://github.com/apache/cloudstack.git
synced 2025-10-26 08:42:29 +01:00
CLOUDSTACK-10146 checksum in java instead of script (#2405)
* CLOUDSTACK-10146 checksum in java instead of script * root cause passing
This commit is contained in:
parent
94776fbfd0
commit
123c20dc57
@ -20,10 +20,13 @@ package com.cloud.agent.direct.download;
|
|||||||
|
|
||||||
import com.cloud.utils.exception.CloudRuntimeException;
|
import com.cloud.utils.exception.CloudRuntimeException;
|
||||||
import com.cloud.utils.script.Script;
|
import com.cloud.utils.script.Script;
|
||||||
import org.apache.cloudstack.utils.security.ChecksumValue;
|
import org.apache.cloudstack.utils.security.DigestHelper;
|
||||||
import org.apache.commons.lang.StringUtils;
|
import org.apache.commons.lang.StringUtils;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.security.NoSuchAlgorithmException;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
public abstract class DirectTemplateDownloaderImpl implements DirectTemplateDownloader {
|
public abstract class DirectTemplateDownloaderImpl implements DirectTemplateDownloader {
|
||||||
@ -149,36 +152,16 @@ public abstract class DirectTemplateDownloaderImpl implements DirectTemplateDown
|
|||||||
return new DirectTemplateInformation(installPath, size, checksum);
|
return new DirectTemplateInformation(installPath, size, checksum);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Return checksum command from algorithm
|
|
||||||
*/
|
|
||||||
private String getChecksumCommandFromAlgorithm(String algorithm) {
|
|
||||||
if (algorithm.equalsIgnoreCase("MD5")) {
|
|
||||||
return "md5sum";
|
|
||||||
} else if (algorithm.equalsIgnoreCase("SHA-1")) {
|
|
||||||
return "sha1sum";
|
|
||||||
} else if (algorithm.equalsIgnoreCase("SHA-224")) {
|
|
||||||
return "sha224sum";
|
|
||||||
} else if (algorithm.equalsIgnoreCase("SHA-256")) {
|
|
||||||
return "sha256sum";
|
|
||||||
} else if (algorithm.equalsIgnoreCase("SHA-384")) {
|
|
||||||
return "sha384sum";
|
|
||||||
} else if (algorithm.equalsIgnoreCase("SHA-512")) {
|
|
||||||
return "sha512sum";
|
|
||||||
} else {
|
|
||||||
throw new CloudRuntimeException("Unknown checksum algorithm: " + algorithm);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean validateChecksum() {
|
public boolean validateChecksum() {
|
||||||
if (StringUtils.isNotBlank(checksum)) {
|
if (StringUtils.isNotBlank(checksum)) {
|
||||||
ChecksumValue providedChecksum = new ChecksumValue(checksum);
|
try {
|
||||||
String algorithm = providedChecksum.getAlgorithm();
|
return DigestHelper.check(checksum, new FileInputStream(downloadedFilePath));
|
||||||
String checksumCommand = "echo '%s %s' | %s -c --quiet";
|
} catch (IOException e) {
|
||||||
String cmd = String.format(checksumCommand, providedChecksum.getChecksum(), downloadedFilePath, getChecksumCommandFromAlgorithm(algorithm));
|
throw new CloudRuntimeException("could not check sum for file: " + downloadedFilePath,e);
|
||||||
int result = Script.runSimpleBashScriptForExitValue(cmd);
|
} catch (NoSuchAlgorithmException e) {
|
||||||
return result == 0;
|
throw new CloudRuntimeException("Unknown checksum algorithm: " + checksum, e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user