示例#1
0
    def setUp(self):
        super(ThriftControlPoolTest, self).setUp()
        #print ezdiscovery.zk
        ezdiscovery.zk = self.client

        config = EZConfiguration()
        applicationName = ApplicationConfiguration.fromConfiguration(config).getApplicationName()

        ezdiscovery.register_common_endpoint('common_service_one', 'localhost', 8080)
        ezdiscovery.register_common_endpoint('common_service_two', 'localhost', 8081)
        ezdiscovery.register_common_endpoint('common_service_three', 'localhost', 8082)
        ezdiscovery.register_common_endpoint('common_service_multi', '192.168.1.1', 6060 )
        ezdiscovery.register_common_endpoint('common_service_multi', '192.168.1.2', 6161)
        
        ezdiscovery.register_endpoint(applicationName, "service_one", 'localhost', 8083)
        ezdiscovery.register_endpoint(applicationName, "service_two", 'localhost', 8084)
        ezdiscovery.register_endpoint(applicationName, "service_three", 'localhost', 8085)

        ezdiscovery.register_endpoint("NotThriftClientPool", "unknown_service_three", 'localhost', 8085)
        ezdiscovery.register_endpoint("NotThriftClientPool", "unknown_service_three", 'localhost', 8085)
        ezdiscovery.register_endpoint("NotThriftClientPool", "unknown_service_three", 'localhost', 8085)



        self.cp = ThriftClientPool(config, shouldConnectToZooKeeper = False)
    def __init__(self, configuration, shouldConnectToZooKeeper=True):

        if configuration is None or not isinstance(configuration, EZConfiguration):
            raise Exception("Invalid configuration.")

        self.__ezConfiguration = configuration
        self.__applicationName = ApplicationConfiguration.fromConfiguration(configuration).getApplicationName()
        self.__securityConfiguration = SecurityConfiguration.fromConfiguration(configuration)
        self.__thriftConfiguration = ThriftConfiguration.fromConfiguration(configuration)

        # TODO: Add logging stuff here                
        logging.basicConfig(filename="/tmp/py.log", level=logging.DEBUG, format='%(asctime)s \t%(levelname)s: \t%(message)s\t')
        self.__log = logging.getLogger(__name__)

        self.__serviceMapLock = threading.RLock()
        self.__serviceMap = {}
        self.__connectionPool = {}
        self.__reverseLookup = {}

        print "Application name: " + self.__applicationName
        if self.__applicationName is None:
            self.__log.warn("No application name was found. Only common services will be discoverable.")
        
        # zookeeper stuff
        if shouldConnectToZooKeeper:
            ezdiscovery.connect('localhost:2181')

        try:
            self.__common_services = list(ezdiscovery.get_common_services())
        except Exception:
            self.__log.error("Unable to get commone services")
            raise

        self.__refreshEndPoints()
        self.__refreshCommonEndPoints()
示例#3
0
def ca_server(ezconfig, service_name=None, ca_name="ezbakeca", zoo_host=None,
              host=None, port=None, verify_pattern=None, ssldir=None):
    Crypto.Random.atfork()
    # make sure zookeeper is available for registering with service discovery
    if zoo_host is None:
        zooConf = ZookeeperConfiguration(ezconfig)
        zoo_host = zooConf.getZookeeperConnectionString()
        if not zoo_host:
            raise RuntimeError("Zookeeper connection string must be specified "
                "in EzConfiguration")

    # make sure the ssl certificate directory is available
    if not ssldir:
        ac = ApplicationConfiguration(ezconfig)
        ssldir = ac.getCertificatesDir()
        if not ssldir:
            raise RuntimeError("Certificates Directory \"{0}\" must be set in"
                " EzConfiguration!".format(
                ApplicationConfiguration.CERTIFICATES_DIRECTORY_KEY))

    # get a free port to bind to (and figure out our hostname)
    if not port:
        port = get_port(range(31005,34999))
    if not host:
        host = socket.gethostname()

    # register with ezdiscovery
    try:
        if service_name is None:
            service_name = ezca.constants.SERVICE_NAME
        ezdiscovery.connect(zoo_host)
        logger.info('Registering with service discovery')
        ezdiscovery.register_common_endpoint(service_name=service_name, host=host, port=port)
    except TimeoutError as e:
        logger.error("Fatal timeout connecting to zookeeper. Unable to "
                     "register with service discovery.")
        raise e
    finally:
        ezdiscovery.disconnect()

    # create the thrift handler
    handler = EzCAHandler(ca_name, ezconfig)

    # generate/get the server SSL certs and write them to disk
    certs = handler._server_certs()
    cert_files = []
    for k, cert in certs.items():
        of = os.path.join(ssldir, k)
        cert_files.append(of)
        with os.fdopen(os.open(of, os.O_WRONLY | os.O_CREAT, 0o600), 'w') as ofs:
            ofs.write(str(cert))

    # generate certs for configured clients (read from ezconfig)
    clients = ezconfig.get(EzCAHandler.CLIENT_CERTS)
    if clients:
        gen_client_certs(handler.ca, clients.split(','),
                         ezconfig.get(EzCAHandler.CLIENT_CERT_O))

    # start the thrift server
    processor = EzCA.Processor(handler)
    transport = TSSLServerSocket(host=host, port=port,
                                 verify_pattern=verify_pattern,
                                 ca_certs=cert_files[0],
                                 cert=cert_files[1],
                                 key=cert_files[2])
    tfactory = TTransport.TBufferedTransportFactory()
    pfactory = TBinaryProtocol.TBinaryProtocolFactory()
    server = TServer.TThreadPoolServer(processor, transport, tfactory, pfactory)
    logger.info('Starting ezca service on {}:{}'.format(host,port))
    server.serve()