def export_keepass(creds, password): db = Database() groups = {} for c in creds: # Create the group if we havent yet if c.group.name not in groups.keys(): groups[c.group.name] = db.create_group(title=c.group.name) kpg = groups[c.group.name] # Add tags list to the end of the description tags = "\n\nTags: " for t in c.tags.all(): tags += "[" tags += t.name tags += "] " desc = unicode(c.description) + tags # Create the entry e = kpg.create_entry(title=c.title, username=c.username, password=c.password, url=c.url, notes=desc) if c.attachment: e.binary_desc = c.attachment_name e.binary = c.attachment.read() # Send the response response = HttpResponse(content_type="application/x-keepass") db.save(response, password=password) response["Content-Disposition"] = "attachment; filename=RatticExport.kdb" response["Content-Length"] = response.tell() return response
def export_keepass(creds, password): db = Database() groups = {} for c in creds: # Create the group if we havent yet if c.group.name not in groups.keys(): groups[c.group.name] = db.create_group(title=c.group.name) kpg = groups[c.group.name] # Add tags list to the end of the description tags = '\n\nTags: ' for t in c.tags.all(): tags += '[' tags += t.name tags += '] ' desc = str(c.description) + tags # Create the entry kpg.create_entry( title=c.title, username=c.username, password=c.password, url=c.url, notes=desc, ) # Send the response response = HttpResponse(mimetype='application/x-keepass') db.save(response, password=password) response['Content-Disposition'] = 'attachment; filename=RatticExport.kdb' response['Content-Length'] = response.tell() return response
def test_save(self): """ Test creating and saving a database. """ db = Database() i_group = db.create_default_group() e_group = db.create_group(title="eMail") e1 = i_group.create_entry(title="FirstEntry", username="******", password="******", url="http://example.com") e2 = i_group.create_entry(title="SecondEntry", username="******", password="******", url="http://example.com") e3 = e_group.create_entry(title="ThirdEntry", username="******", password="******", url="http://example.com") ser = db.to_dict(hierarchy=True, hide_passwords=True) with self.assertRaisesRegexp(ValueError, r"Unable to save without target file."): db.save(password='******') stream = BytesIO() db.save(dbfile=stream, password='******') stream.seek(0) with self.assertRaises(exc.AuthenticationError): db.load(dbfile=stream, password='******') stream.seek(0) db.load(dbfile=stream, password='******') self.maxDiff = None self.assertEquals(ser, db.to_dict(hierarchy=True, hide_passwords=True))
class DB: def __init__(self, db_filename, passwd): self.logger = logging.getLogger('keepass_merge') self.db = Database(db_filename, passwd) self.backup = self.db.groups[-1] self.uuids = [x.uuid for x in self.db.entries] self.groups = [x.id for x in self.db.groups] def get_group(self, group_id): return [x for x in self.db.groups if x.id == group_id][0] def get_entry(self, uuid): return [x for x in self.db.entries if x.uuid == uuid][0] def merge_entry(self, other): """ Assumes the same title and group. """ self_entry = self.get_entry(other.uuid) if self_entry.modified < other.modified: self.logger.debug('update %s', other.title) other_spec = make_new_entry_spec(other) self.db.create_entry(self_entry.group, **other_spec) self.db.move_entry(self_entry, self.backup) elif self_entry.modified == other.modified: self.logger.debug('same %s', other.title) else: self.logger.debug('just backup %s', other.title) copy_spec = make_new_entry_spec(other) self.db.create_entry(self.backup, **copy_spec) def update(self, other): for entry in other.db.entries: if entry.uuid not in self.uuids: if entry.group_id == self.backup.id: continue if entry.group_id in self.groups: group = self.get_group(entry.group_id) else: self.logger.debug('make group %s', entry.group.title) group = self.db.create_group(title=entry.group.title, icon=entry.group.icon) self.db.create_entry(group, **make_new_entry_spec(entry)) else: self.merge_entry(entry) def __getattr__(self, attr): return getattr(self.db, attr)
def test_save(self): """ Test creating and saving a database. """ db = Database() i_group = db.create_default_group() e_group = db.create_group(title="eMail") e1 = i_group.create_entry(title="FirstEntry", username="******", password="******", url="http://example.com") e2 = i_group.create_entry(title="SecondEntry", username="******", password="******", url="http://example.com") e3 = e_group.create_entry(title="ThirdEntry", username="******", password="******", url="http://example.com") ser = db.to_dict(hierarchy=True, hide_passwords=True) with self.assertRaisesRegexp(ValueError, r"Unable to save without target file."): db.save(password='******') stream = BytesIO() db.save(dbfile=stream, password='******') stream.seek(0) with self.assertRaises(exc.AuthenticationError): db.load(dbfile=stream, password='******') stream.seek(0) db.load(dbfile=stream, password='******') self.maxDiff = None self.assertEquals(ser, db.to_dict(hierarchy=True, hide_passwords=True))
def export_keepass(creds, password, filename='RatticExport.kdb'): db = Database() groups = {} for c in creds: # Create the group if we havent yet if c.group.name not in groups.keys(): groups[c.group.name] = db.create_group(title=c.group.name) kpg = groups[c.group.name] # Add tags list to the end of the description tags = '\n\nTags: ' for t in c.tags.all(): tags += '[' tags += t.name tags += '] ' desc = unicode(c.description) + tags # Create the entry e = kpg.create_entry( title=c.title, username=c.username, password=c.password, url=c.url, notes=desc, ) if c.attachment: e.binary_desc = c.attachment_name e.binary = c.attachment.read() # Send the response response = HttpResponse(content_type='application/x-keepass') db.save(response, password=password) response['Content-Disposition'] = 'attachment; filename=' + filename response['Content-Length'] = response.tell() return response