예제 #1
0
def connect():
    global qmgr
    conn_info = '%s(%s)' % (args.host, args.port)
    if (not args.use_client_auth and not args.use_ssl):
        qmgr = pymqi.connect(args.queue_manager, args.channel, conn_info)
    elif (args.use_client_auth and not args.use_ssl):
        qmgr = pymqi.connect(args.queue_manager, args.channel, conn_info,
                             args.username, args.password)
    elif (args.use_ssl):
        cd = pymqi.CD()
        cd.ChannelName = bytes(args.channel, encoding='utf8')
        cd.ConnectionName = bytes(conn_info, encoding='utf8')
        cd.ChannelType = pymqi.CMQC.MQCHT_CLNTCONN
        cd.TransportType = pymqi.CMQC.MQXPT_TCP
        cd.SSLCipherSpec = bytes(args.cipherspec, encoding='utf8')
        sco = pymqi.SCO()
        sco.KeyRepository = bytes(args.server_cert, encoding='utf8')
        qmgr = pymqi.QueueManager(None)
        if (not args.use_client_auth):
            qmgr.connect_with_options(args.queue_manager, cd=cd, sco=sco)
        else:
            qmgr.connect_with_options(args.queue_manager,
                                      cd=cd,
                                      sco=sco,
                                      user=bytes(args.username,
                                                 encoding='utf8'),
                                      password=bytes(args.password,
                                                     encoding='utf8'))
    print("connection succesful")
예제 #2
0
def connect():
    logger.info('Establising Connection with MQ Server')
    try:
        cd = pymqi.CD()
        cd.ChannelName = MQDetails['CHANNEL']
        cd.ConnectionName = conn_info
        cd.ChannelType = pymqi.CMQC.MQCHT_CLNTCONN
        cd.TransportType = pymqi.CMQC.MQXPT_TCP

        # Create an empty SCO object, and optionally set TLS settings
        # if a cipher is set in the envrionment variables.
        sco = pymqi.SCO()
        if MQDetails['CIPHER']:
            cd.SSLCipherSpec = MQDetails['CIPHER']
            sco.KeyRepository = MQDetails['KEY_REPOSITORY']

        #options = pymqi.CMQC.MQPMO_NO_SYNCPOINT | pymqi.CMQC.MQPMO_NEW_MSG_ID | pymqi.CMQC.MQPMO_NEW_CORREL_ID
        options = pymqi.CMQC.MQPMO_NEW_CORREL_ID

        qmgr = pymqi.QueueManager(None)
        qmgr.connect_with_options(MQDetails['QMGR'],
                                  user=credentials['USER'],
                                  password=credentials['PASSWORD'],
                                  opts=options,
                                  cd=cd,
                                  sco=sco)
        return qmgr
    except pymqi.MQMIError as e:
        logger.error("Error connecting")
        logger.error(e)
        return None
예제 #3
0
파일: test_tls.py 프로젝트: kentyu06/pymqi
    def test_connection_with_tls(self):
        """Test connection to QueueManager with TLS."""
        qmgr = pymqi.QueueManager(None)
        conn_info = pymqi.ensure_bytes('{0}({1})'.format(self.host, self.port))

        cd = pymqi.CD(
            Version=pymqi.CMQXC.MQCD_VERSION_7,  # pylint: disable=C0103
            ChannelName=self.tls_channel_name,
            ConnectionName=conn_info,
            SSLCipherSpec=self.cypher_spec)

        sco = pymqi.SCO(Version=pymqi.CMQC.MQSCO_VERSION_5,
                        KeyRepository=os.path.join(
                            self.key_repo_location_client,
                            self.certificate_label_client),
                        CertificateLabel=self.certificate_label_client)

        opts = pymqi.CMQC.MQCNO_HANDLE_SHARE_NO_BLOCK

        qmgr.connectWithOptions(self.queue_manager,
                                cd,
                                sco,
                                opts=opts,
                                user=self.user,
                                password=self.password)
        is_connected = qmgr.is_connected
        if is_connected:
            qmgr.disconnect()

        self.assertTrue(is_connected)
예제 #4
0
def get_ssl_connection(config):
    # type: (IBMMQConfig) -> QueueManager
    """
    Get the connection with SSL
    """
    cd = _get_channel_definition(config)
    cd.SSLCipherSpec = config.ssl_cipher_spec

    sco = pymqi.SCO()
    sco.KeyRepository = config.ssl_key_repository_location

    queue_manager = pymqi.QueueManager(None)
    queue_manager.connect_with_options(config.queue_manager_name, cd, sco)

    return queue_manager
예제 #5
0
def get_ssl_connection(config):
    """
    Get the connection with SSL
    """
    cd = pymqi.CD()
    cd.ChannelName = config.channel
    cd.ConnectionName = config.host_and_port
    cd.ChannelType = pymqi.CMQC.MQCHT_CLNTCONN
    cd.TransportType = pymqi.CMQC.MQXPT_TCP
    cd.SSLCipherSpec = config.ssl_cipher_spec

    sco = pymqi.SCO()
    sco.KeyRepository = config.ssl_key_repository_location

    queue_manager = pymqi.QueueManager(None)
    queue_manager.connect_with_options(config.queue_manager, cd, sco)

    return queue_manager
예제 #6
0
def connect():
    logger.info('Establising Connection with MQ Server')
    try:
        cd = None
        if not EnvStore.ccdtCheck():
            logger.info(
                'CCDT URL export is not set, will be using json envrionment client connections settings'
            )

            cd = pymqi.CD(Version=pymqi.CMQXC.MQCD_VERSION_11)
            cd.ChannelName = MQDetails[EnvStore.CHANNEL]
            cd.ConnectionName = conn_info
            cd.ChannelType = pymqi.CMQC.MQCHT_CLNTCONN
            cd.TransportType = pymqi.CMQC.MQXPT_TCP

            logger.info('Checking Cypher details')
            # If a cipher is set then set the TLS settings
            if MQDetails[EnvStore.CIPHER]:
                logger.info('Making use of Cypher details')
                cd.SSLCipherSpec = MQDetails[EnvStore.CIPHER]

        # Key repository is not specified in CCDT so look in envrionment settings
        # Create an empty SCO object
        sco = pymqi.SCO()
        if MQDetails[EnvStore.KEY_REPOSITORY]:
            logger.info('Setting Key repository')
            sco.KeyRepository = MQDetails[EnvStore.KEY_REPOSITORY]

        #options = pymqi.CMQC.MQPMO_NO_SYNCPOINT | pymqi.CMQC.MQPMO_NEW_MSG_ID | pymqi.CMQC.MQPMO_NEW_CORREL_ID
        options = pymqi.CMQC.MQPMO_NEW_CORREL_ID

        qmgr = pymqi.QueueManager(None)
        qmgr.connect_with_options(MQDetails[EnvStore.QMGR],
                                  user=credentials[EnvStore.USER],
                                  password=credentials[EnvStore.PASSWORD],
                                  opts=options,
                                  cd=cd,
                                  sco=sco)
        return qmgr

    except pymqi.MQMIError as e:
        logger.error("Error connecting")
        logger.error(e)
        return None
예제 #7
0
    def initialize_config(self):
        tags = [f'mq_server:{self.config.mq_server}', f'mq_port:{self.config.mq_port}', *self.config.tags]
        self._tags = tuple(tags)

        cd = pymqi.CD()
        cd.ChannelName = self.config.channel.encode('utf-8')
        cd.ConnectionName = f'{self.config.mq_server}({self.config.mq_port})'.encode('utf-8')
        cd.ChannelType = pymqi.CMQC.MQCHT_CLNTCONN
        cd.TransportType = pymqi.CMQC.MQXPT_TCP
        cd.Version = getattr(pymqi.CMQC, f'MQCD_VERSION_{self.config.mqcd_version}')

        self._connection_options = {'cd': cd, 'user': self.config.mq_user, 'password': self.config.mq_password}

        if self.config.tls_auth:
            cd.SSLCipherSpec = self.config.tls_cipher_spec.encode('utf-8')

            sco = pymqi.SCO()
            sco.KeyRepository = self.config.tls_key_repository_location.encode('utf-8')
            if self.config.tls_certificate_label:
                sco.CertificateLabel = self.config.tls_certificate_label.encode('utf-8')

            self._connection_options['sco'] = sco
예제 #8
0
def get_ssl_connection(config):
    # type: (IBMMQConfig) -> pymqi.QueueManager
    """
    Get the connection with SSL
    """
    cd = _get_channel_definition(config)
    cd.SSLCipherSpec = pymqi.ensure_bytes(config.ssl_cipher_spec)

    sco = pymqi.SCO()
    sco.KeyRepository = pymqi.ensure_bytes(config.ssl_key_repository_location)

    if config.ssl_certificate_label:
        sco.CertificateLabel = pymqi.ensure_bytes(config.ssl_certificate_label)

    connect_options = {}
    if config.username and config.password:
        connect_options.update({
            'user': config.username,
            'password': config.password,
        })

    log.debug(
        "Create SSL connection with ConnectionName=%s, ChannelName=%s, Version=%s, SSLCipherSpec=%s, "
        "KeyRepository=%s, CertificateLabel=%s, user=%s",
        cd.ConnectionName,
        cd.ChannelName,
        cd.Version,
        cd.SSLCipherSpec,
        sco.KeyRepository,
        sco.CertificateLabel,
        connect_options.get('user'),
    )
    queue_manager = pymqi.QueueManager(None)
    queue_manager.connect_with_options(config.queue_manager_name, cd, sco,
                                       **connect_options)
    return queue_manager
예제 #9
0
host = "192.168.1.135"
port = "1434"
queue_name = "TEST.1"
conn_info = "%s(%s)" % (host, port)
ssl_cipher_spec = "TLS_RSA_WITH_AES_256_CBC_SHA"
key_repo_location = "/var/mqm/ssl-db/client/KeyringClient"
message = "Hello from Python!"

cd = pymqi.CD()
cd.ChannelName = channel
cd.ConnectionName = conn_info
cd.ChannelType = pymqi.CMQC.MQCHT_CLNTCONN
cd.TransportType = pymqi.CMQC.MQXPT_TCP
cd.SSLCipherSpec = ssl_cipher_spec

sco = pymqi.SCO()
sco.KeyRepository = key_repo_location

qmgr = pymqi.QueueManager(None)
qmgr.connect_with_options(queue_manager, cd, sco)

put_queue = pymqi.Queue(qmgr, queue_name)
put_queue.put(message)

get_queue = pymqi.Queue(qmgr, queue_name)
logging.info("Here's the message again: [%s]" % get_queue.get())

put_queue.close()
get_queue.close()
qmgr.disconnect()