コード例 #1
0
ファイル: fatcontroler.py プロジェクト: spinto/vmcatcher
 def _retiver_uri(self,metadata):
     retriever = retrieveFacard.retrieveFacard()
     
     if "uri" in metadata.keys():
         if metadata["uri"] != None:
             if len(metadata["uri"]) > 0:
                uri = metadata["uri"]
                retriever.uri = uri
    
     if "userName" in metadata.keys():
         if metadata["userName"] != None:
             if len(metadata["userName"]) > 0:
                 userName = metadata["userName"]
                 retriever.username = userName
     if "password" in metadata.keys():
         if metadata["password"] != None:
             if len(metadata["password"]) > 0:
                 password = metadata["password"]
                 retriever.password = password
     if "anchor" in metadata.keys():
         if metadata["anchor"] != None:
             if len(metadata["anchor"]) > 0:
                anchor = metadata["anchor"]
                retriever.trustanchor = anchor
     if "filename" in metadata.keys():
         if metadata["filename"] != None:
             if len(metadata["filename"]) > 0:
                filename = metadata["filename"]
                retriever.uri = filename
     if "protocol" in metadata.keys():
         if metadata["protocol"] != None:
             if len(metadata["protocol"]) > 0:
                protocol = metadata["protocol"]
                retriever.protocol = protocol
     
                self.log.debug("protocol=%s" % (protocol))
     if "server" in metadata.keys():
         if metadata["server"] != None:
             if len(metadata["server"]) > 0:
                server = metadata["server"]
                retriever.server = server
     
                self.log.debug("server=%s" % (server))
     
     resultDict = retriever.requestAsString()
     
     if resultDict == None:
         return {'code' : 800}
     resultDict['uri'] = uriNormaliseAnonymous(retriever.uri)
     return resultDict
コード例 #2
0
    def _retiver_uri(self, metadata):
        retriever = retrieveFacard.retrieveFacard()

        if "uri" in metadata.keys():
            if metadata["uri"] != None:
                if len(metadata["uri"]) > 0:
                    uri = metadata["uri"]
                    retriever.uri = uri

        if "userName" in metadata.keys():
            if metadata["userName"] != None:
                if len(metadata["userName"]) > 0:
                    userName = metadata["userName"]
                    retriever.username = userName
        if "password" in metadata.keys():
            if metadata["password"] != None:
                if len(metadata["password"]) > 0:
                    password = metadata["password"]
                    retriever.password = password
        if "anchor" in metadata.keys():
            if metadata["anchor"] != None:
                if len(metadata["anchor"]) > 0:
                    anchor = metadata["anchor"]
                    retriever.trustanchor = anchor
        if "filename" in metadata.keys():
            if metadata["filename"] != None:
                if len(metadata["filename"]) > 0:
                    filename = metadata["filename"]
                    retriever.uri = filename
        if "protocol" in metadata.keys():
            if metadata["protocol"] != None:
                if len(metadata["protocol"]) > 0:
                    protocol = metadata["protocol"]
                    retriever.protocol = protocol

                    self.log.debug("protocol=%s" % (protocol))
        if "server" in metadata.keys():
            if metadata["server"] != None:
                if len(metadata["server"]) > 0:
                    server = metadata["server"]
                    retriever.server = server

                    self.log.debug("server=%s" % (server))

        resultDict = retriever.requestAsString()

        if resultDict == None:
            return {'code': 800}
        resultDict['uri'] = uriNormaliseAnonymous(retriever.uri)
        return resultDict
コード例 #3
0
ファイル: fatcontroler.py プロジェクト: spinto/vmcatcher
    def subscription_update(self,Session,subscription):
        subscriptionKey = int(subscription.id)
        ProcessingSubscriptionUuid = str(subscription.identifier)
        self.log.info("Updating:%s" % (ProcessingSubscriptionUuid))
        
        retriever = retrieveFacard.retrieveFacard()
        retriever.uri = subscription.uri
        resultDict = self._retiver_uri({"uri" : subscription.uri,
            "trustAnchor" : subscription.trustAnchor,
            "userName" : subscription.userName,
            "password" : subscription.password,
            })
        
        
        rc = resultDict['code']
        if rc != 0:
            if 'error' in resultDict:
                self.log.error("%s, while retrieving %s" % (resultDict['error'],retriever.uri))
            else:
                self.log.error("Download of uri '%s' failed." % (subscriptionKey))
            if rc > 255:
                return rc
            else:
                return 10
        
        update_unprocessed = resultDict['responce']
        #update_unprocessed = str(f.read())
        # Now we have the update lets first check its hash
        messagehash = hashlib.sha512(update_unprocessed).hexdigest()
        now = datetime.datetime.utcnow()
        metadataFV = {
            u'hv:uri' : str(subscription.uri),
            u'dc:identifier' : str(subscription.identifier),
        }
        #self.log.error("errr:%s" % (ProcessingSubscriptionUuid))
        checker = fileView(self.anchor,update_unprocessed,metadataFV)
        if checker.errorNo != 0:
            self.log.error("Failed to verify subscription '%s' with URI '%s'" % (subscription.identifier,subscription.uri))
            self.log.debug(update_unprocessed)
            return checker.errorNo
        if checker.Json == None:
            return 14
        metadata = checker.vmilist.metadata
        metadata[u'data'] = update_unprocessed
        metadata[u'data-hash'] = messagehash
        if checker.errorNo != 0:
            self.log.info('Message Expired:%s' % (ProcessingSubscriptionUuid))
            metadata[u'expired'] = now
            Session.commit()
        # Now we know the data better check the SubscriptionAuth
        subq = Session.query(model.Subscription, model.SubscriptionAuth).\
            filter(model.Endorser.id == model.EndorserPrincible.id).\
            filter(model.EndorserPrincible.hv_dn == checker.subject).\
            filter(model.EndorserPrincible.hv_ca == checker.issuer).\
            filter(model.SubscriptionAuth.endorser == model.Endorser.id).\
            filter(model.SubscriptionAuth.subscription == model.Subscription.id).\
            filter(model.Subscription.id == subscription.id)

        count = subq.count()
        if count == 0:
            self.log.error("Endorser subject='%s' issuer='%s' not authorised on subscription '%s'" % (checker.subject,checker.issuer,ProcessingSubscriptionUuid))
            # Error code - Endorser not authorised on subscription.
            return 13
        if count != 1:
            self.log.error('Database Error processing subq:%s' % (ProcessingSubscriptionUuid))
            assert (False)
        subscription, auth = subq.one()
        
        
        # Sets 
        VersionCompare = 0
        qeryJunction = Session.query(model.ImageListInstance).\
            filter(model.Subscription.imagelist_latest == model.ImageListInstance.id).\
            filter(model.Subscription.id == subscription.id)
        
        
        
        if qeryJunction.count() == 0:
            #"we have no older version"
            self.log.info("First version of:%s" % (ProcessingSubscriptionUuid))
        else:
            if qeryJunction.count() != 1:
                self.log.error('Database Error processing  qeryJunction:%s' % (ProcessingSubscriptionUuid))
                assert (False)
            imageList = qeryJunction.one()
            if imageList.data_hash == messagehash:
                self.log.debug('Same version:%s' % (ProcessingSubscriptionUuid))
                if now > imageList.expires:
                    self.log.info("Image list '%s' has expired on: '%s'" % (ProcessingSubscriptionUuid,imageList.expires))
                    if imageList.expired == None:
                        imageList.expired = now
                        Session.commit()
                # We now know imageList is not too old.
                if ((imageList.expired != None) and (checker.errorNo == 0)):
                    # we have expired previously but now it looks good.
                    self.log.info('imageList Validated:%s' % (ProcessingSubscriptionUuid))
                    imageList.expired = None
                    Session.commit()
                if ((imageList.expired == None) and (checker.errorNo != 0)):
                    # should expire.
                    self.log.info('imageList Expired:%s' % (ProcessingSubscriptionUuid))
                    imageList.expired = now
                    Session.commit()
                return 0
            
                
                 
            messageVersion = checker.Json[u'hv:imagelist'][u'hv:version']
            self.log.debug('Downloaded version:%s' % (messageVersion))
            VersionCompare = split_numeric_sort(imageList.version,messageVersion)
            if VersionCompare == 0:
                self.log.warning('Downloaded version "%s" version "%s" has the same version number than the old version "%s".' % (ProcessingSubscriptionUuid,messageVersion, imageList.version))                
                #return 16 #  16 New version number is same as old version number.
            if VersionCompare < 0:
                self.log.error('Downloaded version "%s" version "%s" has lower version number than the old version "%s".' % (ProcessingSubscriptionUuid,messageVersion, imageList.version))
                return 17 #  17 New version number is less than old version number.
        
        metadata[u'hv:uri'] = uriNormaliseAnonymous(metadata[u'hv:uri'])
        
        imagelist = model.ImageListInstance(auth.id,metadata)
        Session.add(imagelist)
        try:
            Session.commit()
        except IntegrityError,E:
            self.log.error("Database integrity error '%s' processing '%s'." % (E.args,ProcessingSubscriptionUuid))
            self.log.debug(E.params)
            Session.rollback()
            # Error code - Database integrity error.
            return 15
コード例 #4
0
    def subscription_update(self, Session, subscription):
        subscriptionKey = int(subscription.id)
        ProcessingSubscriptionUuid = str(subscription.identifier)
        self.log.info("Updating:%s" % (ProcessingSubscriptionUuid))

        retriever = retrieveFacard.retrieveFacard()
        retriever.uri = subscription.uri
        resultDict = self._retiver_uri({
            "uri": subscription.uri,
            "trustAnchor": subscription.trustAnchor,
            "userName": subscription.userName,
            "password": subscription.password,
        })

        rc = resultDict['code']
        if rc != 0:
            if 'error' in resultDict:
                self.log.error("%s, while retrieving %s" %
                               (resultDict['error'], retriever.uri))
            else:
                self.log.error("Download of uri '%s' failed." %
                               (subscriptionKey))
            if rc > 255:
                return rc
            else:
                return 10

        update_unprocessed = resultDict['responce']
        #update_unprocessed = str(f.read())
        # Now we have the update lets first check its hash
        messagehash = hashlib.sha512(update_unprocessed).hexdigest()
        now = datetime.datetime.utcnow()
        metadataFV = {
            u'hv:uri': str(subscription.uri),
            u'dc:identifier': str(subscription.identifier),
        }
        #self.log.error("errr:%s" % (ProcessingSubscriptionUuid))
        checker = fileView(self.anchor, update_unprocessed, metadataFV)
        if checker.errorNo != 0:
            self.log.error("Failed to verify subscription '%s' with URI '%s'" %
                           (subscription.identifier, subscription.uri))
            self.log.debug(update_unprocessed)
            return checker.errorNo
        if checker.Json == None:
            return 14
        metadata = checker.vmilist.metadata
        metadata[u'data'] = update_unprocessed
        metadata[u'data-hash'] = messagehash
        if checker.errorNo != 0:
            self.log.info('Message Expired:%s' % (ProcessingSubscriptionUuid))
            metadata[u'expired'] = now
            Session.commit()
        # Now we know the data better check the SubscriptionAuth
        subq = Session.query(model.Subscription, model.SubscriptionAuth).\
            filter(model.Endorser.id == model.EndorserPrincible.id).\
            filter(model.EndorserPrincible.hv_dn == checker.subject).\
            filter(model.EndorserPrincible.hv_ca == checker.issuer).\
            filter(model.SubscriptionAuth.endorser == model.Endorser.id).\
            filter(model.SubscriptionAuth.subscription == model.Subscription.id).\
            filter(model.Subscription.id == subscription.id)

        count = subq.count()
        if count == 0:
            self.log.error(
                "Endorser subject='%s' issuer='%s' not authorised on subscription '%s'"
                %
                (checker.subject, checker.issuer, ProcessingSubscriptionUuid))
            # Error code - Endorser not authorised on subscription.
            return 13
        if count != 1:
            self.log.error('Database Error processing subq:%s' %
                           (ProcessingSubscriptionUuid))
            assert (False)
        subscription, auth = subq.one()

        # Sets
        VersionCompare = 0
        qeryJunction = Session.query(model.ImageListInstance).\
            filter(model.Subscription.imagelist_latest == model.ImageListInstance.id).\
            filter(model.Subscription.id == subscription.id)

        if qeryJunction.count() == 0:
            #"we have no older version"
            self.log.info("First version of:%s" % (ProcessingSubscriptionUuid))
        else:
            if qeryJunction.count() != 1:
                self.log.error('Database Error processing  qeryJunction:%s' %
                               (ProcessingSubscriptionUuid))
                assert (False)
            imageList = qeryJunction.one()
            if imageList.data_hash == messagehash:
                self.log.debug('Same version:%s' %
                               (ProcessingSubscriptionUuid))
                if now > imageList.expires:
                    self.log.info(
                        "Image list '%s' has expired on: '%s'" %
                        (ProcessingSubscriptionUuid, imageList.expires))
                    if imageList.expired == None:
                        imageList.expired = now
                        Session.commit()
                # We now know imageList is not too old.
                if ((imageList.expired != None) and (checker.errorNo == 0)):
                    # we have expired previously but now it looks good.
                    self.log.info('imageList Validated:%s' %
                                  (ProcessingSubscriptionUuid))
                    imageList.expired = None
                    Session.commit()
                if ((imageList.expired == None) and (checker.errorNo != 0)):
                    # should expire.
                    self.log.info('imageList Expired:%s' %
                                  (ProcessingSubscriptionUuid))
                    imageList.expired = now
                    Session.commit()
                return 0

            messageVersion = checker.Json[u'hv:imagelist'][u'hv:version']
            self.log.debug('Downloaded version:%s' % (messageVersion))
            VersionCompare = split_numeric_sort(imageList.version,
                                                messageVersion)
            if VersionCompare == 0:
                self.log.warning(
                    'Downloaded version "%s" version "%s" has the same version number than the old version "%s".'
                    % (ProcessingSubscriptionUuid, messageVersion,
                       imageList.version))
                #return 16 #  16 New version number is same as old version number.
            if VersionCompare < 0:
                self.log.error(
                    'Downloaded version "%s" version "%s" has lower version number than the old version "%s".'
                    % (ProcessingSubscriptionUuid, messageVersion,
                       imageList.version))
                return 17  #  17 New version number is less than old version number.

        metadata[u'hv:uri'] = uriNormaliseAnonymous(metadata[u'hv:uri'])

        imagelist = model.ImageListInstance(auth.id, metadata)
        Session.add(imagelist)
        try:
            Session.commit()
        except IntegrityError, E:
            self.log.error("Database integrity error '%s' processing '%s'." %
                           (E.args, ProcessingSubscriptionUuid))
            self.log.debug(E.params)
            Session.rollback()
            # Error code - Database integrity error.
            return 15