Repository: borgbackup/borg

Issue:
crash with relative BORG_KEY_FILE
Hi,

I'm backing up into multiple repositories, but I want to use a single keyfile for all of them. However I don't see a way to generate a keyfile with borg itself:

    [kbabioch@antares ~]$ BORG_KEY_FILE=keyfile borg init -e keyfile repo
    Enter new passphrase: 
    Enter same passphrase again: 
    Do you want your passphrase to be displayed for verification? [yN]: 
    Local Exception
    Traceback (most recent call last):
      File "/usr/lib/python3.6/site-packages/borg/archiver.py", line 4024, in main
        exit_code = archiver.run(args)
      File "/usr/lib/python3.6/site-packages/borg/archiver.py", line 3952, in run
        return set_ec(func(args))
      File "/usr/lib/python3.6/site-packages/borg/archiver.py", line 148, in wrapper
        return method(self, args, repository=repository, **kwargs)
      File "/usr/lib/python3.6/site-packages/borg/archiver.py", line 248, in do_init
        key = key_creator(repository, args)
      File "/usr/lib/python3.6/site-packages/borg/crypto/key.py", line 106, in key_creator
        return key.create(repository, args)
      File "/usr/lib/python3.6/site-packages/borg/crypto/key.py", line 657, in create
        key.save(target, passphrase)
      File "/usr/lib/python3.6/site-packages/borg/crypto/key.py", line 728, in save
        fd.write('\n')
      File "/usr/lib/python3.6/site-packages/borg/platform/base.py", line 175, in __exit__
        platform.sync_dir(os.path.dirname(self.path))
      File "/usr/lib/python3.6/site-packages/borg/platform/base.py", line 56, in sync_dir
        fd = os.open(path, os.O_RDONLY)
    FileNotFoundError: [Errno 2] No such file or directory: ''

    Platform: Linux antares 4.13.7-1-ARCH #1 SMP PREEMPT Sat Oct 14 20:13:26 CEST 2017 x86_64 
    Linux: arch Arch Linux
    Borg: 1.1.0  Python: CPython 3.6.2
    PID: 25811  CWD: /home/kbabioch
    sys.argv: ['/usr/bin/borg', 'init', '-e', 'keyfile', 'repo']
    SSH_ORIGINAL_COMMAND: None

I think either the use case mentioned above should work, or there should be some "borg --genkey" option, e.g. as with `openvpn(8)`.


Discussion:
So, your intention was to use an existing keyfile located in `./keyfile` to initialize a new repo in keyfile mode?
No, I want to create a new repo with a new keyfile at the given location (rather than using borg's internal logic to put keys somewhere within `.config/borg/keys`).
A general comment about using same key for different repos:

AES-CTR mode encryption is only secure if counter values are not repeated (for same AES key).

If you use the same key for multiple independent repos, you can not be sure to avoid that.

Resolving patch:
diff --git a/src/borg/crypto/key.py b/src/borg/crypto/key.py
--- a/src/borg/crypto/key.py
+++ b/src/borg/crypto/key.py
@@ -687,7 +687,7 @@ def find_key(self):
         id = self.repository.id
         keyfile = os.environ.get('BORG_KEY_FILE')
         if keyfile:
-            return self.sanity_check(keyfile, id)
+            return self.sanity_check(os.path.abspath(keyfile), id)
         keys_dir = get_keys_dir()
         for name in os.listdir(keys_dir):
             filename = os.path.join(keys_dir, name)
@@ -700,7 +700,7 @@ def find_key(self):
     def get_new_target(self, args):
         keyfile = os.environ.get('BORG_KEY_FILE')
         if keyfile:
-            return keyfile
+            return os.path.abspath(keyfile)
         filename = args.location.to_key_filename()
         path = filename
         i = 1


Test patch: