-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathelastic_guardian_test.go
248 lines (201 loc) · 6.58 KB
/
elastic_guardian_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
package main
import (
"encoding/base64"
aa "github.com/alexaandru/elastic_guardian/authentication"
az "github.com/alexaandru/elastic_guardian/authorization"
"net/http"
"net/http/httptest"
"net/url"
"testing"
)
type testCase struct {
url, header, body string
}
var foobar, foobogus, bazboo = base64.StdEncoding.EncodeToString([]byte("foo:bar")),
base64.StdEncoding.EncodeToString([]byte("foo:bogus")),
base64.StdEncoding.EncodeToString([]byte("baz:boo"))
var testCases = map[string]testCase{
"request_authentication_if_blank": {"whatever", "", "401 Unauthorized\n"},
"fail_with_incorrect_credentials": {"whatever", "Basic " + foobogus, "403 Forbidden (authentication)\n"},
"pass_when_blacklisting_allows": {"/_cluster/stats", "Basic " + foobar, ""},
"fail_when_blacklisting_forbids": {"/_cluster/health", "Basic " + foobar, "403 Forbidden (authorization)\n"},
"pass_when_whitelisting_allows": {"/_cluster/health", "Basic " + bazboo, ""},
"fail_when_whitelisting_forbids": {"/_cluster/stats", "Basic " + bazboo, "403 Forbidden (authorization)\n"},
}
func loadCredentials() {
aa.LoadCredentials(aa.CredentialsStore{
"foo": aa.Hash("bar"),
"baz": aa.Hash("boo"),
})
}
func loadAuthorizations() {
az.LoadAuthorizations(az.AuthorizationStore{
"foo": az.AuthorizationRules{DefaultRule: az.Allow, Rules: []string{"GET /_cluster/health"}},
"baz": az.AuthorizationRules{DefaultRule: az.Deny, Rules: []string{"GET /_cluster/health"}},
})
}
// Test wrappers
func TestShouldRequestAuthenticationIfBlank(t *testing.T) {
assertPassesTestCase(t, testCases["request_authentication_if_blank"])
}
func TestShouldFailWithIncorrectCredentials(t *testing.T) {
assertPassesTestCase(t, testCases["fail_with_incorrect_credentials"])
}
func TestShouldPassWhenBlacklistingAllows(t *testing.T) {
assertPassesTestCase(t, testCases["pass_when_blacklisting_allows"])
}
func TestShouldFailWhenBlacklistingForbids(t *testing.T) {
assertPassesTestCase(t, testCases["fail_when_blacklisting_forbids"])
}
func TestShouldPassWhenWhitelistingAllows(t *testing.T) {
assertPassesTestCase(t, testCases["pass_when_whitelisting_allows"])
}
func TestShouldFailWhenWhitelistingForbids(t *testing.T) {
assertPassesTestCase(t, testCases["fail_when_whitelisting_forbids"])
}
func assertPassesTestCase(t *testing.T, tc testCase) {
loadCredentials()
loadAuthorizations()
uri, err := url.Parse("http://localhost:9000")
if err != nil {
t.Fail()
}
handler := initReverseProxy(uri, wrapAuthorization, wrapAuthentication)
recorder := httptest.NewRecorder()
req, err := http.NewRequest("GET", tc.url, nil)
if err != nil {
t.Error("Failed to perform the request:", err)
}
if tc.header != "" {
req.Header.Set("Authorization", tc.header)
}
handler.ServeHTTP(recorder, req)
if expectedBody, actualBody := tc.body, recorder.Body.String(); actualBody != expectedBody {
t.Error("Expected", expectedBody, "got", actualBody)
}
}
// Test command line
func TestCmdLineFlagDefaults(t *testing.T) {
processCmdLineFlags()
assertions := []([]string){
{"BackendURL", BackendURL, "http://localhost:9200"},
{"FrontendURL", FrontendURL, ":9600"},
{"Realm", Realm, "Elasticsearch"},
{"LogPath", LogPath, ""},
}
for _, row := range assertions {
label, actual, expected := row[0], row[1], row[2]
if actual != expected {
t.Errorf("Failed to set %s: expected %s got %s", label, expected, actual)
}
}
}
// Test logging
func TestLogpathEmpty(t *testing.T) {
f, err := redirectLogsToFile("")
if f != nil {
t.Error("Should have NOT returned a file pointer on empty path")
}
if err != nil {
t.Error("Should have NOT returned an error on empty path")
}
}
func TestLogpathInvalid(t *testing.T) {
_, err := redirectLogsToFile("what/a/bogus/path/this/is")
if err == nil {
t.Error("Should have returned error on invalid path")
}
}
func TestLogpathValid(t *testing.T) {
f, err := redirectLogsToFile("test.test")
if f == nil {
t.Error("Should have returned a file pointer on valid path")
}
if err != nil {
t.Error("Should NOT have returned error on valid path")
}
}
// Test setup
func TestSetupInline(t *testing.T) {
CredentialsPath, AuthorizationsPath, LogPath = "", "", ""
uri, f, err := setup()
if f != nil {
defer f.Close()
t.Error("Log should not be redirected when logpath empty")
}
if uri == nil {
t.Error("Uri should not be nil")
}
if err != nil {
t.Error("Error should be nil")
}
}
func TestSetupWithCorrectFilePaths(t *testing.T) {
CredentialsPath, AuthorizationsPath, LogPath = "authentication/authentication_test.txt", "authorization/authorization_test.txt", "test.test"
uri, f, err := setup()
if f != nil {
defer f.Close()
} else {
t.Error("Log should be redirected to file when logpath NOT empty")
}
if uri == nil {
t.Error("Uri should not be nil")
}
if err != nil {
t.Error("Error should be nil")
}
}
func TestSetupWithIncorrectAuthenticationFilePath(t *testing.T) {
CredentialsPath, AuthorizationsPath, LogPath = "authentication/bogus/authentication_test.txt", "authorization/authorization_test.txt", "test.test"
_, f, err := setup()
if f != nil {
defer f.Close()
}
expected := "open authentication/bogus/authentication_test.txt: no such file or directory"
if err == nil {
t.Error("Error should NOT be nil")
} else if err.Error() != expected {
t.Errorf("Expected %s error got %v", expected, err)
}
}
func TestSetupWithIncorrectAuthorizationFilePath(t *testing.T) {
CredentialsPath, AuthorizationsPath, LogPath = "authentication/authentication_test.txt", "authorization/bogus/authorization_test.txt", "test.test"
_, f, err := setup()
if f != nil {
defer f.Close()
}
expected := "open authorization/bogus/authorization_test.txt: no such file or directory"
if err == nil {
t.Error("Error should NOT be nil")
} else if err.Error() != expected {
t.Errorf("Expected %s error got %v", expected, err)
}
}
func TestSetupWithIncorrectURI(t *testing.T) {
CredentialsPath, AuthorizationsPath, LogPath = "", "", ""
BackendURL = "%BOGUS"
_, f, err := setup()
if f != nil {
defer f.Close()
}
BackendURL = ""
expected := "parse %BOGUS: invalid URL escape \"%BO\""
if err == nil {
t.Error("Error should NOT be nil")
} else if err.Error() != expected {
t.Errorf("Expected %s error got %v", expected, err)
}
}
func TestSetupWithIncorrectLogPath(t *testing.T) {
CredentialsPath, AuthorizationsPath, LogPath = "", "", "bogus/bogus"
_, f, err := setup()
if f != nil {
defer f.Close()
}
expected := "open bogus/bogus: no such file or directory"
if err == nil {
t.Error("Error should NOT be nil")
} else if err.Error() != expected {
t.Errorf("Expected %s error got %v", expected, err)
}
}