-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathopa.go
282 lines (238 loc) · 6.42 KB
/
opa.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package authz
import (
"context"
"embed"
"errors"
"fmt"
"strings"
"github.com/dzungtran/echo-rest-api/modules/core/domains"
"github.com/dzungtran/echo-rest-api/pkg/contexts"
"github.com/dzungtran/echo-rest-api/pkg/logger"
"github.com/labstack/echo/v4"
"github.com/open-policy-agent/opa/ast"
"github.com/open-policy-agent/opa/rego"
"github.com/open-policy-agent/opa/storage/inmem"
"github.com/open-policy-agent/opa/util"
"github.com/tidwall/sjson"
)
var (
//go:embed data.json
dataFile []byte
//go:embed routes.json
routesFile []byte
//go:embed rego/*.rego
regoFs embed.FS
//go:embed rego/deny/*.rego
regoDenyFs embed.FS
//go:embed rego/utils/*.rego
regoUtilsFs embed.FS
regoInstance *rego.Rego
preparedQuery rego.PreparedEvalQuery
)
type opaInputOpts struct {
ResourcePermissions map[string][]string
ExtraData map[string]interface{}
RequestEndpoint string
RequestMethod string
Org *domains.Org
}
type CallOPAInputOption struct {
applyFunc func(*opaInputOpts)
}
func init() {
regoContents := map[string]string{}
readRegoFiles(regoFs, "rego", regoContents)
readRegoFiles(regoDenyFs, "rego/deny", regoContents)
readRegoFiles(regoUtilsFs, "rego/utils", regoContents)
// Compile the module. The keys are used as identifiers in error messages.
compiler, err := ast.CompileModules(regoContents)
if err != nil {
logger.Log().Fatalf("error while init rego compiler, details: %s", err.Error())
}
var jsonData map[string]interface{}
var routesData map[string]interface{}
err = util.UnmarshalJSON(routesFile, &routesData)
if err != nil {
logger.Log().Fatalf("error while init rego instance, details: %s", err.Error())
}
dataFile, err = sjson.SetBytes(dataFile, "endpoints_acl", routesData)
if err != nil {
logger.Log().Fatalf("error while build data file, details: %s", err.Error())
}
err = util.UnmarshalJSON(dataFile, &jsonData)
if err != nil {
logger.Log().Fatalf("error while init rego instance, details: %s", err.Error())
}
// Manually create the storage layer. inmem.NewFromObject returns an
// in-memory store containing the supplied data.
store := inmem.NewFromObject(jsonData)
/*
allow = data.authz.allow
deny = data.authz.deny
req = data.authz.usr_role
*/
// Create new query that returns the value
regoInstance = rego.New(
rego.Query(`
allow = data.authz.allow
deny = data.authz.deny
`),
rego.Store(store),
rego.Compiler(compiler),
)
ctx := context.Background()
preparedQuery, err = regoInstance.PrepareForEval(ctx)
if err != nil {
logger.Log().Fatalf("error while prepared eval opa, details: %v", err.Error())
}
}
func readRegoFiles(dfs embed.FS, folderName string, filesContent map[string]string) {
dirs, err := dfs.ReadDir(folderName)
if err != nil {
logger.Log().Fatalf("error while init rego compiler, details: %s", err.Error())
}
for _, d := range dirs {
if d.IsDir() {
continue
}
f, err := d.Info()
if err != nil {
continue
}
fName := folderName + "/" + f.Name()
fcb, err := dfs.ReadFile(fName)
if err != nil {
continue
}
filesContent[fName] = string(fcb)
}
}
func WithInputResourcePermissions(resPerms map[string][]string) CallOPAInputOption {
return CallOPAInputOption{
applyFunc: func(oio *opaInputOpts) {
if oio.ResourcePermissions == nil {
oio.ResourcePermissions = make(map[string][]string)
}
if resPerms != nil {
currResPerms := oio.ResourcePermissions
for perm, resIds := range resPerms {
currResIds := currResPerms[perm]
if currResIds == nil {
currResIds = make([]string, 0)
}
if len(resIds) > 0 {
currResIds = append(currResIds, resIds...)
}
currResPerms[perm] = currResIds
}
oio.ResourcePermissions = currResPerms
}
},
}
}
func WithInputRequestMethod(reqMethod string) CallOPAInputOption {
return CallOPAInputOption{
applyFunc: func(oio *opaInputOpts) {
reqMethod = strings.Trim(reqMethod, " ")
if reqMethod != "" {
oio.RequestMethod = reqMethod
}
},
}
}
func WithInputRequestEndpoint(reqEndpoint string) CallOPAInputOption {
return CallOPAInputOption{
applyFunc: func(oio *opaInputOpts) {
reqEndpoint = strings.Trim(reqEndpoint, " ")
if reqEndpoint != "" {
oio.RequestEndpoint = reqEndpoint
}
},
}
}
func WithInputExtraData(key string, data interface{}) CallOPAInputOption {
return CallOPAInputOption{
applyFunc: func(oio *opaInputOpts) {
if oio.ExtraData == nil {
oio.ExtraData = make(map[string]interface{})
}
oio.ExtraData[key] = data
},
}
}
func WithInputOrg(org *domains.Org) CallOPAInputOption {
return CallOPAInputOption{
applyFunc: func(oio *opaInputOpts) {
oio.Org = org
},
}
}
func CheckPolicies(user *domains.UserWithRoles, callOpts ...CallOPAInputOption) (denyMsg []string, err error) {
ctx := context.Background()
opts := appliedOPAInputOption(callOpts)
input := map[string]interface{}{
"user": user,
}
if opts.Org != nil {
input["org"] = opts.Org
}
if len(opts.ExtraData) > 0 {
for k, v := range opts.ExtraData {
input[k] = v
}
}
// Apply options to input
if opts.RequestMethod != "" && opts.RequestEndpoint != "" {
input["method"] = opts.RequestMethod
input["endpoint"] = opts.RequestEndpoint
}
// Run evaluation.
rs, err := preparedQuery.Eval(ctx, rego.EvalInput(input))
if err != nil {
logger.Log().Errorf("error while eval opa input, details %v", err.Error())
return
}
if len(rs) == 0 {
err = errors.New("empty decision result")
return
}
var result bool
result, ok := rs[0].Bindings["allow"].(bool)
if msg, _ := rs[0].Bindings["deny"].([]interface{}); len(msg) > 0 {
denyMsg = make([]string, 0)
for _, v := range msg {
denyMsg = append(denyMsg, fmt.Sprint(v))
}
}
if !ok {
logger.Log().Errorf("unexpected decision result, details %v", result)
err = errors.New("unexpected decision result")
return
}
if !result {
err = errors.New("forbidden")
return
}
return
}
func CheckPoliciesContext(c echo.Context, callOpts ...CallOPAInputOption) (denyMsg []string, err error) {
u, err := contexts.GetUserFromContext(c)
if err != nil {
return
}
callOpts = append(callOpts,
WithInputRequestMethod(c.Request().Method),
WithInputRequestEndpoint(c.Path()),
)
return CheckPolicies(u, callOpts...)
}
func appliedOPAInputOption(callOptions []CallOPAInputOption) *opaInputOpts {
if len(callOptions) == 0 {
return &opaInputOpts{}
}
optCopy := &opaInputOpts{}
for _, f := range callOptions {
f.applyFunc(optCopy)
}
return optCopy
}