feat(rewriter): add redirect(), get_cookie(), add_cookie() methods to rule engine (#36)
Some checks are pending
Cadoles/bouncer/pipeline/pr-develop Build started...

This commit is contained in:
2024-09-25 15:50:13 +02:00
committed by wpetit
parent cb9260ac2b
commit 04b41baea3
11 changed files with 710 additions and 110 deletions

View File

@ -3,6 +3,7 @@ package http
import (
"context"
"fmt"
"net/http"
"net/url"
"strconv"
"strings"
@ -28,7 +29,7 @@ func setRequestHostFunc() expr.Option {
return nil, errors.WithStack(err)
}
r, ok := ctxRequest(ctx)
r, ok := CtxRequest(ctx)
if !ok {
return nil, errors.New("could not find http request in context")
}
@ -60,7 +61,7 @@ func setRequestURLFunc() expr.Option {
return false, errors.WithStack(err)
}
r, ok := ctxRequest(ctx)
r, ok := CtxRequest(ctx)
if !ok {
return nil, errors.New("could not find http request in context")
}
@ -89,7 +90,7 @@ func addRequestHeaderFunc() expr.Option {
value := formatValue(params[2])
r, ok := ctxRequest(ctx)
r, ok := CtxRequest(ctx)
if !ok {
return nil, errors.New("could not find http request in context")
}
@ -118,7 +119,7 @@ func setRequestHeaderFunc() expr.Option {
value := formatValue(params[2])
r, ok := ctxRequest(ctx)
r, ok := CtxRequest(ctx)
if !ok {
return nil, errors.New("could not find http request in context")
}
@ -145,7 +146,7 @@ func delRequestHeadersFunc() expr.Option {
return nil, errors.WithStack(err)
}
r, ok := ctxRequest(ctx)
r, ok := CtxRequest(ctx)
if !ok {
return nil, errors.New("could not find http request in context")
}
@ -167,6 +168,145 @@ func delRequestHeadersFunc() expr.Option {
)
}
type CookieVar struct {
Name string `expr:"name"`
Value string `expr:"value"`
Path string `expr:"path"`
Domain string `expr:"domain"`
Expires time.Time `expr:"expires"`
MaxAge int `expr:"max_age"`
Secure bool `expr:"secure"`
HttpOnly bool `expr:"http_only"`
SameSite http.SameSite `expr:"same_site"`
}
func getRequestCookieFunc() expr.Option {
return expr.Function(
"get_cookie",
func(params ...any) (any, error) {
ctx, err := rule.Assert[context.Context](params[0])
if err != nil {
return nil, errors.WithStack(err)
}
name, err := rule.Assert[string](params[1])
if err != nil {
return nil, errors.WithStack(err)
}
r, ok := CtxRequest(ctx)
if !ok {
return nil, errors.New("could not find http request in context")
}
cookie, err := r.Cookie(name)
if err != nil && !errors.Is(err, http.ErrNoCookie) {
return nil, errors.WithStack(err)
}
if cookie == nil {
return nil, nil
}
return CookieVar{
Name: cookie.Name,
Value: cookie.Value,
Path: cookie.Path,
Domain: cookie.Domain,
Expires: cookie.Expires,
MaxAge: cookie.MaxAge,
Secure: cookie.Secure,
HttpOnly: cookie.HttpOnly,
SameSite: cookie.SameSite,
}, nil
},
new(func(context.Context, string) CookieVar),
)
}
func addRequestCookieFunc() expr.Option {
return expr.Function(
"add_cookie",
func(params ...any) (any, error) {
ctx, err := rule.Assert[context.Context](params[0])
if err != nil {
return nil, errors.WithStack(err)
}
values, err := rule.Assert[map[string]any](params[1])
if err != nil {
return nil, errors.WithStack(err)
}
cookie, err := cookieFrom(values)
if err != nil {
return nil, errors.WithStack(err)
}
r, ok := CtxRequest(ctx)
if !ok {
return nil, errors.New("could not find http request in context")
}
r.AddCookie(cookie)
return true, nil
},
new(func(context.Context, map[string]any) bool),
)
}
func cookieFrom(values map[string]any) (*http.Cookie, error) {
cookie := &http.Cookie{}
if name, ok := values["name"].(string); ok {
cookie.Name = name
}
if value, ok := values["value"].(string); ok {
cookie.Value = value
}
if domain, ok := values["domain"].(string); ok {
cookie.Domain = domain
}
if path, ok := values["path"].(string); ok {
cookie.Path = path
}
if httpOnly, ok := values["http_only"].(bool); ok {
cookie.HttpOnly = httpOnly
}
if maxAge, ok := values["max_age"].(int); ok {
cookie.MaxAge = maxAge
}
if secure, ok := values["secure"].(bool); ok {
cookie.Secure = secure
}
if sameSite, ok := values["same_site"].(http.SameSite); ok {
cookie.SameSite = sameSite
} else if sameSite, ok := values["same_site"].(int); ok {
cookie.SameSite = http.SameSite(sameSite)
}
if expires, ok := values["expires"].(time.Time); ok {
cookie.Expires = expires
} else if rawExpires, ok := values["expires"].(string); ok {
expires, err := time.Parse(http.TimeFormat, rawExpires)
if err != nil {
return nil, errors.WithStack(err)
}
cookie.Expires = expires
}
return cookie, nil
}
func formatValue(v any) string {
var value string
switch v := v.(type) {