Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get a password from the user in Fabric, not echoing the value

I'm using Fabric to automate a deployment. In this process I use the prompt function to ask the user some input. In particular I need to ask for a password and I'd like to hide the value the user inputs, like using Python getpass. I'd like to use prompt because of the handling of key and validate args.

Is there a Fabric built-in way to do so, or do have I to change prompt source (eventually sending a pull request)?

like image 725
Paolo Avatar asked Nov 01 '12 15:11

Paolo


1 Answers

You might be able to use prompt_for_password in fabric.network

def prompt_for_password(prompt=None, no_colon=False, stream=None):
    """
    Prompts for and returns a new password if required; otherwise, returns
    None.

    A trailing colon is appended unless ``no_colon`` is True.

    If the user supplies an empty password, the user will be re-prompted until
    they enter a non-empty password.

    ``prompt_for_password`` autogenerates the user prompt based on the current
    host being connected to. To override this, specify a string value for
    ``prompt``.

    ``stream`` is the stream the prompt will be printed to; if not given,
    defaults to ``sys.stderr``.
    """
    from fabric.state import env
    handle_prompt_abort("a connection or sudo password")
    stream = stream or sys.stderr
    # Construct prompt
    default = "[%s] Login password for '%s'" % (env.host_string, env.user)
    password_prompt = prompt if (prompt is not None) else default
    if not no_colon:
        password_prompt += ": "
    # Get new password value
    new_password = getpass.getpass(password_prompt, stream)
    # Otherwise, loop until user gives us a non-empty password (to prevent
    # returning the empty string, and to avoid unnecessary network overhead.)
    while not new_password:
        print("Sorry, you can't enter an empty password. Please try again.")
        new_password = getpass.getpass(password_prompt, stream)
    return new_password

It looks like this is how fabric retrieves password for ssh, They then set this to env using:

def set_password(password):
    from fabric.state import env
    env.password = env.passwords[env.host_string] = password

Key is easily replaceable by setting env, but looks like you might have to validate yourself...

like image 154
dm03514 Avatar answered Nov 20 '22 02:11

dm03514