forked from kiali/kiali
-
Notifications
You must be signed in to change notification settings - Fork 0
/
authentication_test.go
263 lines (209 loc) · 10.8 KB
/
authentication_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
package handlers
import (
"fmt"
"math/rand"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"time"
osproject_v1 "github.com/openshift/api/project/v1"
"github.com/stretchr/testify/assert"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/kiali/kiali/business"
"github.com/kiali/kiali/business/authentication"
"github.com/kiali/kiali/config"
"github.com/kiali/kiali/kubernetes"
"github.com/kiali/kiali/kubernetes/kubetest"
"github.com/kiali/kiali/util"
)
// TestStrategyTokenAuthentication checks that a user with no active
// session is logged in successfully
func TestStrategyTokenAuthentication(t *testing.T) {
rand.New(rand.NewSource(time.Now().UnixNano()))
cfg := config.NewConfig()
cfg.Auth.Strategy = config.AuthStrategyToken
cfg.LoginToken.SigningKey = util.RandomString(16)
config.Set(cfg)
authentication.InitializeAuthenticationController("token")
clockTime := time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)
util.Clock = util.ClockMock{Time: clockTime}
// Mock K8S API to accept credentials
mockK8s(t, false)
// Create request
form := url.Values{}
form.Add("token", "foo")
request := httptest.NewRequest("POST", "http://kiali/api/authenticate", nil)
request.PostForm = form
responseRecorder := httptest.NewRecorder()
Authenticate(responseRecorder, request)
response := responseRecorder.Result()
assert.Equal(t, http.StatusOK, response.StatusCode)
assert.Len(t, response.Cookies(), 1)
cookie := response.Cookies()[0]
assert.Equal(t, authentication.AESSessionCookieName, cookie.Name)
assert.True(t, cookie.HttpOnly)
assert.NotEmpty(t, cookie.Value)
assert.Equal(t, clockTime.Add(time.Second*time.Duration(cfg.LoginToken.ExpirationSeconds)), cookie.Expires)
}
// TestStrategyTokenFails checks that a login attempt is
// rejected if user provides wrong credentials
func TestStrategyTokenFails(t *testing.T) {
cfg := config.NewConfig()
cfg.Auth.Strategy = config.AuthStrategyToken
config.Set(cfg)
// Mock k8s API to reject authentication
mockK8s(t, true)
// Send a request
form := url.Values{}
form.Add("token", "dummy")
request := httptest.NewRequest("POST", "http://kiali/api/authenticate", nil)
request.PostForm = form
responseRecorder := httptest.NewRecorder()
Authenticate(responseRecorder, request)
response := responseRecorder.Result()
assert.Equal(t, http.StatusUnauthorized, response.StatusCode)
assert.Len(t, response.Cookies(), 0)
}
// TestLogoutWhenNoSession checks that the Logout handler
// returns a blank response with no cookies being set when the
// user is not logged in.
func TestLogoutWhenNoSession(t *testing.T) {
request := httptest.NewRequest("GET", "http://kiali/api/logout", nil)
responseRecorder := httptest.NewRecorder()
Logout(responseRecorder, request)
response := responseRecorder.Result()
assert.Equal(t, http.StatusNoContent, response.StatusCode)
assert.Zero(t, len(response.Cookies()))
}
// TestLogout checks that the Logout handler
// sets a blank cookie to terminate the user's session
func TestLogout(t *testing.T) {
clockTime := time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)
util.Clock = util.ClockMock{Time: clockTime}
request := httptest.NewRequest("GET", "http://kiali/api/logout", nil)
request.AddCookie(&http.Cookie{
Name: config.TokenCookieName,
Value: "foo",
})
responseRecorder := httptest.NewRecorder()
Logout(responseRecorder, request)
response := responseRecorder.Result()
assert.Equal(t, http.StatusNoContent, response.StatusCode)
assert.Equal(t, 1, len(response.Cookies()))
cookie := response.Cookies()[0]
assert.Equal(t, config.TokenCookieName, cookie.Name)
assert.True(t, cookie.HttpOnly)
// assert.Equal(t,, http.SameSiteStrictMode, cookie.SameSite) ** Commented out because unsupported in go < 1.11
assert.Equal(t, "", cookie.Value)
assert.True(t, cookie.Expires.Before(clockTime))
}
// TestStrategyHeaderOidcAuthentication checks that a user with no active
// session is logged in successfully with an OIDC header
func TestStrategyHeaderOidcAuthentication(t *testing.T) {
rand.New(rand.NewSource(time.Now().UnixNano()))
cfg := config.NewConfig()
cfg.Auth.Strategy = config.AuthStrategyHeader
cfg.LoginToken.SigningKey = util.RandomString(16)
config.Set(cfg)
clockTime := time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)
util.Clock = util.ClockMock{Time: clockTime}
// Mock K8S API to accept credentials
mockK8s(t, false)
// OIDC Token
oidcToken := "eyJraWQiOiJDPU15Q291bnRyeSwgU1Q9U3RhdGUgb2YgQ2x1c3RlciwgTD1NeSBDbHVzdGVyLCBPPU15T3JnLCBPVT1LdWJlcm5ldGVzLCBDTj11bmlzb24tc2FtbDItcnAtc2lnLUM9TXlDb3VudHJ5LCBTVD1TdGF0ZSBvZiBDbHVzdGVyLCBMPU15IENsdXN0ZXIsIE89TXlPcmcsIE9VPUt1YmVybmV0ZXMsIENOPXVuaXNvbi1zYW1sMi1ycC1zaWctMTYwOTQ1MjY2NjA1NyIsImFsZyI6IlJTMjU2In0.eyJpc3MiOiJodHRwczovL2s4c291LmFwcHMuMTkyLTE2OC0yLTE0OC5uaXAuaW8vYXV0aC9pZHAvazhzSWRwIiwiYXVkIjoia3ViZXJuZXRlcyIsImV4cCI6MTYwOTc1ODQzNywianRpIjoiZUFJS2xibllmc1ZSWkNFb1FERHJCUSIsImlhdCI6MTYwOTc1ODM3NywibmJmIjoxNjA5NzU4MjU3LCJzdWIiOiJtbW9zbGV5IiwibmFtZSI6IiBuYSIsImdyb3VwcyI6WyJDTj1rOHNfbG9naW5fY2t1c3Rlcl9hZG1pbnMsQ049VXNlcnMsREM9ZW50MmsxMixEQz1kb21haW4sREM9Y29tIiwiQ049b3VfYXVkaXRvcnMsQ049VXNlcnMsREM9ZW50MmsxMixEQz1kb21haW4sREM9Y29tIiwiQ049UG9ydGFsIFVzZXJzLENOPVVzZXJzLERDPWVudDJrMTIsREM9ZG9tYWluLERDPWNvbSJdLCJwcmVmZXJyZWRfdXNlcm5hbWUiOiJtbW9zbGV5IiwiZW1haWwiOiJtYXJjKzExMTFAdHJlbW9sby5pbyJ9.psr0VzpvXD9J2mjoumc9IHICFI4v4NWdFKA2plUW4ehXJyjCf96JlBipbtTmvHJwDGeHoR_cFaHqyfY_PQm65_Y6qBk9gF23BPxGBzjz8jug-SwxwF3U9d3XoxGjXNB2HpmG_tawJlUmwyMaTdu8TtDnAUB5hIOMTFNYrgK8SMhbr6tTohT8SXFnrlegSTiKngNEtYiB-wubQIO8laX6QO3OTx_nkd99-mnu2LE9q-S_Yl0dg3KbyrdYBUO1MPlgh0wy3KOOnfrz910LsqbcSrBHwEGeCO65lNinXFdSXuid3OJOt7it1s5jjx0h-5fYHzrfzrmogWPjlMlhkvQHvw"
// Create request
form := url.Values{}
form.Add("token", "foo")
request := httptest.NewRequest("POST", "http://kiali/api/authenticate", nil)
request.Header.Set("Authorization", "Bearer "+oidcToken)
request.PostForm = form
responseRecorder := httptest.NewRecorder()
Authenticate(responseRecorder, request)
response := responseRecorder.Result()
assert.Equal(t, http.StatusOK, response.StatusCode)
assert.Len(t, response.Cookies(), 1)
// Simply check that some cookie is set and has the right expiration. Testing cookie content is left to the session_persistor_test.go
cookie := response.Cookies()[0]
assert.Equal(t, authentication.AESSessionCookieName, cookie.Name)
assert.True(t, cookie.HttpOnly)
assert.Equal(t, clockTime.Add(time.Second*time.Duration(cfg.LoginToken.ExpirationSeconds)), cookie.Expires)
}
// TestStrategyHeaderAuthentication checks that a user with no active
// session is logged in successfully with a header that is NOT OIDC
func TestStrategyHeaderAuthentication(t *testing.T) {
rand.New(rand.NewSource(time.Now().UnixNano()))
cfg := config.NewConfig()
cfg.Auth.Strategy = config.AuthStrategyHeader
cfg.LoginToken.SigningKey = util.RandomString(16)
config.Set(cfg)
clockTime := time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)
util.Clock = util.ClockMock{Time: clockTime}
// Mock K8S API to accept credentials
mockK8s(t, false)
// OIDC Token
oidcToken := "not_an_oidc_token"
// Create request
form := url.Values{}
form.Add("token", "foo")
request := httptest.NewRequest("POST", "http://kiali/api/authenticate", nil)
request.Header.Set("Authorization", "Bearer "+oidcToken)
request.PostForm = form
responseRecorder := httptest.NewRecorder()
Authenticate(responseRecorder, request)
response := responseRecorder.Result()
assert.Equal(t, http.StatusOK, response.StatusCode)
assert.Len(t, response.Cookies(), 1)
// Simply check that some cookie is set and has the right expiration. Testing cookie content is left to the session_persistor_test.go
cookie := response.Cookies()[0]
assert.Equal(t, authentication.AESSessionCookieName, cookie.Name)
assert.True(t, cookie.HttpOnly)
assert.Equal(t, clockTime.Add(time.Second*time.Duration(cfg.LoginToken.ExpirationSeconds)), cookie.Expires)
}
// TestStrategyHeaderOidcWithImpersonationAuthentication checks that a user with no active
// session is logged in successfully with a header that is NOT OIDC
func TestStrategyHeaderOidcWithImpersonationAuthentication(t *testing.T) {
rand.New(rand.NewSource(time.Now().UnixNano()))
cfg := config.NewConfig()
cfg.Auth.Strategy = config.AuthStrategyHeader
cfg.LoginToken.SigningKey = util.RandomString(16)
config.Set(cfg)
clockTime := time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)
util.Clock = util.ClockMock{Time: clockTime}
// Mock K8S API to accept credentials
mockK8s(t, false)
// OIDC Token
oidcToken := "eyJhbGciOiJSUzI1NiIsImtpZCI6Imh1MUIyczUxR2xQbjRBWmJTNHNpWjR6VXY0MkZCcUhGM1g0Q3hjY3B4WU0ifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJvcGVudW5pc29uIiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZWNyZXQubmFtZSI6Im9wZW51bmlzb24tb3JjaGVzdHJhLXRva2VuLTV4ZmZwIiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQubmFtZSI6Im9wZW51bmlzb24tb3JjaGVzdHJhIiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQudWlkIjoiNWU4NTcwMDItMmIwMy00ODUxLTljNDEtOGM5NGRhZTNhZWQzIiwic3ViIjoic3lzdGVtOnNlcnZpY2VhY2NvdW50Om9wZW51bmlzb246b3BlbnVuaXNvbi1vcmNoZXN0cmEifQ.eXlVuwZYYF85menphWJEHgSVNL8BTnCQfQiuE3QoJCEKO3Mi-xG1psPMxXnFkgeNlRdu30sejyd23_2ccW2b7q7Ss94o_m3ypWVV95ylGLegQOR8-b4mnysA8W9H1xpDsDii6kqc6k0IkJggUhBqImZHjSxbuvexuNuBmp-E_EOTuALIPmfWH3A7_z6dQEYc6sZ6xcmwBFJ-CuTDTpmYO-FvHvmBKVELpgCkEtMTeaXL3Avjg9KrrZ9T6rMcFfeDlMxNj-8KCEFV3QIiZCzULERuGU1WKKfukmb_sgEm5CshOHfC06ah0dyclZq8ctDPRqPVyRTgF5ZGtA_p4U6RsA"
// Create request
form := url.Values{}
form.Add("token", "foo")
request := httptest.NewRequest("POST", "http://kiali/api/authenticate", nil)
request.Header.Set("Authorization", "Bearer "+oidcToken)
request.Header.Set("Impersonate-User", "mmosley")
request.PostForm = form
responseRecorder := httptest.NewRecorder()
Authenticate(responseRecorder, request)
response := responseRecorder.Result()
assert.Equal(t, http.StatusOK, response.StatusCode)
assert.Len(t, response.Cookies(), 1)
// Simply check that some cookie is set and has the right expiration. Testing cookie content is left to the session_persistor_test.go
cookie := response.Cookies()[0]
assert.Equal(t, authentication.AESSessionCookieName, cookie.Name)
assert.True(t, cookie.HttpOnly)
assert.Equal(t, clockTime.Add(time.Second*time.Duration(cfg.LoginToken.ExpirationSeconds)), cookie.Expires)
}
type rejectClient struct{ kubernetes.ClientInterface }
func (r *rejectClient) GetProjects(labelSelector string) ([]osproject_v1.Project, error) {
return nil, fmt.Errorf("Rejecting")
}
func mockK8s(t *testing.T, reject bool) {
kubernetes.KialiTokenForHomeCluster = "notrealtoken"
kubernetes.KialiTokenFileForHomeCluster = "notrealtokenpath"
k8s := kubetest.NewFakeK8sClient(&osproject_v1.Project{ObjectMeta: meta_v1.ObjectMeta{Name: "tutorial"}})
k8s.OpenShift = true
var kubeClient kubernetes.ClientInterface = k8s
if reject {
kubeClient = &rejectClient{k8s}
}
business.SetupBusinessLayer(t, kubeClient, *config.Get())
}