extend open_index() API for info file handling
authorPhilipp Gesang <philipp.gesang@intra2net.com>
Thu, 23 Mar 2017 10:48:59 +0000 (11:48 +0100)
committerPhilipp Gesang <philipp.gesang@intra2net.com>
Thu, 23 Mar 2017 10:49:05 +0000 (11:49 +0100)
In fact, backup_python’s “info file” is just another “index file”
to deltatar. Conceptually they’re quite different though
especially regarding encryption. To allow requesting an info
flavored index file, add a parameter to communicate with the
crypto layer.

deltatar/deltatar.py
deltatar/tarfile.py

index dd6f584..b04afc4 100644 (file)
@@ -18,6 +18,8 @@
 
 # Author: Eduardo Robles Elvira <edulix@wadobo.com>
 
+I2N_XXX_ENCRYPTION_VERSION = 0
+
 import logging
 import datetime
 import binascii
@@ -476,11 +478,16 @@ class DeltaTar(object):
                 return path[len(prefix):]
         return path
 
-    def open_index(self, path, mode='r'):
+    def open_index(self, path, mode='r', kind="index"):
         '''
         Given the specified configuration, opens the index for reading or
         writing. It transparently handles if the index is encrypted and/or
-        compressed, returning a file object reading to use.
+        compressed, returning a file object ready to use.
+
+        :param kind:        Role of file. The only special value is “info”
+                            which sets the appropriate counter in the crypto
+                            layer.
+        :type  kind:        str
         '''
         filemode = None
 
@@ -493,11 +500,15 @@ class DeltaTar(object):
 
         encver = None
         if 'aes' in self.index_mode:
-            encver = 1
+            encver = I2N_XXX_ENCRYPTION_VERSION
+            counter = None
+            if kind == "info": # fixed counter
+                counter = crypto.AES_GCM_IV_CNT_INFOFILE
 
         return tarfile._Stream(name=path, mode=mode, comptype=comptype,
-                       bufsize=tarfile.RECORDSIZE, fileobj=None,
-                       encver=encver, password=self.password)
+                               bufsize=tarfile.RECORDSIZE, fileobj=None,
+                               encver=encver, enccounter=counter,
+                               password=self.password)
 
     def create_full_backup(self, source_path, backup_path,
                            max_volume_size=None, extra_data=dict()):
index ef50e1d..61fcc9f 100644 (file)
@@ -381,7 +381,8 @@ class _Stream:
     remainder = -1 # track size in encrypted entries
 
     def __init__(self, name, mode, comptype, fileobj, bufsize,
-                 concat_stream=False, encver=None, password=None,
+                 concat_stream=False,
+                 encver=None, enccounter=None, password=None,
                  nacl=None, compresslevel=9):
         """Construct a _Stream object.
         """
@@ -419,6 +420,8 @@ class _Stream:
         self.encryption = None
         self.lasthdr    = None
 
+        enccounter = enccounter or crypto.AES_GCM_IV_CNT_DATA
+
         try:
             if comptype == "gz":
                 try:
@@ -450,7 +453,7 @@ class _Stream:
                         try:
                             enc = crypto.Encrypt (password,
                                                   I2N_XXX_ENCRYPTION_VERSION, nacl,
-                                                  counter=crypto.AES_GCM_IV_CNT_DATA)
+                                                  counter=enccounter)
                         except ValueError as exn:
                             raise InvalidEncryptionError \
                                     ("ctor failed crypto.Encrypt(<PASSWORD>, “%s”, %r)"
@@ -519,7 +522,7 @@ class _Stream:
                     try:
                         enc = crypto.Encrypt (password,
                                               I2N_XXX_ENCRYPTION_VERSION, nacl,
-                                              counter=crypto.AES_GCM_IV_CNT_DATA)
+                                              counter=enccounter)
                     except ValueError as exn:
                         raise InvalidEncryptionError \
                                 ("ctor failed crypto.Encrypt(<PASSWORD>, “%s”, %r)"