Exemplo n.º 1
0
 def doSendMyListFiles(self, *args, **kwargs):
     """
     Action method.
     """
     json_list_files = backup_fs.Serialize(
         to_json=True,
         filter_cb=lambda path_id, path, info: True if strng.to_text(info.key_id) == strng.to_text(self.key_id) else False,
     )
     # raw_list_files = json.dumps(json_list_files, indent=2, encoding='utf-8')
     raw_list_files = serialization.DictToBytes(json_list_files, keys_to_text=True, values_to_text=True)
     if _Debug:
         lg.out(_DebugLevel, 'shared_access_donor.doSendMyListFiles prepared list of files for %s :\n%s' % (
             self.remote_idurl, raw_list_files))
     block = encrypted.Block(
         CreatorID=my_id.getLocalID(),
         BackupID=self.key_id,
         Data=raw_list_files,
         SessionKey=key.NewSessionKey(),
         EncryptKey=self.key_id,
     )
     encrypted_list_files = block.Serialize()
     packet_id = "%s:%s" % (self.key_id, packetid.UniqueID(), )
     p2p_service.SendFiles(
         idurl=self.remote_idurl,
         raw_list_files_info=encrypted_list_files,
         packet_id=packet_id,
         callbacks={
             commands.Ack(): lambda response, _: self.automat('list-files-ok', response),
             commands.Fail(): lambda response, _: self.automat('fail', Exception(str(response))),
             None: lambda pkt_out: self.automat('fail', Exception('timeout')),
         },
     )
Exemplo n.º 2
0
def send(customer_idurl, packet_id, format_type, key_id, remote_idurl):
    if not my_keys.is_key_registered(key_id):
        lg.warn(
            'not able to return Files() for customer %s, key %s not registered'
            % (
                customer_idurl,
                key_id,
            ))
        return p2p_service.SendFailNoRequest(customer_idurl, packet_id)
    if _Debug:
        lg.out(
            _DebugLevel,
            "list_files.send to %s, customer_idurl=%s, key_id=%s" % (
                remote_idurl,
                customer_idurl,
                key_id,
            ))
    ownerdir = settings.getCustomerFilesDir(customer_idurl)
    plaintext = ''
    if os.path.isdir(ownerdir):
        for key_alias in os.listdir(ownerdir):
            if not misc.ValidKeyAlias(str(key_alias)):
                continue
            key_alias_dir = os.path.join(ownerdir, key_alias)
            plaintext += TreeSummary(key_alias_dir, key_alias)
    else:
        lg.warn('did not found customer dir: %s' % ownerdir)
    if _Debug:
        lg.out(_DebugLevel + 8, '\n%s' % plaintext)
    raw_list_files = PackListFiles(plaintext, format_type)
    block = encrypted.Block(
        CreatorID=my_id.getLocalID(),
        BackupID=key_id,
        Data=raw_list_files,
        SessionKey=key.NewSessionKey(),
        EncryptKey=key_id,
    )
    encrypted_list_files = block.Serialize()
    newpacket = p2p_service.SendFiles(
        idurl=remote_idurl,
        raw_list_files_info=encrypted_list_files,
        packet_id=packet_id,
        callbacks={
            commands.Ack(): on_acked,
            commands.Fail(): on_failed,
            None: on_timeout,
        },
    )
    return newpacket
Exemplo n.º 3
0
def send(customer_idurl, packet_id, format_type):
    customer_name = nameurl.GetName(customer_idurl)
    if _Debug:
        lg.out(
            _DebugLevel, "list_files.send to %s, format is '%s'" %
            (customer_name, format_type))
    ownerdir = settings.getCustomerFilesDir(customer_idurl)
    plaintext = ''
    if os.path.isdir(ownerdir):
        for key_alias in os.listdir(ownerdir):
            if not misc.ValidKeyAlias(str(key_alias)):
                continue
            key_alias_dir = os.path.join(ownerdir, key_alias)
            plaintext += TreeSummary(key_alias_dir, key_alias)
    else:
        lg.warn('did not found customer dir: %s' % ownerdir)
    if _Debug:
        lg.out(_DebugLevel + 8, '\n%s' % plaintext)
    return p2p_service.SendFiles(
        idurl=customer_idurl,
        raw_list_files_info=PackListFiles(plaintext, format_type),
        packet_id=packet_id,
    )
Exemplo n.º 4
0
 def doSendMyListFiles(self, arg):
     """
     Action method.
     """
     json_list_files = backup_fs.Serialize(
         to_json=True,
         filter_cb=lambda path_id, path, info: True
         if info.key_id == self.key_id else False,
     )
     raw_list_files = json.dumps(json_list_files,
                                 indent=2,
                                 encoding='utf-8')
     block = encrypted.Block(
         CreatorID=my_id.getLocalID(),
         BackupID=self.key_id,
         Data=raw_list_files,
         SessionKey=key.NewSessionKey(),
         # encrypt data using public key of recipient
         EncryptKey=lambda inp: self.remote_identity.encrypt(inp),
     )
     encrypted_list_files = block.Serialize()
     packet_id = "%s:%s" % (self.key_id, packetid.UniqueID())
     p2p_service.SendFiles(
         idurl=self.remote_idurl,
         raw_list_files_info=encrypted_list_files,
         packet_id=packet_id,
         callbacks={
             commands.Ack():
             lambda response, _: self.automat('list-files-ok', response),
             commands.Fail():
             lambda response, _: self.automat('fail',
                                              Exception(str(response))),
             None:
             lambda pkt_out: self.automat('fail', Exception('timeout')),
         },
     )
Exemplo n.º 5
0
def send(customer_idurl,
         packet_id,
         format_type,
         key_id,
         remote_idurl,
         query_items=[]):
    if not query_items:
        query_items = [
            '*',
        ]
    key_id = my_keys.latest_key_id(key_id)
    parts = global_id.ParseGlobalID(key_id)
    if parts['key_alias'] == 'master' and parts['idurl'] != my_id.getIDURL():
        # lg.warn('incoming ListFiles() request with customer "master" key: %r' % key_id)
        if not my_keys.is_key_registered(key_id) and identitycache.HasKey(
                parts['idurl']):
            lg.info(
                'customer public key %r to be registered locally for the first time'
                % key_id)
            known_ident = identitycache.FromCache(parts['idurl'])
            if not my_keys.register_key(key_id, known_ident.getPublicKey()):
                lg.err(
                    'failed to register known public key of the customer: %r' %
                    key_id)
    if not my_keys.is_key_registered(key_id):
        lg.warn(
            'not able to return Files() for customer %s, key %s not registered'
            % (
                customer_idurl,
                key_id,
            ))
        return p2p_service.SendFailNoRequest(customer_idurl,
                                             packet_id,
                                             response='key not registered')
    if _Debug:
        lg.out(
            _DebugLevel,
            "list_files.send to %s, customer_idurl=%s, key_id=%s, query_items=%r"
            % (
                remote_idurl,
                customer_idurl,
                key_id,
                query_items,
            ))
    ownerdir = settings.getCustomerFilesDir(customer_idurl)
    plaintext = ''
    if os.path.isdir(ownerdir):
        try:
            for query_path in query_items:
                plaintext += process_query_item(query_path, parts['key_alias'],
                                                ownerdir)
        except:
            lg.exc()
            return p2p_service.SendFailNoRequest(
                customer_idurl,
                packet_id,
                response='list files query processing error')
    else:
        lg.warn('did not found customer folder: %s' % ownerdir)
    if _Debug:
        lg.out(_DebugLevel, '\n%s' % plaintext)
    raw_list_files = PackListFiles(plaintext, format_type)
    block = encrypted.Block(
        CreatorID=my_id.getIDURL(),
        BackupID=key_id,
        Data=raw_list_files,
        SessionKey=key.NewSessionKey(session_key_type=key.SessionKeyType()),
        SessionKeyType=key.SessionKeyType(),
        EncryptKey=key_id,
    )
    encrypted_list_files = block.Serialize()
    newpacket = p2p_service.SendFiles(
        idurl=remote_idurl,
        raw_list_files_info=encrypted_list_files,
        packet_id=packet_id,
        callbacks={
            commands.Ack(): on_acked,
            commands.Fail(): on_failed,
            None: on_timeout,
        },
    )
    return newpacket