-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathapi_test.go
218 lines (186 loc) · 4.95 KB
/
api_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
package main
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
var (
server *httptest.Server
reader io.Reader
index string
)
func init() {
server = httptest.NewServer(Handlers())
index = fmt.Sprintf("%s/", server.URL)
LoadConfigFromFile("examples/example1.hcl")
}
func postIndex(data string) (*http.Response, error) {
reader = strings.NewReader(data)
request, err := http.NewRequest("POST", index, reader)
res, err := http.DefaultClient.Do(request)
return res, err
}
func parseResponse(r *http.Response) (*AuthorizationResponse, error) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return nil, err
}
respJSON := NewAuthorizationResponse(true)
err = json.Unmarshal(body, &respJSON)
return respJSON, err
}
func TestNewAuthorizationByNamespace(t *testing.T) {
var saTests = []struct {
namespace string
username string
status int
allowed bool
}{
{"namespace-dev", "system:serviceaccount:namespace-sss:default", 200, true},
{"namespace", "system:serviceaccount:namespace-sss:default", 200, true},
{"namespace-any", "badguy", 403, false},
{"namespace-dev", "someuser", 200, true},
{"namespace-dev", "system:serviceaccount:default-ns:default", 403, false},
{"sample-app", "system:serviceaccount:sample-app:default", 200, true},
{"sample-app", "system:serviceaccount:sample-app:default", 200, true},
{"sample-app-dev", "system:serviceaccount:kube-system:default", 200, true},
}
for _, tst := range saTests {
reqJSON := fmt.Sprintf(`
{
"spec":{
"resourceAttributes": {
"namespace":"%s",
"verb": "get"
},
"user":"%s"
}
}`, tst.namespace, tst.username)
result, err := postIndex(reqJSON)
if err != nil {
t.Error(err)
}
if result.StatusCode != tst.status {
t.Errorf("Expected status %d, got: %d (%s)", tst.status, result.StatusCode, reqJSON)
}
respJSON, err := parseResponse(result)
if err != nil {
t.Error(err)
}
if respJSON.Status.Allowed != tst.allowed {
t.Errorf("Bad response status, expected %t got: %t (%s)", tst.allowed, respJSON.Status.Allowed, reqJSON)
}
}
}
func TestNewAuthorizationBadRequest(t *testing.T) {
reqJSON := `asd`
result, err := postIndex(reqJSON)
if err != nil {
t.Error(err)
}
if result.StatusCode != 400 {
t.Errorf("Bad request expected: %d", result.StatusCode)
}
}
func TestNewAuthorizationByPath(t *testing.T) {
var saTests = []struct {
path string
verb string
username string
status int
}{
{"/apis", "get", "system:serviceaccount:random:default", 200},
{"/api", "get", "system:serviceaccount:random:default", 200},
{"/version", "get", "system:serviceaccount:random:default", 200},
{"/swaggerapi/apis/extensions/v1beta1", "get", "system:serviceaccount:random:default", 200},
{"/api/v1", "get", "system:serviceaccount:random:default", 403},
}
for _, tst := range saTests {
reqJSON := fmt.Sprintf(`
{
"spec":{
"nonResourceAttributes":{
"path": "%s",
"verb": "%s"
},
"user": "%s"
}
}`, tst.path, tst.verb, tst.username)
result, err := postIndex(reqJSON)
if err != nil {
t.Error(err)
}
if result.StatusCode != tst.status {
t.Errorf("Expected %d, got: %d, (%s)", tst.status, result.StatusCode, reqJSON)
}
}
}
func TestNewAuthorizationByVerb(t *testing.T) {
var saTests = []struct {
namespace string
verb string
username string
status int
}{
{"default", "watch", "system:serviceaccount:kube-system:default", 200},
{"default", "watch", "system:serviceaccount:random:default", 403},
{"default", "list", "system:serviceaccount:kube-system:default", 200},
{"default", "list", "system:serviceaccount:random:default", 403},
}
for _, tst := range saTests {
reqJSON := fmt.Sprintf(`
{
"spec":{
"resourceAttributes":{
"namespace":"%s",
"verb":"%s",
"resource": "services"
},
"user":"%s"
}
}`, tst.namespace, tst.verb, tst.username)
result, err := postIndex(reqJSON)
if err != nil {
t.Error(err)
}
if result.StatusCode != tst.status {
t.Errorf("Expected status %d, got: %d (%s)", tst.status, result.StatusCode, reqJSON)
}
}
}
func TestCustomVerb(t *testing.T) {
var saTests = []struct {
verb string
group string
resource string
username string
status int
}{
{"create", "extensions", "thirdpartyresources", "system:serviceaccount:random:default", 200},
}
for _, tst := range saTests {
reqJSON := fmt.Sprintf(`
{
"spec":{
"resourceAttributes":{
"group":"%s",
"verb":"%s",
"resource": "%s"
},
"user":"%s"
}
}`, tst.group, tst.verb, tst.resource, tst.username)
result, err := postIndex(reqJSON)
if err != nil {
t.Error(err)
}
if result.StatusCode != tst.status {
t.Errorf("Expected status %d, got: %d (%s)", tst.status, result.StatusCode, reqJSON)
}
}
}