Skip to content

Commit 883b89e

Browse files
author
Roland Hedberg
committed
Made MetaData instances pickleable.
1 parent cff1391 commit 883b89e

17 files changed

+135
-340
lines changed

example/idp2/idp.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,11 +1073,11 @@ def application(environ, start_response):
10731073
digest_alg = None
10741074
try:
10751075
sign_alg = CONFIG.SIGN_ALG
1076-
except:
1076+
except AttributeError:
10771077
pass
10781078
try:
10791079
digest_alg = CONFIG.DIGEST_ALG
1080-
except:
1080+
except AttributeError:
10811081
pass
10821082
ds.DefaultSignature(sign_alg, digest_alg)
10831083

src/saml2/config.py

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,6 @@
2626

2727
logger = logging.getLogger(__name__)
2828

29-
from saml2 import md
30-
from saml2 import saml
31-
from saml2.extension import mdui
32-
from saml2.extension import idpdisc
33-
from saml2.extension import dri
34-
from saml2.extension import mdattr
35-
from saml2.extension import ui
36-
from saml2 import xmldsig
37-
from saml2 import xmlenc
38-
39-
ONTS = {
40-
saml.NAMESPACE: saml,
41-
mdui.NAMESPACE: mdui,
42-
mdattr.NAMESPACE: mdattr,
43-
dri.NAMESPACE: dri,
44-
ui.NAMESPACE: ui,
45-
idpdisc.NAMESPACE: idpdisc,
46-
md.NAMESPACE: md,
47-
xmldsig.NAMESPACE: xmldsig,
48-
xmlenc.NAMESPACE: xmlenc
49-
}
5029

5130
COMMON_ARGS = [
5231
"entityid", "xmlsec_binary", "debug", "key_file", "cert_file",
@@ -408,8 +387,7 @@ def load_metadata(self, metadata_conf):
408387
except:
409388
disable_validation = False
410389

411-
mds = MetadataStore(
412-
list(ONTS.values()), acs, self, ca_certs,
390+
mds = MetadataStore(acs, self, ca_certs,
413391
disable_ssl_certificate_validation=disable_validation)
414392

415393
mds.imp(metadata_conf)

src/saml2/entity_category/edugain.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
# "displayName", "schacHomeOrganization"],
1010
COCO: ["eduPersonPrincipalName", "eduPersonScopedAffiliation",
1111
'eduPersonAffiliation', "mail", "displayName", 'cn',
12-
"schacHomeOrganization", 'schacHomeOrganizationType']
12+
"schacHomeOrganization"]
1313
}
1414

src/saml2/mdstore.py

Lines changed: 54 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,24 @@
99
import requests
1010
import six
1111
from hashlib import sha1
12-
from os.path import isfile, join
13-
from saml2.httpbase import HTTPBase
14-
from saml2.extension.idpdisc import BINDING_DISCO
15-
from saml2.extension.idpdisc import DiscoveryResponse
16-
from saml2.md import EntitiesDescriptor
17-
from saml2.mdie import to_dict
12+
from os.path import isfile
13+
from os.path import join
14+
1815
from saml2 import md
16+
from saml2 import saml
1917
from saml2 import samlp
18+
from saml2 import xmldsig
19+
from saml2 import xmlenc
2020
from saml2 import SAMLError
2121
from saml2 import BINDING_HTTP_REDIRECT
2222
from saml2 import BINDING_HTTP_POST
2323
from saml2 import BINDING_SOAP
24+
25+
from saml2.httpbase import HTTPBase
26+
from saml2.extension.idpdisc import BINDING_DISCO
27+
from saml2.extension.idpdisc import DiscoveryResponse
28+
from saml2.md import EntitiesDescriptor
29+
from saml2.mdie import to_dict
2430
from saml2.s_utils import UnsupportedBinding
2531
from saml2.s_utils import UnknownSystemEntity
2632
from saml2.sigver import split_len
@@ -83,6 +89,24 @@ def load_extensions():
8389
return ext_map
8490

8591

92+
def load_metadata_modules():
93+
mods = {
94+
saml.NAMESPACE: saml,
95+
md.NAMESPACE: md,
96+
xmldsig.NAMESPACE: xmldsig,
97+
xmlenc.NAMESPACE: xmlenc
98+
}
99+
100+
mods.update(load_extensions())
101+
return mods
102+
103+
104+
def metadata_modules():
105+
_res = [saml, md, xmldsig, xmlenc]
106+
_res.extend(list(load_extensions().values()))
107+
return _res
108+
109+
86110
def destinations(srvs):
87111
return [s["location"] for s in srvs]
88112

@@ -129,14 +153,16 @@ def repack_cert(cert):
129153

130154

131155
class MetaData(object):
132-
def __init__(self, onts, attrc, metadata='', node_name=None,
156+
def __init__(self, attrc, metadata='', node_name=None,
133157
check_validity=True, security=None, **kwargs):
134-
self.onts = onts
135158
self.attrc = attrc
136159
self.metadata = metadata
137160
self.entity = None
138161
self.cert = None
139162
self.to_old = []
163+
self.node_name = node_name
164+
self.check_validity = check_validity
165+
self.security = security
140166

141167
def items(self):
142168
'''
@@ -369,9 +395,9 @@ def extract_certs(srvs):
369395

370396

371397
class InMemoryMetaData(MetaData):
372-
def __init__(self, onts, attrc, metadata="", node_name=None,
398+
def __init__(self, attrc, metadata="", node_name=None,
373399
check_validity=True, security=None, **kwargs):
374-
super(InMemoryMetaData, self).__init__(onts, attrc, metadata=metadata)
400+
super(InMemoryMetaData, self).__init__(attrc, metadata=metadata)
375401
self.entity = {}
376402
self.security = security
377403
self.node_name = node_name
@@ -424,7 +450,7 @@ def do_entity_descriptor(self, entity_descr):
424450
entity_descr.entity_id, file=sys.stderr)
425451
return
426452

427-
_ent = to_dict(entity_descr, self.onts)
453+
_ent = to_dict(entity_descr, metadata_modules())
428454
flag = 0
429455
# verify support for SAML2
430456
for descr in ["spsso", "idpsso", "role", "authn_authority",
@@ -597,8 +623,8 @@ class MetaDataFile(InMemoryMetaData):
597623
the SAML Metadata format.
598624
"""
599625

600-
def __init__(self, onts, attrc, filename=None, cert=None, **kwargs):
601-
super(MetaDataFile, self).__init__(onts, attrc, **kwargs)
626+
def __init__(self, attrc, filename=None, cert=None, **kwargs):
627+
super(MetaDataFile, self).__init__(attrc, **kwargs)
602628
if not filename:
603629
raise SAMLError('No file specified.')
604630
self.filename = filename
@@ -618,9 +644,9 @@ class MetaDataLoader(MetaDataFile):
618644
The format of the file is the SAML Metadata format.
619645
"""
620646

621-
def __init__(self, onts, attrc, loader_callable, cert=None,
647+
def __init__(self, attrc, loader_callable, cert=None,
622648
security=None, **kwargs):
623-
super(MetaDataLoader, self).__init__(onts, attrc, **kwargs)
649+
super(MetaDataLoader, self).__init__(attrc, **kwargs)
624650
self.metadata_provider_callable = self.get_metadata_loader(
625651
loader_callable)
626652
self.cert = cert
@@ -662,17 +688,16 @@ class MetaDataExtern(InMemoryMetaData):
662688
Accessible but HTTP GET.
663689
"""
664690

665-
def __init__(self, onts, attrc, url=None, security=None, cert=None,
691+
def __init__(self, attrc, url=None, security=None, cert=None,
666692
http=None, **kwargs):
667693
"""
668-
:params onts:
669694
:params attrc:
670695
:params url: Location of the metadata
671696
:params security: SecurityContext()
672697
:params cert: CertificMDloaderate used to sign the metadata
673698
:params http:
674699
"""
675-
super(MetaDataExtern, self).__init__(onts, attrc, **kwargs)
700+
super(MetaDataExtern, self).__init__(attrc, **kwargs)
676701
if not url:
677702
raise SAMLError('URL not specified.')
678703
else:
@@ -704,8 +729,8 @@ class MetaDataMD(InMemoryMetaData):
704729
of the Python representation of the metadata.
705730
"""
706731

707-
def __init__(self, onts, attrc, filename, **kwargs):
708-
super(MetaDataMD, self).__init__(onts, attrc, **kwargs)
732+
def __init__(self, attrc, filename, **kwargs):
733+
super(MetaDataMD, self).__init__(attrc, **kwargs)
709734
self.filename = filename
710735

711736
def load(self):
@@ -771,18 +796,16 @@ def single_sign_on_service(self, entity_id, binding=None, typ="idpsso"):
771796

772797

773798
class MetadataStore(MetaData):
774-
def __init__(self, onts, attrc, config, ca_certs=None,
799+
def __init__(self, attrc, config, ca_certs=None,
775800
check_validity=True,
776801
disable_ssl_certificate_validation=False,
777802
filter=None):
778803
"""
779-
:params onts:
780804
:params attrc:
781805
:params config: Config()
782806
:params ca_certs:
783807
:params disable_ssl_certificate_validation:
784808
"""
785-
self.onts = onts
786809
self.attrc = attrc
787810

788811
if disable_ssl_certificate_validation:
@@ -810,18 +833,18 @@ def load(self, typ, *args, **kwargs):
810833
files = [f for f in os.listdir(key) if isfile(join(key, f))]
811834
for fil in files:
812835
_fil = join(key, fil)
813-
_md = MetaDataFile(self.onts, self.attrc, _fil, **_args)
836+
_md = MetaDataFile(self.attrc, _fil, **_args)
814837
_md.load()
815838
self.metadata[_fil] = _md
816839
return
817840
else:
818841
# else it's just a plain old file so read it
819-
_md = MetaDataFile(self.onts, self.attrc, key, **_args)
842+
_md = MetaDataFile(self.attrc, key, **_args)
820843
elif typ == "inline":
821844
self.ii += 1
822845
key = self.ii
823846
kwargs.update(_args)
824-
_md = InMemoryMetaData(self.onts, self.attrc, args[0])
847+
_md = InMemoryMetaData(self.attrc, args[0])
825848
elif typ == "remote":
826849
key = kwargs["url"]
827850
for _key in ["node_name", "check_validity"]:
@@ -833,15 +856,15 @@ def load(self, typ, *args, **kwargs):
833856
if "cert" not in kwargs:
834857
kwargs["cert"] = ""
835858

836-
_md = MetaDataExtern(self.onts, self.attrc,
859+
_md = MetaDataExtern(self.attrc,
837860
kwargs["url"], self.security,
838861
kwargs["cert"], self.http, **_args)
839862
elif typ == "mdfile":
840863
key = args[0]
841-
_md = MetaDataMD(self.onts, self.attrc, args[0], **_args)
864+
_md = MetaDataMD(self.attrc, args[0], **_args)
842865
elif typ == "loader":
843866
key = args[0]
844-
_md = MetaDataLoader(self.onts, self.attrc, args[0], **_args)
867+
_md = MetaDataLoader(self.attrc, args[0], **_args)
845868
else:
846869
raise SAMLError("Unknown metadata type '%s'" % typ)
847870
_md.load()
@@ -891,7 +914,7 @@ def imp(self, spec):
891914
isfile(join(key[0], f))]
892915
for fil in files:
893916
_fil = join(key[0], fil)
894-
_md = MetaDataFile(self.onts, self.attrc, _fil)
917+
_md = MetaDataFile(self.attrc, _fil)
895918
_md.load()
896919
self.metadata[_fil] = _md
897920
if _md.to_old:
@@ -901,7 +924,7 @@ def imp(self, spec):
901924
if len(key) == 2:
902925
kwargs["cert"] = key[1]
903926

904-
_md = MDloader(self.onts, self.attrc, key[0], **kwargs)
927+
_md = MDloader(self.attrc, key[0], **kwargs)
905928
_md.load()
906929
self.metadata[key[0]] = _md
907930
if _md.to_old:

src/saml2/mongo_store.py

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,39 +7,25 @@
77
import pymongo.errors
88
from saml2.eptid import Eptid
99
from saml2.mdstore import InMemoryMetaData
10+
from saml2.mdstore import metadata_modules
11+
from saml2.mdstore import load_metadata_modules
1012
from saml2.s_utils import PolicyError
1113

12-
from saml2.ident import code_binary, IdentDB, Unknown
13-
from saml2.mdie import to_dict, from_dict
14-
15-
from saml2 import md
16-
from saml2 import saml
17-
from saml2.extension import mdui
18-
from saml2.extension import idpdisc
19-
from saml2.extension import dri
20-
from saml2.extension import mdattr
21-
from saml2.extension import ui
22-
from saml2 import xmldsig
23-
from saml2 import xmlenc
24-
import six
14+
from saml2.ident import code_binary
15+
from saml2.ident import IdentDB
16+
from saml2.ident import Unknown
17+
from saml2.mdie import to_dict
18+
from saml2.mdie import from_dict
2519

20+
import six
2621

27-
ONTS = {
28-
saml.NAMESPACE: saml,
29-
mdui.NAMESPACE: mdui,
30-
mdattr.NAMESPACE: mdattr,
31-
dri.NAMESPACE: dri,
32-
ui.NAMESPACE: ui,
33-
idpdisc.NAMESPACE: idpdisc,
34-
md.NAMESPACE: md,
35-
xmldsig.NAMESPACE: xmldsig,
36-
xmlenc.NAMESPACE: xmlenc
37-
}
3822

3923
__author__ = 'rolandh'
4024

4125
logger = logging.getLogger(__name__)
4226

27+
ONTS = load_metadata_modules()
28+
MMODS = metadata_modules()
4329

4430
class CorruptDatabase(Exception):
4531
pass
@@ -64,7 +50,7 @@ def store_assertion(self, assertion, to_sign):
6450
doc = {
6551
"name_id_key": nkey,
6652
"assertion_id": assertion.id,
67-
"assertion": to_dict(assertion, ONTS.values(), True),
53+
"assertion": to_dict(assertion, MMODS, True),
6854
"to_sign": to_sign
6955
}
7056

@@ -151,7 +137,7 @@ def create_id(self, nformat, name_qualifier="", sp_name_qualifier=""):
151137
return _id
152138

153139
def store(self, ident, name_id):
154-
self.mdb.store(ident, name_id=to_dict(name_id, ONTS.values(), True))
140+
self.mdb.store(ident, name_id=to_dict(name_id, MMODS, True))
155141

156142
def find_nameid(self, userid, nformat=None, sp_name_qualifier=None,
157143
name_qualifier=None, sp_provided_id=None, **kwargs):
@@ -172,13 +158,13 @@ def find_nameid(self, userid, nformat=None, sp_name_qualifier=None,
172158
return res
173159

174160
def find_local_id(self, name_id):
175-
cnid = to_dict(name_id, ONTS.values(), True)
161+
cnid = to_dict(name_id, MMODS, True)
176162
for item in self.mdb.get(name_id=cnid):
177163
return item[self.mdb.primary_key]
178164
return None
179165

180166
def remove_remote(self, name_id):
181-
cnid = to_dict(name_id, ONTS.values(), True)
167+
cnid = to_dict(name_id, MMODS, True)
182168
self.mdb.remove(name_id=cnid)
183169

184170
def handle_name_id_mapping_request(self, name_id, name_id_policy):

src/saml2/sdb.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,6 @@
1515
from saml2 import xmlenc
1616

1717

18-
ONTS = {
19-
saml.NAMESPACE: saml,
20-
mdui.NAMESPACE: mdui,
21-
mdattr.NAMESPACE: mdattr,
22-
dri.NAMESPACE: dri,
23-
ui.NAMESPACE: ui,
24-
idpdisc.NAMESPACE: idpdisc,
25-
md.NAMESPACE: md,
26-
xmldsig.NAMESPACE: xmldsig,
27-
xmlenc.NAMESPACE: xmlenc
28-
}
29-
3018
__author__ = 'rolandh'
3119

3220
logger = logging.getLogger(__name__)

0 commit comments

Comments
 (0)