Example #1
0
    def configure_client_certificate_authentication_customized_settings(
            props, key_file, key_store_password, key_store_url):
        """
        For a client to use a client certificate authentication scheme, the host event broker must be
        properly configured for TLS/SSL connections, and Client Certificate Verification must be
        enabled for the particular Message VPN that the client is connecting to. On client side client
        certificate needs to be present in a keystore file.

        Args:
            props:
            key_store_password: password for the key store
            key_store_url: url to the key store file
            key_file: key file

        Returns:
            configured and connected instance of MessagingService ready to be used for messaging tasks
        """
        try:
            transport_security = TLS.create() \
                .with_certificate_validation(True, validate_server_name=False,
                                             trust_store_file_path=SamplerUtil.get_trusted_store_dir())

            messaging_service = MessagingService.builder() \
                .from_properties(props) \
                .with_transport_security_strategy(transport_security) \
                .with_authentication_strategy(ClientCertificateAuthentication.of(certificate_file=key_store_url,
                                                                                 key_file=key_file,
                                                                                 key_password=key_store_password)) \
                .build(SamplerUtil.get_new_application_id())
            return messaging_service.connect()
        except Exception as exception:
            print(exception)
        finally:
            messaging_service.disconnect()
Example #2
0
    def run():
        """method to run all the other authentication configuration methods"""
        props_unsecured = boot.broker_properties()
        props_secured = boot.secured_broker_properties()
        props_compressed = boot.compressed_broker_properties()
        user_name = props_unsecured[
            authentication_properties.SCHEME_BASIC_USER_NAME]
        password = props_unsecured[
            authentication_properties.SCHEME_BASIC_PASSWORD]
        key_store_url = SamplerUtil.get_valid_client_certificate()
        key_store_password = SolaceConstants.KEY_STORE_PASSWORD
        key_file = SamplerUtil.get_valid_client_key()

        result = HowToConfigureAuthentication.configure_basic_auth_credentials(
            props_unsecured, user_name, password)
        SamplerUtil.print_sampler_result(
            Template("Message Service[SYNC] connect with AUTH strategy $status"
                     ).substitute(status="SUCCESS" if result else "FAILED"))

        result = HowToConfigureAuthentication \
            .configure_client_certificate_authentication_customized_settings(props_secured, key_file,
                                                                             key_store_password, key_store_url)
        SamplerUtil.print_sampler_result(
            Template("Message Service[SYNC] connect with TLS strategy $status"
                     ).substitute(status="SUCCESS" if result else "FAILED"))

        result = HowToConfigureAuthentication.basic_compression(
            props_compressed, compression_range=1)
        SamplerUtil.print_sampler_result(
            Template("Message Service[SYNC] connect with COMPRESSION $status").
            substitute(status="SUCCESS" if result else "FAILED"))
    def run():
        """method to run all the methods related to tls configuration """
        props = broker_properties_value
        trust_store_path_name = SamplerUtil.get_trusted_store_dir()
        cipher_suite = "ECDHE-RSA-AES256-GCM-SHA384"

        HowToConnectWithTls.\
            tls_with_certificate_validation_and_trusted_store_settings(props, ignore_expiration=True,
                                                                       trust_store_path=trust_store_path_name)
        try:
            HowToConnectWithTls.tls_downgradable_to_plain_text(
                props, trust_store_path=trust_store_path_name)
        except PubSubPlusClientError as err:
            # tls downgrade must be enabled on the connected broker:
            #  - message vpn must have the TLS downgrade option enabled see, https://docs.solace.com/Configuring-and-Managing/Enabling-TLS-Downgrade.htm
            #  - the client username provided must have profile with this enabled see, https://docs.solace.com/Configuring-and-Managing/Configuring-Client-Profiles.htm#Configur3
            print(
                f'Failed to connect with tls downgrade to plain text, is this enabled on the broker? Error: {err}'
            )
        HowToConnectWithTls.tls_with_excluded_protocols(
            props,
            excluded_protocol=TLS.SecureProtocols.SSLv3,
            trust_store_path=trust_store_path_name)

        HowToConnectWithTls.tls_with_cipher_suites(
            props,
            cipher_suite=cipher_suite,
            trust_store_path=trust_store_path_name)
    def connect_parametrized_retry(retries, retry_interval):
        """
             creates a new instance of message service, that is used to configure
             direct message instances from config

             Returns: new connection for Direct messaging
             Raises:
                PubSubPlusClientError
         """
        try:
            message_service = MessagingService.builder() \
                .from_properties(boot.broker_properties()) \
                .with_reconnection_retry_strategy(RetryStrategy.parametrized_retry(retries, retry_interval)) \
                .build(SamplerUtil.get_new_application_id())
            message_service.connect()
            return message_service
        except PubSubPlusClientError as exception:
            print(f'Exception: {exception}')
            raise exception
        finally:
            message_service.disconnect()
    def run():
        """this method is used to run the connect messaging service sampler"""
        broker_props = boot.broker_properties()

        result = HowToConnectMessagingService().create_from_properties(
            broker_props)
        SamplerUtil.print_sampler_result(
            Template("Message Service[SYNC] connect $status").substitute(
                status="SUCCESS" if result else "FAILED"))

        result = HowToConnectMessagingService().create_from_properties_async(
            broker_props)
        SamplerUtil.print_sampler_result(
            Template("Message Service[ASYNC] connect $status").substitute(
                status="SUCCESS" if isinstance(result, MessagingService
                                               ) else "FAILED"))

        result = HowToConnectMessagingService() \
            .create_from_properties_async_application_id(broker_props, SamplerUtil.get_new_application_id())
        SamplerUtil.print_sampler_result(
            Template(
                "Message Service[ASYNC] connect with applicationId $status").
            substitute(status="SUCCESS" if isinstance(result, MessagingService
                                                      ) else "FAILED"))
Example #6
0
        messaging_service.connect()
        return messaging_service

    @staticmethod
    def run_samplers():
        """method to run all the samplers"""
        HowToUseShareNameWithRequestReplyPattern.run()
        HowToUseRequestReplyPattern.run()
        HowToWorkWithSolaceSDTTypesAndMessages().run()
        HowToConnectMessagingService().run()
        HowToConfigureAuthentication.run()
        HowToConnectWithDifferentStrategy().run()
        HowToConnectWithTls.run()
        HowToDirectPublishMessage().run()
        HowToDirectMessagingHealthCheckSampler().run()
        HowToDirectPublishWithBackPressureSampler().run()
        HowToDirectConsumeBusinessObjectSampler().publish_and_subscribe()
        HowToAccessApiMetrics().run()
        HowToDirectConsumeSampler.run()
        HowToDirectConsumeShareNameSampler().publish_and_subscribe()
        #HowToSetCoreApiLogLevel.run()


if __name__ == '__main__':
    boot = SamplerBoot()
    broker_props = boot.broker_properties()
    semp_config = boot.read_semp_configuration()
    SamplerUtil.cert_feature(semp_props=semp_config, broker_props=broker_props)

    SamplerMaster.run_samplers()
""" Run this file to publish messages using direct message publisher with back pressure scenarios"""
from typing import TypeVar

from solace.messaging.errors.pubsubplus_client_error import PublisherOverflowError
from solace.messaging.messaging_service import MessagingService
from solace.messaging.resources.topic import Topic
from howtos.sampler_boot import SamplerBoot, SolaceConstants, SamplerUtil

X = TypeVar('X')
constants = SolaceConstants
boot = SamplerBoot()
util = SamplerUtil()


class HowToDirectPublishWithBackPressureSampler:
    """
    class to show how to create a messaging service
    """
    @staticmethod
    def direct_message_publish_on_backpressure_reject(
            messaging_service: MessagingService, destination, message,
            buffer_capacity, message_count):
        """ to publish str or byte array type message using back pressure"""
        try:
            direct_publish_service = messaging_service.create_direct_message_publisher_builder() \
                .on_back_pressure_reject(buffer_capacity=buffer_capacity) \
                .build()
            direct_publish_service.start()
            for e in range(message_count):
                direct_publish_service.publish(destination=destination,
                                               message=message)