def save_image_new(self, imageId, report=None): # setup dir structure self.make_image_structure(imageId) if not report: report = load_image_new(imageId) # store the reports self.save_image_report(imageId, report['image_report']) # Strip back out the file content if it was added by the load_image_new call. Just to be safe for now. if report['analysis_report'].get('retrieve_files', {}).get('file_content.all'): report['analysis_report']['retrieve_files'].pop('file_content.all') self.save_analysis_report(imageId, report['analysis_report']) self.save_analyzer_manifest(imageId, report['analyzer_manifest']) self.save_gates_report(imageId, report['gates_report']) self.save_gates_eval_report(imageId, report['gates_eval_report']) # populate the analyzer_outputs for module_name in report['analysis_report'].keys(): for module_value in report['analysis_report'][module_name].keys(): for module_type in ['base', 'extra', 'user']: if module_type in report['analysis_report'][module_name][ module_value]: adata = report['analysis_report'][module_name][ module_value][module_type] if adata: self.save_analysis_output(imageId, module_name, module_value, adata, module_type=module_type) # populate gates outputs for gname in report['gates_report'].keys(): self.save_gate_output(imageId, gname, report['gates_report'][gname]) # populate image metadata thedir = os.path.join(self.imagerootdir, imageId, "image_output", "image_info") if not os.path.exists(thedir): os.makedirs(thedir) thefile = os.path.join(thedir, "image.meta") anchore_utils.write_kvfile_fromdict(thefile, report['image_report']['meta']) thefile = os.path.join(thedir, "Dockerfile") anchore_utils.write_plainfile_fromstr( thefile, report['image_report']['dockerfile_contents']) return (True)
def run_differs(self, image, baseimage): self._logger.debug("comparison of " + str(image.meta['imagename']) + " to " + str(image.meta['imagename']) + ": begin") shortida = image.meta['shortId'] shortidb = baseimage.meta['shortId'] if not image.is_analyzed(): self._logger.error("cannot compare image " + shortida + " - need to analyze first.") return (False) if not baseimage.is_analyzed(): self._logger.error("cannot compare image " + shortidb + " - need to analyze first") return (False) outputdir = image.anchore_imagedir if not self.force and os.path.exists( outputdir + "/compare_output/" + baseimage.meta['imageId'] + "/differs.done"): self._logger.debug("images already compared and --force not specified, nothing to do") self._logger.info(shortida + " to " + shortidb + ": compared.") return (True) self._logger.info(shortida + " to " + shortidb + ": comparing ...") if not os.path.exists(outputdir): self._logger.debug("output directory '" + str(outputdir) + "' does not exist, creating") os.makedirs(outputdir) thedir = outputdir + "/compare_output/" + baseimage.meta['imageId'] + "/" if not os.path.exists(thedir): self._logger.debug("output directory '" + str(thedir) + "' does not exist, creating") os.makedirs(thedir) compares = anchore_utils.diff_images(image, baseimage) for azkey in compares.keys(): for aokey in compares[azkey].keys(): outputdict = compares[azkey][aokey] thedir = outputdir + "/compare_output/" + baseimage.meta['imageId'] + "/" + azkey + "/" if not os.path.exists(thedir): os.makedirs(thedir) thefile = thedir + "/" + aokey anchore_utils.write_kvfile_fromdict(thefile, outputdict) self._logger.debug("all comparisons completed") anchore_utils.touch_file(outputdir + "/compare_output/" + baseimage.meta['imageId'] + "/differs.done") self._logger.info(shortida + " to " + shortidb + ": compared.") self._logger.debug("comparison of " + str(image.meta['imagename']) + " to " + str(image.meta['imagename']) + ": end") return (True)
def save_analysis_output(self, imageId, module_name, module_value, data, module_type=None, directory_data=False): if not module_type or module_type == 'base': odir = '/'.join( [self.imagerootdir, imageId, "analyzer_output", module_name]) else: odir = '/'.join([ self.imagerootdir, imageId, "analyzer_output_" + module_type, module_name ]) if not directory_data: thefile = '/'.join([odir, module_value]) if not os.path.exists(odir): os.makedirs(odir) return (anchore_utils.write_kvfile_fromdict(thefile, data)) else: if os.path.isdir(data): if os.path.isdir(odir): shutil.rmtree(odir) os.makedirs(odir) shutil.move(data, odir)
def save_image_report(self, imageId, report): # populate image metadata thedir = os.path.join(self.imagerootdir, imageId, "image_output", "image_info") if not os.path.exists(thedir): os.makedirs(thedir) thefile = os.path.join(thedir, "image.meta") if 'meta' in report: anchore_utils.write_kvfile_fromdict(thefile, report['meta']) # save the report itself date = str(int(time.time())) thedir = self.imagerootdir + "/" + imageId + "/reports/" if not os.path.exists(thedir): os.makedirs(thedir) thefile = thedir + "/image_report.json" if os.path.exists(thefile): oldreport = self.load_image_report(imageId) if 'tag_history' in oldreport: report['tag_history'] = list(oldreport['tag_history']) else: report['tag_history'] = list() if 'anchore_current_tags' not in oldreport: oldreport['anchore_current_tags'] = list( [date, report['anchore_current_tags']]) diff = list( set(oldreport['anchore_current_tags']).symmetric_difference( set(report['anchore_current_tags']))) if len(diff) > 0: # there is a difference between stored tags and new tags report['tag_history'].append( [date, oldreport['anchore_current_tags']]) if 'tag_history' not in report: report['tag_history'] = list() if len(report['tag_history']) <= 0: report['tag_history'].append( [date, report['anchore_current_tags']]) anchore_utils.update_file_jsonstr(json.dumps(report), thefile, False)
def save_image_new(self, imageId, report=None): # setup dir structure self.make_image_structure(imageId) if not report: report = load_image_new(imageId) # store the reports self.save_image_report(imageId, report['image_report']) # Strip back out the file content if it was added by the load_image_new call. Just to be safe for now. if report['analysis_report'].get('retrieve_files',{}).get('file_content.all'): report['analysis_report']['retrieve_files'].pop('file_content.all') self.save_analysis_report(imageId, report['analysis_report']) self.save_analyzer_manifest(imageId, report['analyzer_manifest']) self.save_gates_report(imageId, report['gates_report']) self.save_gates_eval_report(imageId, report['gates_eval_report']) # populate the analyzer_outputs for module_name in report['analysis_report'].keys(): for module_value in report['analysis_report'][module_name].keys(): for module_type in ['base', 'extra', 'user']: if module_type in report['analysis_report'][module_name][module_value]: adata = report['analysis_report'][module_name][module_value][module_type] if adata: self.save_analysis_output(imageId, module_name, module_value, adata, module_type=module_type) # populate gates outputs for gname in report['gates_report'].keys(): self.save_gate_output(imageId, gname, report['gates_report'][gname]) # populate image metadata thedir = os.path.join(self.imagerootdir, imageId, "image_output", "image_info") if not os.path.exists(thedir): os.makedirs(thedir) thefile = os.path.join(thedir, "image.meta") anchore_utils.write_kvfile_fromdict(thefile, report['image_report']['meta']) thefile = os.path.join(thedir, "Dockerfile") anchore_utils.write_plainfile_fromstr(thefile, report['image_report']['dockerfile_contents']) return(True)
def save_image_report(self, imageId, report): # populate image metadata thedir = os.path.join(self.imagerootdir, imageId, "image_output", "image_info") if not os.path.exists(thedir): os.makedirs(thedir) thefile = os.path.join(thedir, "image.meta") if 'meta' in report: anchore_utils.write_kvfile_fromdict(thefile, report['meta']) # save the report itself date = str(int(time.time())) thedir = self.imagerootdir + "/" + imageId + "/reports/" if not os.path.exists(thedir): os.makedirs(thedir) thefile = thedir + "/image_report.json" if os.path.exists(thefile): oldreport = self.load_image_report(imageId) if 'tag_history' in oldreport: report['tag_history'] = list(oldreport['tag_history']) else: report['tag_history'] = list() if 'anchore_current_tags' not in oldreport: oldreport['anchore_current_tags'] = list([date, report['anchore_current_tags']]) diff = list(set(oldreport['anchore_current_tags']).symmetric_difference(set(report['anchore_current_tags']))) if len(diff) > 0: # there is a difference between stored tags and new tags report['tag_history'].append([date, oldreport['anchore_current_tags']]) if 'tag_history' not in report: report['tag_history'] = list() if len(report['tag_history']) <= 0: report['tag_history'].append([date, report['anchore_current_tags']]) anchore_utils.update_file_jsonstr(json.dumps(report), thefile, False)
def save_analysis_output(self, imageId, module_name, module_value, data, module_type=None, directory_data=False): if not module_type or module_type == 'base': odir = '/'.join([self.imagerootdir, imageId, "analyzer_output", module_name]) else: odir = '/'.join([self.imagerootdir, imageId, "analyzer_output_"+module_type, module_name]) if not directory_data: thefile = '/'.join([odir, module_value]) if not os.path.exists(odir): os.makedirs(odir) return(anchore_utils.write_kvfile_fromdict(thefile, data)) else: if os.path.isdir(data): if os.path.isdir(odir): shutil.rmtree(odir) os.makedirs(odir) shutil.move(data, odir)
def run_differs(self, image, baseimage): self._logger.debug("comparison of " + str(image.meta['imagename']) + " to " + str(image.meta['imagename']) + ": begin") shortida = image.meta['shortId'] shortidb = baseimage.meta['shortId'] if not image.is_analyzed(): self._logger.error("cannot compare image " + shortida + " - need to analyze first.") return (False) if not baseimage.is_analyzed(): self._logger.error("cannot compare image " + shortidb + " - need to analyze first") return (False) outputdir = image.anchore_imagedir if not self.force and os.path.exists( outputdir + "/compare_output/" + baseimage.meta['imageId'] + "/differs.done"): self._logger.debug("images already compared and --force not specified, nothing to do") self._logger.info(shortida + " to " + shortidb + ": compared.") return (True) self._logger.info(shortida + " to " + shortidb + ": comparing ...") if not os.path.exists(outputdir): self._logger.debug("output directory '" + str(outputdir) + "' does not exist, creating") os.makedirs(outputdir) thedir = outputdir + "/compare_output/" + baseimage.meta['imageId'] + "/" if not os.path.exists(thedir): self._logger.debug("output directory '" + str(thedir) + "' does not exist, creating") os.makedirs(thedir) self._logger.debug("getting analysis reports for images") areport = image.get_analysis_report() breport = baseimage.get_analysis_report() self._logger.debug("performing comparison") for azkey in areport.keys(): if azkey in breport: for aokey in areport[azkey].keys(): if aokey in breport[azkey]: outputdict = {} adatadict = {} for l in areport[azkey][aokey]: l = l.strip() (k, v) = l.split() adatadict[k] = v bdatadict = {} for l in breport[azkey][aokey]: l = l.strip() (k, v) = l.split() bdatadict[k] = v for dkey in adatadict.keys(): if not dkey in bdatadict: outputdict[dkey] = "INIMG_NOTINBASE" elif adatadict[dkey] != bdatadict[dkey]: outputdict[dkey] = "VERSION_DIFF" for dkey in bdatadict.keys(): if not dkey in adatadict: outputdict[dkey] = "INBASE_NOTINIMG" thedir = outputdir + "/compare_output/" + baseimage.meta['imageId'] + "/" + azkey + "/" if not os.path.exists(thedir): os.makedirs(thedir) thefile = thedir + "/" + aokey anchore_utils.write_kvfile_fromdict(thefile, outputdict) self._logger.debug("all comparisons completed") anchore_utils.touch_file(outputdir + "/compare_output/" + baseimage.meta['imageId'] + "/differs.done") self._logger.info(shortida + " to " + shortidb + ": compared.") self._logger.debug("comparison of " + str(image.meta['imagename']) + " to " + str(image.meta['imagename']) + ": end") return (True)
def run_differs(self, image, baseimage): self._logger.debug("comparison of " + str(image.meta['imagename']) + " to " + str(image.meta['imagename']) + ": begin") shortida = image.meta['shortId'] shortidb = baseimage.meta['shortId'] if not image.is_analyzed(): self._logger.error("cannot compare image " + shortida + " - need to analyze first.") return (False) if not baseimage.is_analyzed(): self._logger.error("cannot compare image " + shortidb + " - need to analyze first") return (False) outputdir = image.anchore_imagedir if not self.force and os.path.exists(outputdir + "/compare_output/" + baseimage.meta['imageId'] + "/differs.done"): self._logger.debug( "images already compared and --force not specified, nothing to do" ) self._logger.info(shortida + " to " + shortidb + ": compared.") return (True) self._logger.info(shortida + " to " + shortidb + ": comparing ...") if not os.path.exists(outputdir): self._logger.debug("output directory '" + str(outputdir) + "' does not exist, creating") os.makedirs(outputdir) thedir = outputdir + "/compare_output/" + baseimage.meta[ 'imageId'] + "/" if not os.path.exists(thedir): self._logger.debug("output directory '" + str(thedir) + "' does not exist, creating") os.makedirs(thedir) compares = anchore_utils.diff_images(image, baseimage) for azkey in compares.keys(): for aokey in compares[azkey].keys(): outputdict = compares[azkey][aokey] thedir = outputdir + "/compare_output/" + baseimage.meta[ 'imageId'] + "/" + azkey + "/" if not os.path.exists(thedir): os.makedirs(thedir) thefile = thedir + "/" + aokey anchore_utils.write_kvfile_fromdict(thefile, outputdict) self._logger.debug("all comparisons completed") anchore_utils.touch_file(outputdir + "/compare_output/" + baseimage.meta['imageId'] + "/differs.done") self._logger.info(shortida + " to " + shortidb + ": compared.") self._logger.debug("comparison of " + str(image.meta['imagename']) + " to " + str(image.meta['imagename']) + ": end") return (True)