-
Notifications
You must be signed in to change notification settings - Fork 4
/
kcc_test.go
387 lines (310 loc) · 9.66 KB
/
kcc_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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*
* Copyright 2019 Kopano and its licensors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package kcc
import (
"context"
"crypto/tls"
"net/http"
"os"
"testing"
)
var (
testUsername = "user1"
testUserPassword = "pass"
testSSOKCOIDCUsername *string
testSSOKCOIDCTokenValue *string
testX509ClientCertificate *string
testX509ClientPrivateKey *string
testSystemUsername = "SYSTEM"
)
func init() {
username := os.Getenv("TEST_USERNAME")
if username != "" {
testUsername = username
}
password := os.Getenv("TEST_PASSWORD")
if password != "" {
testUserPassword = password
}
kcoicdUsername := os.Getenv("TEST_KCOIDC_USERNAME")
if kcoicdUsername != "" {
testSSOKCOIDCUsername = &kcoicdUsername
}
kcoidcTokenValue := os.Getenv("TEST_KCOIDC_TOKEN_VALUE")
if kcoidcTokenValue != "" {
testSSOKCOIDCTokenValue = &kcoidcTokenValue
}
x509ClientCertificate := os.Getenv("TEST_X509_CERTIFICATE")
if x509ClientCertificate != "" {
testX509ClientCertificate = &x509ClientCertificate
}
x509ClientPrivateKey := os.Getenv("TEST_X509_PRIVATE_KEY")
if x509ClientPrivateKey != "" {
testX509ClientPrivateKey = &x509ClientPrivateKey
}
}
func logon(ctx context.Context, t testing.TB, c *KCC, username *string, userPassword *string, logonFlags KCFlag) (*KCC, *LogonResponse) {
var err error
if username == nil {
username = &testUsername
}
if userPassword == nil {
userPassword = &testUserPassword
}
if c == nil {
c, err = NewKCCFromURI(nil)
if err != nil {
t.Fatalf("failed to create kcc from uri: %v", err)
}
}
resp, err := _logon(ctx, t, c, *username, *userPassword, logonFlags)
if err != nil {
t.Fatal(err)
}
if resp.Er != KCSuccess {
t.Fatalf("logon returned wrong er: got %v want 0", resp.Er)
}
if resp.SessionID == KCNoSessionID {
t.Errorf("logon returned invalid session ID")
}
if resp.ServerGUID == "" {
t.Errorf("logon return invalid server GUID")
}
return c, resp
}
func _logon(ctx context.Context, t testing.TB, c *KCC, username string, userPassword string, logonFlags KCFlag) (*LogonResponse, error) {
var err error
if c == nil {
c, err = NewKCCFromURI(nil)
if err != nil {
t.Fatalf("failed to create kcc from uri: %v", err)
}
}
return c.Logon(ctx, username, userPassword, logonFlags)
}
func x509Logon(ctx context.Context, t testing.TB, c *KCC, username *string, userPassword *string, certFile *string, keyFile *string, logonFlags KCFlag) (*KCC, *LogonResponse) {
if username == nil {
username = &testUsername
}
if userPassword == nil {
empty := ""
userPassword = &empty
}
if c == nil {
transport := http.DefaultTransport.(*http.Transport).Clone()
if defaultHTTPInsecureSkipVerify {
transport.TLSClientConfig = &tls.Config{
InsecureSkipVerify: defaultHTTPInsecureSkipVerify,
}
}
client, err := NewSOAPHTTPClient(nil, &http.Client{
Transport: transport,
})
if err != nil {
t.Fatalf("failed to create SOAP HTTP client: %v", err)
}
c = NewKCCWithClient(client)
if certFile == nil {
certFile = testX509ClientCertificate
}
if keyFile == nil {
keyFile = testX509ClientPrivateKey
}
if certFile == nil || keyFile == nil {
t.Skip("missing TEST_X509_CERTIFICATE or TEST_X509_PRIVATE_KEY")
}
err = useX509KeyPair(transport, *certFile, *keyFile)
if err != nil {
t.Fatalf("failed to load X509 key pair: %v", err)
}
} else {
if certFile != nil || keyFile != nil {
t.Errorf("cannot use x509Logon with already initialized client and cert/key")
}
}
return logon(ctx, t, c, username, userPassword, logonFlags)
}
func ssoKCOIDCLogon(ctx context.Context, t testing.TB, c *KCC, sessionID KCSessionID, username *string, tokenValue *string, logonFlags KCFlag) (*KCC, *LogonResponse) {
var err error
if username == nil {
username = testSSOKCOIDCUsername
}
if tokenValue == nil {
tokenValue = testSSOKCOIDCTokenValue
}
if username == nil || tokenValue == nil {
t.Skip("missing TEST_KCOIDC_USERNAME or TEST_KCOIDC_TOKEN_VALUE")
}
if c == nil {
c, err = NewKCCFromURI(nil)
if err != nil {
t.Fatalf("failed to create kcc from uri: %v", err)
}
}
resp, err := c.SSOLogon(ctx, KOPANO_SSO_TYPE_KCOIDC, *username, []byte(*tokenValue), sessionID, logonFlags)
if err != nil {
t.Fatal(err)
}
if resp.Er != KCSuccess {
t.Fatalf("sso logon returned wrong er: got %v want 0", resp.Er)
}
if resp.SessionID == KCNoSessionID {
t.Errorf("sso logon returned invalid session ID")
}
if resp.ServerGUID == "" {
t.Errorf("sso logon return invalid server GUID")
}
return c, resp
}
func getUser(ctx context.Context, t testing.TB, c *KCC, userEntryID string, sessionID KCSessionID) (*KCC, *GetUserResponse) {
var err error
if c == nil {
c, err = NewKCCFromURI(nil)
if err != nil {
t.Fatalf("failed to create kcc from uri: %v", err)
}
}
if sessionID == 0 {
_, session := logon(ctx, t, c, nil, nil, 0)
sessionID = session.SessionID
}
resp, err := c.GetUser(ctx, userEntryID, sessionID)
if err != nil {
t.Fatal(err)
}
if resp.Er != KCSuccess {
t.Fatalf("getUser returned wrong er: got %v want 0", resp.Er)
}
if resp.User == nil {
t.Fatal("getUser returned no user")
}
if resp.User.ID == 0 {
t.Errorf("getUser user returned invalid User.ID: got %v", resp.User.ID)
}
if resp.User.UserEntryID == "" {
t.Errorf("getUser user returned invalid User.UserID")
}
return c, resp
}
func TestLogon(t *testing.T) {
_, resp := logon(context.Background(), t, nil, nil, nil, 0)
t.Logf("Session ID : %d", resp.SessionID)
t.Logf("Server GUID : %v", resp.ServerGUID)
}
func TestLogonWithSpecialPasswordWhichNeedsQuoting(t *testing.T) {
username := testUsername
password := "<test"
resp, err := _logon(context.Background(), t, nil, username, password, 0)
if err != nil {
t.Fatal(err)
}
if resp.Er != KCERR_LOGON_FAILED {
t.Fatalf("logon returned wrong er: got %v want %v", resp.Er, KCERR_LOGON_FAILED)
}
}
func TestLogonWithX509KeyPair(t *testing.T) {
_, resp := x509Logon(context.Background(), t, nil, nil, nil, nil, nil, 0)
t.Logf("session ID : %d", resp.SessionID)
t.Logf("server GUID : %v", resp.ServerGUID)
}
func TestLogonSystemWithX509KeyPair(t *testing.T) {
_, resp := x509Logon(context.Background(), t, nil, &testSystemUsername, nil, nil, nil, 0)
t.Logf("session ID : %d", resp.SessionID)
t.Logf("server GUID : %v", resp.ServerGUID)
}
func TestSSOLogonWithKCOIDCAccessToken(t *testing.T) {
_, resp := ssoKCOIDCLogon(context.Background(), t, nil, KCNoSessionID, nil, nil, 0)
t.Logf("session ID : %d", resp.SessionID)
t.Logf("server GUID : %v", resp.ServerGUID)
}
func TestLogoff(t *testing.T) {
ctx := context.Background()
c, session := logon(ctx, t, nil, nil, nil, 0)
resp, err := c.Logoff(ctx, session.SessionID)
if err != nil {
t.Fatal(err)
}
if resp.Er != KCSuccess {
t.Fatalf("logoff returned wrong er: got %v want 0", resp.Er)
}
}
func BenchmarkLogon(b *testing.B) {
var err error
ctx := context.Background()
c, err := NewKCCFromURI(nil)
if err != nil {
b.Fatalf("failed to create kcc from uri: %v", err)
}
for n := 0; n < b.N; n++ {
logon(ctx, b, c, nil, nil, KOPANO_LOGON_NO_REGISTER_SESSION)
}
}
func BenchmarkLogonWithX509Keypair(b *testing.B) {
ctx := context.Background()
c, _ := x509Logon(ctx, b, nil, nil, nil, nil, nil, KOPANO_LOGON_NO_REGISTER_SESSION)
for n := 0; n < b.N; n++ {
x509Logon(ctx, b, c, nil, nil, nil, nil, KOPANO_LOGON_NO_REGISTER_SESSION)
}
}
func BenchmarkSSOLogonWithKCOIDCAccessToken(b *testing.B) {
var err error
ctx := context.Background()
c, err := NewKCCFromURI(nil)
if err != nil {
b.Fatalf("failed to create kcc from uri: %v", err)
}
for n := 0; n < b.N; n++ {
// NOTE(longsleep): Currently SSO logon supports no flags, thus we
// pass 0. So for now this creates sessions on the server.
ssoKCOIDCLogon(ctx, b, c, KCNoSessionID, nil, nil, 0)
}
}
func TestGetUserSelf(t *testing.T) {
ctx := context.Background()
c, session := logon(ctx, t, nil, nil, nil, 0)
// NOTE(longsleep): Empty user EntryID returns data for the current user.
_, resp := getUser(ctx, t, c, "", session.SessionID)
if resp.User.Username != testUsername {
t.Errorf("getUser returned wrong User.Username: got %v want %v", resp.User.Username, testUsername)
}
}
func TestResolveUsernameSystemAndGetUser(t *testing.T) {
ctx := context.Background()
c, session := logon(ctx, t, nil, nil, nil, 0)
resp, err := c.ResolveUsername(ctx, "SYSTEM", session.SessionID)
if err != nil {
t.Fatal(err)
}
if resp.Er != KCSuccess {
t.Fatalf("resolveUsername returned wrong er: got %v want 0", resp.Er)
}
if resp.ID == 0 {
t.Errorf("resolveUsername returned invalid ID: got %v", resp.ID)
}
if resp.UserEntryID == "" {
t.Errorf("getUser user returned invalid UserID")
}
_, userResp := getUser(ctx, t, c, resp.UserEntryID, session.SessionID)
if userResp.User.Username != "SYSTEM" {
t.Errorf("getUser of resolved SYSTEM user did not return the SYSTEM user: got %v", userResp.User.Username)
}
if userResp.User.ID != resp.ID {
t.Errorf("resolveUsername user.ID does not match getUser result: got %v want %v", userResp.User.ID, resp.ID)
}
if userResp.User.UserEntryID != resp.UserEntryID {
t.Errorf("resolveUsername user.UserEntryID does not match getUser result: got %v want %v", userResp.User.UserEntryID, resp.UserEntryID)
}
}