Skip to content

Commit efa005e

Browse files
author
Roland Hedberg
committed
When doing local import allow for importing all files within a directory.
1 parent 725cef1 commit efa005e

File tree

5 files changed

+200
-2
lines changed

5 files changed

+200
-2
lines changed

src/saml2/mdstore.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
from dircache import listdir
12
import logging
3+
import os
24
import sys
35
import json
46

57
from hashlib import sha1
68
from urllib import urlencode, quote_plus
9+
from os.path import isfile, join
710
from saml2.httpbase import HTTPBase
811
from saml2.extension.idpdisc import BINDING_DISCO
912
from saml2.extension.idpdisc import DiscoveryResponse
@@ -649,7 +652,18 @@ def __init__(self, onts, attrc, config, ca_certs=None,
649652
def load(self, typ, *args, **kwargs):
650653
if typ == "local":
651654
key = args[0]
652-
_md = MetaDataFile(self.onts, self.attrc, args[0])
655+
# if library read every file in the library
656+
if os.path.isdir(key):
657+
files = [f for f in listdir(key) if isfile(join(key, f))]
658+
for fil in files:
659+
_md = MetaDataFile(self.onts, self.attrc, fil)
660+
_md.load()
661+
_key = join(key, fil)
662+
self.metadata[_key] = _md
663+
return
664+
else:
665+
# else it's just a plain old file so read it
666+
_md = MetaDataFile(self.onts, self.attrc, key)
653667
elif typ == "inline":
654668
self.ii += 1
655669
key = self.ii

tests/metadata/idp.xml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?xml version='1.0' encoding='UTF-8'?>
2+
<ns0:EntitiesDescriptor xmlns:ns0="urn:oasis:names:tc:SAML:2.0:metadata">
3+
<ns0:EntityDescriptor entityID="urn:mace:example.com:saml:roland:idp">
4+
<ns0:IDPSSODescriptor WantAuthnRequestsSigned="true"
5+
protocolSupportEnumeration="urn:oasis:names:tc:SAML:2.0:protocol">
6+
<ns0:KeyDescriptor>
7+
<ns1:KeyInfo xmlns:ns1="http://www.w3.org/2000/09/xmldsig#">
8+
<ns1:X509Data>
9+
<ns1:X509Certificate>
10+
MIICsDCC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urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST"
31+
Location="http://localhost:8088/slo"/>
32+
<ns0:SingleSignOnService
33+
Binding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect"
34+
Location="http://localhost:8088/sso"/>
35+
<ns0:SingleSignOnService
36+
Binding="urn:oasis:names:tc:SAML:2.0:bindings:SOAP"
37+
Location="http://localhost:8088/sso_soap"/>
38+
</ns0:IDPSSODescriptor>
39+
<ns0:Organization>
40+
<ns0:OrganizationName xml:lang="en">Exempel AB
41+
</ns0:OrganizationName>
42+
<ns0:OrganizationDisplayName xml:lang="se">Exempel AB
43+
</ns0:OrganizationDisplayName>
44+
<ns0:OrganizationDisplayName xml:lang="en">Example Co.
45+
</ns0:OrganizationDisplayName>
46+
<ns0:OrganizationURL xml:lang="en">http://www.example.com/roland
47+
</ns0:OrganizationURL>
48+
</ns0:Organization>
49+
<ns0:ContactPerson contactType="technical">
50+
<ns0:GivenName>John</ns0:GivenName>
51+
<ns0:SurName>Smith</ns0:SurName>
52+
<ns0:EmailAddress>[email protected]</ns0:EmailAddress>
53+
</ns0:ContactPerson>
54+
</ns0:EntityDescriptor>
55+
</ns0:EntitiesDescriptor>

tests/metadata/idp_2.xml

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?xml version='1.0' encoding='UTF-8'?>
2+
<ns0:EntitiesDescriptor xmlns:ns0="urn:oasis:names:tc:SAML:2.0:metadata">
3+
<ns0:EntityDescriptor entityID="http://example.com/SAML/IDP">
4+
<ns0:IDPSSODescriptor WantAuthnRequestsSigned="true"
5+
protocolSupportEnumeration="urn:oasis:names:tc:SAML:2.0:protocol">
6+
<ns0:KeyDescriptor>
7+
<ns1:KeyInfo xmlns:ns1="http://www.w3.org/2000/09/xmldsig#">
8+
<ns1:X509Data>
9+
<ns1:X509Certificate>
10+
MIICsDCCAhmgAwIBAgIJAJrzqSSwmDY9MA0GCSqGSIb3DQEBBQUAMEUxCzAJBgNV
11+
BAYTAkFVMRMwEQYDVQQIEwpTb21lLVN0YXRlMSEwHwYDVQQKExhJbnRlcm5ldCBX
12+
aWRnaXRzIFB0eSBMdGQwHhcNMDkxMDA2MTk0OTQxWhcNMDkxMTA1MTk0OTQxWjBF
13+
MQswCQYDVQQGEwJBVTETMBEGA1UECBMKU29tZS1TdGF0ZTEhMB8GA1UEChMYSW50
14+
ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKB
15+
gQDJg2cms7MqjniT8Fi/XkNHZNPbNVQyMUMXE9tXOdqwYCA1cc8vQdzkihscQMXy
16+
3iPw2cMggBu6gjMTOSOxECkuvX5ZCclKr8pXAJM5cY6gVOaVO2PdTZcvDBKGbiaN
17+
efiEw5hnoZomqZGp8wHNLAUkwtH9vjqqvxyS/vclc6k2ewIDAQABo4GnMIGkMB0G
18+
A1UdDgQWBBRePsKHKYJsiojE78ZWXccK9K4aJTB1BgNVHSMEbjBsgBRePsKHKYJs
19+
iojE78ZWXccK9K4aJaFJpEcwRTELMAkGA1UEBhMCQVUxEzARBgNVBAgTClNvbWUt
20+
U3RhdGUxITAfBgNVBAoTGEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZIIJAJrzqSSw
21+
mDY9MAwGA1UdEwQFMAMBAf8wDQYJKoZIhvcNAQEFBQADgYEAJSrKOEzHO7TL5cy6
22+
h3qh+3+JAk8HbGBW+cbX6KBCAw/mzU8flK25vnWwXS3dv2FF3Aod0/S7AWNfKib5
23+
U/SA9nJaz/mWeF9S0farz9AQFc8/NSzAzaVq7YbM4F6f6N2FRl7GikdXRCed45j6
24+
mrPzGzk3ECbupFnqyREH3+ZPSdk=
25+
</ns1:X509Certificate>
26+
</ns1:X509Data>
27+
</ns1:KeyInfo>
28+
</ns0:KeyDescriptor>
29+
<ns0:SingleLogoutService
30+
Binding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST"
31+
Location="http://localhost:8088/slo"/>
32+
<ns0:SingleSignOnService
33+
Binding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect"
34+
Location="http://localhost:8088/sso"/>
35+
</ns0:IDPSSODescriptor>
36+
<ns0:Organization>
37+
<ns0:OrganizationName xml:lang="en">Exempel AB
38+
</ns0:OrganizationName>
39+
<ns0:OrganizationDisplayName xml:lang="se">Exempel AB
40+
</ns0:OrganizationDisplayName>
41+
<ns0:OrganizationDisplayName xml:lang="en">Example Co.
42+
</ns0:OrganizationDisplayName>
43+
<ns0:OrganizationURL xml:lang="en">http://www.example.com/roland
44+
</ns0:OrganizationURL>
45+
</ns0:Organization>
46+
<ns0:ContactPerson contactType="technical">
47+
<ns0:GivenName>John</ns0:GivenName>
48+
<ns0:SurName>Smith</ns0:SurName>
49+
<ns0:EmailAddress>[email protected]</ns0:EmailAddress>
50+
</ns0:ContactPerson>
51+
</ns0:EntityDescriptor>
52+
<ns0:EntityDescriptor entityID="http://example.com/SAML/AA">
53+
<ns0:AttributeAuthorityDescriptor
54+
protocolSupportEnumeration="urn:oasis:names:tc:SAML:2.0:protocol">
55+
<ns0:KeyDescriptor>
56+
<ns2:KeyInfo xmlns:ns2="http://www.w3.org/2000/09/xmldsig#">
57+
<ns2:X509Data>
58+
<ns2:X509Certificate>
59+
MIICsDCCAhmgAwIBAgIJAJrzqSSwmDY9MA0GCSqGSIb3DQEBBQUAMEUxCzAJBgNV
60+
BAYTAkFVMRMwEQYDVQQIEwpTb21lLVN0YXRlMSEwHwYDVQQKExhJbnRlcm5ldCBX
61+
aWRnaXRzIFB0eSBMdGQwHhcNMDkxMDA2MTk0OTQxWhcNMDkxMTA1MTk0OTQxWjBF
62+
MQswCQYDVQQGEwJBVTETMBEGA1UECBMKU29tZS1TdGF0ZTEhMB8GA1UEChMYSW50
63+
ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKB
64+
gQDJg2cms7MqjniT8Fi/XkNHZNPbNVQyMUMXE9tXOdqwYCA1cc8vQdzkihscQMXy
65+
3iPw2cMggBu6gjMTOSOxECkuvX5ZCclKr8pXAJM5cY6gVOaVO2PdTZcvDBKGbiaN
66+
efiEw5hnoZomqZGp8wHNLAUkwtH9vjqqvxyS/vclc6k2ewIDAQABo4GnMIGkMB0G
67+
A1UdDgQWBBRePsKHKYJsiojE78ZWXccK9K4aJTB1BgNVHSMEbjBsgBRePsKHKYJs
68+
iojE78ZWXccK9K4aJaFJpEcwRTELMAkGA1UEBhMCQVUxEzARBgNVBAgTClNvbWUt
69+
U3RhdGUxITAfBgNVBAoTGEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZIIJAJrzqSSw
70+
mDY9MAwGA1UdEwQFMAMBAf8wDQYJKoZIhvcNAQEFBQADgYEAJSrKOEzHO7TL5cy6
71+
h3qh+3+JAk8HbGBW+cbX6KBCAw/mzU8flK25vnWwXS3dv2FF3Aod0/S7AWNfKib5
72+
U/SA9nJaz/mWeF9S0farz9AQFc8/NSzAzaVq7YbM4F6f6N2FRl7GikdXRCed45j6
73+
mrPzGzk3ECbupFnqyREH3+ZPSdk=
74+
</ns2:X509Certificate>
75+
</ns2:X509Data>
76+
</ns2:KeyInfo>
77+
</ns0:KeyDescriptor>
78+
<ns0:AttributeService
79+
Binding="urn:oasis:names:tc:SAML:2.0:bindings:SOAP"
80+
Location="http://localhost:8089/as"/>
81+
</ns0:AttributeAuthorityDescriptor>
82+
<ns0:Organization>
83+
<ns0:OrganizationName xml:lang="en">Exempel AB
84+
</ns0:OrganizationName>
85+
<ns0:OrganizationDisplayName xml:lang="se">Exempel AB
86+
</ns0:OrganizationDisplayName>
87+
<ns0:OrganizationDisplayName xml:lang="en">Example Co.
88+
</ns0:OrganizationDisplayName>
89+
<ns0:OrganizationURL xml:lang="en">http://www.example.com/roland
90+
</ns0:OrganizationURL>
91+
</ns0:Organization>
92+
<ns0:ContactPerson contactType="technical">
93+
<ns0:GivenName>John</ns0:GivenName>
94+
<ns0:SurName>Smith</ns0:SurName>
95+
<ns0:EmailAddress>[email protected]</ns0:EmailAddress>
96+
</ns0:ContactPerson>
97+
</ns0:EntityDescriptor>
98+
</ns0:EntitiesDescriptor>

tests/metadata/idp_uiinfo.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version='1.0' encoding='UTF-8'?>
2+
<ns0:EntitiesDescriptor xmlns:ns0="urn:oasis:names:tc:SAML:2.0:metadata" xmlns:ns1="urn:mace:shibboleth:metadata:1.0" xmlns:ns2="urn:oasis:names:tc:SAML:metadata:ui" xmlns:ns3="http://www.w3.org/2000/09/xmldsig#"><ns0:EntityDescriptor entityID="http://example.com/saml2/idp.xml"><ns0:IDPSSODescriptor WantAuthnRequestsSigned="false" protocolSupportEnumeration="urn:oasis:names:tc:SAML:2.0:protocol"><ns0:Extensions><ns1:Scope regexp="false">example.org</ns1:Scope><ns2:UIInfo><ns2:Keywords xml:lang="en">foo bar</ns2:Keywords><ns2:Logo height="40" width="30">http://example.com/logo.jpg</ns2:Logo><ns2:InformationURL>http://example.com/saml2/info.html</ns2:InformationURL><ns2:DisplayName>Example Co.</ns2:DisplayName><ns2:Description xml:lang="se">Exempel bolag</ns2:Description><ns2:PrivacyStatementURL>http://example.com/saml2/privacyStatement.html</ns2:PrivacyStatementURL></ns2:UIInfo></ns0:Extensions><ns0:KeyDescriptor><ns3:KeyInfo><ns3:X509Data><ns3:X509Certificate>MIICsDCCAhmgAwIBAgIJAJrzqSSwmDY9MA0GCSqGSIb3DQEBBQUAMEUxCzAJBgNV
3+
BAYTAkFVMRMwEQYDVQQIEwpTb21lLVN0YXRlMSEwHwYDVQQKExhJbnRlcm5ldCBX
4+
aWRnaXRzIFB0eSBMdGQwHhcNMDkxMDA2MTk0OTQxWhcNMDkxMTA1MTk0OTQxWjBF
5+
MQswCQYDVQQGEwJBVTETMBEGA1UECBMKU29tZS1TdGF0ZTEhMB8GA1UEChMYSW50
6+
ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKB
7+
gQDJg2cms7MqjniT8Fi/XkNHZNPbNVQyMUMXE9tXOdqwYCA1cc8vQdzkihscQMXy
8+
3iPw2cMggBu6gjMTOSOxECkuvX5ZCclKr8pXAJM5cY6gVOaVO2PdTZcvDBKGbiaN
9+
efiEw5hnoZomqZGp8wHNLAUkwtH9vjqqvxyS/vclc6k2ewIDAQABo4GnMIGkMB0G
10+
A1UdDgQWBBRePsKHKYJsiojE78ZWXccK9K4aJTB1BgNVHSMEbjBsgBRePsKHKYJs
11+
iojE78ZWXccK9K4aJaFJpEcwRTELMAkGA1UEBhMCQVUxEzARBgNVBAgTClNvbWUt
12+
U3RhdGUxITAfBgNVBAoTGEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZIIJAJrzqSSw
13+
mDY9MAwGA1UdEwQFMAMBAf8wDQYJKoZIhvcNAQEFBQADgYEAJSrKOEzHO7TL5cy6
14+
h3qh+3+JAk8HbGBW+cbX6KBCAw/mzU8flK25vnWwXS3dv2FF3Aod0/S7AWNfKib5
15+
U/SA9nJaz/mWeF9S0farz9AQFc8/NSzAzaVq7YbM4F6f6N2FRl7GikdXRCed45j6
16+
mrPzGzk3ECbupFnqyREH3+ZPSdk=
17+
</ns3:X509Certificate></ns3:X509Data></ns3:KeyInfo></ns0:KeyDescriptor><ns0:SingleSignOnService Binding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect" Location="http://example.com/saml2/" /></ns0:IDPSSODescriptor></ns0:EntityDescriptor></ns0:EntitiesDescriptor>

tests/test_30_mdstore.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@
7373
},
7474
"8": {
7575
"mdfile": [full_path("swamid.md")]
76+
},
77+
"9": {
78+
"local": [full_path("metadata")]
7679
}
7780
}
7881

@@ -252,5 +255,16 @@ def test_mdx_certs():
252255

253256
assert len(foo) == 1
254257

258+
259+
def test_load_local_dir():
260+
sec_config.xmlsec_binary = sigver.get_xmlsec_binary(["/opt/local/bin"])
261+
mds = MetadataStore(ONTS.values(), ATTRCONV, sec_config,
262+
disable_ssl_certificate_validation=True)
263+
264+
mds.imp(METADATACONF["9"])
265+
print mds
266+
assert len(mds) == 3 # Three sources
267+
assert len(mds.keys()) == 4 # number of idps
268+
255269
if __name__ == "__main__":
256-
test_mdx_certs()
270+
test_load_local_dir()

0 commit comments

Comments
 (0)