-
Notifications
You must be signed in to change notification settings - Fork 19
/
tls_connections_test.go
262 lines (210 loc) · 9.24 KB
/
tls_connections_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
* Copyright (c) IBM Corporation 2019
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*/
package main
import (
"fmt"
"testing"
"time"
"github.com/ibm-messaging/mq-golang-jms20/mqjms"
"github.com/stretchr/testify/assert"
)
// This test file contains tests that demonstrate how to create TLS connections
// to a queue manager.
//
// Note that instructions on how to configure the queue manager TLS channels
// and the corresponding certificate keystores required by these tests can be
// found in the ./tls-samples/README.md file.
/*
* Test that we can connect successfully if we provide the correct anonymous
* ("ony way") TLS configuration.
*/
func TestAnonymousTLSConnection(t *testing.T) {
cf, err := mqjms.CreateConnectionFactoryFromDefaultJSONFiles()
assert.Nil(t, err)
// Override the connection settings to point to the anonymous ("one way") TLS
// channel (must be configured on the queue manager)
cf.ChannelName = "TLS.ANON.SVRCONN"
// Set the channel settings that tells the client what TLS configuration to use
// to connect to the queue manager.
cf.TLSCipherSpec = "ANY_TLS12"
cf.KeyRepository = "./tls-samples/anon-tls" // points to .kdb file
// Creates a connection to the queue manager, using defer to close it automatically
// at the end of the function (if it was created successfully)
context, errCtx := cf.CreateContext()
if context != nil {
defer context.Close()
}
if errCtx != nil && (errCtx.GetReason() == "MQRC_UNKNOWN_CHANNEL_NAME" ||
errCtx.GetReason() == "MQRC_CHANNEL_CONFIG_ERROR") {
// See ./tls-samples/README.md for details on how to configure the required channel.
fmt.Println("Skipping TestAnonymousTLSConnection as required channel is not defined.")
return
}
if errCtx != nil && errCtx.GetReason() == "MQRC_NOT_AUTHORIZED" {
// See ./tls-samples/README.md for details on how to configure the required channel.
fmt.Println("TLS connection was successfully negotiated, but client was blocked from connecting.")
// Allow test to fail below.
}
// This connection should have been created successfully.
assert.Nil(t, errCtx)
}
/*
* Test that we can connect successfully if we provide the correct anonymous
* ("ony way") TLS configuration.
*/
func TestTLSAlreadyInitialized(t *testing.T) {
cf, err := mqjms.CreateConnectionFactoryFromDefaultJSONFiles()
assert.Nil(t, err)
// Override the connection settings to point to the anonymous ("one way") TLS
// channel (must be configured on the queue manager)
cf.ChannelName = "TLS.ANON.SVRCONN"
// Set the channel settings that tells the client what TLS configuration to use
// to connect to the queue manager.
cf.TLSCipherSpec = "TLS_RSA_WITH_AES_128_CBC_SHA256" // ANY_TLS12
cf.KeyRepository = "./tls-samples/anon-tls" // points to .kdb file
// Creates a connection to the queue manager, using defer to close it automatically
// at the end of the function (if it was created successfully)
context, errCtx := cf.CreateContext()
if context != nil {
defer context.Close()
}
if errCtx != nil && (errCtx.GetReason() == "MQRC_UNKNOWN_CHANNEL_NAME" ||
errCtx.GetReason() == "MQRC_CHANNEL_CONFIG_ERROR") {
// See ./tls-samples/README.md for details on how to configure the required channel.
fmt.Println("Skipping TestTLSAlreadyInitialized as required channel is not defined.")
return
}
if errCtx != nil && errCtx.GetReason() == "MQRC_NOT_AUTHORIZED" {
// See ./tls-samples/README.md for details on how to configure the required channel.
fmt.Println("TLS connection was successfully negotiated, but client was blocked from connecting.")
// Allow test to fail below.
}
// This connection should have been created successfully.
assert.Nil(t, errCtx)
// Above this is just to create a standard TLS connection to the queue manager.
// Now try to set up a connection using different connection parameters
// We are aiming to trigger an MQRC_SSL_ALREADY_INITIALIZED response, which is a Warning that <also>
// returns a valid connection.
cf2, err2 := mqjms.CreateConnectionFactoryFromDefaultJSONFiles()
assert.Nil(t, err2)
cf2.ChannelName = "TLS.ANON.SVRCONN"
cf2.TLSCipherSpec = "ANY_TLS12"
cf2.TLSClientAuth = mqjms.TLSClientAuth_REQUIRED
cf2.CertificateLabel = "SampleClientA" // point to the client certificate
cf2.KeyRepository = "./tls-samples/mutual-tls" // points to .kdb file
context2, errCtx2 := cf2.CreateContext()
if context2 != nil {
defer context2.Close()
}
assert.NotNil(t, errCtx2)
if errCtx2 != nil {
assert.Equal(t, "MQRC_SSL_ALREADY_INITIALIZED", errCtx2.GetReason())
}
assert.NotNil(t, context2) // should ALSO get a connection back.
time.Sleep(time.Second)
}
/*
* Test that we can connect successfully if we provide the correct mutual
* TLS configuration.
*/
func TestMutualTLSConnection(t *testing.T) {
cf, err := mqjms.CreateConnectionFactoryFromDefaultJSONFiles()
assert.Nil(t, err)
// Override the connection settings to point to the mutual TLS
// channel (must be configured on the queue manager)
cf.ChannelName = "TLS.MUTUAL.SVRCONN"
// Set the channel settings that tells the client what TLS configuration to use
// to connect to the queue manager.
cf.TLSCipherSpec = "ANY_TLS12"
cf.TLSClientAuth = mqjms.TLSClientAuth_REQUIRED
cf.CertificateLabel = "SampleClientA" // point to the client certificate
cf.KeyRepository = "./tls-samples/mutual-tls" // points to .kdb file
// Creates a connection to the queue manager, using defer to close it automatically
// at the end of the function (if it was created successfully)
context, errCtx := cf.CreateContext()
if context != nil {
defer context.Close()
}
if errCtx != nil && (errCtx.GetReason() == "MQRC_UNKNOWN_CHANNEL_NAME" ||
errCtx.GetReason() == "MQRC_CHANNEL_CONFIG_ERROR") {
// See ./tls-samples/README.md for details on how to configure the required channel.
fmt.Println("Skipping TestMutualTLSConnection as required channel is not defined.")
return
}
if errCtx != nil && errCtx.GetReason() == "MQRC_NOT_AUTHORIZED" {
// See ./tls-samples/README.md for details on how to configure the required channel.
fmt.Println("TLS connection was successfully negotiated, but client was blocked from connecting.")
// Allow test to fail below.
}
// This connection should have been created successfully.
assert.Nil(t, errCtx)
// MQRC_SSL_INITIALIZATION_ERROR is most likely to indicate that the client certificate
// is not trusted by the queue manager - make sure you have run "REFRESH SECURITY TYPE(SSL)".
//
// Also see the queue manager error log for more details.
}
/*
* Test that we get the correct error message when connecting to a TLS channel
* without providing any TLS configuration.
*/
func TestNonTLSConnectionFails(t *testing.T) {
cf, err := mqjms.CreateConnectionFactoryFromDefaultJSONFiles()
assert.Nil(t, err)
// Override the connection settings to point to the anonymous ("one way") TLS
// channel (must be configured on the queue manager)
cf.ChannelName = "TLS.ANON.SVRCONN"
// Creates a connection to the queue manager, using defer to close it automatically
// at the end of the function (if it was created successfully)
context, errCtx := cf.CreateContext()
if context != nil {
defer context.Close()
}
// We expect this test to fail because we are trying to connect to a TLS channel
// without providing any TLS information.
assert.NotNil(t, errCtx)
if errCtx.GetReason() == "MQRC_UNKNOWN_CHANNEL_NAME" {
// See ./tls-samples/README.md for details on how to configure the required channel.
fmt.Println("Skipping TestNonTLSConnectionFails as required channel is not defined.")
return
}
assert.Equal(t, "MQRC_SSL_INITIALIZATION_ERROR", errCtx.GetReason())
assert.Equal(t, "2393", errCtx.GetErrorCode())
}
/*
* Test that we get the correct error message when connecting to a mutual TLS channel
* when we only provide anonymous TLS configuration.
*/
func TestAnonConfigToMutualTLSConnectionFails(t *testing.T) {
cf, err := mqjms.CreateConnectionFactoryFromDefaultJSONFiles()
assert.Nil(t, err)
// Override the connection settings to point to the anonymous ("one way") TLS
// channel (must be configured on the queue manager)
cf.ChannelName = "TLS.MUTUAL.SVRCONN"
// Set the channel settings that tells the client what TLS configuration to use
// to connect to the queue manager.
cf.TLSCipherSpec = "ANY_TLS12"
cf.KeyRepository = "./tls-samples/anon-tls" // Deliberately pointing to "wrong" config.
// Creates a connection to the queue manager, using defer to close it automatically
// at the end of the function (if it was created successfully)
context, errCtx := cf.CreateContext()
if context != nil {
defer context.Close()
}
// We expect this test to fail because we are trying to connect to a TLS channel
// without providing any TLS information.
assert.NotNil(t, errCtx)
if errCtx.GetReason() == "MQRC_UNKNOWN_CHANNEL_NAME" {
// See ./tls-samples/README.md for details on how to configure the required channel.
fmt.Println("Skipping TestAnonConfigToMutualTLSConnectionFails as required channel is not defined.")
return
}
assert.Equal(t, "MQRC_SSL_INITIALIZATION_ERROR", errCtx.GetReason())
assert.Equal(t, "2393", errCtx.GetErrorCode())
}