forked from gofiber/contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiber.go
104 lines (95 loc) · 2.69 KB
/
fiber.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
package opafiber
import (
"context"
"fmt"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/utils"
"github.com/open-policy-agent/opa/rego"
"io"
)
type InputCreationFunc func(c *fiber.Ctx) (map[string]interface{}, error)
type Config struct {
RegoPolicy io.Reader
RegoQuery string
IncludeHeaders []string
IncludeQueryString bool
DeniedStatusCode int
DeniedResponseMessage string
InputCreationMethod InputCreationFunc
}
func New(cfg Config) fiber.Handler {
err := cfg.fillAndValidate()
if err != nil {
panic(err)
}
readedBytes, err := io.ReadAll(cfg.RegoPolicy)
if err != nil {
panic(fmt.Sprint("could not read rego policy %w", err))
}
query, err := rego.New(
rego.Query(cfg.RegoQuery),
rego.Module("policy.rego", utils.UnsafeString(readedBytes)),
).PrepareForEval(context.Background())
if err != nil {
panic(fmt.Sprint("rego policy error: %w", err))
}
return func(c *fiber.Ctx) error {
input, err := cfg.InputCreationMethod(c)
if err != nil {
c.Response().SetStatusCode(fiber.StatusInternalServerError)
c.Response().SetBodyString(fmt.Sprintf("Error creating input: %s", err))
return err
}
if cfg.IncludeQueryString {
queryStringData := make(map[string][]string)
c.Request().URI().QueryArgs().VisitAll(func(key, value []byte) {
queryStringData[utils.UnsafeString(key)] = append(queryStringData[utils.UnsafeString(key)], utils.UnsafeString(value))
})
input["query"] = queryStringData
}
if len(cfg.IncludeHeaders) > 0 {
headers := make(map[string]string)
for _, header := range cfg.IncludeHeaders {
headers[header] = c.Get(header)
}
input["headers"] = headers
}
res, err := query.Eval(context.Background(), rego.EvalInput(input))
if err != nil {
c.Response().SetStatusCode(fiber.StatusInternalServerError)
c.Response().SetBodyString(fmt.Sprintf("Error evaluating rego policy: %s", err))
return err
}
if !res.Allowed() {
c.Response().SetStatusCode(cfg.DeniedStatusCode)
c.Response().SetBodyString(cfg.DeniedResponseMessage)
return nil
}
return c.Next()
}
}
func (c *Config) fillAndValidate() error {
if c.RegoQuery == "" {
return fmt.Errorf("rego query can not be empty")
}
if c.DeniedStatusCode == 0 {
c.DeniedStatusCode = fiber.StatusBadRequest
}
if c.DeniedResponseMessage == "" {
c.DeniedResponseMessage = fiber.ErrBadRequest.Error()
}
if c.IncludeHeaders == nil {
c.IncludeHeaders = []string{}
}
if c.InputCreationMethod == nil {
c.InputCreationMethod = defaultInput
}
return nil
}
func defaultInput(ctx *fiber.Ctx) (map[string]interface{}, error) {
input := map[string]interface{}{
"method": ctx.Method(),
"path": ctx.Path(),
}
return input, nil
}