def test_parse_manifest(): # a manifest file is similar to HTTP headers, but are limited to # 72 character lines (70 characters in practice because \r\n is two # bytes). long lines are wrapped to the next line beginning with a space. manifest = """ Manifest-Version: 1.0 Built-By: anchore Long-Attribute: 12345678901234567890123456789012345678901234567890123 45678901234567890123456789012345678901234567890123456789012345678901 23456789012345678901234567890123456789012345678901234567890123456789 0 Another-Attribute: 12345678901234567890123456789012345678901234567890 with space """.strip() attrs = java_util.parse_manifest(manifest.splitlines()) assert "Manifest-Version" in attrs assert attrs["Manifest-Version"] == "1.0" assert attrs["Built-By"] == "anchore" assert attrs["Long-Attribute"] == ("1234567890" * 19) assert attrs["Another-Attribute"] == ("1234567890" * 5) + " with space"
def test_parse_manifest(): # a manifest file is similar to HTTP headers, but are limited to # 72 character lines (70 characters in practice because \r\n is two # bytes). long lines are wrapped to the next line beginning with a space. manifest = """ Manifest-Version: 1.0 Built-By: anchore Long-Attribute: 12345678901234567890123456789012345678901234567890123 45678901234567890123456789012345678901234567890123456789012345678901 23456789012345678901234567890123456789012345678901234567890123456789 0 Another-Attribute: 12345678901234567890123456789012345678901234567890 with space """.strip() attrs = java_util.parse_manifest(manifest.splitlines()) assert 'Manifest-Version' in attrs assert attrs['Manifest-Version'] == '1.0' assert attrs['Built-By'] == 'anchore' assert attrs['Long-Attribute'] == ('1234567890' * 19) assert attrs['Another-Attribute'] == ('1234567890' * 5) + ' with space'
def process_java_archive(prefix, filename, inZFH=None): ret = [] fullpath = '/'.join([prefix, filename]) jtype = None patt = re.match(java_library_file, fullpath) if patt: jtype = patt.group(1) else: return [] name = re.sub("\." + jtype + "$", "", fullpath.split("/")[-1]) top_el = {} sub_els = [] try: # set up the zipfile handle try: if not inZFH: if not os.access(fullpath, os.R_OK): os.chmod(fullpath, 0o444) if zipfile.is_zipfile(fullpath): ZFH = zipfile.ZipFile(fullpath, 'r') location = filename else: return [] else: zdata = BytesIO(inZFH.read()) ZFH = zipfile.ZipFile(zdata, 'r') location = prefix + ":" + filename except Exception as err: raise err top_el = { 'metadata': {}, 'specification-version': "N/A", 'implementation-version': "N/A", 'maven-version': "N/A", 'origin': "N/A", 'location': location, 'type': "java-" + str(jtype), 'name': name } filenames = ZFH.namelist() if 'META-INF/MANIFEST.MF' in filenames: try: with ZFH.open('META-INF/MANIFEST.MF', 'r') as MFH: top_el['metadata']['MANIFEST.MF'] = anchore_engine.utils.ensure_str(MFH.read()) manifest = java_util.parse_manifest(top_el['metadata']['MANIFEST.MF'].splitlines()) top_el['specification-version'] = manifest.get('Specification-Version', 'N/A') top_el['implementation-version'] = manifest.get('Implementation-Version', 'N/A') if 'Specification-Vendor' in manifest: top_el['origin'] = manifest['Specification-Vendor'] elif 'Implementation-Vendor' in manifest: top_el['origin'] = manifest['Implementation-Vendor'] except: # no manifest could be parsed out, leave the el values unset pass else: print('WARN: no META-INF/MANIFEST.MF found in ' + fullpath) archives = [fname for fname in filenames if re.match(java_library_file, fname)] pomprops = [fname for fname in filenames if fname.endswith('/pom.properties')] for archive in archives: with ZFH.open(archive, 'r') as ZZFH: sub_els += process_java_archive(location, archive, ZZFH) for pomprop in pomprops: pom_el = { 'metadata': {}, 'specification-version': "N/A", 'implementation-version': "N/A", 'maven-version': "N/A", 'origin': "N/A", 'location': top_el['location'], 'type': "java-" + str(jtype), 'name': "N/A" } with ZFH.open(pomprop) as pomfile: pombuf = anchore_engine.utils.ensure_str(pomfile.read()) props = parse_properties(pombuf) group = props.get('groupId', "N/A") artifact = props.get('artifactId', "N/A") mversion = props.get('version', "N/A") addnew = False if re.match("^{}.*".format(artifact), top_el['name']): the_el = top_el else: the_el = pom_el the_el['location'] = ":".join([the_el['location'], artifact]) addnew = True the_el['metadata']['pom.properties'] = anchore_engine.utils.ensure_str(pombuf) if group: the_el['origin'] = group if artifact: the_el['name'] = artifact if mversion: the_el['maven-version'] = mversion if addnew: sub_els.append(the_el) except Exception as err: raise err finally: if inZFH: try: inZFH.close() except: pass ret = [top_el] if sub_els: ret += sub_els return ret