Skip to content

Instantly share code, notes, and snippets.

@simonLeary42
Created October 15, 2025 18:19
Show Gist options
  • Select an option

  • Save simonLeary42/e4d0a7e2ebe2d64536f07ef9fcef3e14 to your computer and use it in GitHub Desktop.

Select an option

Save simonLeary42/e4d0a7e2ebe2d64536f07ef9fcef3e14 to your computer and use it in GitHub Desktop.
import requests
import urllib.parse
import xml.etree.ElementTree as ET
from ansible.errors import AnsibleError
from ansible.utils.display import Display
from ansible.plugins.lookup import LookupBase
DOCUMENTATION = """
name: incommon_scopes
author: Simon Leary <simon.leary42@proton.me>
requirements:
- python requests library
short_description: Convert a list of InCommon entityIDs into a list of InCommon Scopes
version_added: 2.18.9
description: |
makes a blocking HTTP request to the InCommon Metadata Query Service for each entityID
options:
_terms:
description: |
_terms is a list of position arguments.
exactly 1 postitional argument required: list of entityIDs
required: true
"""
XML_NS = {
"md": "urn:oasis:names:tc:SAML:2.0:metadata",
"shibmd": "urn:mace:shibboleth:metadata:1.0",
}
MDQ_URL = "https://mdq.incommon.org/entities/{entity_id}"
display = Display()
class LookupModule(LookupBase):
def run(self, terms, variables=None, **kwargs):
(entity_ids,) = terms
output = set()
for entity_id in entity_ids:
display.v(f"fetching metadata for {entity_id}...")
url_entity_id = urllib.parse.quote_plus(entity_id)
r = requests.get(MDQ_URL.format(entity_id=entity_id), timeout=5)
display.v(f"done fetching metadata for {entity_id}.")
r.raise_for_status()
tree = ET.fromstring(r.text)
scopes = tree.findall(".//shibmd:Scope", XML_NS)
output.update([x.text for x in scopes])
return [list(output)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment