-#----------------------------------------------------------------------\r
-# Copyright (c) 2014 Raytheon BBN Technologies\r
-#\r
-# Permission is hereby granted, free of charge, to any person obtaining\r
-# a copy of this software and/or hardware specification (the "Work") to\r
-# deal in the Work without restriction, including without limitation the\r
-# rights to use, copy, modify, merge, publish, distribute, sublicense,\r
-# and/or sell copies of the Work, and to permit persons to whom the Work\r
-# is furnished to do so, subject to the following conditions:\r
-#\r
-# The above copyright notice and this permission notice shall be\r
-# included in all copies or substantial portions of the Work.\r
-#\r
-# THE WORK IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS\r
-# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\r
-# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
-# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT\r
-# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,\r
-# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
-# OUT OF OR IN CONNECTION WITH THE WORK OR THE USE OR OTHER DEALINGS\r
-# IN THE WORK.\r
-#----------------------------------------------------------------------\r
-\r
-from sfa.util.sfalogging import logger\r
-from sfa.trust.credential import Credential\r
-from sfa.trust.abac_credential import ABACCredential\r
-\r
-import json\r
-import re\r
-\r
-# Factory for creating credentials of different sorts by type.\r
-# Specifically, this factory can create standard SFA credentials\r
-# and ABAC credentials from XML strings based on their identifying content\r
-\r
-class CredentialFactory:\r
-\r
- UNKNOWN_CREDENTIAL_TYPE = 'geni_unknown'\r
-\r
- # Static Credential class method to determine the type of a credential\r
- # string depending on its contents\r
- @staticmethod\r
- def getType(credString):\r
- credString_nowhitespace = re.sub('\s', '', credString)\r
- if credString_nowhitespace.find('<type>abac</type>') > -1:\r
- return ABACCredential.ABAC_CREDENTIAL_TYPE\r
- elif credString_nowhitespace.find('<type>privilege</type>') > -1:\r
- return Credential.SFA_CREDENTIAL_TYPE\r
- else:\r
- st = credString_nowhitespace.find('<type>')\r
- end = credString_nowhitespace.find('</type>', st)\r
- return credString_nowhitespace[st + len('<type>'):end]\r
-# return CredentialFactory.UNKNOWN_CREDENTIAL_TYPE\r
-\r
- # Static Credential class method to create the appropriate credential\r
- # (SFA or ABAC) depending on its type\r
- @staticmethod\r
- def createCred(credString=None, credFile=None):\r
- if not credString and not credFile:\r
- raise Exception("CredentialFactory.createCred called with no argument")\r
- if credFile:\r
- try:\r
- credString = open(credFile).read()\r
- except Exception, e:\r
- logger.info("Error opening credential file %s: %s" % credFile, e)\r
- return None\r
-\r
- # Try to treat the file as JSON, getting the cred_type from the struct\r
- try:\r
- credO = json.loads(credString, encoding='ascii')\r
- if credO.has_key('geni_value') and credO.has_key('geni_type'):\r
- cred_type = credO['geni_type']\r
- credString = credO['geni_value']\r
- except Exception, e:\r
- # It wasn't a struct. So the credString is XML. Pull the type directly from the string\r
- logger.debug("Credential string not JSON: %s" % e)\r
- cred_type = CredentialFactory.getType(credString)\r
-\r
- if cred_type == Credential.SFA_CREDENTIAL_TYPE:\r
- try:\r
- cred = Credential(string=credString)\r
- return cred\r
- except Exception, e:\r
- if credFile:\r
- msg = "credString started: %s" % credString[:50]\r
- raise Exception("%s not a parsable SFA credential: %s. " % (credFile, e) + msg)\r
- else:\r
- raise Exception("SFA Credential not parsable: %s. Cred start: %s..." % (e, credString[:50]))\r
-\r
- elif cred_type == ABACCredential.ABAC_CREDENTIAL_TYPE:\r
- try:\r
- cred = ABACCredential(string=credString)\r
- return cred\r
- except Exception, e:\r
- if credFile:\r
- raise Exception("%s not a parsable ABAC credential: %s" % (credFile, e))\r
- else:\r
- raise Exception("ABAC Credential not parsable: %s. Cred start: %s..." % (e, credString[:50]))\r
- else:\r
- raise Exception("Unknown credential type '%s'" % cred_type)\r
-\r
-if __name__ == "__main__":\r
- c2 = open('/tmp/sfa.xml').read()\r
- cred1 = CredentialFactory.createCred(credFile='/tmp/cred.xml')\r
- cred2 = CredentialFactory.createCred(credString=c2)\r
-\r
- print "C1 = %s" % cred1\r
- print "C2 = %s" % cred2\r
- c1s = cred1.dump_string()\r
- print "C1 = %s" % c1s\r
-# print "C2 = %s" % cred2.dump_string()\r
+#----------------------------------------------------------------------
+# Copyright (c) 2014 Raytheon BBN Technologies
+#
+# Permission is hereby granted, free of charge, to any person obtaining
+# a copy of this software and/or hardware specification (the "Work") to
+# deal in the Work without restriction, including without limitation the
+# rights to use, copy, modify, merge, publish, distribute, sublicense,
+# and/or sell copies of the Work, and to permit persons to whom the Work
+# is furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice shall be
+# included in all copies or substantial portions of the Work.
+#
+# THE WORK IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+# OUT OF OR IN CONNECTION WITH THE WORK OR THE USE OR OTHER DEALINGS
+# IN THE WORK.
+#----------------------------------------------------------------------
+
+from __future__ import print_function
+
+from sfa.util.sfalogging import logger
+from sfa.trust.credential import Credential
+from sfa.trust.abac_credential import ABACCredential
+
+import json
+import re
+
+# Factory for creating credentials of different sorts by type.
+# Specifically, this factory can create standard SFA credentials
+# and ABAC credentials from XML strings based on their identifying content
+
+
+class CredentialFactory:
+
+ UNKNOWN_CREDENTIAL_TYPE = 'geni_unknown'
+
+ # Static Credential class method to determine the type of a credential
+ # string depending on its contents
+ @staticmethod
+ def getType(credString):
+ credString_nowhitespace = re.sub('\s', '', credString)
+ if credString_nowhitespace.find('<type>abac</type>') > -1:
+ return ABACCredential.ABAC_CREDENTIAL_TYPE
+ elif credString_nowhitespace.find('<type>privilege</type>') > -1:
+ return Credential.SFA_CREDENTIAL_TYPE
+ else:
+ st = credString_nowhitespace.find('<type>')
+ end = credString_nowhitespace.find('</type>', st)
+ return credString_nowhitespace[st + len('<type>'):end]
+# return CredentialFactory.UNKNOWN_CREDENTIAL_TYPE
+
+ # Static Credential class method to create the appropriate credential
+ # (SFA or ABAC) depending on its type
+ @staticmethod
+ def createCred(credString=None, credFile=None):
+ if not credString and not credFile:
+ raise Exception(
+ "CredentialFactory.createCred called with no argument")
+ if credFile:
+ try:
+ credString = open(credFile).read()
+ except Exception as e:
+ logger.info("Error opening credential file %s: %s" %
+ credFile, e)
+ return None
+
+ # Try to treat the file as JSON, getting the cred_type from the struct
+ try:
+ credO = json.loads(credString, encoding='ascii')
+ if 'geni_value' in credO and 'geni_type' in credO:
+ cred_type = credO['geni_type']
+ credString = credO['geni_value']
+ except Exception as e:
+ # It wasn't a struct. So the credString is XML. Pull the type
+ # directly from the string
+ logger.debug("Credential string not JSON: %s" % e)
+ cred_type = CredentialFactory.getType(credString)
+
+ if cred_type == Credential.SFA_CREDENTIAL_TYPE:
+ try:
+ cred = Credential(string=credString)
+ return cred
+ except Exception as e:
+ if credFile:
+ msg = "credString started: %s" % credString[:50]
+ raise Exception(
+ "%s not a parsable SFA credential: %s. " % (credFile, e) + msg)
+ else:
+ raise Exception(
+ "SFA Credential not parsable: %s. Cred start: %s..." % (e, credString[:50]))
+
+ elif cred_type == ABACCredential.ABAC_CREDENTIAL_TYPE:
+ try:
+ cred = ABACCredential(string=credString)
+ return cred
+ except Exception as e:
+ if credFile:
+ raise Exception(
+ "%s not a parsable ABAC credential: %s" % (credFile, e))
+ else:
+ raise Exception(
+ "ABAC Credential not parsable: %s. Cred start: %s..." % (e, credString[:50]))
+ else:
+ raise Exception("Unknown credential type '%s'" % cred_type)
+
+if __name__ == "__main__":
+ c2 = open('/tmp/sfa.xml').read()
+ cred1 = CredentialFactory.createCred(credFile='/tmp/cred.xml')
+ cred2 = CredentialFactory.createCred(credString=c2)
+
+ print("C1 = %s" % cred1)
+ print("C2 = %s" % cred2)
+ c1s = cred1.dump_string()
+ print("C1 = %s" % c1s)
+# print "C2 = %s" % cred2.dump_string()