X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=sfa%2Ftrust%2Fcredential.py;h=135d8170c0b4cb956bb7c52e4e3a6adb250aa015;hb=f2282434e40e06365e0fdd3f9bc273a793f41235;hp=09835d94f63c19bc7890d812f0876acf07c73e55;hpb=dd07ad19e9c39e121c6ff177a863c9e7da1f1eac;p=sfa.git diff --git a/sfa/trust/credential.py b/sfa/trust/credential.py index 09835d94..135d8170 100644 --- a/sfa/trust/credential.py +++ b/sfa/trust/credential.py @@ -26,7 +26,7 @@ # Credentials are signed XML files that assign a subject gid privileges to an object gid ## -import os,sys +import os from types import StringTypes import datetime from StringIO import StringIO @@ -272,11 +272,13 @@ class Credential(object): if os.path.isfile(path + '/' + 'xmlsec1'): self.xmlsec_path = path + '/' + 'xmlsec1' break + if not self.xmlsec_path: + logger.warn("Could not locate binary for xmlsec1 - SFA will be unable to sign stuff !!") def get_subject(self): if not self.gidObject: self.decode() - return self.gidObject.get_printable_subject() + return self.gidObject.get_subject() # sounds like this should be __repr__ instead ?? def get_summary_tostring(self): @@ -364,8 +366,6 @@ class Credential(object): if not self.gidObject: self.decode() return self.gidObject - - ## # Expiration: an absolute UTC time of expiration (as either an int or string or datetime) @@ -404,8 +404,7 @@ class Credential(object): if isinstance(privs, str): self.privileges = Rights(string = privs) else: - self.privileges = privs - + self.privileges = privs ## # return the privileges as a Rights object @@ -589,15 +588,13 @@ class Credential(object): def updateRefID(self): if not self.parent: - self.set_refid('ref0') + self.set_refid('ref0') return [] refs = [] next_cred = self.parent - while next_cred: - refs.append(next_cred.get_refid()) if next_cred.parent: next_cred = next_cred.parent @@ -631,7 +628,11 @@ class Credential(object): # you have loaded an existing signed credential, do not call encode() or sign() on it. def sign(self): - if not self.issuer_privkey or not self.issuer_gid: + if not self.issuer_privkey: + logger.warn("Cannot sign credential (no private key)") + return + if not self.issuer_gid: + logger.warn("Cannot sign credential (no issuer gid)") return doc = parseString(self.get_xml()) sigs = doc.getElementsByTagName("signatures")[0] @@ -660,8 +661,10 @@ class Credential(object): # Call out to xmlsec1 to sign it ref = 'Sig_%s' % self.get_refid() filename = self.save_to_random_tmp_file() - signed = os.popen('%s --sign --node-id "%s" --privkey-pem %s,%s %s' \ - % (self.xmlsec_path, ref, self.issuer_privkey, ",".join(gid_files), filename)).read() + command='%s --sign --node-id "%s" --privkey-pem %s,%s %s' \ + % (self.xmlsec_path, ref, self.issuer_privkey, ",".join(gid_files), filename) +# print 'command',command + signed = os.popen(command).read() os.remove(filename) for gid_file in gid_files: @@ -836,7 +839,7 @@ class Credential(object): # Verify the gids of this cred and of its parents for cur_cred in self.get_credential_list(): cur_cred.get_gid_object().verify_chain(trusted_cert_objects) - cur_cred.get_gid_caller().verify_chain(trusted_cert_objects) + cur_cred.get_gid_caller().verify_chain(trusted_cert_objects) refs = [] refs.append("Sig_%s" % self.get_refid()) @@ -844,6 +847,7 @@ class Credential(object): parentRefs = self.updateRefID() for ref in parentRefs: refs.append("Sig_%s" % ref) + for ref in refs: # If caller explicitly passed in None that means skip xmlsec1 validation. # Strange and not typical @@ -864,10 +868,11 @@ class Credential(object): msg = verified[mstart:mend] raise CredentialNotVerifiable("xmlsec1 error verifying cred %s using Signature ID %s: %s %s" % (self.get_summary_tostring(), ref, msg, verified.strip())) os.remove(filename) - + # Verify the parents (delegation) if self.parent: self.verify_parent(self.parent) + # Make sure the issuer is the target's authority, and is # itself a valid GID self.verify_issuer(trusted_cert_objects) @@ -1038,7 +1043,7 @@ class Credential(object): print self.dump_string(*args, **kwargs) - def dump_string(self, dump_parents=False): + def dump_string(self, dump_parents=False, show_xml=False): result="" result += "CREDENTIAL %s\n" % self.get_subject() filename=self.get_filename() @@ -1053,6 +1058,9 @@ class Credential(object): print " gidIssuer:" self.get_signature().get_issuer_gid().dump(8, dump_parents) + if self.expiration: + print " expiration:", self.expiration.isoformat() + gidObject = self.get_gid_object() if gidObject: result += " gidObject:\n" @@ -1062,4 +1070,16 @@ class Credential(object): result += "\nPARENT" result += self.parent.dump_string(True) + if show_xml: + try: + tree = etree.parse(StringIO(self.xml)) + aside = etree.tostring(tree, pretty_print=True) + result += "\nXML\n" + result += aside + result += "\nEnd XML\n" + except: + import traceback + print "exc. Credential.dump_string / XML" + traceback.print_exc() + return result