1
0
Fork 0
mirror of https://github.com/borgbackup/borg.git synced 2025-03-03 10:17:40 +00:00

More user friendly key file handling

This commit is contained in:
Jonas Borgström 2011-08-04 15:27:52 +02:00
parent 60298e1469
commit 64a318501d
3 changed files with 28 additions and 13 deletions

View file

@ -46,7 +46,7 @@ def do_serve(self, args):
def do_init(self, args): def do_init(self, args):
store = self.open_store(args.store, create=True) store = self.open_store(args.store, create=True)
key = Key.create(store) key = Key.create(store, args.store.to_key_filename())
def do_create(self, args): def do_create(self, args):
store = self.open_store(args.archive) store = self.open_store(args.archive)

View file

@ -284,6 +284,12 @@ def __str__(self):
items.append('archive=%r' % self.archive) items.append('archive=%r' % self.archive)
return ', '.join(items) return ', '.join(items)
def to_key_filename(self):
name = re.sub('[^\w]', '_', self.path).strip('_')
if self.proto != 'file':
name = self.host + '__' + name
return os.path.join(os.path.expanduser('~'), '.darc', 'keys', name)
def __repr__(self): def __repr__(self):
return "Location(%s)" % self return "Location(%s)" % self

View file

@ -20,12 +20,21 @@ class Key(object):
def __init__(self, store=None): def __init__(self, store=None):
if store: if store:
self.open(store) self.open(self.find_key_file(store))
def open(self, store): def find_key_file(self, store):
path = os.path.join(os.path.expanduser('~'), id = store.id.encode('hex')
'.darc', 'keys', store.id.encode('hex')) keys_dir = os.path.join(os.path.expanduser('~'), '.darc', 'keys')
with open(path, 'rb') as fd: for name in os.listdir(keys_dir):
filename = os.path.join(keys_dir, name)
with open(filename, 'rb') as fd:
line = fd.readline().strip()
if line and line.startswith(self.FILE_ID) and line[9:] == id:
return filename
raise Exception('Key file for store with ID %s not found' % id)
def open(self, filename):
with open(filename, 'rb') as fd:
lines = fd.readlines() lines = fd.readlines()
if not lines[0].startswith(self.FILE_ID) != self.FILE_ID: if not lines[0].startswith(self.FILE_ID) != self.FILE_ID:
raise ValueError('Not a DARC key file') raise ValueError('Not a DARC key file')
@ -90,7 +99,7 @@ def save(self, path, password):
with open(path, 'wb') as fd: with open(path, 'wb') as fd:
fd.write('%s %s\n' % (self.FILE_ID, self.store_id.encode('hex'))) fd.write('%s %s\n' % (self.FILE_ID, self.store_id.encode('hex')))
fd.write(data.encode('base64')) fd.write(data.encode('base64'))
print 'Key chain "%s" created' % path print 'Key file "%s" created' % path
def chpass(self): def chpass(self):
password, password2 = 1, 2 password, password2 = 1, 2
@ -103,12 +112,12 @@ def chpass(self):
return 0 return 0
@staticmethod @staticmethod
def create(store): def create(store, filename):
path = os.path.join(os.path.expanduser('~'), i = 1
'.darc', 'keys', store.id.encode('hex')) path = filename
if os.path.exists(path): while os.path.exists(path):
print '%s already exists' % path i += 1
return 1 path = filename + '.%d' % i
password, password2 = 1, 2 password, password2 = 1, 2
while password != password2: while password != password2:
password = getpass('Keychain password: ') password = getpass('Keychain password: ')