def test_gpg_hidden_asym(self): u"""Test GPG asymmetric encryption with hidden key id""" profile = gpg.GPGProfile( passphrase=self.sign_passphrase, hidden_recipients=[self.encrypt_key1, self.encrypt_key2]) self.gpg_cycle(b"aoensutha aonetuh saoe", profile) profile2 = gpg.GPGProfile(passphrase=self.sign_passphrase, hidden_recipients=[self.encrypt_key1]) self.gpg_cycle(b"aoeu" * 10000, profile2)
def test_gpg_asym(self): """Test GPG asymmetric encryption""" profile = gpg.GPGProfile(passphrase = helper.sign_passphrase, recipients = [helper.encrypt_key1, helper.encrypt_key2]) self.gpg_cycle("aoensutha aonetuh saoe", profile) profile2 = gpg.GPGProfile(passphrase = helper.sign_passphrase, recipients = [helper.encrypt_key1]) self.gpg_cycle("aoeu" * 10000, profile2)
def try_fileobj_ops(self, backend): """Test above try_fileobj_filename with a few filenames""" # Must set dup_time strings because they are used by file_naming dup_time.setcurtime(2000) dup_time.setprevtime(1000) # Also set profile for encryption globals.gpg_profile = gpg.GPGProfile(passphrase = "foobar") filename1 = file_naming.get('full', manifest = 1, gzipped = 1) self.try_fileobj_filename(backend, filename1) filename2 = file_naming.get('new-sig', encrypted = 1) self.try_fileobj_filename(backend, filename2)
def test_GPGWriteFile(self): """Test GPGWriteFile""" size = 400 * 1000 gwfh = GPGWriteFile_Helper() profile = gpg.GPGProfile(passphrase="foobar") for i in range(10): # @UnusedVariable gpg.GPGWriteFile(gwfh, "testfiles/output/gpgwrite.gpg", profile, size=size) # print os.stat("testfiles/output/gpgwrite.gpg").st_size-size assert size - 64 * 1024 <= os.stat("testfiles/output/gpgwrite.gpg").st_size <= size + 64 * 1024 gwfh.set_at_end() gpg.GPGWriteFile(gwfh, "testfiles/output/gpgwrite.gpg", profile, size=size)
def test_gpg_signing(self): u"""Test to make sure GPG reports the proper signature key""" plaintext = b"hello" * 50000 signing_profile = gpg.GPGProfile(passphrase=self.sign_passphrase, sign_key=self.sign_key, recipients=[self.encrypt_key1]) epath = path.Path(u"testfiles/output/encrypted_file") encrypted_signed_file = gpg.GPGFile(1, epath, signing_profile) encrypted_signed_file.write(plaintext) encrypted_signed_file.close() decrypted_file = gpg.GPGFile(0, epath, signing_profile) assert decrypted_file.read() == plaintext decrypted_file.close() sig = decrypted_file.get_signature() assert sig == self.sign_key, sig
def test_gpg_signing_and_hidden_encryption(self): """Test to make sure GPG reports the proper signature key even with hidden encryption key id""" plaintext = "hello" * 50000 signing_profile = gpg.GPGProfile(passphrase=self.sign_passphrase, sign_key=self.sign_key, hidden_recipients=[self.encrypt_key1]) epath = path.Path("testfiles/output/encrypted_file") encrypted_signed_file = gpg.GPGFile(1, epath, signing_profile) encrypted_signed_file.write(plaintext) encrypted_signed_file.close() decrypted_file = gpg.GPGFile(0, epath, signing_profile) assert decrypted_file.read() == plaintext decrypted_file.close() sig = decrypted_file.get_signature() assert sig == self.sign_key, sig[-8:]
def test_GPGWriteFile(self): u"""Test GPGWriteFile""" size = 400 * 1000 gwfh = GPGWriteFile_Helper() profile = gpg.GPGProfile(passphrase=u"foobar") for i in range(10): gpg.GPGWriteFile( gwfh, u"{0}/testfiles/output/gpgwrite.gpg".format(_runtest_dir), profile, size=size) # print os.stat("/tmp/testfiles/output/gpgwrite.gpg").st_size-size assert size - 64 * 1024 <= os.stat( u"{0}/testfiles/output/gpgwrite.gpg".format( _runtest_dir)).st_size <= size + 64 * 1024 gwfh.set_at_end() gpg.GPGWriteFile( gwfh, u"{0}/testfiles/output/gpgwrite.gpg".format(_runtest_dir), profile, size=size)
def ProcessCommandLine(cmdline_list): """Process command line, set globals, return action action will be "list-current", "collection-status", "cleanup", "remove-old", "restore", "verify", "full", or "inc". """ globals.gpg_profile = gpg.GPGProfile() args = parse_cmdline_options(cmdline_list) # we can now try to import all the backends backend.import_backends() # parse_cmdline_options already verified that we got exactly 1 or 2 # non-options arguments assert len(args) >= 1 and len( args) <= 2, "arg count should have been checked already" if len(args) == 1: if list_current: action = "list-current" elif collection_status: action = "collection-status" elif cleanup: action = "cleanup" elif globals.remove_time is not None: action = "remove-old" elif globals.remove_all_but_n_full_mode: action = "remove-all-but-n-full" elif globals.remove_all_inc_of_but_n_full_mode: action = "remove-all-inc-of-but-n-full" else: command_line_error("Too few arguments") globals.backend = backend.get_backend(args[0]) if not globals.backend: log.FatalError( _("""Bad URL '%s'. Examples of URL strings are "scp://[email protected]:1234/path" and "file:///usr/local". See the man page for more information.""") % (args[0], ), log.ErrorCode.bad_url) elif len(args) == 2: # Figure out whether backup or restore backup, local_pathname = set_backend(args[0], args[1]) if backup: if full_backup: action = "full" else: action = "inc" else: if verify: action = "verify" else: action = "restore" process_local_dir(action, local_pathname) if action in ['full', 'inc', 'verify']: set_selection() elif len(args) > 2: raise AssertionError("this code should not be reachable") check_consistency(action) log.Info(_("Main action: ") + action) return action
def main(): output = [] def Log(s, verb_level, code=1, extra=None, force_print=False): if verb_level <= log.getverbosity(): output.extend(s.split("\n")) # def PrintCollectionStatus(col_stats, force_print=False): # # raise ValueError(type(col_stats.matched_chain_pair[1])) # output.append({ # "num_backup_sets": # }) # log.PrintCollectionStatus = PrintCollectionStatus results = None try: settings = dict() Intersplunk.readResults(None, settings, True) dup_time.setcurtime() archive_dir = os.path.join(app_dir, "local", "data", "archive") try: os.makedirs(archive_dir) except: pass if sys.argv[1] == "splunk-last-backups": ap = argparse.ArgumentParser() ap.add_argument("--time", type=int) ap.add_argument("backend") args = ap.parse_args(sys.argv[2:]) dup_globals.gpg_profile = gpg.GPGProfile() dup_globals.gpg_profile.passphrase = os.environ["PASSPHRASE"] backend.import_backends() dup_globals.backend = backend.get_backend(args.backend) if dup_globals.backup_name is None: dup_globals.backup_name = commandline.generate_default_backup_name( args.backend) commandline.set_archive_dir(archive_dir) results = [] time = args.time col_stats = dup_collections.CollectionsStatus( dup_globals.backend, dup_globals.archive_dir_path, "list-current").set_values() try: sig_chain = col_stats.get_backup_chain_at_time(time) except dup_collections.CollectionsError: results.append({ "last_full_backup_time": 0, "last_incr_backup_time": 0, }) else: if sig_chain.incset_list: last_incr_backup_time = max( [incset.end_time for incset in sig_chain.incset_list]) else: last_incr_backup_time = 0 results.append({ "last_full_backup_time": col_stats.get_last_full_backup_time(), "last_incr_backup_time": last_incr_backup_time }) elif sys.argv[1] == "splunk-file-list": ap = argparse.ArgumentParser() ap.add_argument("--time") ap.add_argument("backend") args = ap.parse_args(sys.argv[2:]) args.time = int(args.time.split(".")[0]) dup_time.setcurtime(args.time) dup_globals.restore_time = args.time dup_globals.gpg_profile = gpg.GPGProfile() dup_globals.gpg_profile.passphrase = os.environ["PASSPHRASE"] backend.import_backends() dup_globals.backend = backend.get_backend(args.backend) if dup_globals.backup_name is None: dup_globals.backup_name = commandline.generate_default_backup_name( args.backend) commandline.set_archive_dir(archive_dir) results = [] col_stats = dup_collections.CollectionsStatus( dup_globals.backend, dup_globals.archive_dir_path, "list-current").set_values() time = args.time sig_chain = col_stats.get_signature_chain_at_time(time) path_iter = diffdir.get_combined_path_iter( sig_chain.get_fileobjs(time)) for path in path_iter: if path.difftype != u"deleted" and path.index: mode = bin(path.mode)[2:] perms = "" for p, val in enumerate(mode): if p in (0, 3, 6): c = "r" elif p in (1, 4, 7): c = "w" elif p in (2, 5, 8): c = "x" perms += c if int(val) else "-" if path.type == "dir": perms = "d" + perms elif path.type == "sym": perms = "l" + perms else: perms = "-" + perms results.append({ "perms": perms, "owner": path.stat.st_uid, "group": path.stat.st_gid, "size": path.stat.st_size, "modtime": path.stat.st_mtime, "filename": os.path.join(*path.index), }) else: args = ["--archive-dir", archive_dir] + sys.argv[1:] action = commandline.ProcessCommandLine(args) log.Log = Log try: dup_main.do_backup(action) except dup_collections.CollectionsError: results = [] except SystemExit: pass except Exception as e: import traceback # sys.stderr.write(traceback.format_exc()) Intersplunk.generateErrorResults("Traceback: %s" % traceback.format_exc()) return if output and not results: import time results = [{"_raw": "\n".join(output), "_time": time.time()}] if results: try: Intersplunk.outputResults(results) except Exception: import traceback sys.stderr.write(traceback.format_exc()) results = Intersplunk.generateErrorResults("Traceback: %s" % traceback.format_exc()) Intersplunk.outputResults(results)
def setUp(self): super(GPGTest, self).setUp() self.unpack_testfiles() self.default_profile = gpg.GPGProfile(passphrase=u"foobar")
def set_gpg_profile(self): u"""Set gpg profile to standard "foobar" sym""" self.set_global(u'gpg_profile', gpg.GPGProfile(passphrase=u"foobar"))
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # General Public License for more details. # # You should have received a copy of the GNU General Public License # along with duplicity; if not, write to the Free Software Foundation, # Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA import helper import sys, os, unittest, random from duplicity import gpg from duplicity import path helper.setup() default_profile = gpg.GPGProfile(passphrase = "foobar") class GPGTest(unittest.TestCase): """Test GPGFile""" def setUp(self): assert not os.system("tar xzf testfiles.tar.gz > /dev/null 2>&1") def tearDown(self): assert not os.system("rm -rf testfiles tempdir temp2.tar") def deltmp(self): """Delete testfiles/output and recreate""" assert not os.system("rm -rf testfiles/output") assert not os.system("mkdir testfiles/output") def gpg_cycle(self, s, profile = None):
def set_gpg_profile(self): """Set gpg profile to standard "foobar" sym""" globals.gpg_profile = gpg.GPGProfile(passphrase="foobar")