-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfiber_middleware.go
94 lines (86 loc) · 2.71 KB
/
fiber_middleware.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
package opamiddleware
import (
"errors"
"github.com/Joffref/opa-middleware/config"
"github.com/Joffref/opa-middleware/internal"
"github.com/gofiber/fiber/v2"
"github.com/valyala/fasthttp"
"net/http"
)
// FiberInputCreationMethod is the method that is used to create the input for the policy.
type FiberInputCreationMethod func(c *fiber.Ctx) (map[string]interface{}, error)
type FiberMiddleware struct {
// Config is the configuration for the middleware.
Config *config.Config
// InputCreationMethod is a function that returns the value to be sent to the OPA server.
InputCreationMethod FiberInputCreationMethod `json:"binding_method,omitempty"`
}
// NewFiberMiddleware is the constructor for the opa fiber middleware.
func NewFiberMiddleware(cfg *config.Config, input FiberInputCreationMethod) (*FiberMiddleware, error) {
err := cfg.Validate()
if err != nil {
return nil, err
}
if input == nil && cfg.InputCreationMethod == nil {
return nil, errors.New("[opa-middleware-fiber] InputCreationMethod must be provided")
}
return &FiberMiddleware{
Config: cfg,
InputCreationMethod: input,
}, nil
}
// Use returns the handler for the middleware that is used by fiber to evaluate the request against the policy.
func (g *FiberMiddleware) Use() func(c *fiber.Ctx) error {
return func(c *fiber.Ctx) error {
if g.Config.Debug {
g.Config.Logger.Printf("[opa-middleware-fiber] Request received")
}
result, err := g.query(c)
if err != nil {
if g.Config.Debug {
g.Config.Logger.Printf("[opa-middleware-fiber] Error: %s", err.Error())
}
c.Status(http.StatusInternalServerError)
}
if g.Config.Debug {
g.Config.Logger.Printf("[opa-middleware-fiber] Result: %t", result)
}
if result != g.Config.ExceptedResult {
c.Status(g.Config.DeniedStatusCode)
return errors.New("[opa-middleware-fiber] Access denied")
}
err = c.Next()
if err != nil {
return err
}
return nil
}
}
func (g *FiberMiddleware) query(c *fiber.Ctx) (bool, error) {
bind, err := g.InputCreationMethod(c)
if err != nil {
return !g.Config.ExceptedResult, err
}
if g.Config.URL != "" {
input := make(map[string]interface{})
input["input"] = bind
return internal.QueryURL(transformFastHTTP(c.Context()), g.Config, input)
}
return internal.QueryPolicy(transformFastHTTP(c.Context()), g.Config, bind)
}
func transformFastHTTP(ctx *fasthttp.RequestCtx) *http.Request {
req := &http.Request{
Header: make(http.Header),
}
headers := make(map[string][]string)
ctx.Request.Header.VisitAll(func(key, value []byte) {
headers[string(key)] = append(headers[string(key)], string(value))
})
for k, v := range headers {
for _, vv := range v {
req.Header.Add(k, vv)
}
}
req = req.WithContext(ctx)
return req
}