def ask_passwords(): ''' prompt for connection and become passwords if needed ''' op = context.CLIARGS sshpass = None becomepass = None become_prompt = '' become_prompt_method = "BECOME" if C.AGNOSTIC_BECOME_PROMPT else op[ 'become_method'].upper() try: if op['ask_pass']: sshpass = getpass.getpass(prompt="SSH password: "******"%s password[defaults to SSH password]: " % become_prompt_method else: become_prompt = "%s password: " % become_prompt_method if op['become_ask_pass']: becomepass = getpass.getpass(prompt=become_prompt) if op['ask_pass'] and becomepass == '': becomepass = sshpass except EOFError: pass # we 'wrap' the passwords to prevent templating as # they can contain special chars and trigger it incorrectly if sshpass: sshpass = to_unsafe_text(sshpass) if becomepass: becomepass = to_unsafe_text(becomepass) return (sshpass, becomepass)
def get_password_from_file(pwd_file): b_pwd_file = to_bytes(pwd_file) secret = None if b_pwd_file == b'-': if PY3: # ensure its read as bytes secret = sys.stdin.buffer.read() else: secret = sys.stdin.read() elif not os.path.exists(b_pwd_file): raise AnsibleError("The password file %s was not found" % pwd_file) elif os.path.is_executable(b_pwd_file): display.vvvv(u'The password file %s is a script.' % to_text(pwd_file)) cmd = [b_pwd_file] try: p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) except OSError as e: raise AnsibleError( "Problem occured when trying to run the password script %s (%s)." " If this is not a script, remove the executable bit from the file." % (pwd_file, e)) stdout, stderr = p.communicate() if p.returncode != 0: raise AnsibleError( "The password script %s returned an error (rc=%s): %s" % (pwd_file, p.returncode, stderr)) secret = stdout else: try: f = open(b_pwd_file, "rb") secret = f.read().strip() f.close() except (OSError, IOError) as e: raise AnsibleError("Could not read password file %s: %s" % (pwd_file, e)) secret = secret.strip(b'\r\n') if not secret: raise AnsibleError('Empty password was provided from file (%s)' % pwd_file) return to_unsafe_text(secret)
def _clean_res(res, errors='surrogate_or_strict'): if isinstance(res, binary_type): return to_unsafe_text(res, errors=errors) elif isinstance(res, dict): for k in res: try: res[k] = _clean_res(res[k], errors=errors) except UnicodeError: if k == 'diff': # If this is a diff, substitute a replacement character if the value # is undecodable as utf8. (Fix #21804) display.warning("We were unable to decode all characters in the module return data." " Replaced some in an effort to return as much as possible") res[k] = _clean_res(res[k], errors='surrogate_then_replace') else: raise elif isinstance(res, list): for idx, item in enumerate(res): res[idx] = _clean_res(item, errors=errors) return res
def _get_secret(prompt): secret = getpass.getpass(prompt=prompt) if secret: secret = to_unsafe_text(secret) return secret