From 3ab53c5221664528325a0edc7ca40c57eb783428 Mon Sep 17 00:00:00 2001 From: Thierry Parmentelat Date: Thu, 7 Dec 2006 09:13:55 +0000 Subject: [PATCH] prevents side-effect on foreign objects when appropriate --- PLC/Faults.py | 50 ++++++++++++++++++++++--- PLC/Methods/AddConfFileToNode.py | 1 + PLC/Methods/AddNodeToNodeGroup.py | 1 + PLC/Methods/AddNodeToPCU.py | 1 + PLC/Methods/AddPersonKey.py | 1 + PLC/Methods/AddPersonToSite.py | 2 + PLC/Methods/AddPersonToSlice.py | 3 ++ PLC/Methods/AddRoleToPerson.py | 1 + PLC/Methods/AddSliceToNodes.py | 1 + PLC/Methods/AdmDeletePersonKeys.py | 1 + PLC/Methods/AdmGenerateNodeConfFile.py | 1 + PLC/Methods/BlacklistKey.py | 1 + PLC/Methods/DeleteKey.py | 1 + PLC/Methods/DeleteNode.py | 2 + PLC/Methods/DeletePerson.py | 1 + PLC/Methods/DeletePersonFromSite.py | 1 + PLC/Methods/DeletePersonFromSlice.py | 1 + PLC/Methods/DeleteRoleFromPerson.py | 1 + PLC/Methods/DeleteSite.py | 2 + PLC/Methods/DeleteSlice.py | 1 + PLC/Methods/DeleteSliceAttribute.py | 2 + PLC/Methods/DeleteSliceAttributeType.py | 1 + PLC/Methods/DeleteSliceFromNodes.py | 1 + PLC/Methods/SetPersonPrimarySite.py | 2 + PLC/Methods/UpdateKey.py | 1 + PLC/Methods/UpdateNode.py | 1 + PLC/Methods/UpdatePerson.py | 1 + PLC/Methods/UpdateSite.py | 1 + PLC/Methods/UpdateSlice.py | 1 + PLC/Methods/UpdateSliceAttribute.py | 2 + PLC/Methods/UpdateSliceAttributeType.py | 1 + 31 files changed, 83 insertions(+), 5 deletions(-) diff --git a/PLC/Faults.py b/PLC/Faults.py index 1b2cdd3..b2db117 100644 --- a/PLC/Faults.py +++ b/PLC/Faults.py @@ -5,7 +5,7 @@ # Mark Huang # # Copyright (C) 2004-2006 The Trustees of Princeton University -# $Id$ +# $Id: Faults.py,v 1.1 2006/09/06 15:36:06 mlhuang Exp $ # import xmlrpclib @@ -46,10 +46,13 @@ class PLCAuthenticationFailure(PLCFault): faultString = "Failed to authenticate call" PLCFault.__init__(self, 103, faultString, extra) -class PLCNotImplemented(PLCFault): - def __init__(self, extra = None): - faultString = "Not fully implemented" - PLCFault.__init__(self, 109, faultString, extra) +class PLCLocalObjectRequired(PLCFault): + def __init__(self,method_name="anonymous",obj_name="anonymous", + peer_id=None,extra=None): + faultString = "Method: <%s> - Object <%s> must be local"%(method_name,obj_name) + if peer_id is not None: + faultString += " (authoritative plc has peer_id %d)"%peer_id + PLCFault.__init__(self, 104, faultString, extra) class PLCDBError(PLCFault): def __init__(self, extra = None): @@ -61,7 +64,44 @@ class PLCPermissionDenied(PLCFault): faultString = "Permission denied" PLCFault.__init__(self, 108, faultString, extra) +class PLCNotImplemented(PLCFault): + def __init__(self, extra = None): + faultString = "Not fully implemented" + PLCFault.__init__(self, 109, faultString, extra) + class PLCAPIError(PLCFault): def __init__(self, extra = None): faultString = "Internal API error" PLCFault.__init__(self, 111, faultString, extra) + +#################### +# shorthands to check various types of objects for localness (are we authoritative) +def PLCCheckLocalNode (node,method_name): + if node['peer_id'] is not None: + raise PLCLocalObjectRequired(method_name,node['hostname'],node['peer_id']) + +def PLCCheckLocalPerson (person,method_name): + if person['peer_id'] is not None: + raise PLCLocalObjectRequired(method_name,person['email'],person['peer_id']) + +def PLCCheckLocalSite (site,method_name): + if site['peer_id'] is not None: + raise PLCLocalObjectRequired(method_name,site['name'],site['peer_id']) + +def PLCCheckLocalSlice (slice,method_name): + if slice['peer_id'] is not None: + raise PLCLocalObjectRequired(method_name,slice['name'],slice['peer_id']) + +def PLCCheckLocalKey (key,method_name): + if key['peer_id'] is not None: + raise PLCLocalObjectRequired(method_name,key['key_id'],key['peer_id']) + +def PLCCheckLocalSliceAttributeType (sliceAttributeType,method_name): + if sliceAttributeType['peer_id'] is not None: + raise PLCLocalObjectRequired(method_name,sliceAttributeType['name'],sliceAttributeType['peer_id']) + +def PLCCheckLocalSliceAttribute (sliceAttribute,method_name): + if sliceAttribute['peer_id'] is not None: + raise PLCLocalObjectRequired(method_name,sliceAttribute['name'],sliceAttribute['peer_id']) + + diff --git a/PLC/Methods/AddConfFileToNode.py b/PLC/Methods/AddConfFileToNode.py index 7bcdcc0..0c4899e 100644 --- a/PLC/Methods/AddConfFileToNode.py +++ b/PLC/Methods/AddConfFileToNode.py @@ -37,6 +37,7 @@ class AddConfFileToNode(Method): if not nodes: raise PLCInvalidArgument, "No such node" node = nodes[0] + PLCCheckLocalNode (node,"AddConfFileToNode") # Link configuration file to node if node['node_id'] not in conf_file['node_ids']: diff --git a/PLC/Methods/AddNodeToNodeGroup.py b/PLC/Methods/AddNodeToNodeGroup.py index 1720dfb..336387b 100644 --- a/PLC/Methods/AddNodeToNodeGroup.py +++ b/PLC/Methods/AddNodeToNodeGroup.py @@ -32,6 +32,7 @@ class AddNodeToNodeGroup(Method): if not nodes: raise PLCInvalidArgument, "No such node" node = nodes[0] + PLCCheckLocalNode (node,"AddNodeToNodeGroup") # Get nodegroup info nodegroups = NodeGroups(self.api, [nodegroup_id_or_name]) diff --git a/PLC/Methods/AddNodeToPCU.py b/PLC/Methods/AddNodeToPCU.py index 4314a67..e57e168 100644 --- a/PLC/Methods/AddNodeToPCU.py +++ b/PLC/Methods/AddNodeToPCU.py @@ -34,6 +34,7 @@ class AddNodeToPCU(Method): raise PLCInvalidArgument, "No such node" node = nodes[0] + PLCCheckLocalNode(node,"AddNodeToPCU") # Get PCU pcus = PCUs(self.api, [pcu_id]) diff --git a/PLC/Methods/AddPersonKey.py b/PLC/Methods/AddPersonKey.py index c404d05..a06ea06 100644 --- a/PLC/Methods/AddPersonKey.py +++ b/PLC/Methods/AddPersonKey.py @@ -38,6 +38,7 @@ class AddPersonKey(Method): if not persons: raise PLCInvalidArgument, "No such account" person = persons[0] + PLCCheckLocalPerson (person,"AddPersonKey") # If we are not admin, make sure caller is adding a key to their account if 'admin' not in self.caller['roles']: diff --git a/PLC/Methods/AddPersonToSite.py b/PLC/Methods/AddPersonToSite.py index 5ada731..0753e9a 100644 --- a/PLC/Methods/AddPersonToSite.py +++ b/PLC/Methods/AddPersonToSite.py @@ -33,6 +33,7 @@ class AddPersonToSite(Method): raise PLCInvalidArgument, "No such account" person = persons[0] + PLCCheckLocalPerson(person,"AddPersonToSite") # Get site information sites = Sites(self.api, [site_id_or_login_base]) @@ -40,6 +41,7 @@ class AddPersonToSite(Method): raise PLCInvalidArgument, "No such site" site = sites[0] + PLCCheckLocalSite(site,"AddPersonToSite") if site['site_id'] not in person['site_ids']: site.add_person(person) diff --git a/PLC/Methods/AddPersonToSlice.py b/PLC/Methods/AddPersonToSlice.py index bd16e04..a5b8072 100644 --- a/PLC/Methods/AddPersonToSlice.py +++ b/PLC/Methods/AddPersonToSlice.py @@ -32,6 +32,8 @@ class AddPersonToSlice(Method): raise PLCInvalidArgument, "No such account" person = persons[0] + # Let's be open-minded as a start + #PLCCheckLocalPerson(person,"AddPersonToSlice") # Get slice information slices = Slices(self.api, [slice_id_or_name]) @@ -39,6 +41,7 @@ class AddPersonToSlice(Method): raise PLCInvalidArgument, "No such slice" slice = slices[0] + PLCCheckLocalSlice(slice,"AddPersonToSlice") # If we are not admin, make sure the caller is a PI # of the site associated with the slice diff --git a/PLC/Methods/AddRoleToPerson.py b/PLC/Methods/AddRoleToPerson.py index 08f183e..100bb66 100644 --- a/PLC/Methods/AddRoleToPerson.py +++ b/PLC/Methods/AddRoleToPerson.py @@ -49,6 +49,7 @@ class AddRoleToPerson(Method): raise PLCInvalidArgument, "No such account" person = persons[0] + PLCCheckLocalPerson(person,"AddRoleToPerson") # Authenticated function assert self.caller is not None diff --git a/PLC/Methods/AddSliceToNodes.py b/PLC/Methods/AddSliceToNodes.py index e1950f5..f2b042f 100644 --- a/PLC/Methods/AddSliceToNodes.py +++ b/PLC/Methods/AddSliceToNodes.py @@ -36,6 +36,7 @@ class AddSliceToNodes(Method): raise PLCInvalidArgument, "No such slice" slice = slices[0] + PLCCheckLocalSlice(slice,"AddSliceToNodes") if 'admin' not in self.caller['roles']: if self.caller['person_id'] in slice['person_ids']: diff --git a/PLC/Methods/AdmDeletePersonKeys.py b/PLC/Methods/AdmDeletePersonKeys.py index 01d4055..57c63f0 100644 --- a/PLC/Methods/AdmDeletePersonKeys.py +++ b/PLC/Methods/AdmDeletePersonKeys.py @@ -36,6 +36,7 @@ class AdmDeletePersonKeys(Method): raise PLCInvalidArgument, "No such account" person = persons[0] + PLCCheckLocalPerson(person,"AdmDeletePersonKeys") if 'admin' not in self.caller['roles']: if self.caller['person_id'] != person['person_id']: diff --git a/PLC/Methods/AdmGenerateNodeConfFile.py b/PLC/Methods/AdmGenerateNodeConfFile.py index ed968f9..8781e55 100644 --- a/PLC/Methods/AdmGenerateNodeConfFile.py +++ b/PLC/Methods/AdmGenerateNodeConfFile.py @@ -41,6 +41,7 @@ class AdmGenerateNodeConfFile(Method): if not nodes: raise PLCInvalidArgument, "No such node" node = nodes[0] + PLCCheckLocalNode(node,"AdmGenerateNodeConfFile") # If we are not an admin, make sure that the caller is a # member of the site at which the node is located. diff --git a/PLC/Methods/BlacklistKey.py b/PLC/Methods/BlacklistKey.py index 17a0418..a412865 100644 --- a/PLC/Methods/BlacklistKey.py +++ b/PLC/Methods/BlacklistKey.py @@ -34,6 +34,7 @@ class BlacklistKey(Method): if not keys: raise PLCInvalidArgument, "No such key" key = keys[0] + PLCCheckLocalKey(key,"BlackListKey") key.blacklist() self.object_ids = [key['key_id']] diff --git a/PLC/Methods/DeleteKey.py b/PLC/Methods/DeleteKey.py index bdd3397..d3eb68d 100644 --- a/PLC/Methods/DeleteKey.py +++ b/PLC/Methods/DeleteKey.py @@ -29,6 +29,7 @@ class DeleteKey(Method): if not keys: raise PLCInvalidArgument, "No such key" key = keys[0] + PLCCheckLocalKey(key,"DeleteKey") if 'admin' not in self.caller['roles']: if key['key_id'] not in self.caller['key_ids']: diff --git a/PLC/Methods/DeleteNode.py b/PLC/Methods/DeleteNode.py index 64f149a..c8dc2d8 100644 --- a/PLC/Methods/DeleteNode.py +++ b/PLC/Methods/DeleteNode.py @@ -32,6 +32,8 @@ class DeleteNode(Method): raise PLCInvalidArgument, "No such node" node = nodes[0] + ### xxx here xxx + PLCCheckLocalNode(node,"DeleteNode") # If we are not an admin, make sure that the caller is a # member of the site at which the node is located. diff --git a/PLC/Methods/DeletePerson.py b/PLC/Methods/DeletePerson.py index 38534c7..fd930a1 100644 --- a/PLC/Methods/DeletePerson.py +++ b/PLC/Methods/DeletePerson.py @@ -33,6 +33,7 @@ class DeletePerson(Method): raise PLCInvalidArgument, "No such account" person = persons[0] + PLCCheckLocalPerson(person,"DeletePerson") # Authenticated function assert self.caller is not None diff --git a/PLC/Methods/DeletePersonFromSite.py b/PLC/Methods/DeletePersonFromSite.py index c17384d..2c48127 100644 --- a/PLC/Methods/DeletePersonFromSite.py +++ b/PLC/Methods/DeletePersonFromSite.py @@ -41,6 +41,7 @@ class DeletePersonFromSite(Method): raise PLCInvalidArgument, "No such site" site = sites[0] + PLCCheckLocalSite(site,"DeletePersonFromSite") if site['site_id'] in person['site_ids']: site.remove_person(person) diff --git a/PLC/Methods/DeletePersonFromSlice.py b/PLC/Methods/DeletePersonFromSlice.py index bca374c..22e4be3 100644 --- a/PLC/Methods/DeletePersonFromSlice.py +++ b/PLC/Methods/DeletePersonFromSlice.py @@ -40,6 +40,7 @@ class DeletePersonFromSlice(Method): raise PLCInvalidArgument, "No such slice" slice = slices[0] + PLCCheckLocalSlice(slice,"DeletePersonFromSlice") # If we are not admin, make sure the caller is a pi # of the site associated with the slice diff --git a/PLC/Methods/DeleteRoleFromPerson.py b/PLC/Methods/DeleteRoleFromPerson.py index 1119ad4..f2106a7 100644 --- a/PLC/Methods/DeleteRoleFromPerson.py +++ b/PLC/Methods/DeleteRoleFromPerson.py @@ -49,6 +49,7 @@ class DeleteRoleFromPerson(Method): raise PLCInvalidArgument, "No such account" person = persons[0] + PLCCheckLocalPerson(person,"DeleteRoleFromPerson") # Authenticated function assert self.caller is not None diff --git a/PLC/Methods/DeleteSite.py b/PLC/Methods/DeleteSite.py index 743e228..2f5b54d 100644 --- a/PLC/Methods/DeleteSite.py +++ b/PLC/Methods/DeleteSite.py @@ -35,6 +35,8 @@ class DeleteSite(Method): raise PLCInvalidArgument, "No such site" site = sites[0] + PLCCheckLocalSite(site,"DeleteSite") + site.delete() self.object_ids = [site['site_id']] diff --git a/PLC/Methods/DeleteSlice.py b/PLC/Methods/DeleteSlice.py index 888f6e0..e635b0d 100644 --- a/PLC/Methods/DeleteSlice.py +++ b/PLC/Methods/DeleteSlice.py @@ -31,6 +31,7 @@ class DeleteSlice(Method): if not slices: raise PLCInvalidArgument, "No such slice" slice = slices[0] + PLCCheckLocalSlice(slice,"DeleteSlice") if 'admin' not in self.caller['roles']: if self.caller['person_id'] in slice['person_ids']: diff --git a/PLC/Methods/DeleteSliceAttribute.py b/PLC/Methods/DeleteSliceAttribute.py index 989b112..c283c45 100644 --- a/PLC/Methods/DeleteSliceAttribute.py +++ b/PLC/Methods/DeleteSliceAttribute.py @@ -34,11 +34,13 @@ class DeleteSliceAttribute(Method): if not slice_attributes: raise PLCInvalidArgument, "No such slice attribute" slice_attribute = slice_attributes[0] + PLCCheckLocalSliceAttribute(slice_attribute,"DeleteSliceAttribute") slices = Slices(self.api, [slice_attribute['slice_id']]) if not slices: raise PLCInvalidArgument, "No such slice" slice = slices[0] + PLCCheckLocalSlice(slice,"DeleteSliceAttribute") assert slice_attribute['slice_attribute_id'] in slice['slice_attribute_ids'] diff --git a/PLC/Methods/DeleteSliceAttributeType.py b/PLC/Methods/DeleteSliceAttributeType.py index 922b6ac..ab10911 100644 --- a/PLC/Methods/DeleteSliceAttributeType.py +++ b/PLC/Methods/DeleteSliceAttributeType.py @@ -27,6 +27,7 @@ class DeleteSliceAttributeType(Method): if not attribute_types: raise PLCInvalidArgument, "No such slice attribute type" attribute_type = attribute_types[0] + PLCCheckLocalSliceAttributeType(attribute_type,"DeleteSliceAttributeType") attribute_type.delete() self.object_ids = [attribute_type['attribute_type_id']] diff --git a/PLC/Methods/DeleteSliceFromNodes.py b/PLC/Methods/DeleteSliceFromNodes.py index 2945d31..321169e 100644 --- a/PLC/Methods/DeleteSliceFromNodes.py +++ b/PLC/Methods/DeleteSliceFromNodes.py @@ -33,6 +33,7 @@ class DeleteSliceFromNodes(Method): raise PLCInvalidArgument, "No such slice" slice = slices[0] + PLCCheckLocalSlice(slice,"DeleteSliceFromNodes") if 'admin' not in self.caller['roles']: if self.caller['person_id'] in slice['person_ids']: diff --git a/PLC/Methods/SetPersonPrimarySite.py b/PLC/Methods/SetPersonPrimarySite.py index 61ab287..e2e913f 100644 --- a/PLC/Methods/SetPersonPrimarySite.py +++ b/PLC/Methods/SetPersonPrimarySite.py @@ -32,6 +32,7 @@ class SetPersonPrimarySite(Method): raise PLCInvalidArgument, "No such account" person = persons[0] + PLCCheckLocalPerson(person,"SetPersonPrimarySite") # Authenticated function assert self.caller is not None @@ -47,6 +48,7 @@ class SetPersonPrimarySite(Method): raise PLCInvalidArgument, "No such site" site = sites[0] + PLCCheckLocalSite(site,"SetPersonPrimarySite") if site['site_id'] not in person['site_ids']: raise PLCInvalidArgument, "Not a member of the specified site" diff --git a/PLC/Methods/UpdateKey.py b/PLC/Methods/UpdateKey.py index 5e14b9a..14e521e 100644 --- a/PLC/Methods/UpdateKey.py +++ b/PLC/Methods/UpdateKey.py @@ -37,6 +37,7 @@ class UpdateKey(Method): if not keys: raise PLCInvalidArgument, "No such key" key = keys[0] + PLCCheckLocalKey(key,"UpdateKey") if 'admin' not in self.caller['roles']: if key['key_id'] not in self.caller['key_ids']: diff --git a/PLC/Methods/UpdateNode.py b/PLC/Methods/UpdateNode.py index 0e40564..5530874 100644 --- a/PLC/Methods/UpdateNode.py +++ b/PLC/Methods/UpdateNode.py @@ -46,6 +46,7 @@ class UpdateNode(Method): raise PLCInvalidArgument, "No such node" node = nodes[0] + PLCCheckLocalNode(node,"UpdateNode") # Authenticated function assert self.caller is not None diff --git a/PLC/Methods/UpdatePerson.py b/PLC/Methods/UpdatePerson.py index 5d0161c..65a490f 100644 --- a/PLC/Methods/UpdatePerson.py +++ b/PLC/Methods/UpdatePerson.py @@ -42,6 +42,7 @@ class UpdatePerson(Method): raise PLCInvalidArgument, "No such account" person = persons[0] + PLCCheckLocalPerson(person,"UpdatePerson") # Authenticated function assert self.caller is not None diff --git a/PLC/Methods/UpdateSite.py b/PLC/Methods/UpdateSite.py index 0e570f2..d09a21b 100644 --- a/PLC/Methods/UpdateSite.py +++ b/PLC/Methods/UpdateSite.py @@ -42,6 +42,7 @@ class UpdateSite(Method): raise PLCInvalidArgument, "No such site" site = sites[0] + PLCCheckLocalSite(site,"UpdateSite") # Authenticated function assert self.caller is not None diff --git a/PLC/Methods/UpdateSlice.py b/PLC/Methods/UpdateSlice.py index a9d6cb2..ac454af 100644 --- a/PLC/Methods/UpdateSlice.py +++ b/PLC/Methods/UpdateSlice.py @@ -46,6 +46,7 @@ class UpdateSlice(Method): if not slices: raise PLCInvalidArgument, "No such slice" slice = slices[0] + PLCCheckLocalSlice(slice,"UpdateSlice") if 'admin' not in self.caller['roles']: if self.caller['person_id'] in slice['person_ids']: diff --git a/PLC/Methods/UpdateSliceAttribute.py b/PLC/Methods/UpdateSliceAttribute.py index a9f26ec..81096a4 100644 --- a/PLC/Methods/UpdateSliceAttribute.py +++ b/PLC/Methods/UpdateSliceAttribute.py @@ -32,11 +32,13 @@ class UpdateSliceAttribute(Method): if not slice_attributes: raise PLCInvalidArgument, "No such slice attribute" slice_attribute = slice_attributes[0] + PLCCheckLocalSliceAttribute(slice_attribute,"UpdateSliceAttribute") slices = Slices(self.api, [slice_attribute['slice_id']]) if not slices: raise PLCInvalidArgument, "No such slice" slice = slices[0] + PLCCheckLocalSlice(slice,"UpdateSliceAttribute") assert slice_attribute['slice_attribute_id'] in slice['slice_attribute_ids'] diff --git a/PLC/Methods/UpdateSliceAttributeType.py b/PLC/Methods/UpdateSliceAttributeType.py index a6c7701..4dc852f 100644 --- a/PLC/Methods/UpdateSliceAttributeType.py +++ b/PLC/Methods/UpdateSliceAttributeType.py @@ -35,6 +35,7 @@ class UpdateSliceAttributeType(Method): if not attribute_types: raise PLCInvalidArgument, "No such attribute" attribute_type = attribute_types[0] + PLCCheckLocalSliceAttributeType(attribute_type,"UpdateSliceAttributeType") attribute_type.update(attribute_type_fields) attribute_type.sync() -- 2.43.0