1 #----------------------------------------------------------------------
2 # Copyright (c) 2014 Raytheon BBN Technologies
4 # Permission is hereby granted, free of charge, to any person obtaining
5 # a copy of this software and/or hardware specification (the "Work") to
6 # deal in the Work without restriction, including without limitation the
7 # rights to use, copy, modify, merge, publish, distribute, sublicense,
8 # and/or sell copies of the Work, and to permit persons to whom the Work
9 # is furnished to do so, subject to the following conditions:
11 # The above copyright notice and this permission notice shall be
12 # included in all copies or substantial portions of the Work.
14 # THE WORK IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
18 # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 # OUT OF OR IN CONNECTION WITH THE WORK OR THE USE OR OTHER DEALINGS
22 #----------------------------------------------------------------------
24 from sfa.util.sfalogging import logger
25 from sfa.trust.credential import Credential
26 from sfa.trust.abac_credential import ABACCredential
31 # Factory for creating credentials of different sorts by type.
32 # Specifically, this factory can create standard SFA credentials
33 # and ABAC credentials from XML strings based on their identifying content
35 class CredentialFactory:
37 UNKNOWN_CREDENTIAL_TYPE = 'geni_unknown'
39 # Static Credential class method to determine the type of a credential
40 # string depending on its contents
42 def getType(credString):
43 credString_nowhitespace = re.sub('\s', '', credString)
44 if credString_nowhitespace.find('<type>abac</type>') > -1:
45 return ABACCredential.ABAC_CREDENTIAL_TYPE
46 elif credString_nowhitespace.find('<type>privilege</type>') > -1:
47 return Credential.SFA_CREDENTIAL_TYPE
49 st = credString_nowhitespace.find('<type>')
50 end = credString_nowhitespace.find('</type>', st)
51 return credString_nowhitespace[st + len('<type>'):end]
52 # return CredentialFactory.UNKNOWN_CREDENTIAL_TYPE
54 # Static Credential class method to create the appropriate credential
55 # (SFA or ABAC) depending on its type
57 def createCred(credString=None, credFile=None):
58 if not credString and not credFile:
59 raise Exception("CredentialFactory.createCred called with no argument")
62 credString = open(credFile).read()
64 logger.info("Error opening credential file %s: %s" % credFile, e)
67 # Try to treat the file as JSON, getting the cred_type from the struct
69 credO = json.loads(credString, encoding='ascii')
70 if credO.has_key('geni_value') and credO.has_key('geni_type'):
71 cred_type = credO['geni_type']
72 credString = credO['geni_value']
74 # It wasn't a struct. So the credString is XML. Pull the type directly from the string
75 logger.debug("Credential string not JSON: %s" % e)
76 cred_type = CredentialFactory.getType(credString)
78 if cred_type == Credential.SFA_CREDENTIAL_TYPE:
80 cred = Credential(string=credString)
84 msg = "credString started: %s" % credString[:50]
85 raise Exception("%s not a parsable SFA credential: %s. " % (credFile, e) + msg)
87 raise Exception("SFA Credential not parsable: %s. Cred start: %s..." % (e, credString[:50]))
89 elif cred_type == ABACCredential.ABAC_CREDENTIAL_TYPE:
91 cred = ABACCredential(string=credString)
95 raise Exception("%s not a parsable ABAC credential: %s" % (credFile, e))
97 raise Exception("ABAC Credential not parsable: %s. Cred start: %s..." % (e, credString[:50]))
99 raise Exception("Unknown credential type '%s'" % cred_type)
101 if __name__ == "__main__":
102 c2 = open('/tmp/sfa.xml').read()
103 cred1 = CredentialFactory.createCred(credFile='/tmp/cred.xml')
104 cred2 = CredentialFactory.createCred(credString=c2)
106 print "C1 = %s" % cred1
107 print "C2 = %s" % cred2
108 c1s = cred1.dump_string()
109 print "C1 = %s" % c1s
110 # print "C2 = %s" % cred2.dump_string()