Exemplo n.º 1
0
def setup_auth_client(protocol, host, port, session_token=None):
    """
    Setup the Thrift authentication client. Returns the client object and the
    session token for the session.
    """

    if not session_token:
        manager = UserCredentials()
        session_token = manager.get_token(host, port)
        session_token_new = perform_auth_for_handler(protocol, manager, host,
                                                     port, session_token)
        if session_token_new:
            session_token = session_token_new

    client = authentication_helper.ThriftAuthHelper(
        protocol, host, port, '/v' + CLIENT_API + '/Authentication',
        session_token)
    check_api_version(client)

    return client, session_token
Exemplo n.º 2
0
def handle_auth(protocol, host, port, username, login=False):
    session = UserCredentials()
    auth_token = session.get_token(host, port)
    auth_client = authentication_helper.ThriftAuthHelper(
        protocol, host, port, '/v' + CLIENT_API + '/Authentication',
        auth_token)
    check_api_version(auth_client)

    if not login:
        logout_done = auth_client.destroySession()
        if logout_done:
            session.save_token(host, port, None, True)
            LOG.info("Successfully logged out.")
        return

    try:
        handshake = auth_client.getAuthParameters()

        if not handshake.requiresAuthentication:
            LOG.info("This server does not require privileged access.")
            return

        if auth_token and handshake.sessionStillActive:
            LOG.info("You are already logged in.")
            return

    except TApplicationException:
        LOG.info("This server does not support privileged access.")
        return

    methods = auth_client.getAcceptedAuthMethods()
    # Attempt username-password auth first.
    if 'Username:Password' in str(methods):

        # Try to use a previously saved credential from configuration file.
        saved_auth = session.get_auth_string(host, port)

        if saved_auth:
            LOG.info("Logging in using preconfigured credentials...")
            username = saved_auth.split(":")[0]
            pwd = saved_auth.split(":")[1]
        else:
            LOG.info("Logging in using credentials from command line...")
            pwd = getpass.getpass(
                "Please provide password for user '{0}': ".format(username))

        LOG.debug("Trying to login as {0} to {1}:{2}".format(
            username, host, port))
        try:
            session_token = auth_client.performLogin("Username:Password",
                                                     username + ":" + pwd)

            session.save_token(host, port, session_token)
            LOG.info("Server reported successful authentication.")
        except shared.ttypes.RequestFailed as reqfail:
            LOG.error("Authentication failed! Please check your credentials.")
            LOG.error(reqfail.message)
            sys.exit(1)
    else:
        LOG.critical("No authentication methods were reported by the server "
                     "that this client could support.")
        sys.exit(1)