-
Notifications
You must be signed in to change notification settings - Fork 162
/
grpc_test.go
315 lines (251 loc) · 9.25 KB
/
grpc_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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package backends
import (
"context"
"crypto/tls"
"crypto/x509"
"io/ioutil"
"net"
"testing"
"github.com/golang/protobuf/ptypes/empty"
gs "github.com/iegomez/mosquitto-go-auth/grpc"
log "github.com/sirupsen/logrus"
. "github.com/smartystreets/goconvey/convey"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
const (
grpcUsername string = "test_user"
grpcSuperuser string = "superuser"
grpcPassword string = "test_password"
grpcTopic string = "test/topic"
grpcAcc int32 = 1
grpcClientId string = "test_client"
)
type AuthServiceAPI struct{}
func NewAuthServiceAPI() *AuthServiceAPI {
return &AuthServiceAPI{}
}
func (a *AuthServiceAPI) GetUser(ctx context.Context, req *gs.GetUserRequest) (*gs.AuthResponse, error) {
if req.Username == grpcUsername && req.Password == grpcPassword {
return &gs.AuthResponse{
Ok: true,
}, nil
}
return &gs.AuthResponse{
Ok: false,
}, nil
}
func (a *AuthServiceAPI) GetSuperuser(ctx context.Context, req *gs.GetSuperuserRequest) (*gs.AuthResponse, error) {
if req.Username == grpcSuperuser {
return &gs.AuthResponse{
Ok: true,
}, nil
}
return &gs.AuthResponse{
Ok: false,
}, nil
}
func (a *AuthServiceAPI) CheckAcl(ctx context.Context, req *gs.CheckAclRequest) (*gs.AuthResponse, error) {
if req.Username == grpcUsername && req.Topic == grpcTopic && req.Clientid == grpcClientId && req.Acc == grpcAcc {
return &gs.AuthResponse{
Ok: true,
}, nil
}
return &gs.AuthResponse{
Ok: false,
}, nil
}
func (a *AuthServiceAPI) GetName(ctx context.Context, req *empty.Empty) (*gs.NameResponse, error) {
return &gs.NameResponse{
Name: "MyGRPCBackend",
}, nil
}
func (a *AuthServiceAPI) Halt(ctx context.Context, req *empty.Empty) (*empty.Empty, error) {
return &empty.Empty{}, nil
}
func TestGRPC(t *testing.T) {
Convey("given a mock grpc server", t, func(c C) {
grpcServer := grpc.NewServer()
gs.RegisterAuthServiceServer(grpcServer, NewAuthServiceAPI())
lis, err := net.Listen("tcp", ":3123")
So(err, ShouldBeNil)
go grpcServer.Serve(lis)
defer grpcServer.Stop()
authOpts := make(map[string]string)
authOpts["grpc_host"] = "localhost"
authOpts["grpc_port"] = "3123"
authOpts["grpc_dial_timeout_ms"] = "100"
Convey("given wrong host", func(c C) {
wrongOpts := make(map[string]string)
wrongOpts["grpc_host"] = "localhost"
wrongOpts["grpc_port"] = "1111"
Convey("when grpc_fail_on_dial_error is set to true, it should return an error", func(c C) {
wrongOpts["grpc_fail_on_dial_error"] = "true"
_, err := NewGRPC(wrongOpts, log.DebugLevel)
c.So(err, ShouldNotBeNil)
})
Convey("when grpc_fail_on_dial_error is not set to true, it should not return an error", func(c C) {
wrongOpts["grpc_fail_on_dial_error"] = "false"
g, err := NewGRPC(wrongOpts, log.DebugLevel)
c.So(err, ShouldBeNil)
Convey("but it should return an error on any user or acl check", func(c C) {
auth, err := g.GetUser(grpcUsername, grpcPassword, grpcClientId)
So(err, ShouldNotBeNil)
c.So(auth, ShouldBeFalse)
name := g.GetName()
So(name, ShouldEqual, "gRPC")
})
Convey("it should work after the service comes back up", func(c C) {
lis, err := net.Listen("tcp", ":1111")
So(err, ShouldBeNil)
go grpcServer.Serve(lis)
defer grpcServer.Stop()
auth, err := g.GetUser(grpcUsername, grpcPassword, grpcClientId)
So(err, ShouldBeNil)
c.So(auth, ShouldBeTrue)
name := g.GetName()
So(name, ShouldEqual, "MyGRPCBackend")
})
})
})
Convey("given a correct host grpc backend should be able to initialize", func(c C) {
g, err := NewGRPC(authOpts, log.DebugLevel)
c.So(err, ShouldBeNil)
So(g.timeout, ShouldEqual, 100)
Convey("given incorrect credentials user should not be authenticated", func(c C) {
auth, err := g.GetUser(grpcUsername, "wrong", grpcClientId)
So(err, ShouldBeNil)
c.So(auth, ShouldBeFalse)
Convey("given correct credential user should be authenticated", func(c C) {
auth, err := g.GetUser(grpcUsername, grpcPassword, grpcClientId)
So(err, ShouldBeNil)
c.So(auth, ShouldBeTrue)
Convey("given a non superuser user the service should respond false", func(c C) {
auth, err = g.GetSuperuser(grpcUsername)
So(err, ShouldBeNil)
So(auth, ShouldBeFalse)
Convey("switching to a superuser should return true", func(c C) {
auth, err = g.GetSuperuser(grpcSuperuser)
So(err, ShouldBeNil)
So(auth, ShouldBeTrue)
Convey("but if we disable superuser checks it should return false", func(c C) {
authOpts["grpc_disable_superuser"] = "true"
g, err = NewGRPC(authOpts, log.DebugLevel)
c.So(err, ShouldBeNil)
auth, err = g.GetSuperuser(grpcSuperuser)
So(err, ShouldBeNil)
So(auth, ShouldBeFalse)
})
Convey("authorizing a wrong topic should fail", func(c C) {
auth, err = g.CheckAcl(grpcUsername, "wrong/topic", grpcClientId, grpcAcc)
So(err, ShouldBeNil)
So(auth, ShouldBeFalse)
Convey("switching to a correct one should succedd", func(c C) {
auth, err = g.CheckAcl(grpcUsername, grpcTopic, grpcClientId, grpcAcc)
So(err, ShouldBeNil)
So(auth, ShouldBeTrue)
})
})
})
})
})
})
})
})
}
func TestGRPCTls(t *testing.T) {
Convey("Given a mock grpc server with TLS", t, func(c C) {
serverCert, err := tls.LoadX509KeyPair("/test-files/certificates/grpc/fullchain-server.pem",
"/test-files/certificates/grpc/server-key.pem")
c.So(err, ShouldBeNil)
config := &tls.Config{
Certificates: []tls.Certificate{serverCert},
ClientAuth: tls.NoClientCert,
}
grpcServer := grpc.NewServer(grpc.Creds(credentials.NewTLS(config)))
gs.RegisterAuthServiceServer(grpcServer, NewAuthServiceAPI())
listen, err := net.Listen("tcp", ":3123")
c.So(err, ShouldBeNil)
go grpcServer.Serve(listen)
defer grpcServer.Stop()
authOpts := make(map[string]string)
authOpts["grpc_host"] = "localhost"
authOpts["grpc_port"] = "3123"
authOpts["grpc_dial_timeout_ms"] = "100"
authOpts["grpc_fail_on_dial_error"] = "true"
Convey("Given client connects without TLS, it should fail", func() {
g, err := NewGRPC(authOpts, log.DebugLevel)
c.So(err, ShouldBeError)
c.So(err.Error(), ShouldEqual, "context deadline exceeded")
c.So(g, ShouldBeNil)
})
authOpts["grpc_ca_cert"] = "/test-files/certificates/db/ca.pem"
Convey("Given client connects with TLS but with wrong CA, it should fail", func() {
g, err := NewGRPC(authOpts, log.DebugLevel)
c.So(err, ShouldBeError)
c.So(err.Error(), ShouldEqual, "context deadline exceeded")
c.So(g, ShouldBeNil)
})
authOpts["grpc_ca_cert"] = "/test-files/certificates/ca.pem"
Convey("Given client connects with TLS, it should work", func() {
g, err := NewGRPC(authOpts, log.DebugLevel)
c.So(err, ShouldBeNil)
c.So(g, ShouldNotBeNil)
})
})
}
func TestGRPCMutualTls(t *testing.T) {
Convey("Given a mock grpc server with TLS", t, func(c C) {
serverCert, err := tls.LoadX509KeyPair("/test-files/certificates/grpc/fullchain-server.pem",
"/test-files/certificates/grpc/server-key.pem")
c.So(err, ShouldBeNil)
clientCaBytes, err := ioutil.ReadFile("/test-files/certificates/grpc/ca.pem")
c.So(err, ShouldBeNil)
clientCaCertPool := x509.NewCertPool()
c.So(clientCaCertPool.AppendCertsFromPEM(clientCaBytes), ShouldBeTrue)
config := &tls.Config{
Certificates: []tls.Certificate{serverCert},
ClientAuth: tls.RequireAndVerifyClientCert,
ClientCAs: clientCaCertPool,
}
grpcServer := grpc.NewServer(grpc.Creds(credentials.NewTLS(config)))
gs.RegisterAuthServiceServer(grpcServer, NewAuthServiceAPI())
listen, err := net.Listen("tcp", ":3123")
c.So(err, ShouldBeNil)
go grpcServer.Serve(listen)
defer grpcServer.Stop()
authOpts := make(map[string]string)
authOpts["grpc_host"] = "localhost"
authOpts["grpc_port"] = "3123"
authOpts["grpc_dial_timeout_ms"] = "100"
authOpts["grpc_fail_on_dial_error"] = "true"
Convey("Given client connects without TLS, it should fail", func() {
g, err := NewGRPC(authOpts, log.DebugLevel)
c.So(err, ShouldBeError)
c.So(err.Error(), ShouldEqual, "context deadline exceeded")
c.So(g, ShouldBeNil)
})
authOpts["grpc_ca_cert"] = "/test-files/certificates/ca.pem"
Convey("Given client connects with TLS but without a client certificate, it should fail", func() {
g, err := NewGRPC(authOpts, log.DebugLevel)
c.So(err, ShouldBeError)
c.So(err.Error(), ShouldEqual, "context deadline exceeded")
c.So(g, ShouldBeNil)
})
authOpts["grpc_tls_cert"] = "/test-files/certificates/db/client.pem"
authOpts["grpc_tls_key"] = "/test-files/certificates/db/client-key.pem"
Convey("Given client connects with mTLS but with client cert from wrong CA, it should fail", func() {
g, err := NewGRPC(authOpts, log.DebugLevel)
c.So(err, ShouldBeError)
c.So(err.Error(), ShouldEqual, "context deadline exceeded")
c.So(g, ShouldBeNil)
})
authOpts["grpc_tls_cert"] = "/test-files/certificates/grpc/client.pem"
authOpts["grpc_tls_key"] = "/test-files/certificates/grpc/client-key.pem"
Convey("Given client connects with mTLS, it should work", func() {
g, err := NewGRPC(authOpts, log.DebugLevel)
c.So(err, ShouldBeNil)
c.So(g, ShouldNotBeNil)
})
})
}