def refresh(self): with self._syncRoot: lines = self._metaRW.readlines() self.log.debug("read lines (%s)=%s", self._metaRW.__class__.__name__, lines) newMD = {} declaredChecksum = None for line in lines: try: key, value = line.split("=", 1) value = value.strip() except ValueError: self.log.warn("Could not parse line `%s`.", line) continue if key == SHA_CKSUM_TAG: declaredChecksum = value continue newMD[key] = value if not newMD: self.log.debug("Empty metadata") self._isValid = True self._metadata = newMD return if declaredChecksum is None: # No checksum in the metadata, let it through as is # FIXME : This is ugly but necessary, What we need is a class # method that creates the initial metadata. Then we can assume # that empty metadata is always invalid. self.log.warn("data has no embedded checksum - " "trust it as it is") self._isValid = True self._metadata = newMD return checksumCalculator = hashlib.sha1() keys = newMD.keys() keys.sort() for key in keys: value = newMD[key] line = "%s=%s" % (key, value) checksumCalculator.update(_preprocessLine(line)) computedChecksum = checksumCalculator.hexdigest() if declaredChecksum != computedChecksum: self.log.warning( "data seal is broken metadata declares `%s` " "should be `%s` (lines=%s)", declaredChecksum, computedChecksum, newMD) raise se.MetaDataSealIsBroken(declaredChecksum, computedChecksum) self._isValid = True self._metadata = newMD
def _refresh(self): with self._syncRoot: lines = self._metaRW.readlines() log.debug("read lines (%s)=%s", self._metaRW.__class__.__name__, lines) newMD = _parse_lines(lines) declaredChecksum = newMD.pop(SHA_CKSUM_TAG, None) if not newMD: log.debug("Empty metadata") self._isValid = True self._metadata = newMD return if declaredChecksum is None: # No checksum in the metadata, let it through as is # FIXME : This is ugly but necessary, What we need is a class # method that creates the initial metadata. Then we can assume # that empty metadata is always invalid. log.warning("data has no embedded checksum - " "trust it as it is") self._isValid = True self._metadata = newMD return computedChecksum = _calc_checksum(_dump_lines(newMD)) if declaredChecksum != computedChecksum: log.warning("data seal is broken metadata declares `%s` " "should be `%s` (lines=%s)", declaredChecksum, computedChecksum, newMD) raise se.MetaDataSealIsBroken(declaredChecksum, computedChecksum) self._isValid = True self._metadata = newMD