-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync_bw_kp.py
170 lines (153 loc) · 5.29 KB
/
sync_bw_kp.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
import argparse
import csv
import json
import os.path
import xml.etree.ElementTree as ET
def read_bw(bw_file):
entries = {}
with open(bw_file, 'rb') as bw_in:
bw_data = json.load(bw_in)
for item in bw_data["items"]:
if item["type"] != 1:
continue # skip non-login entries
entries[item["name"]] = item["login"]
return entries
GENERIC_CSV_FIELDS = [
"folder",
"favorite",
"type",
"name",
"notes",
"fields",
"login_uri",
"login_username",
"login_password",
"login_totp",
]
def create_generic_csv_dict(name, entry):
""" https://help.bitwarden.com/article/import-data/ """
try:
url = entry["url"]
except KeyError:
try:
url = entry['uris'][0]['uri']
except (KeyError, IndexError):
url = ""
return {
"folder": "",
"favorite": "",
"type": 1,
"name": name,
"notes": "",
"fields": "",
"login_uri": url,
"login_username": entry["username"],
"login_password": entry["password"],
"login_totp": "",
}
def compare_to_kp(kp_db, bw_entries):
changed = False
bw_set = {x for x in bw_entries}
kp_set = set()
to_bitwarden = {}
for node in kp_db.findall('.//Group/Entry'):
name = node.find("./String[Key='Title']/Value").text
user = node.find("./String[Key='UserName']/Value").text
password = node.find("./String[Key='Password']/Value").text
url = node.find("./String[Key='URL']/Value").text
kp_set.add(name)
if name in bw_entries:
new_user = bw_entries[name]["username"]
new_password = bw_entries[name]["password"]
if new_user != user:
print("{}: Username changed from '{}' to '{}'".format(name, user, new_user))
node.find("./String[Key='UserName']/Value").text = new_user
changed = True
if new_password != password:
print("{}: Pass changed from '{}...' to '{}...'".format(name, password[0], new_password[0]))
node.find("./String[Key='Password']/Value").text = new_password
changed = True
else:
to_bitwarden[name] = {
"username": user,
"password": password,
"url": url
}
to_keepass = bw_set - kp_set
return changed, to_bitwarden, to_keepass
def main(args):
print("Processing...")
bw_entries = read_bw(args.bitwarden_file)
kp_db = ET.parse(args.keepass_file)
changed, to_bitwarden, to_keepass = compare_to_kp(kp_db, bw_entries)
print("===")
print("Entries in KP, but not in BW:", list(to_bitwarden.keys()))
print("Entries in BW, but not in KP:", to_keepass)
if args.difference:
to_keepass_csv = []
for entry_name in to_keepass:
csv_entry = create_generic_csv_dict(entry_name, bw_entries[entry_name])
to_keepass_csv.append(csv_entry)
if to_keepass_csv:
to_keepass_filename = 'to-keepass.csv'
with open(to_keepass_filename, 'w', newline='') as out_file:
out_csv = csv.DictWriter(out_file, delimiter=',', fieldnames=GENERIC_CSV_FIELDS)
out_csv.writeheader()
out_csv.writerows(to_keepass_csv)
print("Wrote", to_keepass_filename)
to_bitwarden_csv = []
for entry_name in to_bitwarden:
csv_entry = create_generic_csv_dict(entry_name, to_bitwarden[entry_name])
to_bitwarden_csv.append(csv_entry)
if to_bitwarden_csv:
to_bitwarden_filename = 'to-bitwarden.csv'
with open(to_bitwarden_filename, 'w', newline='') as out_file:
out_csv = csv.DictWriter(out_file, delimiter=',', fieldnames=GENERIC_CSV_FIELDS)
out_csv.writeheader()
out_csv.writerows(to_bitwarden_csv)
print("Wrote", to_bitwarden_filename)
if changed:
out_filename = args.out_xml
if not out_filename:
out_filename = "{}-out{}".format(*os.path.splitext(args.keepass_file))
kp_db.write(out_filename)
print("===")
print("Wrote", out_filename)
def parse_args():
parser = argparse.ArgumentParser(
description="Import changes from Bitwarden into KeePass"
)
parser.add_argument(
"-b",
"--bitwarden",
required=True,
metavar="JSON_FILE",
dest="bitwarden_file",
help="Bitwarden JSON export file"
)
parser.add_argument(
"-k",
"--keepass",
required=True,
metavar="XML_FILE",
dest="keepass_file",
help="KeePass 2 XML export file"
)
parser.add_argument(
"-o",
"--out-xml",
default=None,
dest="out_xml",
help="Ouput KeePass 2 XML file"
)
parser.add_argument(
"-d",
"--difference",
dest="difference",
action="store_true",
help="Also write CSV importable files for differing entries"
)
return parser.parse_args()
if __name__ == '__main__':
args = parse_args()
main(args)