def installEndPoints(i, localMemberId, tcId): """ Add the local endpoints as defined in the config file in the protocols folder. """ _printAction (i, "Initialise protocols and their endpoints", 1) from protocolcontroller import getProtocolController from communicationmanager import getProtocolManager from communicationmanager import Protocol from communicationmanager import getEndpointManager from communicationmanager import Endpoint from protocols.config import endpoints # these are actually addesses, not really endpoints from securitymanager import getCredentialManager from securitymanager import getAlgorithmManager for protocolName in getProtocolController().getOpenProtocols(): _printAction (i+1, "Add Protocol '" + protocolName + "'") ## protocol = Protocol(None, protocolName) ## getProtocolManager().addProtocol(protocol) protocol = getProtocolManager().getProtocolByNameAndInsert(protocolName) _finishActionLine() _printAction (i+1, "Add Endpoints for protocol '" + protocolName + "'", 1) address = endpoints[protocolName] for credential in getCredentialManager().getCredentialsForMember(localMemberId): algName = getAlgorithmManager().getAlgorithm(credential.getAlgorithmId()).getName() _printAction (i+2, "Add Endpoint for protocol '" + protocolName + "' and Algorithm '" + algName + "'") endpoint = Endpoint (None, localMemberId, tcId, protocol.getId(), address, credential.getId()) getEndpointManager().addEndpoint(endpoint) _finishActionLine() _printAction (i, "Finished protocols and endpoints") _finishActionLine()
def findBestEndpointForMember(self, memberid, communityid = None, allowDefaultCommunity = 0, allowRouting =1): """ Picks the most approriate endpoint for the given member. Per default, the default community is disabled here. Currently, simply the first one is taken. """ from config import default_tc_id from communicationmanager import getEndpointManager from communitymanager import getMemberManager negCommunityList = [] if not allowDefaultCommunity: negCommunityList.append(default_tc_id) endpoints = getEndpointManager().getEndpointsForMember(memberid, communityid, negCommunityList) # if no routing allowed, remove the entries where the TC is not one, we are subscribed to routingless_endpoints = [] local = getMemberManager().getLocalMember() for e in endpoints: try: local.getCommunityIds().index(e.getCommunityId()) routingless_endpoints.append(e) except ValueError, msg: pass
def testUpdatingAndDeletingOnManager(): from communitymanager import getMemberManager, Member from communicationmanager import getEndpointManager, Endpoint, getProtocolManager from securitymanager import getCredentialManager, Credential, getAlgorithmManager m = Member('123','kk test', '<mdl/>','1.0.0.0','2005-07-08') #m.addCommunity('C760394') getMemberManager().updateMember(m, 1) alg = getAlgorithmManager().getAlgorithmByNameAndInsert('nikos alg') getEndpointManager().removeEndpointsForMember(m.getId()) c = Credential(None, alg.getId(),'','key5','123') getCredentialManager().removeCredentialsForMember(m.getId()) getCredentialManager().addCredential(c) protocol = getProtocolManager().getProtocolByNameAndInsert('soap') e = Endpoint(None, '123', 'C426009', protocol.getId(), 'http://localhost', c.getId()) getEndpointManager().addEndpoint(e)
def testCommunicationManager(): protMan = communicationmanager.getProtocolManager() print protMan prot1 = protMan.getProtocols()[0] print prot1 endpMan = communicationmanager.getEndpointManager() print endpMan endp1 = endpMan.getEndpoints()[0] print endp1
def _handleDecryptionAndValidation(self, message, protocolname): """ Helper function for supporting the decryption of the message. The passed message is passed to the L{messagewrapper.MessageWrapper.unwrapForDecryption} for removing the encryption "header" and gaining the name of the algorithm and the cipher text. Afterwards, the function decrypt in the securitycontroller is invoked and the result is returned. @return: Result of the L{securitycontroller.SecurityController.decrypt} @rtype: C{String} """ alg, data = getMessageWrapper().unwrapForDecryption(message) data = getSecurityController().decrypt(data, alg) algName, memberid, communityid, data, signature = getMessageWrapper().unwrapForValidation(data) from communicationmanager import getEndpointManager from errorhandling import G4dsDependencyException, G4dsCommunicationException from g4dslogging import getDefaultLogger, COMMUNICATION_INCOMING_NO_ENDPOINT from securitymanager import getCredentialManager, getAlgorithmManager try: endpoint = getEndpointManager().findEndpoint(memberid, communityid, protocolname, algName) credential = getCredentialManager().getCredential(endpoint.getCredentialId()) if not credential: getDefaultLogger().newMessage(COMMUNICATION_INCOMING_NO_ENDPOINT, 'No credential found for the sender of the incoming message - validation aborted.') raise G4dsCommunicationException('No credential found for the sender of the incoming message - validation aborted.') key = credential.getKey() if not getSecurityController().validate(data, signature, key, algName): raise G4dsCommunicationException('Signature not valid for this message.') return data, memberid, communityid except G4dsDependencyException, msg: getDefaultLogger().newMessage(COMMUNICATION_INCOMING_NO_ENDPOINT, 'Src Enpoint Detemination: %s - attempt key determination' %(msg)) # we have to carry on here - if the message is routed; the end-to-end message integrity must still be ensured; however - both members are not in the # same community; hence - the receiver might not know about the endpoints of the sender; however, for this approach it's compulsary, that the public # key is known creds = getCredentialManager().getCredentialsForMember(memberid) for cred in creds: # well, problem here - the sender might have several keys of the same algorithm - no chance to get around checking them all here if getAlgorithmManager().getAlgorithm(cred.getAlgorithmId()).getName() == algName: key = cred.getKey() if getSecurityController().validate(data, signature, key, algName): return data, memberid, communityid # ohoh - looks like this message is not valied :( getDefaultLogger().newMessage(COMMUNICATION_INCOMING_NO_ENDPOINT, 'Src Enpoint Detemination: manual key search not sucessful.') raise G4dsCommunicationException('Signature not valid for the incoming message.')
def sendMessage(self, message, endpoint): """ Tries to deliver a message directly, if impossible via wrapping into routing message. All messages should be passed here and not be sent from anywhere else. The routing engine looks, whether the given endpoint may be reached directly from the local node. If this is possible, the message is sent off directly. If, however, this is not possible, the message is wrapped into a routing message and gateways are tried to identify for passing the message through the topology. """ # check, whether I am in the community of the given endpoint, if not - we need to attempt to route this message tc = endpoint.getCommunityId() from communitymanager import getMemberManager local = getMemberManager().getLocalMember() ## try: ## local.getCommunityIds().index(tc) from communicationmanager import getProtocolManager, getEndpointManager if len(getEndpointManager().getEndpointsForMember(local.getId(), tc)): from g4dslogging import getDefaultLogger, COMMUNICATION_OUTGOING_MSG, COMMUNICATION_OUTGOING_MSG_DETAILS getDefaultLogger().newMessage(COMMUNICATION_OUTGOING_MSG, 'New outgoing message - direct delivery (%s | %s)' %(endpoint.getMemberId(), tc)) getDefaultLogger().newMessage(COMMUNICATION_OUTGOING_MSG_DETAILS, '-- Endpoint %s' %(str(endpoint))) getDefaultLogger().newMessage(COMMUNICATION_OUTGOING_MSG_DETAILS, '-- Size of Data %d chars' %(len(message))) protocol = getProtocolManager().getProtocol(endpoint.getProtocolId()) from protocolcontroller import getProtocolController protocol = getProtocolController().getOpenProtocol(protocol.getName()) from socket import error from errorhandling import G4dsCommunicationException try: protocol.sendMessage(endpoint.getAddress(), message) except error, msg: raise G4dsCommunicationException('Sending Message: %s' %(msg))
def sendG4dsMessage(self, endpointid, message, refid = None): """ Send a message using the given protocol. The message is encyrpted first of all using the algorithm with the requested id. (In fact, for this matter the message is passed to the private function L{_handleSigningAndEncryption}, which will thereupon pass the request to the SecurityController and return the result. The encrypted result is wrapped into a valid G4DS message using the L{messagewrapper.MessageWrapper.wrapG4dsMessage} function. Finally, the result is passed to the routing engine, which will send it off using the requested protocol. @param endpointid: Id of the endpoint for this message. @type endpointid: C{String} @param message: Message to be send via G4DS. Should "actually" be a service or control message @type message: C{String} (XML) @param refid: An id of a previous message which shall be referenced here @type refid: C{String} @return: ID of the message @rtype: C{String} """ from communicationmanager import getEndpointManager endpoint = getEndpointManager().getEndpoint(endpointid) from tools import generateId, TYPE_MESSAGE mid = generateId(TYPE_MESSAGE) from config import memberid message, doc = getMessageWrapper().wrapG4dsPlain(message, mid, memberid, refid) message, tree, rootnode = self._handleSigningAndEncryption(message, endpoint) xmltext, domtree = getMessageWrapper().wrapG4dsMessage(rootnode) from routingcontroller import getRoutingEngine ## import thread ## thread.start_new_thread(getRoutingEngine().sendMessage, (xmltext, endpoint)) getRoutingEngine().sendMessage(xmltext, endpoint) return mid
def checkEndpoints(): from communicationmanager import getEndpointManager print len(getEndpointManager().getEndpointsForMember('M111', 'C12345'))