This is an automated email from the ASF dual-hosted git repository.
rohit pushed a commit to branch 4.11
in repository https://gitbox.apache.org/repos/asf/cloudstack.git
The following commit(s) were added to refs/heads/4.11 by this push:
new 5cf163d server: Unify templates/ISOs checksum API output (#2911)
5cf163d is described below
commit 5cf163d888a01a303fffd24a49fe5b53a615eb02
Author: Nicolas Vazquez <[email protected]>
AuthorDate: Sun Oct 21 14:03:04 2018 -0300
server: Unify templates/ISOs checksum API output (#2911)
Unify checksum API output for templates and ISOs: not list the checksum
algorithm on:
KVM direct downloads
On in progress normal template downloads. The algorithm is shown on the
listtemplates API, but after it is downloaded it is not shown anymore.
---
.../cloud/api/query/dao/TemplateJoinDaoImpl.java | 5 ++--
.../cloudstack/utils/security/DigestHelper.java | 15 +++++++++++
.../utils/security/DigestHelperTest.java | 31 +++++++++++++++++++---
3 files changed, 46 insertions(+), 5 deletions(-)
diff --git a/server/src/com/cloud/api/query/dao/TemplateJoinDaoImpl.java
b/server/src/com/cloud/api/query/dao/TemplateJoinDaoImpl.java
index 4f19842..c0d57d7 100644
--- a/server/src/com/cloud/api/query/dao/TemplateJoinDaoImpl.java
+++ b/server/src/com/cloud/api/query/dao/TemplateJoinDaoImpl.java
@@ -25,6 +25,7 @@ import java.util.Set;
import javax.inject.Inject;
+import org.apache.cloudstack.utils.security.DigestHelper;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
@@ -188,7 +189,7 @@ public class TemplateJoinDaoImpl extends
GenericDaoBaseWithTagInformation<Templa
templateResponse.setPhysicalSize(templatePhysicalSize);
}
- templateResponse.setChecksum(template.getChecksum());
+
templateResponse.setChecksum(DigestHelper.getHashValueFromChecksumValue(template.getChecksum()));
if (template.getSourceTemplateId() != null) {
templateResponse.setSourceTemplateId(template.getSourceTemplateUuid());
}
@@ -320,7 +321,7 @@ public class TemplateJoinDaoImpl extends
GenericDaoBaseWithTagInformation<Templa
isoResponse.setFeatured(iso.isFeatured());
isoResponse.setCrossZones(iso.isCrossZones());
isoResponse.setPublic(iso.isPublicTemplate());
- isoResponse.setChecksum(iso.getChecksum());
+
isoResponse.setChecksum(DigestHelper.getHashValueFromChecksumValue(iso.getChecksum()));
isoResponse.setOsTypeId(iso.getGuestOSUuid());
isoResponse.setOsTypeName(iso.getGuestOSName());
diff --git
a/utils/src/main/java/org/apache/cloudstack/utils/security/DigestHelper.java
b/utils/src/main/java/org/apache/cloudstack/utils/security/DigestHelper.java
index 40b0c1c..f856b1f 100644
--- a/utils/src/main/java/org/apache/cloudstack/utils/security/DigestHelper.java
+++ b/utils/src/main/java/org/apache/cloudstack/utils/security/DigestHelper.java
@@ -116,4 +116,19 @@ public class DigestHelper {
}
}
}
+
+ /**
+ * True if the algorithm is present on the checksum value. Format:
{ALG}HASH
+ */
+ protected static boolean isAlgorithmPresent(String checksum) {
+ return StringUtils.isNotBlank(checksum) && checksum.contains("{") &&
checksum.contains("}") &&
+ checksum.indexOf("{") == 0 && checksum.indexOf("}") >
checksum.indexOf("{");
+ }
+
+ /**
+ * Returns the checksum HASH from the checksum value which can have the
following formats: {ALG}HASH or HASH
+ */
+ public static String getHashValueFromChecksumValue(String checksum) {
+ return isAlgorithmPresent(checksum) ? new
ChecksumValue(checksum).getChecksum() : checksum;
+ }
}
diff --git
a/utils/src/test/java/org/apache/cloudstack/utils/security/DigestHelperTest.java
b/utils/src/test/java/org/apache/cloudstack/utils/security/DigestHelperTest.java
index 4a6e3f7..eac234e 100644
---
a/utils/src/test/java/org/apache/cloudstack/utils/security/DigestHelperTest.java
+++
b/utils/src/test/java/org/apache/cloudstack/utils/security/DigestHelperTest.java
@@ -26,6 +26,9 @@ import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
public class DigestHelperTest {
private final static String INPUT_STRING =
"01234567890123456789012345678901234567890123456789012345678901234567890123456789\n";
@@ -46,17 +49,17 @@ public class DigestHelperTest {
@Test
public void check_SHA256() throws Exception {
- Assert.assertTrue(DigestHelper.check(SHA256_CHECKSUM, inputStream));
+ assertTrue(DigestHelper.check(SHA256_CHECKSUM, inputStream));
}
@Test
public void check_SHA1() throws Exception {
- Assert.assertTrue(DigestHelper.check(SHA1_CHECKSUM, inputStream));
+ assertTrue(DigestHelper.check(SHA1_CHECKSUM, inputStream));
}
@Test
public void check_MD5() throws Exception {
- Assert.assertTrue(DigestHelper.check(MD5_CHECKSUM, inputStream));
+ assertTrue(DigestHelper.check(MD5_CHECKSUM, inputStream));
}
@Test
@@ -127,6 +130,28 @@ public class DigestHelperTest {
String checksum = SHA256_CHECKSUM + "XXXXX";
DigestHelper.validateChecksumString(checksum);
}
+
+ @Test
+ public void testIsAlgorithmPresentPositiveCase() {
+ assertTrue(DigestHelper.isAlgorithmSupported(SHA256_CHECKSUM));
+ }
+
+ @Test
+ public void testIsAlgorithmPresentnegativeCase() {
+
assertTrue(DigestHelper.isAlgorithmSupported(SHA256_NO_PREFIX_CHECKSUM));
+ }
+
+ @Test
+ public void testGetHashValueFromChecksumValuePrefixPresent() {
+ String checksum =
DigestHelper.getHashValueFromChecksumValue(SHA256_CHECKSUM);
+ assertEquals(SHA256_NO_PREFIX_CHECKSUM, checksum);
+ }
+
+ @Test
+ public void testGetHashValueFromChecksumValueNoPrefixPresent() {
+ String checksum =
DigestHelper.getHashValueFromChecksumValue(SHA256_NO_PREFIX_CHECKSUM);
+ assertEquals(SHA256_NO_PREFIX_CHECKSUM, checksum);
+ }
}
//Generated with love by TestMe :) Please report issues and submit feature
requests at: http://weirddev.com/forum#!/testme
\ No newline at end of file