Ejemplo n.º 1
0
def save_rd_password(password=None,
                     user=Default.User,
                     customer=Default.Customer,
                     conn=None):

    if not password:
        return {'pass': False, 'message': 'No password was provided.'}

    all_agents = list(
        r.table("agents").filter({
            "customer_name": customer
        }).run(conn))

    result = False
    msg = ''
    missed_count = 0
    for agent in all_agents:
        agent_id = agent['agent_id']

        operation = RaOperation(ra.RaValue.SetRdPassword,
                                agent_id,
                                username=user,
                                password=password,
                                customer=customer,
                                uri=ra.RaUri.Password,
                                method='POST')

        result = False
        msg = ''
        _id = save_operation(operation)
        if _id:

            operation.operation_id = _id
            store_in_agent_queue(operation)

        else:

            msg = ('Unable to process password operation for agent %s.' %
                   agent_id)

            missed_count += 1
            logger.error(msg)

    if missed_count == 0:
        result = True
        msg = ''

    else:

        result = False
        msg = ('Server was unable to process the password request for: '
               '%s agent(s).'
               'Please check the server logs for more details.' % missed_count)

    return {'pass': result, 'message': msg}
Ejemplo n.º 2
0
def remove_session(agent_id=None,
                   user=Default.User,
                   customer=Default.Customer):

    pid = ra.db.get_rd_pid(agent_id=agent_id)
    pid_msg = ''
    if pid:

        if stop_novnc(agent_id, pid):

            logger.info('Stopped NoVNC process for %s.' % agent_id)

        else:

            pid_msg = 'Unable to stop NoVNC for agent %s. ' % agent_id
            logger.error(pid_msg)

    session_closed, session_msg = ra.db.remove_connection(agent_id=agent_id)
    if session_msg:
        logger.info(session_msg)

    operation = RaOperation(ra.RaValue.StopRemoteDesktop,
                            agent_id,
                            username=user,
                            customer=customer)

    operation_id = save_operation(operation)
    _id_msg = ''
    if operation_id:

        operation.operation_id = operation_id
        store_in_agent_queue(operation)
        logger.info('Added operation to queue for %s.' % agent_id)

    else:

        _id_msg = ('Unable to send stop operation to agent %s.'
                   ' Agent side will most likely stay open... ' % agent_id)

        logger.error(_id_msg)

    ra.add_feedback(agent_id, ra.Status.Closing)

    result = (pid and operation_id and session_closed)
    message = pid_msg + _id_msg + session_msg

    return {'pass': result, 'message': message}
Ejemplo n.º 3
0
def remove_session(agent_id=None, user=Default.User, customer=Default.Customer):

    pid = ra.db.get_rd_pid(agent_id=agent_id)
    pid_msg = ""
    if pid:

        if stop_novnc(agent_id, pid):

            logger.info("Stopped NoVNC process for %s." % agent_id)

        else:

            pid_msg = "Unable to stop NoVNC for agent %s. " % agent_id
            logger.error(pid_msg)

    session_closed, session_msg = ra.db.remove_connection(agent_id=agent_id)
    if session_msg:
        logger.info(session_msg)

    operation = RaOperation(ra.RaValue.StopRemoteDesktop, agent_id, username=user, customer=customer)

    operation_id = save_operation(operation)
    _id_msg = ""
    if operation_id:

        operation.operation_id = operation_id
        store_in_agent_queue(operation)
        logger.info("Added operation to queue for %s." % agent_id)

    else:

        _id_msg = "Unable to send stop operation to agent %s." " Agent side will most likely stay open... " % agent_id

        logger.error(_id_msg)

    ra.add_feedback(agent_id, ra.Status.Closing)

    result = pid and operation_id and session_closed
    message = pid_msg + _id_msg + session_msg

    return {"pass": result, "message": message}
Ejemplo n.º 4
0
def new_rd_session(agent_id=None,
                   user=Default.User,
                   customer=Default.Customer):

    if not agent_id:
        return {'pass': False, 'message': 'No agent id was provided.'}

    res, session = ra.session_exist(agent_id)
    if res:

        status, web_port = session

        if status == ra.Status.Waiting:
            return {
                'pass': False,
                'message':
                ('Session exist for this agent. Waiting for response.'),
                ra.RaKey.Status: status
            }

        host = ra.get_hostname()
        uri = ra.create_vnc_uri(host, web_port)

        if status == ra.Status.Ready:
            return {
                'pass': False,
                'message': ('Session exist for this agent. Using session.'),
                ra.RaKey.Status: status,
                ra.RaKey.WebPort: web_port,
                ra.RaKey.Hostname: host,
                ra.RaKey.Uri: uri
            }

        if status == ra.Status.Timeout:

            ra.db.edit_connection(agent_id=agent_id, status=ra.Status.Ready)

            return {
                'pass': False,
                'message': ('Session exist for this agent. Reusing.'),
                ra.RaKey.Status: status,
                ra.RaKey.WebPort: web_port,
                ra.RaKey.Hostname: host,
                ra.RaKey.Uri: uri
            }

        if status == ra.Status.Closing:

            return {
                'pass':
                False,
                'message': ('Session exist but is being closed.'
                            'Please try again in a few seconds.'),
                ra.RaKey.Status:
                status,
            }

    operation = RaOperation(ra.RaValue.RemoteDesktop,
                            agent_id,
                            username=user,
                            customer=customer,
                            uri=ra.RaUri.StartRemoteDesktop % agent_id,
                            method='POST')

    port = None
    tunnel_needed = True

    if tunnel_needed:

        params = None
        port_range = list(ra.PortRange)
        for p in ra.PortRange:

            params = reverse_tunnel_params(port_range)
            if ra.db.port_available(port=params[TunnelKey.HostPort]):
                break

            port_range.remove(p)
            params = None

        if params:

            port = params[TunnelKey.HostPort]
            ssh_port = params[TunnelKey.SSHPort]
            operation.set_tunnel(host_port=port, ssh_port=ssh_port)

        else:

            return {
                'pass': False,
                'message': "Could not resolve host port for tunnel."
            }

    operation_id = save_operation(operation)
    if operation_id:

        result, msg = ra.db.save_connection(agent_id=agent_id,
                                            host_port=port,
                                            status=ra.Status.Waiting)

        if result:

            operation.operation_id = operation_id
            ra.add_feedback(agent_id, ra.Status.Waiting)
            store_in_agent_queue(operation)

            return {
                'pass': True,
                'message': 'Remote desktop created. Waiting...'
            }

        else:

            return {'pass': False, 'message': msg}

    else:

        return {
            'pass': False,
            'message': "Unable to save operation. Invalid operation ID."
        }
Ejemplo n.º 5
0
def new_rd_session(agent_id=None, user=Default.User, customer=Default.Customer):

    if not agent_id:
        return {"pass": False, "message": "No agent id was provided."}

    res, session = ra.session_exist(agent_id)
    if res:

        status, web_port = session

        if status == ra.Status.Waiting:
            return {
                "pass": False,
                "message": ("Session exist for this agent. Waiting for response."),
                ra.RaKey.Status: status,
            }

        host = ra.get_hostname()
        uri = ra.create_vnc_uri(host, web_port)

        if status == ra.Status.Ready:
            return {
                "pass": False,
                "message": ("Session exist for this agent. Using session."),
                ra.RaKey.Status: status,
                ra.RaKey.WebPort: web_port,
                ra.RaKey.Hostname: host,
                ra.RaKey.Uri: uri,
            }

        if status == ra.Status.Timeout:

            ra.db.edit_connection(agent_id=agent_id, status=ra.Status.Ready)

            return {
                "pass": False,
                "message": ("Session exist for this agent. Reusing."),
                ra.RaKey.Status: status,
                ra.RaKey.WebPort: web_port,
                ra.RaKey.Hostname: host,
                ra.RaKey.Uri: uri,
            }

        if status == ra.Status.Closing:

            return {
                "pass": False,
                "message": ("Session exist but is being closed." "Please try again in a few seconds."),
                ra.RaKey.Status: status,
            }

    operation = RaOperation(
        ra.RaValue.RemoteDesktop,
        agent_id,
        username=user,
        customer=customer,
        uri=ra.RaUri.StartRemoteDesktop % agent_id,
        method="POST",
    )

    port = None
    tunnel_needed = True

    if tunnel_needed:

        params = None
        port_range = list(ra.PortRange)
        for p in ra.PortRange:

            params = reverse_tunnel_params(port_range)
            if ra.db.port_available(port=params[TunnelKey.HostPort]):
                break

            port_range.remove(p)
            params = None

        if params:

            port = params[TunnelKey.HostPort]
            ssh_port = params[TunnelKey.SSHPort]
            operation.set_tunnel(host_port=port, ssh_port=ssh_port)

        else:

            return {"pass": False, "message": "Could not resolve host port for tunnel."}

    operation_id = save_operation(operation)
    if operation_id:

        result, msg = ra.db.save_connection(agent_id=agent_id, host_port=port, status=ra.Status.Waiting)

        if result:

            operation.operation_id = operation_id
            ra.add_feedback(agent_id, ra.Status.Waiting)
            store_in_agent_queue(operation)

            return {"pass": True, "message": "Remote desktop created. Waiting..."}

        else:

            return {"pass": False, "message": msg}

    else:

        return {"pass": False, "message": "Unable to save operation. Invalid operation ID."}
Ejemplo n.º 6
0
def save_rd_password(
    password=None,
    user=Default.User,
    customer=Default.Customer,
    conn=None
):

    if not password:
        return {
            'pass': False,
            'message': 'No password was provided.'
        }

    all_agents = list(
        r.table("agents")
        .filter({"customer_name": customer})
        .run(conn)
    )

    result = False
    msg = ''
    missed_count = 0
    for agent in all_agents:
        agent_id = agent['agent_id']

        operation = RaOperation(
            ra.RaValue.SetRdPassword,
            agent_id,
            username=user,
            password=password,
            customer=customer,
            uri=ra.RaUri.Password,
            method='POST'
        )

        result = False
        msg = ''
        _id = save_operation(operation)
        if _id:

            operation.operation_id = _id
            store_in_agent_queue(operation)

        else:

            msg = (
                'Unable to process password operation for agent %s.' % agent_id
            )

            missed_count += 1
            logger.error(msg)

    if missed_count == 0:
        result = True
        msg = ''

    else:

        result = False
        msg = (
            'Server was unable to process the password request for: '
            '%s agent(s).'
            'Please check the server logs for more details.' % missed_count
        )

    return {
        'pass': result,
        'message': msg
    }