-
Notifications
You must be signed in to change notification settings - Fork 1
/
opa_test.go
132 lines (113 loc) · 3.79 KB
/
opa_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
package traefikopa
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestTraefikOPA(t *testing.T) {
tests := []struct {
name string
opaResponse func(w http.ResponseWriter, r *http.Request)
requests []struct {
method, path, body, authHeader string
expectedCode int
}
}{
{
name: "Test Policy Decision from Body",
opaResponse: mockOPAServerWithRequestBody,
requests: []struct {
method, path, body, authHeader string
expectedCode int
}{
{"POST", "/test", `{"profileID": "12345", "username": "testuser", "role": "user"}`, "12345", http.StatusOK},
{"POST", "/test", `{"profileID": "67890", "username": "testuser", "role": "user"}`, "not_67890", http.StatusForbidden},
},
},
{
name: "Test Method and Path",
opaResponse: mockOPAServerWithMethodPath,
requests: []struct {
method, path, body, authHeader string
expectedCode int
}{
{"GET", "/test", "", "", http.StatusOK},
{"POST", "/test", "", "", http.StatusForbidden},
{"GET", "/forbidden", "", "", http.StatusForbidden},
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
opaServer, tearDown := createMockOPAServer(test.opaResponse)
defer tearDown()
middleware := setupMiddleware(opaServer.URL)
for _, reqTest := range test.requests {
req := httptest.NewRequest(reqTest.method, reqTest.path, strings.NewReader(reqTest.body))
req.Header.Set("Authorization", reqTest.authHeader)
rr := httptest.NewRecorder()
middleware.ServeHTTP(rr, req)
if rr.Code != reqTest.expectedCode {
t.Errorf("Expected status %v but received %v", reqTest.expectedCode, rr.Code)
}
}
})
}
}
func setupMiddleware(opaURL string) http.Handler {
config := CreateConfig()
config.URL = opaURL
stubNextHandler := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
rw.WriteHeader(http.StatusOK)
})
middleware, err := New(context.TODO(), stubNextHandler, config, "test-middleware")
if err != nil {
panic("Failed to create middleware: " + err.Error())
}
return middleware
}
func createMockOPAServer(handlerFunc func(w http.ResponseWriter, r *http.Request)) (*httptest.Server, func()) {
server := httptest.NewServer(http.HandlerFunc(handlerFunc))
return server, server.Close
}
func mockOPAServerWithRequestBody(w http.ResponseWriter, r *http.Request) {
inputQuery := r.URL.Query().Get("input")
var inputData map[string]interface{}
err := json.Unmarshal([]byte(inputQuery), &inputData)
if err != nil {
http.Error(w, "Invalid input", http.StatusBadRequest)
return
}
authzHeader, _ := inputData["http"].(map[string]interface{})["headers"].(map[string]interface{})["Authorization"].(string)
body, _ := inputData["http"].(map[string]interface{})["body"].(string)
var bodyData map[string]interface{}
json.Unmarshal([]byte(body), &bodyData)
profileID, _ := bodyData["profileID"].(string)
if authzHeader == profileID {
w.Write([]byte(`{"result": {"allow": true}}`))
} else {
// Default deny
w.Write([]byte(`{"result": {"allow": false}}`))
}
}
func mockOPAServerWithMethodPath(w http.ResponseWriter, r *http.Request) {
inputQuery := r.URL.Query().Get("input")
var inputData map[string]interface{}
err := json.Unmarshal([]byte(inputQuery), &inputData)
if err != nil {
http.Error(w, "Invalid input", http.StatusBadRequest)
return
}
method, _ := inputData["http"].(map[string]interface{})["method"].(string)
path, _ := inputData["http"].(map[string]interface{})["path"].(string)
if method == "GET" && path == "/test" {
w.Write([]byte(`{"result": {"allow": true}}`))
} else if path == "/forbidden" {
w.Write([]byte(`{"result": {"allow": false}}`))
} else {
w.Write([]byte(`{"result": {"allow": false}}`))
}
}