-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthz_test.go
More file actions
222 lines (168 loc) · 5.48 KB
/
authz_test.go
File metadata and controls
222 lines (168 loc) · 5.48 KB
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
// SPDX-License-Identifier: EUPL-1.2
package api_test
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/casbin/casbin/v2"
"github.com/casbin/casbin/v2/model"
"github.com/gin-gonic/gin"
api "dappco.re/go/core/api"
)
// casbinModel is a minimal RESTful ACL model for testing authorisation.
const casbinModel = `
[request_definition]
r = sub, obj, act
[policy_definition]
p = sub, obj, act
[policy_effect]
e = some(where (p.eft == allow))
[matchers]
m = r.sub == p.sub && keyMatch(r.obj, p.obj) && r.act == p.act
`
// newTestEnforcer creates a Casbin enforcer from the inline model and adds
// the given policies programmatically. Each policy is a [subject, object, action] triple.
func newTestEnforcer(t *testing.T, policies [][3]string) *casbin.Enforcer {
t.Helper()
m, err := model.NewModelFromString(casbinModel)
if err != nil {
t.Fatalf("failed to create casbin model: %v", err)
}
e, err := casbin.NewEnforcer(m)
if err != nil {
t.Fatalf("failed to create casbin enforcer: %v", err)
}
for _, p := range policies {
if _, err := e.AddPolicy(p[0], p[1], p[2]); err != nil {
t.Fatalf("failed to add policy %v: %v", p, err)
}
}
return e
}
// setBasicAuth sets the HTTP Basic Authentication header on a request.
func setBasicAuth(req *http.Request, user, pass string) {
req.SetBasicAuth(user, pass)
}
// ── WithAuthz ─────────────────────────────────────────────────────────────
func TestWithAuthz_Good_AllowsPermittedRequest(t *testing.T) {
gin.SetMode(gin.TestMode)
enforcer := newTestEnforcer(t, [][3]string{
{"alice", "/stub/*", "GET"},
})
e, _ := api.New(api.WithAuthz(enforcer))
e.Register(&stubGroup{})
h := e.Handler()
w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodGet, "/stub/ping", nil)
setBasicAuth(req, "alice", "secret")
h.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("expected 200 for permitted request, got %d", w.Code)
}
}
func TestWithAuthz_Bad_DeniesUnpermittedRequest(t *testing.T) {
gin.SetMode(gin.TestMode)
// Only alice is permitted; bob has no policy entry.
enforcer := newTestEnforcer(t, [][3]string{
{"alice", "/stub/*", "GET"},
})
e, _ := api.New(api.WithAuthz(enforcer))
e.Register(&stubGroup{})
h := e.Handler()
w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodGet, "/stub/ping", nil)
setBasicAuth(req, "bob", "secret")
h.ServeHTTP(w, req)
if w.Code != http.StatusForbidden {
t.Fatalf("expected 403 for unpermitted request, got %d", w.Code)
}
}
func TestWithAuthz_Good_DifferentMethodsEvaluatedSeparately(t *testing.T) {
gin.SetMode(gin.TestMode)
// alice can GET but not DELETE.
enforcer := newTestEnforcer(t, [][3]string{
{"alice", "/stub/*", "GET"},
})
e, _ := api.New(api.WithAuthz(enforcer))
e.Register(&stubGroup{})
h := e.Handler()
// GET should succeed.
w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodGet, "/stub/ping", nil)
setBasicAuth(req, "alice", "secret")
h.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("expected 200 for GET, got %d", w.Code)
}
// DELETE should be denied (no policy for DELETE).
w = httptest.NewRecorder()
req, _ = http.NewRequest(http.MethodDelete, "/stub/ping", nil)
setBasicAuth(req, "alice", "secret")
h.ServeHTTP(w, req)
if w.Code != http.StatusForbidden {
t.Fatalf("expected 403 for DELETE, got %d", w.Code)
}
}
func TestWithAuthz_Good_CombinesWithOtherMiddleware(t *testing.T) {
gin.SetMode(gin.TestMode)
enforcer := newTestEnforcer(t, [][3]string{
{"alice", "/stub/*", "GET"},
})
e, _ := api.New(
api.WithRequestID(),
api.WithAuthz(enforcer),
)
e.Register(&stubGroup{})
h := e.Handler()
w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodGet, "/stub/ping", nil)
setBasicAuth(req, "alice", "secret")
h.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("expected 200, got %d", w.Code)
}
// Both authz (allowed) and request ID should be active.
if w.Header().Get("X-Request-ID") == "" {
t.Fatal("expected X-Request-ID header from WithRequestID")
}
}
// casbinWildcardModel extends the base model with a matcher that treats
// "*" as a wildcard subject, allowing any authenticated user through.
const casbinWildcardModel = `
[request_definition]
r = sub, obj, act
[policy_definition]
p = sub, obj, act
[policy_effect]
e = some(where (p.eft == allow))
[matchers]
m = (r.sub == p.sub || p.sub == "*") && keyMatch(r.obj, p.obj) && r.act == p.act
`
func TestWithAuthz_Ugly_WildcardPolicyAllowsAll(t *testing.T) {
gin.SetMode(gin.TestMode)
// Use a model whose matcher treats "*" as a wildcard subject.
m, err := model.NewModelFromString(casbinWildcardModel)
if err != nil {
t.Fatalf("failed to create casbin model: %v", err)
}
enforcer, err := casbin.NewEnforcer(m)
if err != nil {
t.Fatalf("failed to create casbin enforcer: %v", err)
}
if _, err := enforcer.AddPolicy("*", "/stub/*", "GET"); err != nil {
t.Fatalf("failed to add wildcard policy: %v", err)
}
e, _ := api.New(api.WithAuthz(enforcer))
e.Register(&stubGroup{})
h := e.Handler()
// Any user should be allowed by the wildcard policy.
for _, user := range []string{"alice", "bob", "charlie"} {
w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodGet, "/stub/ping", nil)
setBasicAuth(req, user, "secret")
h.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("expected 200 for user %q with wildcard policy, got %d", user, w.Code)
}
}
}