def list_domains(kf): 'print a list of domain names, if any' domain_names = kf.get_domain_names() if domain_names: echo('\n'.join(domain_names)) else: echo.err('(No domains in protected at %s)' % kf.path) return
def list_domain_secrets(kf, domain_name): 'print a list of secret names for a given domain' secret_names = kf.get_domain_secret_names(domain_name) if secret_names: echo('\n'.join(secret_names)) else: echo.err('(No secrets in domain %r of protected at %s)' % (domain_name, kf.path)) return
def list_all_secrets(kf): 'print a list of all secret names, along with the domains that define each' secrets_map = kf.get_all_secret_names() if not secrets_map: echo.err('(No secrets in protected at %s)' % kf.path) else: for secret_name in sorted(secrets_map): domain_names = sorted(set(secrets_map[secret_name])) echo('%s: %s' % (secret_name, ', '.join(domain_names))) return
def test_echo(capsys): test_str = u'tést' echo(test_str) echo.err(test_str.upper()) captured = capsys.readouterr() assert captured.out == test_str + '\n' assert captured.err == test_str.upper() + '\n' echo(test_str, end='\n\n') assert capsys.readouterr().out == test_str + '\n\n' echo(test_str, nl=False) assert capsys.readouterr().out == test_str
def _get_creds(kf, user=None, interactive=True, check_env=True, passphrase_file=None, user_env_var='PPROTECT_USER', pass_env_var='PPROTECT_PASSPHRASE'): if not interactive and not check_env: raise UsageError('expected at least one of check_env' ' and interactive to be True', 2) user_source = 'argument' passphrase, passphrase_source = None, None if passphrase_file: passphrase_file = os.path.abspath(passphrase_file) try: passphrase = open(passphrase_file, 'rb').read().decode('utf8') except IOError as ioe: if getattr(ioe, 'strerror', None): msg = '%s while reading passphrase from file at "%s"' % (ioe.strerror, passphrase_file) else: msg = 'Failed to read passphrase from file at "%s"' % passphrase_file raise UsageError(msg=msg) else: passphrase_source = "passphrase file: %s" % passphrase_file if user is None and user_env_var: user = os.getenv(user_env_var) user_source = 'env var: %s' % user_env_var if passphrase is None and pass_env_var: passphrase = os.getenv(pass_env_var) passphrase_source = 'env var: %s' % pass_env_var if interactive: msg = '' if user is None: msg = 'Verify credentials for %s' % kf.path elif passphrase is None: msg = 'Verify passphrase for %s (Using user %s from %s)' % (kf.path, user, user_source) if msg: echo.err(msg) if user is None: user = prompt('User email: ') user_source = 'stdin' if passphrase is None: passphrase = prompt.secret('Passphrase: ', confirm=False) passphrase_source = 'stdin' creds = Creds(_get_text(user or ''), _get_text(passphrase or ''), name_source=user_source, passphrase_source=passphrase_source) _check_creds(kf, creds) return creds
def mw_ensure_kf(next_, file, subcmds_): file_path = file or 'protected.yaml' file_abs_path = os.path.abspath(file_path) init_kf = subcmds_[0] == 'init' if init_kf: kf = _create_protected(file_abs_path) else: kf = _ensure_protected(file_abs_path) try: ret = next_(kf=kf) except: if init_kf: try: os.unlink(file_abs_path) except Exception: echo.err('Warning: failed to remove file: %s' % file_abs_path) raise return ret