Esempio n. 1
0
    def read_v0(self, json_obj):
        """
        Reads v0 json data into this structure.
        :return:
        """
        rd = ResultAndData(False, None)

        new_links = []
        for link in json_obj[LINKS_KEY]:
            new_link = Link(None, [], NO_ACCESS, '')
            new_link.from_serializable(link)
            new_links.append(new_link)
        self._links = new_links

        new_groups = []
        for group in json_obj[GROUPS_KEY]:
            new_group = Group(0, '', [])
            new_group.from_serializable(group)
            new_groups.append(new_group)
            if new_group.id > self._next_group_id:
                self._next_group_id = new_group.id + 1
        self._groups = new_groups

        new_files = {}
        for file in json_obj[FILES_KEY]:
            new_file = FilePermissions(None)
            new_file.from_serializable(file)
            new_files[new_file._filepath] = new_file
        self._files = new_files

        rd = ResultAndData(True, None)
        return rd
Esempio n. 2
0
 def read_backend(self):
     rd = ResultAndData(False, None)
     try:
         path = self._file_location()
         in_file = open(path, mode='r')
         data = in_file.read()
         rd = ResultAndData(True, data)
     except IOError, e:
         rd = ResultAndData(False, str(e))
Esempio n. 3
0
 def add_group(self, group_id, permissions):
     # type: (int, int) -> ResultAndData
     rd = ResultAndData(False, None)
     if group_id in self.get_group_ids():
         old_perm = self._groups[str(group_id)]
         self._groups[str(group_id)] = permissions
         rd = ResultAndData(True, old_perm)
     else:
         self._groups[str(group_id)] = permissions
         rd = ResultAndData(True, None)
     return rd
Esempio n. 4
0
 def add_user(self, user_id, permissions):
     # type: (int, int) -> ResultAndData
     rd = ResultAndData(False, None)
     if user_id in self.get_user_ids():
         old_perm = self._users[str(user_id)]
         self._users[str(user_id)] = permissions
         rd = ResultAndData(True, old_perm)
     else:
         self._users[str(user_id)] = permissions
         rd = ResultAndData(True, None)
     return rd
Esempio n. 5
0
def get_client_session(uname, password):
    try:
        rem_sock = setup_test_remote_socket()
        rem_conn = RawConnection(rem_sock)
        request = ClientSessionRequestMessage(uname, password)
        rem_conn.send_obj(request)
        response = rem_conn.recv_obj()
        if not (response.type == CLIENT_SESSION_RESPONSE):
            raise Exception('remote did not respond with success')
        return ResultAndData(True, response)
    except Exception, e:
        return ResultAndData(False, e)
Esempio n. 6
0
 def write_backend(self, data):
     rd = ResultAndData(False, None)
     try:
         path = self._file_location()
         out_file = open(path, mode='w')
         out_file.write(data)
         out_file.close()
         # mylog('Wrote backend for [{}]"{}"'.format(
         #     self._cloud.my_id_from_remote, self._cloud.name), '32')
         rd = ResultAndData(True, None)
     except IOError, e:
         rd = ResultAndData(False, str(e))
         mylog(str(e))
Esempio n. 7
0
    def get_remote_conn(self):
        # type: () -> ResultAndData
        # type: () -> ResultAndData(True, RawConnection)
        # type: () -> ResultAndData(False, Exception)
        from host.util import setup_remote_socket

        rd = ResultAndData(False, None)
        try:
            rd = setup_remote_socket(self)
            if rd.success:
                conn = RawConnection(rd.data)
                rd = ResultAndData(True, conn)
            else:
                return rd
        except Exception, e:
            rd = ResultAndData(False, e)
Esempio n. 8
0
def get_client_host(sid, cloud_uname, cname):
    try:
        rem_sock = setup_test_remote_socket()
        if rem_sock is None:
            log_warn('Failed to get client host socket')
            return Error()
        rem_conn = RawConnection(rem_sock)

        msg = ClientGetCloudHostRequestMessage(sid, cloud_uname, cname)
        rem_conn.send_obj(msg)
        response = rem_conn.recv_obj()
        if not (response.type == CLIENT_GET_CLOUD_HOST_RESPONSE):
            raise Exception('remote did not respond with success CGCR')
        return ResultAndData(True, response)
    except Exception, e:
        return ResultAndData(False, e)
Esempio n. 9
0
def do_client_get_cloud_hosts(db, session_id, cloud_uname, cname):
    # type: (SimpleDB, str, str, str) -> ResultAndData
    # type: (SimpleDB, str, str, str) -> ResultAndData(True, [dict])
    # type: (SimpleDB, str, str, str) -> ResultAndData(False, BaseMessage)
    _log = get_mylog()
    rd = get_user_from_session(db, session_id)
    if not rd.success:
        return ResultAndData(False, InvalidStateMessage(rd.data))
    else:
        user = rd.data

    # todo: also use uname to lookup cloud
    cloud = get_cloud_by_name(db, cloud_uname, cname)
    if cloud is None:
        msg = 'Cloud {}/{} does not exist'.format(cloud_uname, cname)
        _log.debug(msg)
        return Error(InvalidStateMessage(msg))
    if not cloud.can_access(user):
        msg = 'You do not have permission to access {}/{} '.format(
            cloud_uname, cname)
        _log.debug(msg)
        return Error(InvalidStateMessage(msg))

    # todo:37 maybe this should be an option in the API, to get all or only active
    # For now I'm defaulting to active, becuase all mirror attempts make a host,
    #   Which is bad todo:38
    hosts = [host.to_dict() for host in cloud.all_hosts()]
    # hosts = [host.to_dict() for host in cloud.hosts.all()]
    return Success(hosts)
Esempio n. 10
0
def check_file_contents(root, path, data):
    try:
        handle = open(os.path.join(root, path))
        contents = handle.read()
        handle.close()
        return ResultAndData(data == contents,
                             'Checking {} file contents'.format(path))
    except Exception, e:
        return Error(e)
Esempio n. 11
0
 def get_user(self):
     print 'Session get_user, {}'.format(self.uuid)
     # type: () -> ResultAndData
     if self.user is None:
         rd = Error('No user exists on remote\'s session, sid:{}'.format(
             self.uuid))
     else:
         rd = ResultAndData(True, self.user)
     return rd
Esempio n. 12
0
    def read_json(self, json_string):
        rd = ResultAndData(False, None)
        try:
            json_obj = json.loads(json_string)
            if MAJ_VER_KEY not in json_string:
                raise ValueError

            maj_ver = json_obj[MAJ_VER_KEY]
            if maj_ver == 0:
                rd = self.read_v0(json_obj)
            else:
                message = 'Failed to decode .nebs data with invalid version ' \
                          '{}'.format(maj_ver)
                mylog(message, '31')
                rd = ResultAndData(False, message)

        except ValueError, e:
            mylog('ERROR: Failed to decode json data', '31')
            rd = ResultAndData(False, e)
Esempio n. 13
0
 def write(self, path, data):
     msg = ClientFilePutMessage(self.sid, self.cloud_uname, self.cname,
                                path)
     conn = create_sock_and_send(self.ip, self.port, msg)
     msg = ClientFileTransferMessage(self.sid, self.cloud_uname, self.cname,
                                     path, len(data), False)
     conn.send_obj(msg)
     conn.send_next_data(data)
     conn.send_obj(
         ClientFileTransferMessage(self.sid, self.cloud_uname, self.cname,
                                   None, None, None))
     return ResultAndData(True, 'Write doesnt check success LOL todo')
Esempio n. 14
0
def do_client_stat_files(host_obj, connection, address, msg_obj, client,
                         cloud):
    _log = get_mylog()
    cloudname = cloud.name
    session_id = client.uuid if client is not None else None
    client_uid = client.user_id if client else PUBLIC_USER_ID

    private_data = host_obj.get_private_data(cloud)
    if private_data is None:
        msg = 'Somehow the cloud doesn\'t have a privatedata associated with it'
        err = InvalidStateMessage(msg)
        host_obj.log_client(client, 'stat', cloud, None, 'error')
        send_error_and_close(err, connection)
        return Error(err)

    rel_path = RelativePath()
    rd = rel_path.from_relative(msg_obj.fpath)
    if not rd.success:
        msg = '{} is not a valid cloud path'.format(msg_obj.fpath)
        err = InvalidStateMessage(msg)
        _log.debug(err)
        send_error_and_close(err, connection)
        host_obj.log_client(client, 'stat', cloud, rel_path, 'error')
        return Error(err)

    rd = host_obj.client_access_check_or_close(connection, session_id, cloud,
                                               rel_path, READ_ACCESS)
    if rd.success:
        full_path = rel_path.to_absolute(cloud.root_directory)
        if not os.path.exists(full_path):
            resp = FileDoesNotExistErrorMessage()
            host_obj.log_client(client, 'stat', cloud, rel_path, 'error')

        # elif not os.path.isdir(full_path):
        #     mylog('Responding to ClientListFiles with error - {} is a file, not dir.'.format(rel_path.to_string()))
        #     resp = FileIsNotDirErrorMessage()
        #     host_obj.log_client(client, 'ls', cloud, rel_path, 'error')

        else:
            mylog('Responding successfully to ClientStatFile')
            resp = StatFileResponseMessage(cloudname, session_id,
                                           rel_path.to_string())
            resp.stat = make_stat_dict(rel_path, private_data, cloud,
                                       client_uid)
            # resp.ls = make_ls_array(rel_path, private_data, cloud, client_uid)
            host_obj.log_client(client, 'stat', cloud, rel_path, 'success')

        connection.send_obj(resp)
        return ResultAndData(resp.type == STAT_FILE_RESPONSE, resp)
    else:
        # the access check will send error
        host_obj.log_client(client, 'stat', cloud, rel_path, 'error')
        return rd
Esempio n. 15
0
 def mkdir(self, path):
     log_text('making directory for {}/{}, {}'.format(
         self.cloud_uname, self.cname, path))
     msg = ClientFilePutMessage(self.sid, self.cloud_uname, self.cname,
                                path)
     conn = create_sock_and_send(self.ip, self.port, msg)
     msg = ClientFileTransferMessage(self.sid, self.cloud_uname, self.cname,
                                     path, 0, True)
     conn.send_obj(msg)
     conn.send_obj(
         ClientFileTransferMessage(self.sid, self.cloud_uname, self.cname,
                                   None, None, None))
     log_text('Sent CFT message')
     return ResultAndData(True, 'Write doesnt check success LOL todo')
Esempio n. 16
0
def verify_host(db, cloud_uname, cname, local_id, other_id):
    """
    Returns either (False, error_string) or (True, matching_mirror)
    """
    rd = ResultAndData(False, None)
    mylog('verify_host 0')
    # I'm naming this a mirror because that's what it is.
    # The other host was told to come look for a particular mirror here.
    # if that mirror isn't here, (but another mirror of that cloud is), don't
    # process this request.
    mirror = db.session.query(Cloud).filter_by(
        my_id_from_remote=local_id).first()
    mylog('verify_host 1')
    if mirror is None:
        err = 'That mirror isn\'t on this host.'
        rd = ResultAndData(False, err)
        mylog('verify_host 2')
    else:
        rd = mirror.get_remote_conn()
        if rd.success:
            mylog('verify_host 3')
            rem_conn = rd.data
            msg = HostVerifyHostRequestMessage(local_id, other_id, cloud_uname,
                                               cname)
            try:
                rem_conn.send_obj(msg)
                response = rem_conn.recv_obj()
                if response.type == HOST_VERIFY_HOST_SUCCESS:
                    rd = ResultAndData(True, mirror)
                elif response.type == HOST_VERIFY_HOST_FAILURE:
                    rd = ResultAndData(
                        False, 'Remote responded with failure: "{}"'.format(
                            response.message))
                else:
                    rd = ResultAndData(
                        False, 'Unknown error while attempting to verify host')
            except Exception, e:
                rd = ResultAndData(False, e)
Esempio n. 17
0
 def ls(self, path):
     msg = ListFilesRequestMessage(self.sid, self.cloud_uname, self.cname,
                                   path)
     response = create_sock_msg_get_response(self.ip, self.port, msg)
     return ResultAndData(response.type == LIST_FILES_RESPONSE, response)
Esempio n. 18
0
def recv_file_tree(host_obj, msg, cloud, socket_conn, db):
    rd = ResultAndData(True, None)
    while (msg.fsize is not None) and rd.success:
        is_client = msg.type == CLIENT_FILE_TRANSFER
        rd = recv_file_transfer(host_obj, msg, cloud, socket_conn, db, is_client)
        msg = socket_conn.recv_obj()