use more forgiving checksum routine, ignoring dates and order in zip file
Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/694ac1dc Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/694ac1dc Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/694ac1dc Branch: refs/heads/master Commit: 694ac1dc258e226f9f6d5ff3e13909becec58cb3 Parents: 7a42cf8 Author: Alex Heneveld <[email protected]> Authored: Tue Jul 4 18:04:41 2017 +0100 Committer: Alex Heneveld <[email protected]> Committed: Tue Jul 4 18:04:41 2017 +0100 ---------------------------------------------------------------------- .../core/mgmt/ha/OsgiArchiveInstaller.java | 23 ++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/694ac1dc/core/src/main/java/org/apache/brooklyn/core/mgmt/ha/OsgiArchiveInstaller.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/brooklyn/core/mgmt/ha/OsgiArchiveInstaller.java b/core/src/main/java/org/apache/brooklyn/core/mgmt/ha/OsgiArchiveInstaller.java index a634230..1b06463 100644 --- a/core/src/main/java/org/apache/brooklyn/core/mgmt/ha/OsgiArchiveInstaller.java +++ b/core/src/main/java/org/apache/brooklyn/core/mgmt/ha/OsgiArchiveInstaller.java @@ -23,6 +23,9 @@ import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.util.Collections; +import java.util.Map; +import java.util.TreeMap; import java.util.jar.Attributes; import java.util.jar.Manifest; import java.util.zip.ZipEntry; @@ -36,6 +39,7 @@ import org.apache.brooklyn.core.mgmt.internal.ManagementContextInternal; import org.apache.brooklyn.core.typereg.BasicManagedBundle; import org.apache.brooklyn.core.typereg.RegisteredTypePredicates; import org.apache.brooklyn.util.collections.MutableList; +import org.apache.brooklyn.util.collections.MutableMap; import org.apache.brooklyn.util.core.ResourceUtils; import org.apache.brooklyn.util.core.osgi.BundleMaker; import org.apache.brooklyn.util.core.osgi.Osgis; @@ -280,7 +284,7 @@ class OsgiArchiveInstaller { if (result.code!=null) return ReferenceWithError.newInstanceWithoutError(result); assert inferredMetadata.isNameResolved() : "Should have resolved "+inferredMetadata; assert inferredMetadata instanceof BasicManagedBundle : "Only BasicManagedBundles supported"; - ((BasicManagedBundle)inferredMetadata).setChecksum(Streams.getMd5Checksum(new FileInputStream(zipFile))); + ((BasicManagedBundle)inferredMetadata).setChecksum(getChecksum(new ZipFile(zipFile))); final boolean updating; result.metadata = osgiManager.getManagedBundle(inferredMetadata.getVersionedName()); @@ -310,7 +314,7 @@ class OsgiArchiveInstaller { log.warn("Missing bundle checksum data for "+result+"; assuming bundle replacement is permitted"); } else if (!Objects.equal(result.getMetadata().getChecksum(), inferredMetadata.getChecksum())) { throw new IllegalArgumentException("Bundle "+result.getMetadata().getVersionedName()+" already installed; " - + "cannot install a different bundle at a same non-snapshot version"); + + "cannot install a different bundle at a same non-snapshot version"); } result.setIgnoringAlreadyInstalled(); return ReferenceWithError.newInstanceWithoutError(result); @@ -422,6 +426,21 @@ class OsgiArchiveInstaller { } } + private static String getChecksum(ZipFile zf) { + // checksum should ignore time/date stamps on files - just look at entries and contents. also ignore order. + // (tests fail without time/date is one reason, but really if a person rebuilds a ZIP that is the same + // files we should treat it as identical) + try { + Map<String,String> entriesToChecksum = MutableMap.of(); + for (ZipEntry ze: Collections.list(zf.entries())) { + entriesToChecksum.put(ze.getName(), Streams.getMd5Checksum(zf.getInputStream(ze))); + } + return Streams.getMd5Checksum(Streams.newInputStreamWithContents(new TreeMap<>(entriesToChecksum).toString())); + } catch (Exception e) { + throw Exceptions.propagate(e); + } + } + private boolean canUpdate() { // only update if forced, or it's a snapshot for which a byte stream is supplied // (IE don't update a snapshot verison every time its URL is referenced in a 'libraries' section)
