import socket import threading import ssl import logging from sjrpc.client import SimpleRpcClient from sjrpc.utils import ConnectionProxy from sjrpc.core import RpcError from ccnodehandlers import * class CCNode(object): ''' This class handles node initialization and connection and authentication ''' def __init__(self, server, port, cert=None): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) ssl_sock = ssl.wrap_socket(sock, certfile=cert, cert_reqs=ssl.CERT_NONE, ssl_version=ssl.PROTOCOL_TLSv1) ssl_sock.connect((server, port)) self.manager = SimpleRpcClient(ssl_sock, default_handler=NodeHandler(self)) self.server = ConnectionProxy(self.manager) def run(self): self.manager.run() def authentify(self, login, password): logging.debug('Authenticating user %s with password <%s>' % (login, password)) try: role = self.server.authentify(login, password) except RpcError as err: if err.exception == 'AuthenticationError': logging.warning('Authentication error') else: logging.warning('Unknown error while authenticating: %s' % err) return False else: if role != 'hypervisor': logging.warning('Bad role affected by server: %s' % role) return False else: return True