Skip to content

Support metadata reload (plus minor fixes) #809

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jul 26, 2021
3 changes: 3 additions & 0 deletions src/saml2/assertion.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ def _filter_values(vals, vlist=None, must=False):
if not vlist: # No value specified equals any value
return vals

if vals is None: # cannot iterate over None, return early
return vals

if isinstance(vlist, six.string_types):
vlist = [vlist]

Expand Down
33 changes: 33 additions & 0 deletions src/saml2/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,39 @@ def __init__(self, entity_type, config=None, config_file="",

self.msg_cb = msg_cb

def reload_metadata(self, metadata_conf):
"""
Reload metadata configuration.

Load a new metadata configuration as defined by metadata_conf (by
passing this to Config.load_metadata) and make this entity (as well as
subordinate objects with own metadata reference) use the new metadata.

The structure of metadata_conf is the same as the 'metadata' entry in
the configuration passed to saml2.Config.

param metadata_conf: Metadata configuration as passed to Config.load_metadata
return: True if successfully reloaded
"""
logger.debug("Loading new metadata")
try:
new_metadata = self.config.load_metadata(metadata_conf)
except Exception as ex:
logger.error("Loading metadata failed", exc_info=ex)
return False

logger.debug("Applying new metadata to main config")
( self.metadata, self.sec.metadata, self.config.metadata ) = [new_metadata]*3
policy = getattr(self.config, "_%s_policy" % self.entity_type, None)
if policy and policy.metadata_store:
logger.debug("Applying new metadata to %s policy", self.entity_type)
policy.metadata_store = self.metadata

logger.debug("Applying new metadata source_id")
self.sourceid = self.metadata.construct_source_id()

return True

def _issuer(self, entityid=None):
""" Return an Issuer instance """
if entityid:
Expand Down
12 changes: 10 additions & 2 deletions src/saml2/mdstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,14 @@ def parse(self, xmlstr):
try:
self.entities_descr = md.entities_descriptor_from_string(xmlstr)
except Exception as e:
raise SAMLError(f'Failed to parse metadata file: {self.filename}') from e
_md_desc = (
f'metadata file: {self.filename}'
if isinstance(self,MetaDataFile)
else f'remote metadata: {self.url}'
if isinstance(self, MetaDataExtern)
else 'metadata'
)
raise SAMLError(f'Failed to parse {_md_desc}') from e

if not self.entities_descr:
self.entity_descr = md.entity_descriptor_from_string(xmlstr)
Expand Down Expand Up @@ -1693,4 +1700,5 @@ def dumps(self, format="local"):

return "%s" % res
elif format == "md":
return json.dumps(self.items(), indent=2)
# self.items() returns dictitems(), convert that back into a dict
return json.dumps(dict(self.items()), indent=2)
4 changes: 2 additions & 2 deletions src/saml2/time_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ def utc_now():


def before(point):
""" True if point datetime specification is before now.
""" True if current time is before point datetime specification.

NOTE: If point is specified it is supposed to be in local time.
Not UTC/GMT !! This is because that is what gmtime() expects.
Expand All @@ -286,7 +286,7 @@ def before(point):


def after(point):
""" True if point datetime specification is equal or after now """
""" True if current time is after or equal to point datetime specification."""
if not point:
return True
else:
Expand Down