示例#1
0
文件: utils.py 项目: cgwyx/bigchaindb
def mongodb_host(host):
    """Utility function that works as a type for mongodb ``host`` args.

    This function validates the ``host`` args provided by to the
    ``add-replicas`` and ``remove-replicas`` commands and checks if each arg
    is in the form "host:port"

    Args:
        host (str): A string containing hostname and port (e.g. "host:port")

    Raises:
        ArgumentTypeError: if it fails to parse the argument
    """
    # check if mongodb can parse the host
    try:
        hostname, port = uri_parser.parse_host(host, default_port=None)
    except ValueError as exc:
        raise argparse.ArgumentTypeError(exc.args[0])

    # we do require the port to be provided.
    if port is None or hostname == '':
        raise argparse.ArgumentTypeError('expected host in the form '
                                         '`host:port`. Got `{}` instead.'
                                         .format(host))

    return host
示例#2
0
    def kms_request(self, kms_context):
        """Complete a KMS request.

        :Parameters:
          - `kms_context`: A :class:`MongoCryptKmsContext`.

        :Returns:
          None
        """
        endpoint = kms_context.endpoint
        message = kms_context.message
        host, port = parse_host(endpoint, _HTTPS_PORT)
        # Enable strict certificate verification, OCSP, match hostname, and
        # SNI using the system default CA certificates.
        ctx = get_ssl_context(
            None,  # certfile
            None,  # keyfile
            None,  # passphrase
            None,  # ca_certs
            CERT_REQUIRED,  # cert_reqs
            None,  # crlfile
            True,  # match_hostname
            True)  # check_ocsp_endpoint
        opts = PoolOptions(connect_timeout=_KMS_CONNECT_TIMEOUT,
                           socket_timeout=_KMS_CONNECT_TIMEOUT,
                           ssl_context=ctx)
        conn = _configured_socket((host, port), opts)
        try:
            conn.sendall(message)
            while kms_context.bytes_needed > 0:
                data = conn.recv(kms_context.bytes_needed)
                kms_context.feed(data)
        finally:
            conn.close()
def mongodb_host(host):
    """Utility function that works as a type for mongodb ``host`` args.

    This function validates the ``host`` args provided by to the
    ``add-replicas`` and ``remove-replicas`` commands and checks if each arg
    is in the form "host:port"

    Args:
        host (str): A string containing hostname and port (e.g. "host:port")

    Raises:
        ArgumentTypeError: if it fails to parse the argument
    """
    # check if mongodb can parse the host
    try:
        hostname, port = uri_parser.parse_host(host, default_port=None)
    except ValueError as exc:
        raise argparse.ArgumentTypeError(exc.args[0])

    # we do require the port to be provided.
    if port is None or hostname == '':
        raise argparse.ArgumentTypeError(
            'expected host in the form '
            '`host:port`. Got `{}` instead.'.format(host))

    return host
    def kms_request(self, kms_context):
        """Complete a KMS request.

        :Parameters:
          - `kms_context`: A :class:`MongoCryptKmsContext`.

        :Returns:
          None
        """
        endpoint = kms_context.endpoint
        message = kms_context.message
        provider = kms_context.kms_provider
        ctx = self.opts._kms_ssl_contexts.get(provider)
        if ctx is None:
            # Enable strict certificate verification, OCSP, match hostname, and
            # SNI using the system default CA certificates.
            ctx = get_ssl_context(
                None,  # certfile
                None,  # passphrase
                None,  # ca_certs
                None,  # crlfile
                False,  # allow_invalid_certificates
                False,  # allow_invalid_hostnames
                False,
            )  # disable_ocsp_endpoint_check
        opts = PoolOptions(
            connect_timeout=_KMS_CONNECT_TIMEOUT,
            socket_timeout=_KMS_CONNECT_TIMEOUT,
            ssl_context=ctx,
        )
        host, port = parse_host(endpoint, _HTTPS_PORT)
        conn = _configured_socket((host, port), opts)
        try:
            conn.sendall(message)
            while kms_context.bytes_needed > 0:
                data = conn.recv(kms_context.bytes_needed)
                if not data:
                    raise OSError("KMS connection closed")
                kms_context.feed(data)
        finally:
            conn.close()
示例#5
0
    def kms_request(self, kms_context):
        """Complete a KMS request.

        :Parameters:
          - `kms_context`: A :class:`MongoCryptKmsContext`.

        :Returns:
          None
        """
        endpoint = kms_context.endpoint
        message = kms_context.message
        host, port = parse_host(endpoint, _HTTPS_PORT)
        ctx = get_ssl_context(None, None, None, None, None, None, True, True)
        opts = PoolOptions(connect_timeout=_KMS_CONNECT_TIMEOUT,
                           socket_timeout=_KMS_CONNECT_TIMEOUT,
                           ssl_context=ctx)
        conn = _configured_socket((host, port), opts)
        try:
            conn.sendall(message)
            while kms_context.bytes_needed > 0:
                data = conn.recv(kms_context.bytes_needed)
                kms_context.feed(data)
        finally:
            conn.close()