Example #1
0
def checkmononokehost(ui, url, opts):
    sslvalidator = lambda x: None if opts.get("insecure"
                                              ) else sslutil.validatesocket

    _authdata, auth = httpconnection.readauthforuri(ui, str(url), url.user)

    conn = httpclient.HTTPConnection(
        url.host,
        int(url.port),
        use_ssl=True,
        ssl_wrap_socket=sslutil.wrapsocket,
        ssl_validator=sslvalidator,
        ui=ui,
        certfile=auth.get("cert"),
        keyfile=auth.get("key"),
    )

    conn.request(b"GET", b"/health_check", body=None, headers=None)
    res = conn.getresponse()
    while not res.complete():
        res.read(length=BLOCK_SIZE)
    if not httpstatussuccess(res.status):
        raise error.Abort("checkmononokehost: HTTP response status code: %s",
                          res.status)

    hostname = res.headers.get(HEADER_MONONOKE_HOST)
    if hostname:
        ui.status(_("Server hostname is %s\n") % hostname,
                  component="debugnetwork")
    return True
Example #2
0
def _getcreds(ui, url):
    """Get the TLS mutual authentication credentials for the given URL."""
    res = httpconnection.readauthforuri(ui, url, None)
    if res is None:
        return None
    group, auth = res
    if "cert" not in auth or "key" not in auth:
        return None
    return (auth["cert"], auth["key"])
Example #3
0
    def _setuphttpsconnection(self):
        # setting up HTTS connection

        # enable client side compression
        # data in the response is also requested compressed
        self.headers = {
            "Connection": "keep-alive",
            "Content-Type": "application/json",
            "Accept-Encoding": "none, gzip",
            "Content-Encoding": "gzip",
        }
        if self.token:
            self.headers["Authorization"] = "OAuth %s" % self.token

        u = util.url(self.url, parsequery=False, parsefragment=False)
        if u.scheme != "https" or not u.host or u.passwd is not None:
            raise ccerror.ConfigurationError(
                self.ui, _("'commitcloud.url' is invalid or unsupported"))

        remotehost = u.host
        remoteport = int(u.port) if u.port else 443

        sslcontext = ssl.create_default_context()

        # if the token is not set, use the same TLS auth to connect to the Commit Cloud service
        # as it is used to connect to the default path
        if not self.token:
            path = ccutil.getremotepath(self.ui)
            authdata = httpconnection.readauthforuri(self.ui, path, u.user)
            if authdata:
                (authname, auth) = authdata
                cert = auth.get("cert")
                key = auth.get("key")
                cacerts = auth.get("cacerts")
                sslcontext.load_cert_chain(cert, keyfile=key)
                if cacerts:
                    sslcontext.load_verify_locations(cacerts)
            else:
                raise ccerror.TLSConfigurationError(
                    self.ui,
                    _("no certificates have been found to connect to the Commit Cloud Service"
                      ),
                )

        self.connection = httpclient.HTTPConnection(
            remotehost,
            remoteport,
            timeout=DEFAULT_TIMEOUT,
            use_ssl=True,
            ssl_wrap_socket=sslcontext.wrap_socket,
        )

        self.ui.debug(
            "will be connecting to %s:%d\n" % (remotehost, remoteport),
            component="commitcloud",
        )
Example #4
0
def checkspeedhttp(ui, url, opts):
    ui.status(_("Testing connection speed to the server\n"),
              component="debugnetwork")
    download = ui.configbytes("debugnetwork", "speed-test-download-size",
                              10000000)
    upload = ui.configbytes("debugnetwork", "speed-test-upload-size", 1000000)

    sslvalidator = lambda x: None if opts.get("insecure"
                                              ) else sslutil.validatesocket

    _authdata, auth = httpconnection.readauthforuri(ui, str(url), url.user)

    conn = httpclient.HTTPConnection(
        url.host,
        int(url.port),
        use_ssl=True,
        ssl_wrap_socket=sslutil.wrapsocket,
        ssl_validator=sslvalidator,
        ui=ui,
        certfile=auth.get("cert"),
        keyfile=auth.get("key"),
    )

    def downloadtest(_description, bytecount):
        headers = {HEADER_NETSPEEDTEST_NBYTES: bytecount}
        conn.request(b"GET", b"/netspeedtest", body=None, headers=headers)
        starttime = util.timer()
        res = conn.getresponse()
        while not res.complete():
            res.read(length=BLOCK_SIZE)
        endtime = util.timer()
        if not httpstatussuccess(res.status):
            raise error.Abort("downloadtest: HTTP response status code: %s",
                              res.status)

        return endtime - starttime

    def uploadtest(_description, bytecount):
        body = bytecount * b"A"
        starttime = util.timer()
        conn.request(b"POST", b"/netspeedtest", body=body)
        res = conn.getresponse()
        while not res.complete():
            res.read(length=BLOCK_SIZE)
        endtime = util.timer()

        if not httpstatussuccess(res.status):
            raise error.Abort("uploadtest: HTTP response status code: %s" %
                              res.status)
        return endtime - starttime

    def latencytest(n):
        latencies = []
        while n > 0:
            conn.request(b"GET", b"/health_check", body=None)
            starttime = util.timer()
            res = conn.getresponse()
            while not res.complete():
                res.read(length=BLOCK_SIZE)
            endtime = util.timer()
            if not httpstatussuccess(res.status):
                raise error.Abort(
                    "latencytest: HTTP response status code: %s" % res.status)
            latencies.append(endtime - starttime)
            n -= 1
        return latencies

    res = drivespeedtests(
        ui,
        (latencytest, 5),
        (downloadtest, "download", download),
        (uploadtest, "upload", upload),
    )

    conn.close()

    return res