forked from jcmturner/gokrb5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_test.go
349 lines (307 loc) · 10.1 KB
/
http_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
package spnego
import (
"bytes"
"crypto/rand"
"encoding/hex"
"fmt"
"io"
"io/ioutil"
"log"
"mime/multipart"
"net/http"
"net/http/httptest"
"os"
"sync"
"testing"
"github.com/stretchr/testify/assert"
"gopkg.in/jcmturner/goidentity.v3"
"gopkg.in/jcmturner/gokrb5.v7/client"
"gopkg.in/jcmturner/gokrb5.v7/config"
"gopkg.in/jcmturner/gokrb5.v7/keytab"
"gopkg.in/jcmturner/gokrb5.v7/service"
"gopkg.in/jcmturner/gokrb5.v7/test"
"gopkg.in/jcmturner/gokrb5.v7/test/testdata"
)
func TestClient_SetSPNEGOHeader(t *testing.T) {
test.Integration(t)
b, _ := hex.DecodeString(testdata.TESTUSER1_KEYTAB)
kt := keytab.New()
kt.Unmarshal(b)
c, _ := config.NewConfigFromString(testdata.TEST_KRB5CONF)
addr := os.Getenv("TEST_KDC_ADDR")
if addr == "" {
addr = testdata.TEST_KDC_ADDR
}
c.Realms[0].KDC = []string{addr + ":" + testdata.TEST_KDC}
l := log.New(os.Stderr, "SPNEGO Client:", log.LstdFlags)
cl := client.NewClientWithKeytab("testuser1", "TEST.GOKRB5", kt, c, client.Logger(l))
err := cl.Login()
if err != nil {
t.Fatalf("error on AS_REQ: %v\n", err)
}
urls := []string{
"http://cname.test.gokrb5",
"http://host.test.gokrb5",
}
paths := []string{
"/modkerb/index.html",
//"/modgssapi/index.html",
}
for _, url := range urls {
for _, p := range paths {
r, _ := http.NewRequest("GET", url+p, nil)
httpResp, err := http.DefaultClient.Do(r)
if err != nil {
t.Fatalf("%s request error: %v", url+p, err)
}
assert.Equal(t, http.StatusUnauthorized, httpResp.StatusCode, "Status code in response to client with no SPNEGO not as expected")
err = SetSPNEGOHeader(cl, r, "")
if err != nil {
t.Fatalf("error setting client SPNEGO header: %v", err)
}
httpResp, err = http.DefaultClient.Do(r)
if err != nil {
t.Fatalf("%s request error: %v\n", url+p, err)
}
assert.Equal(t, http.StatusOK, httpResp.StatusCode, "Status code in response to client SPNEGO request not as expected")
}
}
}
func TestSPNEGOHTTPClient(t *testing.T) {
test.Integration(t)
b, _ := hex.DecodeString(testdata.TESTUSER1_KEYTAB)
kt := keytab.New()
kt.Unmarshal(b)
c, _ := config.NewConfigFromString(testdata.TEST_KRB5CONF)
addr := os.Getenv("TEST_KDC_ADDR")
if addr == "" {
addr = testdata.TEST_KDC_ADDR
}
c.Realms[0].KDC = []string{addr + ":" + testdata.TEST_KDC}
l := log.New(os.Stderr, "SPNEGO Client:", log.LstdFlags)
cl := client.NewClientWithKeytab("testuser1", "TEST.GOKRB5", kt, c, client.Logger(l))
err := cl.Login()
if err != nil {
t.Fatalf("error on AS_REQ: %v\n", err)
}
urls := []string{
"http://cname.test.gokrb5",
"http://host.test.gokrb5",
}
// This path issues a redirect which the http client will automatically follow.
// It should cause a replay issue if the negInit token is sent in the first instance.
paths := []string{
"/modgssapi", // This issues a redirect which the http client will automatically follow. Could cause a replay issue
"/redirect",
}
for _, url := range urls {
for _, p := range paths {
r, _ := http.NewRequest("GET", url+p, nil)
httpCl := http.DefaultClient
httpCl.CheckRedirect = func(req *http.Request, via []*http.Request) error {
t.Logf("http client redirect: %+v", *req)
return nil
}
spnegoCl := NewClient(cl, httpCl, "")
httpResp, err := spnegoCl.Do(r)
if err != nil {
t.Fatalf("%s request error: %v", url+p, err)
}
assert.Equal(t, http.StatusOK, httpResp.StatusCode, "Status code in response to client SPNEGO request not as expected")
}
}
}
func TestService_SPNEGOKRB_NoAuthHeader(t *testing.T) {
s := httpServer()
defer s.Close()
r, _ := http.NewRequest("GET", s.URL, nil)
httpResp, err := http.DefaultClient.Do(r)
if err != nil {
t.Fatalf("Request error: %v\n", err)
}
assert.Equal(t, http.StatusUnauthorized, httpResp.StatusCode, "Status code in response to client with no SPNEGO not as expected")
assert.Equal(t, "Negotiate", httpResp.Header.Get("WWW-Authenticate"), "Negotiation header not set by server.")
}
func TestService_SPNEGOKRB_ValidUser(t *testing.T) {
test.Integration(t)
s := httpServer()
defer s.Close()
r, _ := http.NewRequest("GET", s.URL, nil)
cl := getClient()
err := SetSPNEGOHeader(cl, r, "HTTP/host.test.gokrb5")
if err != nil {
t.Fatalf("error setting client's SPNEGO header: %v", err)
}
httpResp, err := http.DefaultClient.Do(r)
if err != nil {
t.Fatalf("Request error: %v\n", err)
}
assert.Equal(t, http.StatusOK, httpResp.StatusCode, "Status code in response to client SPNEGO request not as expected")
}
func TestService_SPNEGOKRB_Replay(t *testing.T) {
test.Integration(t)
s := httpServer()
defer s.Close()
r1, _ := http.NewRequest("GET", s.URL, nil)
cl := getClient()
err := SetSPNEGOHeader(cl, r1, "HTTP/host.test.gokrb5")
if err != nil {
t.Fatalf("error setting client's SPNEGO header: %v", err)
}
// First request with this ticket should be accepted
httpResp, err := http.DefaultClient.Do(r1)
if err != nil {
t.Fatalf("Request error: %v\n", err)
}
assert.Equal(t, http.StatusOK, httpResp.StatusCode, "Status code in response to client SPNEGO request not as expected")
// Use ticket again should be rejected
httpResp, err = http.DefaultClient.Do(r1)
if err != nil {
t.Fatalf("Request error: %v\n", err)
}
assert.Equal(t, http.StatusUnauthorized, httpResp.StatusCode, "Status code in response to client with no SPNEGO not as expected. Expected a replay to be detected.")
// Form a 2nd ticket
r2, _ := http.NewRequest("GET", s.URL, nil)
err = SetSPNEGOHeader(cl, r2, "HTTP/host.test.gokrb5")
if err != nil {
t.Fatalf("error setting client's SPNEGO header: %v", err)
}
// First use of 2nd ticket should be accepted
httpResp, err = http.DefaultClient.Do(r2)
if err != nil {
t.Fatalf("Request error: %v\n", err)
}
assert.Equal(t, http.StatusOK, httpResp.StatusCode, "Status code in response to client SPNEGO request not as expected")
// Using the 1st ticket again should still be rejected
httpResp, err = http.DefaultClient.Do(r1)
if err != nil {
t.Fatalf("Request error: %v\n", err)
}
assert.Equal(t, http.StatusUnauthorized, httpResp.StatusCode, "Status code in response to client with no SPNEGO not as expected. Expected a replay to be detected.")
// Using the 2nd again should be rejected as replay
httpResp, err = http.DefaultClient.Do(r2)
if err != nil {
t.Fatalf("Request error: %v\n", err)
}
assert.Equal(t, http.StatusUnauthorized, httpResp.StatusCode, "Status code in response to client with no SPNEGO not as expected. Expected a replay to be detected.")
}
func TestService_SPNEGOKRB_ReplayCache_Concurrency(t *testing.T) {
test.Integration(t)
s := httpServer()
defer s.Close()
r1, _ := http.NewRequest("GET", s.URL, nil)
cl := getClient()
err := SetSPNEGOHeader(cl, r1, "HTTP/host.test.gokrb5")
if err != nil {
t.Fatalf("error setting client's SPNEGO header: %v", err)
}
r2, _ := http.NewRequest("GET", s.URL, nil)
err = SetSPNEGOHeader(cl, r2, "HTTP/host.test.gokrb5")
if err != nil {
t.Fatalf("error setting client's SPNEGO header: %v", err)
}
// Concurrent 1st requests should be OK
var wg sync.WaitGroup
wg.Add(2)
go httpGet(r1, &wg)
go httpGet(r2, &wg)
wg.Wait()
// A number of concurrent requests with the same ticket should be rejected due to replay
var wg2 sync.WaitGroup
noReq := 10
wg2.Add(noReq * 2)
for i := 0; i < noReq; i++ {
go httpGet(r1, &wg2)
go httpGet(r2, &wg2)
}
wg2.Wait()
}
func TestService_SPNEGOKRB_Upload(t *testing.T) {
test.Integration(t)
s := httpServer()
defer s.Close()
bodyBuf := &bytes.Buffer{}
bodyWriter := multipart.NewWriter(bodyBuf)
fileWriter, err := bodyWriter.CreateFormFile("uploadfile", "testfile.bin")
if err != nil {
t.Fatalf("error writing to buffer: %v", err)
}
data := make([]byte, 10240)
rand.Read(data)
br := bytes.NewReader(data)
_, err = io.Copy(fileWriter, br)
if err != nil {
t.Fatalf("error copying bytes: %v", err)
}
bodyWriter.Close()
r, _ := http.NewRequest("POST", s.URL, bodyBuf)
r.Header.Set("Content-Type", bodyWriter.FormDataContentType())
cl := getClient()
spnegoCl := NewClient(cl, nil, "HTTP/host.test.gokrb5")
httpResp, err := spnegoCl.Do(r)
if err != nil {
t.Fatalf("Request error: %v\n", err)
}
if httpResp.StatusCode != http.StatusOK {
bodyBytes, _ := ioutil.ReadAll(httpResp.Body)
bodyString := string(bodyBytes)
httpResp.Body.Close()
t.Errorf("unexpected code from http server (%d): %s", httpResp.StatusCode, bodyString)
}
}
func httpGet(r *http.Request, wg *sync.WaitGroup) {
defer wg.Done()
http.DefaultClient.Do(r)
}
func httpServer() *httptest.Server {
l := log.New(os.Stderr, "GOKRB5 Service Tests: ", log.Ldate|log.Ltime|log.Lshortfile)
b, _ := hex.DecodeString(testdata.HTTP_KEYTAB)
kt := keytab.New()
kt.Unmarshal(b)
th := http.HandlerFunc(testAppHandler)
s := httptest.NewServer(SPNEGOKRB5Authenticate(th, kt, service.Logger(l)))
return s
}
func testAppHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodPost {
maxUploadSize := int64(11240)
if err := r.ParseMultipartForm(maxUploadSize); err != nil {
http.Error(w, fmt.Sprintf("cannot parse multipart form: %v", err), http.StatusBadRequest)
return
}
r.Body = http.MaxBytesReader(w, r.Body, maxUploadSize)
file, _, err := r.FormFile("uploadfile")
if err != nil {
http.Error(w, "INVALID_FILE", http.StatusBadRequest)
return
}
defer file.Close()
// write out to /dev/null
_, err = io.Copy(ioutil.Discard, file)
if err != nil {
http.Error(w, "WRITE_ERR", http.StatusInternalServerError)
return
}
}
w.WriteHeader(http.StatusOK)
ctx := r.Context()
fmt.Fprintf(w, "<html>\nTEST.GOKRB5 Handler\nAuthenticed user: %s\nUser's realm: %s\n</html>",
ctx.Value(CTXKeyCredentials).(goidentity.Identity).UserName(),
ctx.Value(CTXKeyCredentials).(goidentity.Identity).Domain())
return
}
func getClient() *client.Client {
b, _ := hex.DecodeString(testdata.TESTUSER1_KEYTAB)
kt := keytab.New()
kt.Unmarshal(b)
c, _ := config.NewConfigFromString(testdata.TEST_KRB5CONF)
c.LibDefaults.NoAddresses = true
addr := os.Getenv("TEST_KDC_ADDR")
if addr == "" {
addr = testdata.TEST_KDC_ADDR
}
c.Realms[0].KDC = []string{addr + ":" + testdata.TEST_KDC}
c.Realms[0].KPasswdServer = []string{addr + ":464"}
cl := client.NewClientWithKeytab("testuser1", "TEST.GOKRB5", kt, c)
return cl
}