def setUp(self): # filenames = os.random(4) self.image = SatoriImage() self.image.add_file("/etc/passwd") # Add a file str_pkl_com = SatoriPickler() str_pkl = SatoriPickler(compress=False) str_jsn_com = SatoriJsoner() str_jsn = SatoriJsoner(compress=False) self.filers = [ str_jsn_com, str_jsn, str_pkl_com, str_pkl, ] try: os.mkdir("gen_images") except FileExistsError: pass os.chdir("gen_images") for serializer in self.filers: serializer.write(self.image, 'test') os.listdir('.')
def test_listing_non_dir(self): si = SatoriImage() si.add_file("/etc/shadow") try: con = si.get_dir_contents('/etc/shadow') except NotADirectoryError: self.assertTrue(True)
def test_add_file(self): si = SatoriImage() si.add_file("/etc/shadow") # print(si) ret = si.get_dir_contents("/etc") # print(ret) self.assertTrue('shadow' in ret)
def test_setting_mult_attrs(self): si = SatoriImage() si.set_multiple_attributes('/tmp/test', ('ext1', 'value1'), ('ext2', 'value2'), ('ext3', 'value3'), force_create=True) self.assertTrue(si.get_attribute('/tmp/test', 'ext2') == 'value2')
def read(self, filename, suffixed=True): if not suffixed: filename += self.suffix fd = self.open(filename, 'rb') content = fd.read() data_struct = self.loads(content) fd.close() image = SatoriImage() image._set_data_struct(data_struct) self.last_file = filename return image
def main(): parser = _setup_argument_parser() args = parser.parse_args() if args.quiet: set_quiet_logger() image = SatoriImage() EVENTS["imager.on_start"](parser=parser, args=args, satori_image=image) conn_context = None if args.remote: try: import satoriremote conn_context = get_image_context_from_arg(args.remote, allow_local=False) with conn_context as context: _clone(args, image, context=context) except ImportError: logger.critical( "'--remote' parameter not available without 'satori-remote' package." ) sys.exit(1) else: _clone(args, image, context=os) EVENTS["imager.on_end"]()
class test_Serializers(unittest.TestCase): def setUp(self): # filenames = os.random(4) self.image = SatoriImage() self.image.add_file("/etc/passwd") # Add a file str_pkl_com = SatoriPickler() str_pkl = SatoriPickler(compress=False) str_jsn_com = SatoriJsoner() str_jsn = SatoriJsoner(compress=False) self.filers = [ str_jsn_com, str_jsn, str_pkl_com, str_pkl, ] try: os.mkdir("gen_images") except FileExistsError: pass os.chdir("gen_images") for serializer in self.filers: serializer.write(self.image, 'test') os.listdir('.') def test_create_image(self): for serializer in self.filers: image = serializer.read('test', suffixed=False) self.assertTrue(image == self.image) def tearDown(self): for filename in os.listdir('.'): os.unlink(filename) # print("Cleaning '%s'" % filename) pass os.chdir("..") os.rmdir('gen_images')
def test_multiple_files(self): si = SatoriImage() si.add_file("/etc/shadow") si.add_file("/etc/passwd") si.add_file("/etc/sudoers") con = si.get_dir_contents('/etc') self.assertTrue(len(con) == 3) self.assertTrue('shadow' in con) self.assertTrue('passwd' in con) self.assertTrue('sudoers' in con)
def test_classes(self): si = SatoriImage() si.add_class("class1", section=_DATA_SECTION) try: si.add_class("class1", section=_DATA_SECTION) except KeyError: self.assertTrue(True) # Readding a class throws exception cd = si.get_class('class1', section=_DATA_SECTION) self.assertTrue(not cd) # New class yields empty dict try: cd2 = si.get_class('class2', section=_DATA_SECTION) except KeyError: self.assertTrue(True) # Non-existent class throws Exception # Existent classes are compatible sysinfo = si.get_class('filesystem', section=_DATA_SECTION)
def main(): parser = _setup_argument_parser() args = parser.parse_args() source_context = get_image_context_from_arg(args.original_image) logger.warn("Loaded image '{}'".format(args.original_image)) destination_context = get_image_context_from_arg(args.tested_image) logger.warn("Loaded image '{}'".format(args.tested_image)) # if not args.output: try: results = load_image(args.output) logger.warn("SatoriImage '{}' loaded to archive results".format( args.output)) except TypeError as te: logger.warn("No output image selected") logger.info("Using an Empty SatoriImage to store results") results = SatoriImage() except ValueError: logger.error("Output image file '{}' is not a SatoriImage".format( args.output)) logger.warn("Using an Empty SatoriImage to store results".format( args.output)) results = SatoriImage() assert (results is not None) try: logger.info("Adding DIFF section in SatoriImage") results.add_section(_DIFFS_SECTION) except KeyError: logger.warn("DIFF Section in SatoriImage already exists") existing_diffs = results.get_classes(_DIFFS_SECTION) if existing_diffs: logger.info("Existing DIFFs in SatoriImage: {}".format( str(existing_diffs))) name = get_diff_name(existing_diffs) global DIFF_NAME DIFF_NAME = name logger.warn("New DIFF name is '{}'".format(name)) with source_context as source: with destination_context as destination: if not args.entrypoints: # s_entrypoints try: s_epoints = source.get_entrypoints() logger.info( "Original Image entrypoints: {}".format(s_epoints)) except: logger.warn("Entrypoints for source cannot be specified.") d_epoints = set('/') try: d_epoints = destination.get_entrypoints() logger.info( "Tested Image entrypoints: {}".format(d_epoints)) except: logger.warn( "Entrypoints for destination cannot be specified.") d_epoints = set('/') common_entrypoints = s_epoints & d_epoints if not common_entrypoints: logger.critical("No common entrypoints found. Exiting...") sys.exit(-1) else: logger.info("Common Entrypoints are {}".format( str(common_entrypoints))) args.entrypoints = common_entrypoints logger.warn("Operating for entrypoints: {}".format( str(args.entrypoints))) EVENTS['differ.on_start'](parser=parser, args=args, source=source, destination=destination, results=results, diff_name=DIFF_NAME) logger.warn("Diff Process Started...") diff_images(source, destination, args.entrypoints, results) logger.warn("Diff Process Finished!") if not args.output: args.output = DIFF_NAME image_serializer = SatoriJsoner() # image_serializer = SatoriPickler() if args.output.endswith(image_serializer.suffix): image_serializer.suffix = '' image_serializer.write(results, args.output) logger.warn("Stored to file '{}'".format(image_serializer.last_file)) # print(diff_obj) # print(results) # if args.output: # pass EVENTS['differ.on_end'](results)
def test_creating_directories(self): si = SatoriImage() si.add_file('/etc/sshd/') # trailing '/' creates dir self.assertTrue(si.is_dir('/etc/sshd'))
def setUp(self): self.si = SatoriImage()
class test_EmptyImage(unittest.TestCase): def setUp(self): self.si = SatoriImage() def test_add_file(self): si = SatoriImage() si.add_file("/etc/shadow") # print(si) ret = si.get_dir_contents("/etc") # print(ret) self.assertTrue('shadow' in ret) def test_non_existent_file(self): self.si.add_file("/etc/shadow") try: self.si.get_dir_contents('/asjnas') except FileNotFoundError: self.assertTrue(True) def test_multiple_files(self): si = SatoriImage() si.add_file("/etc/shadow") si.add_file("/etc/passwd") si.add_file("/etc/sudoers") con = si.get_dir_contents('/etc') self.assertTrue(len(con) == 3) self.assertTrue('shadow' in con) self.assertTrue('passwd' in con) self.assertTrue('sudoers' in con) def test_paths(self): si = SatoriImage() si.add_file("/etc/shadow") si.add_file("/etc/dir/") self.assertTrue(si.is_dir("/etc/")) self.assertTrue(si.is_dir("/")) self.assertTrue(si.is_dir("/etc/dir")) self.assertTrue(si.is_dir("/etc/dir/")) self.assertTrue(si.is_dir("/etc///dir/")) self.assertTrue(si.is_dir("//")) self.assertTrue(si.is_dir("/etc///")) self.assertTrue(si.is_dir("//etc")) def test_listing_non_dir(self): si = SatoriImage() si.add_file("/etc/shadow") try: con = si.get_dir_contents('/etc/shadow') except NotADirectoryError: self.assertTrue(True) def test_creating_directories(self): si = SatoriImage() si.add_file('/etc/sshd/') # trailing '/' creates dir self.assertTrue(si.is_dir('/etc/sshd')) # si.listdir('/etc/sshd') def test_setting_mult_attrs(self): si = SatoriImage() si.set_multiple_attributes('/tmp/test', ('ext1', 'value1'), ('ext2', 'value2'), ('ext3', 'value3'), force_create=True) self.assertTrue(si.get_attribute('/tmp/test', 'ext2') == 'value2') # def test_stat(self): # si = SatoriImage() def test_classes(self): si = SatoriImage() si.add_class("class1", section=_DATA_SECTION) try: si.add_class("class1", section=_DATA_SECTION) except KeyError: self.assertTrue(True) # Readding a class throws exception cd = si.get_class('class1', section=_DATA_SECTION) self.assertTrue(not cd) # New class yields empty dict try: cd2 = si.get_class('class2', section=_DATA_SECTION) except KeyError: self.assertTrue(True) # Non-existent class throws Exception # Existent classes are compatible sysinfo = si.get_class('filesystem', section=_DATA_SECTION)
def test_paths(self): si = SatoriImage() si.add_file("/etc/shadow") si.add_file("/etc/dir/") self.assertTrue(si.is_dir("/etc/")) self.assertTrue(si.is_dir("/")) self.assertTrue(si.is_dir("/etc/dir")) self.assertTrue(si.is_dir("/etc/dir/")) self.assertTrue(si.is_dir("/etc///dir/")) self.assertTrue(si.is_dir("//")) self.assertTrue(si.is_dir("/etc///")) self.assertTrue(si.is_dir("//etc"))