-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathconnection_mutual_tls_integration_test.go
192 lines (156 loc) · 4.55 KB
/
connection_mutual_tls_integration_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
package yagnats
import (
"crypto/x509"
"os/exec"
"errors"
. "gopkg.in/check.v1"
)
type MutualTLSSuite struct {
Client *Client
NatsConn NATSConn
NatsCmd *exec.Cmd
}
var _ = Suite(&MutualTLSSuite{})
func (t *MutualTLSSuite) SetUpSuite(c *C) {
t.NatsCmd = startNatsMutualTLS(4556)
}
func (t *MutualTLSSuite) TearDownSuite(c *C) {
stopCmd(t.NatsCmd)
}
func (t *MutualTLSSuite) TestNewMutualTLSConnection(c *C) {
client := NewClient()
roots := x509.NewCertPool()
ok := roots.AppendCertsFromPEM(ValidCA)
c.Assert(ok, Equals, true)
err := client.Connect(&ConnectionInfo{Addr: "127.0.0.1:4556",
Username: "nats",
Password: "nats",
TLSInfo: &ConnectionTLSInfo{
CertPool: roots,
ClientCert: &ValidClientCert,
},
})
c.Assert(err, IsNil)
t.Client = client
pingSuccess := client.Ping()
c.Assert(pingSuccess, Equals, true)
}
func (t *MutualTLSSuite) TestNewMutualTLSConnectionWithWrongCA(c *C) {
client := NewClient()
roots := x509.NewCertPool()
ok := roots.AppendCertsFromPEM(InvalidCA)
c.Assert(ok, Equals, true)
err := client.Connect(&ConnectionInfo{Addr: "127.0.0.1:4556",
Username: "nats",
Password: "nats",
TLSInfo: &ConnectionTLSInfo{
CertPool: roots,
ClientCert: &ValidClientCert,
},
})
c.Assert(err, NotNil)
c.Assert(err.Error(), Matches, "^x509: certificate signed by unknown authority.*$")
}
func (t *MutualTLSSuite) TestNewMutualTLSConnectionWithEmptyCertPool(c *C) {
client := NewClient()
err := client.Connect(&ConnectionInfo{Addr: "127.0.0.1:4556",
Username: "nats",
Password: "nats",
TLSInfo: &ConnectionTLSInfo{
CertPool: x509.NewCertPool(),
ClientCert: &ValidClientCert,
},
})
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "x509: certificate signed by unknown authority")
}
func (t *MutualTLSSuite) TestNewMutualTLSConnectionWithInvalidClientCert(c *C) {
client := NewClient()
roots := x509.NewCertPool()
ok := roots.AppendCertsFromPEM(ValidCA)
c.Assert(ok, Equals, true)
err := client.Connect(&ConnectionInfo{Addr: "127.0.0.1:4556",
Username: "nats",
Password: "nats",
TLSInfo: &ConnectionTLSInfo{
CertPool: roots,
ClientCert: &InvalidClientCert,
},
})
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "remote error: tls: bad certificate")
pingSuccess := client.Ping()
c.Assert(pingSuccess, Equals, false)
}
func (t *MutualTLSSuite) TestNewMutualTLSConnectionWithNoClientCert(c *C) {
client := NewClient()
roots := x509.NewCertPool()
ok := roots.AppendCertsFromPEM(ValidCA)
c.Assert(ok, Equals, true)
err := client.Connect(&ConnectionInfo{Addr: "127.0.0.1:4556",
Username: "nats",
Password: "nats",
TLSInfo: &ConnectionTLSInfo{
CertPool: roots,
},
})
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "remote error: tls: bad certificate")
pingSuccess := client.Ping()
c.Assert(pingSuccess, Equals, false)
}
func (t *MutualTLSSuite) TestNewMutualTLSConnectionWithVerifyPeerCertificateCallbackSuccess(c *C) {
client := NewClient()
roots := x509.NewCertPool()
ok := roots.AppendCertsFromPEM(ValidCA)
c.Assert(ok, Equals, true)
err := client.Connect(&ConnectionInfo{Addr: "127.0.0.1:4556",
Username: "nats",
Password: "nats",
TLSInfo: &ConnectionTLSInfo{
CertPool: roots,
ClientCert: &ValidClientCert,
VerifyPeerCertificate: func(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error {
for _, chain := range verifiedChains {
org := chain[len(chain)-1].Subject.Organization
if len(org) >= 1 && org[0] == "Cloud Foundry" {
return nil
}
}
return errors.New("Unexpected organization.")
},
},
})
c.Assert(err, IsNil)
t.Client = client
pingSuccess := client.Ping()
c.Assert(pingSuccess, Equals, true)
}
func (t *MutualTLSSuite) TestNewMutualTLSConnectionWithVerifyPeerCertificateCallbackError(c *C) {
client := NewClient()
roots := x509.NewCertPool()
ok := roots.AppendCertsFromPEM(ValidCA)
c.Assert(ok, Equals, true)
err := client.Connect(&ConnectionInfo{Addr: "127.0.0.1:4556",
Username: "nats",
Password: "nats",
TLSInfo: &ConnectionTLSInfo{
CertPool: roots,
ClientCert: &ValidClientCert,
VerifyPeerCertificate: func(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error {
for _, chain := range verifiedChains {
org := chain[len(chain)-1].Subject.Organization
if len(org) >= 1 && org[0] == "Yagnats" {
return nil
}
}
return errors.New("Unexpected organization.")
},
},
})
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "Unexpected organization.")
t.Client = client
pingSuccess := client.Ping()
c.Assert(pingSuccess, Equals, false)
}