| 1 |
package service |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/hex" |
| 5 |
"fmt" |
| 6 |
"io/ioutil" |
| 7 |
"log" |
| 8 |
"net/http" |
| 9 |
"net/http/httptest" |
| 10 |
"sync" |
| 11 |
"testing" |
| 12 |
"time" |
| 13 |
|
| 14 |
"github.com/stretchr/testify/assert" |
| 15 |
"gopkg.in/jcmturner/gokrb5.v5/client" |
| 16 |
"gopkg.in/jcmturner/gokrb5.v5/credentials" |
| 17 |
"gopkg.in/jcmturner/gokrb5.v5/iana/nametype" |
| 18 |
"gopkg.in/jcmturner/gokrb5.v5/keytab" |
| 19 |
"gopkg.in/jcmturner/gokrb5.v5/messages" |
| 20 |
"gopkg.in/jcmturner/gokrb5.v5/testdata" |
| 21 |
"gopkg.in/jcmturner/gokrb5.v5/types" |
| 22 |
) |
| 23 |
|
| 24 |
func TestService_SPNEGOKRB_NoAuthHeader(t *testing.T) { |
| 25 |
s := httpServer() |
| 26 |
defer s.Close() |
| 27 |
r, _ := http.NewRequest("GET", s.URL, nil) |
| 28 |
httpResp, err := http.DefaultClient.Do(r) |
| 29 |
if err != nil { |
| 30 |
t.Fatalf("Request error: %v\n", err) |
| 31 |
} |
| 32 |
assert.Equal(t, http.StatusUnauthorized, httpResp.StatusCode, "Status code in response to client with no SPNEGO not as expected") |
| 33 |
assert.Equal(t, "Negotiate", httpResp.Header.Get("WWW-Authenticate"), "Negitation header not set by server.") |
| 34 |
} |
| 35 |
|
| 36 |
func TestService_SPNEGOKRB_ValidUser(t *testing.T) { |
| 37 |
s := httpServer() |
| 38 |
defer s.Close() |
| 39 |
|
| 40 |
cl := getClient() |
| 41 |
sname := types.PrincipalName{ |
| 42 |
NameType: nametype.KRB_NT_PRINCIPAL, |
| 43 |
NameString: []string{"HTTP", "host.test.gokrb5"}, |
| 44 |
} |
| 45 |
b, _ := hex.DecodeString(testdata.HTTP_KEYTAB) |
| 46 |
kt, _ := keytab.Parse(b) |
| 47 |
st := time.Now().UTC() |
| 48 |
tkt, sessionKey, err := messages.NewTicket(cl.Credentials.CName, cl.Credentials.Realm, |
| 49 |
sname, "TEST.GOKRB5", |
| 50 |
types.NewKrbFlags(), |
| 51 |
kt, |
| 52 |
18, |
| 53 |
1, |
| 54 |
st, |
| 55 |
st, |
| 56 |
st.Add(time.Duration(24)*time.Hour), |
| 57 |
st.Add(time.Duration(48)*time.Hour), |
| 58 |
) |
| 59 |
if err != nil { |
| 60 |
t.Fatalf("Error getting test ticket: %v", err) |
| 61 |
} |
| 62 |
|
| 63 |
r, _ := http.NewRequest("GET", s.URL, nil) |
| 64 |
err = client.SetSPNEGOHeader(*cl.Credentials, tkt, sessionKey, r) |
| 65 |
if err != nil { |
| 66 |
t.Fatalf("Error setting client SPNEGO header: %v", err) |
| 67 |
} |
| 68 |
httpResp, err := http.DefaultClient.Do(r) |
| 69 |
if err != nil { |
| 70 |
t.Fatalf("Request error: %v\n", err) |
| 71 |
} |
| 72 |
assert.Equal(t, http.StatusOK, httpResp.StatusCode, "Status code in response to client SPNEGO request not as expected") |
| 73 |
} |
| 74 |
|
| 75 |
func TestService_SPNEGOKRB_Replay(t *testing.T) { |
| 76 |
s := httpServer() |
| 77 |
defer s.Close() |
| 78 |
|
| 79 |
cl := getClient() |
| 80 |
sname := types.PrincipalName{ |
| 81 |
NameType: nametype.KRB_NT_PRINCIPAL, |
| 82 |
NameString: []string{"HTTP", "host.test.gokrb5"}, |
| 83 |
} |
| 84 |
b, _ := hex.DecodeString(testdata.HTTP_KEYTAB) |
| 85 |
kt, _ := keytab.Parse(b) |
| 86 |
st := time.Now().UTC() |
| 87 |
tkt, sessionKey, err := messages.NewTicket(cl.Credentials.CName, cl.Credentials.Realm, |
| 88 |
sname, "TEST.GOKRB5", |
| 89 |
types.NewKrbFlags(), |
| 90 |
kt, |
| 91 |
18, |
| 92 |
1, |
| 93 |
st, |
| 94 |
st, |
| 95 |
st.Add(time.Duration(24)*time.Hour), |
| 96 |
st.Add(time.Duration(48)*time.Hour), |
| 97 |
) |
| 98 |
if err != nil { |
| 99 |
t.Fatalf("Error getting test ticket: %v", err) |
| 100 |
} |
| 101 |
|
| 102 |
r1, _ := http.NewRequest("GET", s.URL, nil) |
| 103 |
err = client.SetSPNEGOHeader(*cl.Credentials, tkt, sessionKey, r1) |
| 104 |
if err != nil { |
| 105 |
t.Fatalf("Error setting client SPNEGO header: %v", err) |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
httpResp, err := http.DefaultClient.Do(r1) |
| 110 |
if err != nil { |
| 111 |
t.Fatalf("Request error: %v\n", err) |
| 112 |
} |
| 113 |
assert.Equal(t, http.StatusOK, httpResp.StatusCode, "Status code in response to client SPNEGO request not as expected") |
| 114 |
|
| 115 |
|
| 116 |
httpResp, err = http.DefaultClient.Do(r1) |
| 117 |
if err != nil { |
| 118 |
t.Fatalf("Request error: %v\n", err) |
| 119 |
} |
| 120 |
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.") |
| 121 |
|
| 122 |
|
| 123 |
st = time.Now().UTC() |
| 124 |
tkt2, sessionKey2, err := messages.NewTicket(cl.Credentials.CName, cl.Credentials.Realm, |
| 125 |
sname, "TEST.GOKRB5", |
| 126 |
types.NewKrbFlags(), |
| 127 |
kt, |
| 128 |
18, |
| 129 |
1, |
| 130 |
st, |
| 131 |
st, |
| 132 |
st.Add(time.Duration(24)*time.Hour), |
| 133 |
st.Add(time.Duration(48)*time.Hour), |
| 134 |
) |
| 135 |
if err != nil { |
| 136 |
t.Fatalf("Error getting test ticket: %v", err) |
| 137 |
} |
| 138 |
r2, _ := http.NewRequest("GET", s.URL, nil) |
| 139 |
err = client.SetSPNEGOHeader(*cl.Credentials, tkt2, sessionKey2, r2) |
| 140 |
if err != nil { |
| 141 |
t.Fatalf("Error setting client SPNEGO header: %v", err) |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
httpResp, err = http.DefaultClient.Do(r2) |
| 146 |
if err != nil { |
| 147 |
t.Fatalf("Request error: %v\n", err) |
| 148 |
} |
| 149 |
assert.Equal(t, http.StatusOK, httpResp.StatusCode, "Status code in response to client SPNEGO request not as expected") |
| 150 |
|
| 151 |
|
| 152 |
httpResp, err = http.DefaultClient.Do(r1) |
| 153 |
if err != nil { |
| 154 |
t.Fatalf("Request error: %v\n", err) |
| 155 |
} |
| 156 |
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.") |
| 157 |
|
| 158 |
|
| 159 |
httpResp, err = http.DefaultClient.Do(r2) |
| 160 |
if err != nil { |
| 161 |
t.Fatalf("Request error: %v\n", err) |
| 162 |
} |
| 163 |
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.") |
| 164 |
} |
| 165 |
|
| 166 |
func TestService_SPNEGOKRB_ReplayCache_Concurrency(t *testing.T) { |
| 167 |
s := httpServer() |
| 168 |
defer s.Close() |
| 169 |
|
| 170 |
cl := getClient() |
| 171 |
sname := types.PrincipalName{ |
| 172 |
NameType: nametype.KRB_NT_PRINCIPAL, |
| 173 |
NameString: []string{"HTTP", "host.test.gokrb5"}, |
| 174 |
} |
| 175 |
b, _ := hex.DecodeString(testdata.HTTP_KEYTAB) |
| 176 |
kt, _ := keytab.Parse(b) |
| 177 |
st := time.Now().UTC() |
| 178 |
tkt, sessionKey, err := messages.NewTicket(cl.Credentials.CName, cl.Credentials.Realm, |
| 179 |
sname, "TEST.GOKRB5", |
| 180 |
types.NewKrbFlags(), |
| 181 |
kt, |
| 182 |
18, |
| 183 |
1, |
| 184 |
st, |
| 185 |
st, |
| 186 |
st.Add(time.Duration(24)*time.Hour), |
| 187 |
st.Add(time.Duration(48)*time.Hour), |
| 188 |
) |
| 189 |
if err != nil { |
| 190 |
t.Fatalf("Error getting test ticket: %v", err) |
| 191 |
} |
| 192 |
|
| 193 |
r1, _ := http.NewRequest("GET", s.URL, nil) |
| 194 |
err = client.SetSPNEGOHeader(*cl.Credentials, tkt, sessionKey, r1) |
| 195 |
if err != nil { |
| 196 |
t.Fatalf("Error setting client SPNEGO header: %v", err) |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
st = time.Now().UTC() |
| 201 |
tkt2, sessionKey2, err := messages.NewTicket(cl.Credentials.CName, cl.Credentials.Realm, |
| 202 |
sname, "TEST.GOKRB5", |
| 203 |
types.NewKrbFlags(), |
| 204 |
kt, |
| 205 |
18, |
| 206 |
1, |
| 207 |
st, |
| 208 |
st, |
| 209 |
st.Add(time.Duration(24)*time.Hour), |
| 210 |
st.Add(time.Duration(48)*time.Hour), |
| 211 |
) |
| 212 |
if err != nil { |
| 213 |
t.Fatalf("Error getting test ticket: %v", err) |
| 214 |
} |
| 215 |
r2, _ := http.NewRequest("GET", s.URL, nil) |
| 216 |
err = client.SetSPNEGOHeader(*cl.Credentials, tkt2, sessionKey2, r2) |
| 217 |
if err != nil { |
| 218 |
t.Fatalf("Error setting client SPNEGO header: %v", err) |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
var wg sync.WaitGroup |
| 223 |
wg.Add(2) |
| 224 |
go httpGet(r1, &wg) |
| 225 |
go httpGet(r2, &wg) |
| 226 |
wg.Wait() |
| 227 |
|
| 228 |
|
| 229 |
var wg2 sync.WaitGroup |
| 230 |
noReq := 10 |
| 231 |
wg2.Add(noReq * 2) |
| 232 |
for i := 0; i < noReq; i++ { |
| 233 |
go httpGet(r1, &wg2) |
| 234 |
go httpGet(r2, &wg2) |
| 235 |
} |
| 236 |
wg2.Wait() |
| 237 |
} |
| 238 |
|
| 239 |
func httpGet(r *http.Request, wg *sync.WaitGroup) { |
| 240 |
defer wg.Done() |
| 241 |
http.DefaultClient.Do(r) |
| 242 |
} |
| 243 |
|
| 244 |
func httpServer() *httptest.Server { |
| 245 |
l := log.New(ioutil.Discard, "GOKRB5 Service Tests: ", log.Ldate|log.Ltime|log.Lshortfile) |
| 246 |
b, _ := hex.DecodeString(testdata.HTTP_KEYTAB) |
| 247 |
kt, _ := keytab.Parse(b) |
| 248 |
th := http.HandlerFunc(testAppHandler) |
| 249 |
s := httptest.NewServer(SPNEGOKRB5Authenticate(th, kt, "", false, l)) |
| 250 |
return s |
| 251 |
} |
| 252 |
|
| 253 |
func testAppHandler(w http.ResponseWriter, r *http.Request) { |
| 254 |
w.WriteHeader(http.StatusOK) |
| 255 |
ctx := r.Context() |
| 256 |
fmt.Fprintf(w, "<html>\nTEST.GOKRB5 Handler\nAuthenticed user: %s\nUser's realm: %s\n</html>", ctx.Value(CTXKeyCredentials).(credentials.Credentials).Username, ctx.Value(CTXKeyCredentials).(credentials.Credentials).Realm) |
| 257 |
return |
| 258 |
} |