Пример #1
0
    def __validate_connection(self,
                              correlation_id: Optional[str],
                              connection: ConnectionParams,
                              credential: CredentialParams = None):

        # Sometimes when we use https we are on an internal network and do not want to have to deal with security.
        # When we need a https connection and we don't want to pass credentials, flag is 'credential.internal_network',
        # this flag just has to be present and non null for this functionality to work.
        if connection is None:
            raise ConfigException(correlation_id, "NO_CONNECTION",
                                  "HTTP connection is not set")
        uri = connection.get_as_string('uri')

        if uri is not None:
            return None

        protocol = connection.get_protocol_with_default("http")
        if protocol != "http" and 'https' != protocol:
            raise ConfigException(correlation_id,
                                  "WRONG_PROTOCOL",
                                  "Protocol is not supported by REST connection") \
                .with_details("protocol", protocol)

        host = connection.get_as_string('host')
        if host is None:
            raise ConfigException(correlation_id, "NO_HOST",
                                  "Connection host is not set")

        port = connection.get_as_integer('port')
        if port == 0:
            raise ConfigException(correlation_id, "NO_PORT",
                                  "Connection port is not set")

        # Check HTTPS credentials
        if protocol == 'https':
            # Check for credential
            if credential is None or credential.length() == 0:
                raise ConfigException(
                    correlation_id, 'NO_CREDENTIAL',
                    'SSL certificates are not configured for HTTPS protocol')
            else:
                # Sometimes when we use https we are on an internal network and do not want to have to deal with
                # security. When we need a https connection and we don't want to pass credentials,
                # flag is 'credential.internal_network', this flag just has to be present and non null for this
                # functionality to work.
                if credential.get_as_nullable_string(
                        'internal_network') is None:
                    if credential.get_as_nullable_string(
                            'ssl_key_file') is None:
                        raise ConfigException(
                            correlation_id, 'NO_SSL_KEY_FILE',
                            'SSL key file is not configured in credentials')
                    elif credential.get_as_nullable_string(
                            'ssl_crt_file') is None:
                        raise ConfigException(
                            correlation_id, 'NO_SSL_CRT_FILE',
                            'SSL crt file is not configured in credentials')
        return None
Пример #2
0
    def __compose_connection(
            self,
            connections: List[ConnectionParams],
            credential: CredentialParams = None) -> ConfigParams:
        connection = ConfigParams.merge_configs(*connections)

        uri = connection.get_as_string('uri')

        if uri is None or uri == "":

            protocol = connection.get_as_string_with_default('protocol', "uri")
            host = connection.get_as_string('host')
            port = connection.get_as_integer('port')

            uri = protocol + "://" + host if host else None
            if port != 0:
                uri = uri + ":" + str(port)
            connection.set_as_object('uri', uri)

        else:
            address = urlparse(uri)

            connection.set_as_object('protocol', address.scheme)
            connection.set_as_object('host', address.hostname)
            connection.set_as_object('port', address.port)

        if connection.get_as_string('protocol') == 'https':
            if credential.get_as_nullable_string('internal_network') is None:
                connection = connection.override(credential)

        return connection