Skip to content
Snippets Groups Projects
mdsigner.py 1.34 KiB
#!/usr/bin/env python
import sys
import copy
from concurrent.futures import ThreadPoolExecutor

from lxml import etree as ET
# import traceback

from utils import hasher
from signers import Signers

# Find all IdP's in edugain metadata
idps = []
success = 0
failed = 0
maxthreads = 8
signer = Signers('normal_signer')


def sign(xml, name):
    global success, failed, cert, key
    # print("Signer")
    try:
        sha1 = hasher(name)
        signed = signer(xml)
        out = ET.tostring(signed, pretty_print=True).decode()
        # XMLVerifier().verify(out, x509_cert=cert)
        with open(f'output/{sha1}.xml', 'w') as f:
            f.write(out)
        success += 1
    except Exception as e:
        print(name)
        print(f"  {e}")
        # traceback.print_exc()
        failed += 1


with ThreadPoolExecutor(max_workers=maxthreads) as executor:
    for mdfile in sys.argv[1:]:
        tree = ET.ElementTree(file=mdfile)
        root = tree.getroot()
        ns = copy.deepcopy(root.nsmap)
        ns['xml'] = 'http://www.w3.org/XML/1998/namespace'

        for idp in root.findall('md:EntityDescriptor', ns):
            entityID = idp.attrib.get('entityID', 'none')
            if entityID not in idps:
                idps.append(entityID)
                executor.submit(sign, idp, entityID)

print(f"Succeeded: {success}")
print(f"Failed: {failed}")