Esempio n. 1
0
    def start(self,
              iface='',
              network=None,
              bootstrap=None,
              cb=None,
              name=None,
              nodeid=None):
        if bootstrap is None:
            bootstrap = []

        if network is None:
            network = _conf.get_in_order("dht_network_filter", "ALL")
        certpath, cert, certstr = self._runtime_credentials.get_own_cert()
        if not certstr:
            raise ("No runtime certificate available, please restart runtime")
        self._network = network
        self._iface = iface
        self._bootstrap = bootstrap
        self._cb = cb
        self._name = name

        self._dlist = []
        self._ssdps = SSDPServiceDiscovery(self._node_id, self._control_uri,
                                           iface)
        self._dlist += self._ssdps.start()
        domain = _conf.get("security", "domain_name")
        is_ca = False
        try:
            _ca_conf = _conf.get("security", "certificate_authority")
            if "is_ca" in _ca_conf and _ca_conf["is_ca"] == "True":
                is_ca = True
        except:
            is_ca = False
        self._ssdps.update_server_params(CA_SERVICE_UUID,
                                         sign=is_ca,
                                         name=name)
        dht_id = dhtid_from_nodeid(self._node_id)
        self.dht_server = ServerApp(
            AppendServer,
            dht_id,
            node_name=self._name,
            runtime_credentials=self._runtime_credentials)
        ip, port = self.dht_server.start(iface=self._iface)

        self._dlist.append(self.dht_server.bootstrap(self._bootstrap))

        logger("Register service %s %s:%s" % (self._network, ip, port))
        self._ssdps.register_service(self._network, ip, port)

        logger("Set client filter %s" % (self._network))
        self._ssdps.set_client_filter(self._network)

        def bootstrap_proxy(addrs):
            def started(args):
                logger("DHT Started %s" % (args))
                if not self._started and self._cb:
                    reactor.callLater(.2, self._cb, True)
                self._started = True

            def failed(args):
                logger("DHT failed to bootstrap %s" % (args))
                #reactor.callLater(.5, bootstrap_proxy, addrs)

            logger("Trying to bootstrap with %s" % (repr(addrs)))
            d = self.dht_server.bootstrap(addrs)
            d.addCallback(started)
            d.addErrback(failed)

        def start_msearch(args):
            def _later_start():
                self._ssdps.start_search(SERVICE_UUID,
                                         callback=bootstrap_proxy,
                                         stop=False)
                self._ssdps.update_server_params(SERVICE_UUID, cert=certstr)

            logger("** msearch %s args: %s" % (self, repr(args)))
            reactor.callLater(0, _later_start)

        # Wait until servers all listen
        dl = defer.DeferredList(self._dlist)
        dl.addBoth(start_msearch)
        # Only for logging
        self.dht_server.kserver.protocol.sourceNode.port = port
        self.dht_server.kserver.protocol.sourceNode.ip = "0.0.0.0"
        #FIXME handle inside ServerApp
        self.dht_server.kserver.name = self._name
        self.dht_server.kserver.protocol.name = self._name
        self.dht_server.kserver.protocol.storeOwnCert(certstr)
Esempio n. 2
0
    def start(self,
              iface='',
              network=None,
              bootstrap=None,
              cb=None,
              name=None,
              nodeid=None):
        if bootstrap is None:
            bootstrap = []

        if network is None:
            network = _conf.get_in_order("dht_network_filter", "ALL")
        self._network = network
        self._iface = iface
        self._bootstrap = bootstrap
        self._cb = cb
        self._name = name

        self._dlist = []
        self._ssdps = SSDPServiceDiscovery(iface)
        self._dlist += self._ssdps.start()
        domain = _conf.get("security", "security_domain_name")
        is_ca = False
        try:
            if _conf.get("security", "certificate_authority") == "True":
                ca = certificate_authority.CA(domain)
                #make sure private key exist
                if ca.verify_private_key_exist():
                    is_ca = True
        except:
            is_ca = False
        self._ssdps.update_server_params(CA_SERVICE_UUID,
                                         sign=is_ca,
                                         name=name)
        cert, certstr = certificate.get_own_cert(self._name)
        if not cert:
            _log.debug("runtime cert not available, let's create CSR")
            if is_ca:
                # We are the CA, just generate CSR and sign it
                csrfile = certificate.new_runtime(name, domain, nodeid=nodeid)
                _log.debug("Local CA sign runtime CSR")
                try:
                    content = open(csrfile, 'rt').read()
                    certpath = ca.sign_csr(csrfile)
                    certificate.store_own_cert(certpath=certpath)
                    return self._signed_cert_available()
                except:
                    _log.exception("Failed signing with local CA")
                    raise
            else:
                # Discover the signing CA
                _log.debug("No signed cert, discover CA signing CSR")
                self._sde_client = sde.Client(
                    name, nodeid,
                    CalvinCB(self._ssdps.start_search,
                             CA_SERVICE_UUID,
                             callback=self._signed_cert_received),
                    self._signed_cert_available)
        else:
            _log.debug("runtime cert available")
            self._signed_cert_available(cert=cert, certstr=certstr)