Пример #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 test_store_key(self):
        credential = CredentialParams(self)
        credential.set_store_key(None)
        assert None == credential.get_store_key()

        credential.set_store_key("Store key")
        assert "Store key" == credential.get_store_key()
        assert True == credential.use_credential_store()
Пример #3
0
    def test_access_key(self):
        credential = CredentialParams(self)
        credential.set_access_key(None)
        assert None == credential.get_access_key()

        credential.set_access_key("key")
        assert "key" == credential.get_access_key()
Пример #4
0
    def test_password(self):
        credential = CredentialParams(self)
        credential.set_password(None)
        assert None == credential.get_password()

        credential.set_password("qwerty")
        assert "qwerty" == credential.get_password()
Пример #5
0
    def test_username(self):
        credential = CredentialParams(self)
        credential.set_username(None)
        assert None == credential.get_username()

        credential.set_username("Kate Negrienko")
        assert "Kate Negrienko" == credential.get_username()
Пример #6
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
    def test_lookup_and_store(self):
        config = ConfigParams.from_tuples('key1.user', 'user1', 'key1.pass',
                                          'pass1', 'key2.user', 'user2',
                                          'key2.pass', 'pass2')

        credential_store = MemoryCredentialStore()
        credential_store.read_credentials(config)

        cred1 = credential_store.lookup('123', 'key1')
        cred2 = credential_store.lookup('123', 'key2')

        assert cred1.get_username() == 'user1'
        assert cred1.get_password() == 'pass1'
        assert cred2.get_username() == 'user2'
        assert cred2.get_password() == 'pass2'

        cred_config = CredentialParams.from_tuples('user', 'user3', 'pass',
                                                   'pass3', 'access_id', '123')

        credential_store.store(None, 'key3', cred_config)

        cred3 = credential_store.lookup('123', 'key3')

        assert cred3.get_username() == 'user3'
        assert cred3.get_password() == 'pass3'
        assert cred3.get_access_id() == '123'