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()
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 == 0 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 == 0 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 == 0 else "FAILED"))
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_async() HowToDirectMessagingHealthCheckSampler.PUBLISHER_READINESS_SET_COUNTER += 1 direct_publish_service.set_publisher_readiness_listener( listener=PublisherReadinessListenerImpl()) for e in range(message_count): direct_publish_service.publish(destination=destination, message=message) print(f"{e} message(s) sent") except PublisherOverflowError: PublisherOverflowError("Queue maximum limit is reached") finally: util = SamplerUtil() util.publisher_terminate(direct_publish_service)
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) HowToConnectWithTls.tls_downgradable_to_plain_text(props) HowToConnectWithTls.tls_with_excluded_protocols( props, excluded_protocol=TLS.SecureProtocols.SSLv3) HowToConnectWithTls.tls_with_cipher_suites(props, cipher_suite=cipher_suite)
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()) return message_service.connect() 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 == 0 else "FAILED")) result = HowToConnectMessagingService().create_from_properties_async( broker_props) SamplerUtil.print_sampler_result( Template("Message Service[ASYNC] connect $status").substitute( status="SUCCESS" if result == 0 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 result == 0 else "FAILED"))
""" 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 solace_sampler.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)
SamplerBoot().broker_properties()).build() messaging_service.connect() return messaging_service @staticmethod def run_samplers(): """method to run all the samplers""" HowToConnectMessagingService().run() HowToConfigureAuthentication.run() HowToConnectWithDifferentStrategy().run() HowToConnectWithTls.run() HowToDirectPublishMessage().run() HowToDirectMessagingHealthCheckSampler().run() HowToDirectPublishWithBackPressureSampler().run() HowToDirectConsumeBusinessObjectSampler().publish_and_subscribe() HowToDirectConsumeSampler.run() HowToDirectPublishWithBackPressureSampler().run() HowToAccessApiMetrics().run() HowToDirectConsumeShareNameSampler().publish_and_subscribe() HowToConnectMessagingServiceWithReConnectionStrategy().run() 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()