Skip to content

Commit e76b20a

Browse files
authored
Merge pull request #73 from matrober-uk/tls-already-initialized
Allow warning to be returned at same time as connection - #72
2 parents b349e76 + f28f6a7 commit e76b20a

File tree

2 files changed

+76
-3
lines changed

2 files changed

+76
-3
lines changed

mqjms/ConnectionFactoryImpl.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func (cf ConnectionFactoryImpl) CreateContextWithSessionMode(sessionMode int, mq
144144
// queue manager.
145145
qMgr, err := ibmmq.Connx(cf.QMName, cno)
146146

147-
if err == nil {
147+
if (qMgr != ibmmq.MQQueueManager{}) {
148148

149149
// Initialize the countInc value to 1 so that if CheckCount is enabled (>0)
150150
// then an error check will be made after the first message - to catch any
@@ -163,7 +163,9 @@ func (cf ConnectionFactoryImpl) CreateContextWithSessionMode(sessionMode int, mq
163163
sendCheckCountInc: countInc,
164164
}
165165

166-
} else {
166+
}
167+
168+
if err != nil {
167169

168170
// The underlying MQI call returned an error, so extract the relevant
169171
// details and pass it back to the caller as a JMSException

tls_connections_test.go

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ package main
1111

1212
import (
1313
"fmt"
14+
"testing"
15+
"time"
16+
1417
"github.com/ibm-messaging/mq-golang-jms20/mqjms"
1518
"github.com/stretchr/testify/assert"
16-
"testing"
1719
)
1820

1921
// This test file contains tests that demonstrate how to create TLS connections
@@ -66,6 +68,75 @@ func TestAnonymousTLSConnection(t *testing.T) {
6668

6769
}
6870

71+
/*
72+
* Test that we can connect successfully if we provide the correct anonymous
73+
* ("ony way") TLS configuration.
74+
*/
75+
func TestTLSAlreadyInitialized(t *testing.T) {
76+
77+
cf, err := mqjms.CreateConnectionFactoryFromDefaultJSONFiles()
78+
assert.Nil(t, err)
79+
80+
// Override the connection settings to point to the anonymous ("one way") TLS
81+
// channel (must be configured on the queue manager)
82+
cf.ChannelName = "TLS.ANON.SVRCONN"
83+
84+
// Set the channel settings that tells the client what TLS configuration to use
85+
// to connect to the queue manager.
86+
cf.TLSCipherSpec = "TLS_RSA_WITH_AES_128_CBC_SHA256" // ANY_TLS12
87+
cf.KeyRepository = "./tls-samples/anon-tls" // points to .kdb file
88+
89+
// Creates a connection to the queue manager, using defer to close it automatically
90+
// at the end of the function (if it was created successfully)
91+
context, errCtx := cf.CreateContext()
92+
if context != nil {
93+
defer context.Close()
94+
}
95+
96+
if errCtx != nil && (errCtx.GetReason() == "MQRC_UNKNOWN_CHANNEL_NAME" ||
97+
errCtx.GetReason() == "MQRC_CHANNEL_CONFIG_ERROR") {
98+
// See ./tls-samples/README.md for details on how to configure the required channel.
99+
fmt.Println("Skipping TestTLSAlreadyInitialized as required channel is not defined.")
100+
return
101+
}
102+
103+
if errCtx != nil && errCtx.GetReason() == "MQRC_NOT_AUTHORIZED" {
104+
// See ./tls-samples/README.md for details on how to configure the required channel.
105+
fmt.Println("TLS connection was successfully negotiated, but client was blocked from connecting.")
106+
// Allow test to fail below.
107+
}
108+
109+
// This connection should have been created successfully.
110+
assert.Nil(t, errCtx)
111+
112+
// Above this is just to create a standard TLS connection to the queue manager.
113+
114+
// Now try to set up a connection using different connection parameters
115+
// We are aiming to trigger an MQRC_SSL_ALREADY_INITIALIZED response, which is a Warning that <also>
116+
// returns a valid connection.
117+
cf2, err2 := mqjms.CreateConnectionFactoryFromDefaultJSONFiles()
118+
assert.Nil(t, err2)
119+
cf2.ChannelName = "TLS.ANON.SVRCONN"
120+
cf2.TLSCipherSpec = "ANY_TLS12"
121+
cf2.TLSClientAuth = mqjms.TLSClientAuth_REQUIRED
122+
cf2.CertificateLabel = "SampleClientA" // point to the client certificate
123+
cf2.KeyRepository = "./tls-samples/mutual-tls" // points to .kdb file
124+
125+
context2, errCtx2 := cf2.CreateContext()
126+
if context2 != nil {
127+
defer context2.Close()
128+
}
129+
130+
assert.NotNil(t, errCtx2)
131+
if errCtx2 != nil {
132+
assert.Equal(t, "MQRC_SSL_ALREADY_INITIALIZED", errCtx2.GetReason())
133+
}
134+
135+
assert.NotNil(t, context2) // should ALSO get a connection back.
136+
time.Sleep(time.Second)
137+
138+
}
139+
69140
/*
70141
* Test that we can connect successfully if we provide the correct mutual
71142
* TLS configuration.

0 commit comments

Comments
 (0)