Various fixes

This commit is contained in:
snow flurry 2021-10-29 23:22:14 -07:00
parent 9b83a4a2c3
commit 3e7babfdf3

View file

@ -69,7 +69,7 @@ class Bitwarden(object):
def __init__(self, path): def __init__(self, path):
self._cli_path = path self._cli_path = path
try: try:
check_output(self._cli_path) check_output([self._cli_path, "--version"])
except OSError: except OSError:
raise AnsibleError("Command not found: {0}".format(self._cli_path)) raise AnsibleError("Command not found: {0}".format(self._cli_path))
@ -82,27 +82,28 @@ class Bitwarden(object):
return 'BW_SESSION' in os.environ return 'BW_SESSION' in os.environ
def _run(self, args): def _run(self, args):
print("%s %s" % (self.cli_path, args))
p = Popen([self.cli_path] + args, stdin=PIPE, stdout=PIPE, stderr=PIPE) p = Popen([self.cli_path] + args, stdin=PIPE, stdout=PIPE, stderr=PIPE)
out, err = p.communicate() out, err = p.communicate()
rc = p.wait() rc = p.wait()
if rc != 0: if rc != 0:
display.debug("Received error when running '{0} {1}': {2}" display.debug("Received error when running '{0} {1}': {2}"
.format(self.cli_path, args, out)) .format(self.cli_path, args, out))
if out.startswith("Vault is locked."): if err.startswith(b"Vault is locked."):
raise AnsibleError("Error accessing Bitwarden vault. " raise AnsibleError("Error accessing Bitwarden vault. "
"Run 'bw unlock' to unlock the vault.") "Run 'bw unlock' to unlock the vault.")
elif out.startswith("You are not logged in."): elif err.startswith(b"You are not logged in."):
raise AnsibleError("Error accessing Bitwarden vault. " raise AnsibleError("Error accessing Bitwarden vault. "
"Run 'bw login' to login.") "Run 'bw login' to login.")
elif out.startswith("Failed to decrypt."): elif err.startswith(b"Failed to decrypt."):
raise AnsibleError("Error accessing Bitwarden vault. " raise AnsibleError("Error accessing Bitwarden vault. "
"Make sure BW_SESSION is set properly.") "Make sure BW_SESSION is set properly.")
elif out.startswith("Not found."): elif err.startswith(b"Not found."):
raise AnsibleError("Error accessing Bitwarden vault. " raise AnsibleError("Error accessing Bitwarden vault. "
"Specified item not found.") "Specified item not found.")
else: else:
raise AnsibleError("Unknown failure in 'bw' command: " raise AnsibleError("Unknown failure in 'bw' command: "
"{0}".format(out)) "{0}".format(err))
return out.strip() return out.strip()
def sync(self): def sync(self):