Beispiel #1
0
    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'])
        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)
Beispiel #2
0
    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)
Beispiel #3
0
    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)
Beispiel #4
0
    def save_image(self):
        # Dockerfile handling
        if self.dockerfile_contents:
            if self.dockerfile_mode == 'Guessed':
                anchore_utils.update_file_str(self.dockerfile_contents, self.anchore_imagedir + "/Dockerfile.guessed", backup=False)
            elif self.dockerfile_mode == 'Actual':
                anchore_utils.update_file_str(self.dockerfile_contents, self.anchore_imagedir + "/Dockerfile", backup=False)
                if os.path.exists(self.anchore_imagedir + "/Dockerfile.guessed"):
                    os.remove(self.anchore_imagedir + "/Dockerfile.guessed")

        # Image output dir populate
        imageoutputdir = self.anchore_imagedir + "/image_output/image_info"
        if not os.path.exists(imageoutputdir):
            os.makedirs(imageoutputdir)

        anchore_utils.write_kvfile_fromdict(imageoutputdir + "/image.meta", self.meta)

        level = 0
        tagdict = {}
        for t in self.anchore_current_tags:
            tagdict[t] = str(level)
            level = level + 1
        anchore_utils.write_kvfile_fromdict(imageoutputdir + "/image_current.tags", tagdict)


        level = 0
        tagdict = {}
        for t in self.anchore_all_tags:
            tagdict[t] = str(level)
            level = level + 1
        anchore_utils.write_kvfile_fromdict(imageoutputdir + "/image_all.tags", tagdict)

        dfile = self.get_dockerfile()
        if dfile:
            shutil.copy(dfile, imageoutputdir + "/Dockerfile")

        if not os.path.exists(self.anchore_imagedir + "/image_output/image_familytree/"):
            os.makedirs(self.anchore_imagedir + "/image_output/image_familytree/")

        level = 0
        ldict = {}
        for fid in self.get_layers():
            ldict[fid] = str(level)
            level = level + 1
        anchore_utils.write_kvfile_fromdict(self.anchore_imagedir + "/image_output/image_familytree/layers", ldict)

        level = 0
        ldict = {}
        for fid in self.get_familytree():
            ldict[fid] = str(level)
            src = '/'.join([self.anchore_image_datadir, fid])
            dst = '/'.join([self.anchore_imagedir, "/image_output/image_familytree/", fid])
            try:
                os.remove(dst)
            except:
                pass
            os.symlink(src, dst)

            level = level + 1
            if self.get_earliest_base() == fid:
                src = '/'.join([self.anchore_image_datadir, fid])
                dst = '/'.join([self.anchore_imagedir, "/image_output/image_familytree/base"])
                try:
                    os.remove(dst)
                except:
                    pass
                os.symlink(src, dst)
        anchore_utils.write_kvfile_fromdict(self.anchore_imagedir + "/image_output/image_familytree/familytree", ldict)

        # generate and save image report
        report = self.generate_image_report()
        self.anchore_db.save_image_report(self.meta['imageId'], report)
Beispiel #5
0
    def save_image(self):
        # Dockerfile handling
        if self.dockerfile_contents:
            if self.dockerfile_mode == 'Guessed':
                anchore_utils.update_file_str(self.dockerfile_contents, self.anchore_imagedir + "/Dockerfile.guessed", backup=False)
            elif self.dockerfile_mode == 'Actual':
                anchore_utils.update_file_str(self.dockerfile_contents, self.anchore_imagedir + "/Dockerfile", backup=False)
                if os.path.exists(self.anchore_imagedir + "/Dockerfile.guessed"):
                    os.remove(self.anchore_imagedir + "/Dockerfile.guessed")

        # Image output dir populate
        imageoutputdir = self.anchore_imagedir + "/image_output/image_info"
        if not os.path.exists(imageoutputdir):
            os.makedirs(imageoutputdir)

        anchore_utils.write_kvfile_fromdict(imageoutputdir + "/image.meta", self.meta)

        level = 0
        tagdict = {}
        for t in self.anchore_current_tags:
            tagdict[t] = str(level)
            level = level + 1
        anchore_utils.write_kvfile_fromdict(imageoutputdir + "/image_current.tags", tagdict)


        level = 0
        tagdict = {}
        for t in self.anchore_all_tags:
            tagdict[t] = str(level)
            level = level + 1
        anchore_utils.write_kvfile_fromdict(imageoutputdir + "/image_all.tags", tagdict)

        dfile = self.get_dockerfile()
        if dfile:
            shutil.copy(dfile, imageoutputdir + "/Dockerfile")

        if not os.path.exists(self.anchore_imagedir + "/image_output/image_familytree/"):
            os.makedirs(self.anchore_imagedir + "/image_output/image_familytree/")

        level = 0
        ldict = {}
        for fid in self.get_layers():
            ldict[fid] = str(level)
            level = level + 1
        anchore_utils.write_kvfile_fromdict(self.anchore_imagedir + "/image_output/image_familytree/layers", ldict)

        level = 0
        ldict = {}
        for fid in self.get_familytree():
            ldict[fid] = str(level)
            src = '/'.join([self.anchore_image_datadir, fid])
            dst = '/'.join([self.anchore_imagedir, "/image_output/image_familytree/", fid])
            try:
                os.remove(dst)
            except:
                pass
            os.symlink(src, dst)

            level = level + 1
            if self.get_earliest_base() == fid:
                src = '/'.join([self.anchore_image_datadir, fid])
                dst = '/'.join([self.anchore_imagedir, "/image_output/image_familytree/base"])
                try:
                    os.remove(dst)
                except:
                    pass
                os.symlink(src, dst)
        anchore_utils.write_kvfile_fromdict(self.anchore_imagedir + "/image_output/image_familytree/familytree", ldict)

        # generate and save image report
        report = self.generate_image_report()
        self.anchore_db.save_image_report(self.meta['imageId'], report)