-
Notifications
You must be signed in to change notification settings - Fork 0
/
onepassword.py
179 lines (144 loc) · 5.97 KB
/
onepassword.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import base64
import json
import os
import re
import subprocess
from uuid import uuid4
class DeletionFailure(Exception):
def __init__(self, item_name, vault):
message = f"Unable to delete item '{item_name}' from vault '{vault}'"
super().__init__(message)
self.message = message
class Unauthorized(Exception):
pass
class MissingCredentials(Exception):
pass
class SigninFailure(Exception):
pass
class UnknownResource(Exception):
pass
class UnknownResourceItem(Exception):
pass
class UnknownError(Exception):
pass
class OnePassword(object):
def __init__(self, secret=None, token=None, shorthand=None, bin_path=""):
self.op = os.path.join(bin_path, "op")
if secret is not None:
self.shorthand = str(uuid4())
self.session_token = self.get_access_token(secret, shorthand=self.shorthand)
elif token is not None and shorthand is not None:
self.shorthand = shorthand
self.session_token = token
else:
raise MissingCredentials()
def list(self, resource):
op_command = f"{self.op} list {resource} --session={self.session_token}"
try:
return json.loads(run_op_command_in_shell(op_command))
except json.decoder.JSONDecodeError:
raise UnknownResource(resource)
def create_document_in_vault(self, filename, title, vault):
op_command = f"{self.op} create document {filename} --title='{title}' --vault='{vault}' --session={self.session_token}"
return json.loads(run_op_command_in_shell(op_command))
def create_login(self, username, password, title, vault=None, url=None):
login_template = {
"fields": [
{
"value": username,
"name": "username",
"type": "T",
"designation": "username"
},
{
"value": password,
"name": "password",
"type": "P",
"designation": "password"
}
]
}
encoded_item = json.dumps(login_template, separators=(',', ':'))
return self.create_item(category="login",
encoded_item=encoded_item,
title=title,
vault=vault,
url=url)
def create_item(self, category, encoded_item, title, vault=None, url=None):
vault_flag = get_optional_flag(vault=vault)
url_flag = get_optional_flag(url=url)
command = f"""
{self.op} create item {category} '{encoded_item}' \
--title='{title}' \
--session={self.session_token} \
{vault_flag} {url_flag}
"""
return json.loads(run_op_command_in_shell(command))
def delete_item(self, item_name, vault=None):
vault_flag = get_optional_flag(vault=vault)
op_command = f"{self.op} delete item {item_name} {vault_flag} --session={self.session_token}"
try:
run_op_command_in_shell(op_command)
except subprocess.CalledProcessError:
raise DeletionFailure(item_name, vault)
except UnknownError as e:
error_message = str(e)
if "multiple items found" in error_message:
multiple_uuids = []
rg = re.compile(f"\s*for the item {item_name} in vault {vault}: (.*)")
for line in error_message.split("\n"):
match = rg.match(line)
if match:
multiple_uuids.append(match.group(1))
return {"multiple_uuids": multiple_uuids}
if "no item found" in error_message:
return "not found"
return "ok"
def get(self, resource, item_name):
op_command = f"{self.op} get {resource} '{item_name}' --session={self.session_token}"
try:
return json.loads(run_op_command_in_shell(op_command))
except subprocess.CalledProcessError:
raise UnknownResourceItem(f"{resource}: {item_name}")
def get_access_token(self, secret, shorthand):
try:
if not os.environ.get("OP_DEVICE"):
os.environ["OP_DEVICE"] = base64.b32encode(os.urandom(16)).decode().lower().rstrip("=")
process = subprocess.run(
(f"echo '{secret['password']}' | "
f"{self.op} signin {secret['signin_address']} {secret['username']} {secret['secret_key']} "
f"--output=raw --shorthand={shorthand}"),
shell=True,
capture_output=True,
env=os.environ
)
process.check_returncode()
return process.stdout.decode('UTF-8').strip()
except subprocess.CalledProcessError:
raise SigninFailure(f"Error signing in: '{process.stderr.decode('UTF-8').strip()}'")
def get_version(self):
return run_op_command_in_shell(f"{self.op} --version")
def run_op_command_in_shell(op_command, verbose=False):
process = subprocess.run(op_command,
shell=True,
check=False,
capture_output=True,
env=os.environ)
try:
process.check_returncode()
except subprocess.CalledProcessError:
if verbose:
print(process.stderr.decode("UTF-8").strip())
error_messages = ["not currently signed in",
"Authentication required"]
full_error_message = process.stderr.decode("UTF-8")
if any(msg in full_error_message for msg in error_messages):
raise Unauthorized()
else:
raise UnknownError(full_error_message)
return process.stdout.decode("UTF-8").strip()
def get_optional_flag(**kwargs):
key, value = list(kwargs.items())[0]
return (f"--{key}='{value}'"
if value
else "")