def load_image(self, dockerfile=None): if self.anchore_db.is_image_present(self.meta['imageId']): self.load_image_from_anchore() self.sync_image_meta() # if a dockerfile is passed in, override that which is stored if dockerfile: self.dockerfile_contents = anchore_utils.read_plainfile_tostr(dockerfile) self.dockerfile_mode = "Actual" #if image is in docker, load the docker data and combine try: self.load_image_from_docker() except Exception as err: self._logger.debug("could not load image ("+str(self.meta['imageId']) + ") from docker: " + str(err)) self.sync_image_meta() return (True)
def __init__(self, imagename, anchore_image_datadir, allimages, tmpdirroot="/tmp", dockerfile=None, docker_cli=None, anchore_db=None, usertype=None): # all members self.allimages = allimages self.initialized = False self.docleanup = True self.tmpdirroot = tmpdirroot self.tmpdir = '/'.join([self.tmpdirroot, str(random.randint(0, 9999999)) + ".anchoretmp"]) self.dockerfile = dockerfile self.dockerfile_contents = None self.dockerfile_mode = None self.docker_cli = None self.docker_data = {} self.docker_data_json = "" self.meta = {'imagename': imagename, 'shortname': None, 'humanname': None, 'imageId': None, 'shortId': None, 'parentId': None, 'shortparentId': None, 'usertype': usertype} self.anchore_image_datadir = None self.anchore_imagedir = None self.anchore_data = {} self.anchore_data_json = "" self.anchore_allfiles = {} self.anchore_allpkgs = {} self.anchore_familytree = None self.anchore_layers = None self.anchore_current_tags = [] self.anchore_all_tags = [] self.anchore_tag_history = [] self.anchore_analyzer_meta_json = None self.anchore_analyzer_meta = None self.anchore_analysis_report = None self.anchore_compare_report = None self.anchore_gates_report = None self.anchore_gates_eval_report = None self.anchore_image_report = None self.anchore_db = None # do some setup patt = re.compile('[0-9a-fA-F]+') if (len(self.meta['imagename']) == 64 and patt.match(self.meta['imagename'])): # imagename is a docker long uuid self.meta['shortname'] = self.meta['imagename'][0:12] else: # image name is a non-uuid or a short uuid self.meta['shortname'] = self.meta['imagename'] if docker_cli: self.docker_cli = docker_cli else: self.docker_cli = docker.Client(base_url='unix://var/run/docker.sock') self.anchore_image_datadir = anchore_image_datadir if not os.path.exists(self.anchore_image_datadir): os.makedirs(self.anchore_image_datadir) if anchore_db: self.anchore_db = anchore_db else: self.anchore_db = anchore_image_db.AnchoreImageDB(imagerootdir=self.anchore_image_datadir) # set up metadata about the image from anchore and docker if not self.load_image(): raise Exception("could not load image from Docker or Anchore") # set up image directory structure try: self.outputdirs = {'image': 'image_output', 'analyzer': 'analyzer_output', 'compare': 'compare_output', 'gate': 'gates_output'} for d in self.outputdirs.keys(): thedir = '/'.join([self.anchore_imagedir, self.outputdirs[d]]) if not os.path.exists(thedir): os.makedirs(thedir) except Exception as err: raise err # set up any additional internal members self.initialized = True self.discover_layers() self.discover_familytree() newlist = list(self.anchore_familytree) while self.meta['imageId'] in newlist: newlist.remove(self.meta['imageId']) anchore_utils.image_context_add(newlist, self.allimages, docker_cli=self.docker_cli, anchore_datadir=self.anchore_image_datadir, tmproot=self.tmpdirroot, anchore_db=self.anchore_db) # Dockerfile handling if self.dockerfile: shutil.copy(self.dockerfile, self.anchore_imagedir + "/Dockerfile") if os.path.exists(self.anchore_imagedir + "/Dockerfile"): self.dockerfile_contents = anchore_utils.read_plainfile_tostr(self.anchore_imagedir + "/Dockerfile") self.dockerfile_mode = 'Actual' self.meta['usertype'] = 'user' elif os.path.exists(self.anchore_imagedir + "/Dockerfile.guessed"): self.dockerfile_contents = anchore_utils.read_plainfile_tostr(self.anchore_imagedir + "/Dockerfile.guessed") self.dockerfile_mode = 'Guessed' else: self.dockerfile_contents = self.discover_dockerfile_contents() self.dockerfile_mode = 'Guessed'
def __init__(self, imagename, anchore_image_datadir, allimages, tmpdirroot="/tmp", dockerfile=None, docker_cli=None, anchore_db=None, usertype=None): # all members self.allimages = allimages self.initialized = False self.docleanup = True self.tmpdirroot = tmpdirroot self.tmpdir = '/'.join([self.tmpdirroot, str(random.randint(0, 9999999)) + ".anchoretmp"]) self.dockerfile = dockerfile self.dockerfile_contents = None self.dockerfile_mode = None self.docker_cli = None self.docker_data = {} self.docker_data_json = "" self.meta = {'imagename': imagename, 'shortname': None, 'humanname': None, 'imageId': None, 'shortId': None, 'parentId': None, 'shortparentId': None, 'usertype': usertype} self.anchore_image_datadir = None self.anchore_imagedir = None self.anchore_data = {} self.anchore_data_json = "" self.anchore_allfiles = {} self.anchore_allpkgs = {} self.anchore_familytree = None self.anchore_layers = None self.anchore_current_tags = [] self.anchore_all_tags = [] self.anchore_tag_history = [] self.anchore_analyzer_meta_json = None self.anchore_analyzer_meta = None self.anchore_analysis_report = None self.anchore_compare_report = None self.anchore_gates_report = None self.anchore_gates_eval_report = None self.anchore_image_report = None self.anchore_db = None # do some setup patt = re.compile('[0-9a-fA-F]+') if (len(self.meta['imagename']) == 64 and patt.match(self.meta['imagename'])): # imagename is a docker long uuid self.meta['shortname'] = self.meta['imagename'][0:12] else: # image name is a non-uuid or a short uuid self.meta['shortname'] = self.meta['imagename'] if docker_cli: self.docker_cli = docker_cli else: self.docker_cli = docker.Client(base_url='unix://var/run/docker.sock', timeout=300) self.anchore_image_datadir = anchore_image_datadir if not os.path.exists(self.anchore_image_datadir): os.makedirs(self.anchore_image_datadir) if anchore_db: self.anchore_db = anchore_db else: self.anchore_db = anchore_image_db.AnchoreImageDB(imagerootdir=self.anchore_image_datadir) # set up metadata about the image from anchore and docker if not self.load_image(): raise Exception("could not load image from Docker or Anchore") # set up image directory structure try: self.outputdirs = {'image': 'image_output', 'analyzer': 'analyzer_output', 'compare': 'compare_output', 'gate': 'gates_output'} for d in self.outputdirs.keys(): thedir = '/'.join([self.anchore_imagedir, self.outputdirs[d]]) if not os.path.exists(thedir): os.makedirs(thedir) except Exception as err: raise err # set up any additional internal members self.initialized = True self.discover_layers() self.discover_familytree() newlist = list(self.anchore_familytree) while self.meta['imageId'] in newlist: newlist.remove(self.meta['imageId']) anchore_utils.image_context_add(newlist, self.allimages, docker_cli=self.docker_cli, anchore_datadir=self.anchore_image_datadir, tmproot=self.tmpdirroot, anchore_db=self.anchore_db) # Dockerfile handling if self.dockerfile: shutil.copy(self.dockerfile, self.anchore_imagedir + "/Dockerfile") if os.path.exists(self.anchore_imagedir + "/Dockerfile"): self.dockerfile_contents = anchore_utils.read_plainfile_tostr(self.anchore_imagedir + "/Dockerfile") self.dockerfile_mode = 'Actual' self.meta['usertype'] = 'user' elif os.path.exists(self.anchore_imagedir + "/Dockerfile.guessed"): self.dockerfile_contents = anchore_utils.read_plainfile_tostr(self.anchore_imagedir + "/Dockerfile.guessed") self.dockerfile_mode = 'Guessed' else: self.dockerfile_contents = self.discover_dockerfile_contents() self.dockerfile_mode = 'Guessed'