Browse Source

refactor key file searching functions

I want to change the key lookup logic for the 'borg key import' command.
Extract methods out of the KeyfileKey.find_key and
KeyfileKey.get_new_target to make this future change possible without
duplicating code.

This commit should not change behavior.
Matthew Glazar 5 năm trước cách đây
mục cha
commit
538d3245cd
1 tập tin đã thay đổi với 17 bổ sung4 xóa
  1. 17 4
      src/borg/crypto/key.py

+ 17 - 4
src/borg/crypto/key.py

@@ -712,10 +712,16 @@ class KeyfileKey(ID_HMAC_SHA_256, KeyfileKeyBase):
             return filename
 
     def find_key(self):
+        keyfile = self._find_key_file_from_environment()
+        if keyfile is not None:
+            return self.sanity_check(keyfile, self.repository.id)
+        keyfile = self._find_key_in_keys_dir()
+        if keyfile is not None:
+            return keyfile
+        raise KeyfileNotFoundError(self.repository._location.canonical_path(), get_keys_dir())
+
+    def _find_key_in_keys_dir(self):
         id = self.repository.id
-        keyfile = os.environ.get('BORG_KEY_FILE')
-        if keyfile:
-            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)
@@ -723,12 +729,19 @@ class KeyfileKey(ID_HMAC_SHA_256, KeyfileKeyBase):
                 return self.sanity_check(filename, id)
             except (KeyfileInvalidError, KeyfileMismatchError):
                 pass
-        raise KeyfileNotFoundError(self.repository._location.canonical_path(), get_keys_dir())
 
     def get_new_target(self, args):
+        keyfile = self._find_key_file_from_environment()
+        if keyfile is not None:
+            return keyfile
+        return self._get_new_target_in_keys_dir(args)
+
+    def _find_key_file_from_environment(self):
         keyfile = os.environ.get('BORG_KEY_FILE')
         if keyfile:
             return os.path.abspath(keyfile)
+
+    def _get_new_target_in_keys_dir(self, args):
         filename = args.location.to_key_filename()
         path = filename
         i = 1