def test_decrypt_cfg_algorithm(self): """ Decrypt a Cfg file with a non-default algorithm """ # this can't be done with self._cfg_decrypt or even # self._decrypt because we have to set the algorithm after # other options are set, but before the decrypt is performed cli = CLI(["--decrypt", "--cfg", "-p", "basic", os.path.join(self.basedir, "basic-des-cbc.crypt")]) self.set_options() Bcfg2.Options.setup.algorithm = "des_cbc" cli.run() self.assertExists("basic-des-cbc") actual = open(os.path.join(self.basedir, "basic-des-cbc")).read() self.assertEqual(self.cfg_plaintext, actual) self.assertNotEncrypted(actual)
def test_cfg_stdout(self): """ Decrypt a Cfg file to stdout """ cli = CLI(["--decrypt", "--cfg", "-p", "basic", "--stdout", os.path.join(self.basedir, "basic.crypt")]) self.set_options() old_stdout = sys.stdout sys.stdout = StringIO() cli.run() output = sys.stdout.getvalue() sys.stdout = old_stdout self.assertNotExists("basic") self.assertEqual(self.cfg_plaintext.strip(), output.strip()) self.assertNotEncrypted(output)
def test_cfg_multiple(self): """ Decrypt multiple Cfg files """ cli = CLI(["--decrypt", "--cfg", "-p", "basic", os.path.join(self.basedir, "basic.crypt"), os.path.join(self.basedir, "basic2.crypt")]) self.set_options() cli.run() self.assertExists("basic") self.assertExists("basic2") actual1 = open(os.path.join(self.basedir, "basic")).read() actual2 = open(os.path.join(self.basedir, "basic2")).read() self.assertEqual(self.cfg_plaintext, actual1) self.assertEqual(self.cfg_plaintext, actual2) self.assertNotEncrypted(actual1) self.assertNotEncrypted(actual2)
def test_encrypt_props_as_cfg(self): """ Encrypt an XML file as a Cfg file """ cli = CLI([ "--encrypt", "--cfg", "-p", "basic", os.path.join(self.basedir, "plaintext.xml") ]) self._encrypt(cli, "plaintext.xml.crypt", "plaintext.xml") os.rename(os.path.join(self.basedir, "plaintext.xml.crypt"), os.path.join(self.basedir, "test.xml.crypt")) cli = CLI([ "--decrypt", "--cfg", "-p", "basic", os.path.join(self.basedir, "test.xml.crypt") ]) self._decrypt(cli, "test.xml", "plaintext.xml")
def test_decrypt_cfg_algorithm(self): """ Decrypt a Cfg file with a non-default algorithm """ # this can't be done with self._cfg_decrypt or even # self._decrypt because we have to set the algorithm after # other options are set, but before the decrypt is performed cli = CLI([ "--decrypt", "--cfg", "-p", "basic", os.path.join(self.basedir, "basic-des-cbc.crypt") ]) self.set_options() Bcfg2.Options.setup.algorithm = "des_cbc" cli.run() self.assertExists("basic-des-cbc") actual = open(os.path.join(self.basedir, "basic-des-cbc")).read() self.assertEqual(self.cfg_plaintext, actual) self.assertNotEncrypted(actual)
def test_cfg_stdout(self): """ Decrypt a Cfg file to stdout """ cli = CLI([ "--decrypt", "--cfg", "-p", "basic", "--stdout", os.path.join(self.basedir, "basic.crypt") ]) self.set_options() old_stdout = sys.stdout sys.stdout = StringIO() cli.run() output = sys.stdout.getvalue() sys.stdout = old_stdout self.assertNotExists("basic") self.assertEqual(self.cfg_plaintext.strip(), output.strip()) self.assertNotEncrypted(output)
def test_cfg_multiple(self): """ Decrypt multiple Cfg files """ cli = CLI([ "--decrypt", "--cfg", "-p", "basic", os.path.join(self.basedir, "basic.crypt"), os.path.join(self.basedir, "basic2.crypt") ]) self.set_options() cli.run() self.assertExists("basic") self.assertExists("basic2") actual1 = open(os.path.join(self.basedir, "basic")).read() actual2 = open(os.path.join(self.basedir, "basic2")).read() self.assertEqual(self.cfg_plaintext, actual1) self.assertEqual(self.cfg_plaintext, actual2) self.assertNotEncrypted(actual1) self.assertNotEncrypted(actual2)
def _cfg_decrypt(self, opts, encrypted): if encrypted.endswith(".crypt"): decrypted = encrypted[:-6] else: self.fail("Could not determine decrypted filename for %s" % encrypted) cli = CLI(opts + [os.path.join(self.basedir, encrypted)]) self._decrypt(cli, decrypted)
def _props_decrypt(self, opts, encrypted, expected): test = os.path.join(self.basedir, "test.xml") shutil.copy(os.path.join(self.basedir, encrypted), test) cli = CLI(opts + [test]) self._decrypt(cli, "test.xml", expected) try: xdata = lxml.etree.parse(test) except: self.fail("Could not parse decrypted Properties file: %s" % sys.exc_info()[1]) for el in xdata.iter(): if el.tag is not lxml.etree.Comment and el.text.strip(): self.assertNotEncrypted(el.text)
def _props_encrypt(self, opts, plaintext, check_all=True): test = os.path.join(self.basedir, "test.xml") shutil.copy(os.path.join(self.basedir, plaintext), test) cli = CLI(opts + [test]) self._encrypt(cli, "test.xml", plaintext) try: xdata = lxml.etree.parse(test) except: self.fail("Could not parse encrypted Properties file: %s" % sys.exc_info()[1]) if check_all: for el in xdata.iter(): if el.tag is not lxml.etree.Comment and el.text.strip(): self.assertIsEncrypted(el.text)
def _cfg_encrypt(self, opts, plaintext): cli = CLI(opts + [os.path.join(self.basedir, plaintext)]) self._encrypt(cli, plaintext + ".crypt")