fix(rewriter): prevent mixing of cached rule engines (#44)
This commit is contained in:
parent
16fa751dc7
commit
c611705d45
|
@ -10,6 +10,9 @@ import (
|
||||||
"forge.cadoles.com/Cadoles/go-proxy"
|
"forge.cadoles.com/Cadoles/go-proxy"
|
||||||
"forge.cadoles.com/Cadoles/go-proxy/wildcard"
|
"forge.cadoles.com/Cadoles/go-proxy/wildcard"
|
||||||
"forge.cadoles.com/cadoles/bouncer/internal/proxy/director"
|
"forge.cadoles.com/cadoles/bouncer/internal/proxy/director"
|
||||||
|
"forge.cadoles.com/cadoles/bouncer/internal/proxy/director/layer/util"
|
||||||
|
"forge.cadoles.com/cadoles/bouncer/internal/rule"
|
||||||
|
ruleHTTP "forge.cadoles.com/cadoles/bouncer/internal/rule/http"
|
||||||
"forge.cadoles.com/cadoles/bouncer/internal/store"
|
"forge.cadoles.com/cadoles/bouncer/internal/store"
|
||||||
"github.com/Masterminds/sprig/v3"
|
"github.com/Masterminds/sprig/v3"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
@ -21,6 +24,8 @@ type Layer struct {
|
||||||
auth Authenticator
|
auth Authenticator
|
||||||
debug bool
|
debug bool
|
||||||
|
|
||||||
|
ruleEngineCache *util.RuleEngineCache[*Vars, *LayerOptions]
|
||||||
|
|
||||||
templateDir string
|
templateDir string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -74,7 +79,7 @@ func (l *Layer) Middleware(layer *store.Layer) proxy.Middleware {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := l.applyRules(ctx, r, options, user); err != nil {
|
if err := l.applyRules(ctx, r, layer, options, user); err != nil {
|
||||||
if errors.Is(err, ErrForbidden) {
|
if errors.Is(err, ErrForbidden) {
|
||||||
l.renderForbiddenPage(w, r, layer, options, user)
|
l.renderForbiddenPage(w, r, layer, options, user)
|
||||||
return
|
return
|
||||||
|
@ -189,6 +194,18 @@ func NewLayer(layerType store.LayerType, auth Authenticator, funcs ...OptionFunc
|
||||||
opts := NewOptions(funcs...)
|
opts := NewOptions(funcs...)
|
||||||
|
|
||||||
return &Layer{
|
return &Layer{
|
||||||
|
ruleEngineCache: util.NewInMemoryRuleEngineCache[*Vars, *LayerOptions](func(options *LayerOptions) (*rule.Engine[*Vars], error) {
|
||||||
|
engine, err := rule.NewEngine[*Vars](
|
||||||
|
rule.WithRules(options.Rules...),
|
||||||
|
rule.WithExpr(getAuthnAPI()...),
|
||||||
|
ruleHTTP.WithRequestFuncs(),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.WithStack(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return engine, nil
|
||||||
|
}),
|
||||||
layerType: layerType,
|
layerType: layerType,
|
||||||
auth: auth,
|
auth: auth,
|
||||||
templateDir: opts.TemplateDir,
|
templateDir: opts.TemplateDir,
|
||||||
|
|
|
@ -28,12 +28,13 @@ func DefaultLayerOptions() LayerOptions {
|
||||||
return LayerOptions{
|
return LayerOptions{
|
||||||
MatchURLs: []string{"*"},
|
MatchURLs: []string{"*"},
|
||||||
Rules: []string{
|
Rules: []string{
|
||||||
"del_headers('Remote-*')",
|
"del_headers(ctx, 'Remote-*')",
|
||||||
"set_header('Remote-User', user.subject)",
|
"set_header(ctx,'Remote-User', vars.user.subject)",
|
||||||
`map(
|
`map(
|
||||||
toPairs(user.attrs), {
|
toPairs(vars.user.attrs), {
|
||||||
let name = replace(lower(string(get(#, 0))), '_', '-');
|
let name = replace(lower(string(get(#, 0))), '_', '-');
|
||||||
set_header(
|
set_header(
|
||||||
|
ctx,
|
||||||
'Remote-User-Attr-' + name,
|
'Remote-User-Attr-' + name,
|
||||||
get(#, 1)
|
get(#, 1)
|
||||||
)
|
)
|
||||||
|
|
|
@ -4,8 +4,8 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"forge.cadoles.com/cadoles/bouncer/internal/rule"
|
|
||||||
ruleHTTP "forge.cadoles.com/cadoles/bouncer/internal/rule/http"
|
ruleHTTP "forge.cadoles.com/cadoles/bouncer/internal/rule/http"
|
||||||
|
"forge.cadoles.com/cadoles/bouncer/internal/store"
|
||||||
"github.com/expr-lang/expr"
|
"github.com/expr-lang/expr"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
@ -14,17 +14,11 @@ type Vars struct {
|
||||||
User *User `expr:"user"`
|
User *User `expr:"user"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *Layer) applyRules(ctx context.Context, r *http.Request, options *LayerOptions, user *User) error {
|
func (l *Layer) applyRules(ctx context.Context, r *http.Request, layer *store.Layer, options *LayerOptions, user *User) error {
|
||||||
rules := options.Rules
|
key := string(layer.Proxy) + "-" + string(layer.Name)
|
||||||
if len(rules) == 0 {
|
revisionedEngine := l.ruleEngineCache.Get(key)
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
engine, err := rule.NewEngine[*Vars](
|
engine, err := revisionedEngine.Get(ctx, layer.Revision, options)
|
||||||
rule.WithRules(options.Rules...),
|
|
||||||
rule.WithExpr(getAuthnAPI()...),
|
|
||||||
ruleHTTP.WithRequestFuncs(),
|
|
||||||
)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.WithStack(err)
|
return errors.WithStack(err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,8 +16,8 @@ import (
|
||||||
const LayerType store.LayerType = "rewriter"
|
const LayerType store.LayerType = "rewriter"
|
||||||
|
|
||||||
type Layer struct {
|
type Layer struct {
|
||||||
requestRuleEngine *util.RevisionedRuleEngine[*RequestVars, *LayerOptions]
|
requestRuleEngineCache *util.RuleEngineCache[*RequestVars, *LayerOptions]
|
||||||
responseRuleEngine *util.RevisionedRuleEngine[*ResponseVars, *LayerOptions]
|
responseRuleEngineCache *util.RuleEngineCache[*ResponseVars, *LayerOptions]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *Layer) LayerType() store.LayerType {
|
func (l *Layer) LayerType() store.LayerType {
|
||||||
|
@ -42,7 +42,7 @@ func (l *Layer) Middleware(layer *store.Layer) proxy.Middleware {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := l.applyRequestRules(ctx, r, layer.Revision, options); err != nil {
|
if err := l.applyRequestRules(ctx, r, layer, options); err != nil {
|
||||||
var redirect *errRedirect
|
var redirect *errRedirect
|
||||||
if errors.As(err, &redirect) {
|
if errors.As(err, &redirect) {
|
||||||
http.Redirect(w, r, redirect.URL(), redirect.StatusCode())
|
http.Redirect(w, r, redirect.URL(), redirect.StatusCode())
|
||||||
|
@ -76,7 +76,7 @@ func (l *Layer) ResponseTransformer(layer *store.Layer) proxy.ResponseTransforme
|
||||||
|
|
||||||
ctx := r.Request.Context()
|
ctx := r.Request.Context()
|
||||||
|
|
||||||
if err := l.applyResponseRules(ctx, r, layer.Revision, options); err != nil {
|
if err := l.applyResponseRules(ctx, r, layer, options); err != nil {
|
||||||
return errors.WithStack(err)
|
return errors.WithStack(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -86,7 +86,7 @@ func (l *Layer) ResponseTransformer(layer *store.Layer) proxy.ResponseTransforme
|
||||||
|
|
||||||
func New(funcs ...OptionFunc) *Layer {
|
func New(funcs ...OptionFunc) *Layer {
|
||||||
return &Layer{
|
return &Layer{
|
||||||
requestRuleEngine: util.NewRevisionedRuleEngine(func(options *LayerOptions) (*rule.Engine[*RequestVars], error) {
|
requestRuleEngineCache: util.NewInMemoryRuleEngineCache(func(options *LayerOptions) (*rule.Engine[*RequestVars], error) {
|
||||||
engine, err := rule.NewEngine[*RequestVars](
|
engine, err := rule.NewEngine[*RequestVars](
|
||||||
rule.WithRules(options.Rules.Request...),
|
rule.WithRules(options.Rules.Request...),
|
||||||
ruleHTTP.WithRequestFuncs(),
|
ruleHTTP.WithRequestFuncs(),
|
||||||
|
@ -98,7 +98,7 @@ func New(funcs ...OptionFunc) *Layer {
|
||||||
|
|
||||||
return engine, nil
|
return engine, nil
|
||||||
}),
|
}),
|
||||||
responseRuleEngine: util.NewRevisionedRuleEngine(func(options *LayerOptions) (*rule.Engine[*ResponseVars], error) {
|
responseRuleEngineCache: util.NewInMemoryRuleEngineCache(func(options *LayerOptions) (*rule.Engine[*ResponseVars], error) {
|
||||||
engine, err := rule.NewEngine[*ResponseVars](
|
engine, err := rule.NewEngine[*ResponseVars](
|
||||||
rule.WithRules(options.Rules.Response...),
|
rule.WithRules(options.Rules.Response...),
|
||||||
ruleHTTP.WithResponseFuncs(),
|
ruleHTTP.WithResponseFuncs(),
|
||||||
|
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"forge.cadoles.com/cadoles/bouncer/internal/proxy/director"
|
"forge.cadoles.com/cadoles/bouncer/internal/proxy/director"
|
||||||
"forge.cadoles.com/cadoles/bouncer/internal/rule"
|
"forge.cadoles.com/cadoles/bouncer/internal/rule"
|
||||||
ruleHTTP "forge.cadoles.com/cadoles/bouncer/internal/rule/http"
|
ruleHTTP "forge.cadoles.com/cadoles/bouncer/internal/rule/http"
|
||||||
|
"forge.cadoles.com/cadoles/bouncer/internal/store"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -87,13 +88,13 @@ func fromRequest(r *http.Request) RequestVar {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *Layer) applyRequestRules(ctx context.Context, r *http.Request, layerRevision int, options *LayerOptions) error {
|
func (l *Layer) applyRequestRules(ctx context.Context, r *http.Request, layer *store.Layer, options *LayerOptions) error {
|
||||||
rules := options.Rules.Request
|
rules := options.Rules.Request
|
||||||
if len(rules) == 0 {
|
if len(rules) == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
engine, err := l.getRequestRuleEngine(ctx, layerRevision, options)
|
engine, err := l.getRequestRuleEngine(ctx, layer, options)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.WithStack(err)
|
return errors.WithStack(err)
|
||||||
}
|
}
|
||||||
|
@ -117,8 +118,11 @@ func (l *Layer) applyRequestRules(ctx context.Context, r *http.Request, layerRev
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *Layer) getRequestRuleEngine(ctx context.Context, layerRevision int, options *LayerOptions) (*rule.Engine[*RequestVars], error) {
|
func (l *Layer) getRequestRuleEngine(ctx context.Context, layer *store.Layer, options *LayerOptions) (*rule.Engine[*RequestVars], error) {
|
||||||
engine, err := l.requestRuleEngine.Get(ctx, layerRevision, options)
|
key := string(layer.Proxy) + "-" + string(layer.Name)
|
||||||
|
revisionedEngine := l.requestRuleEngineCache.Get(key)
|
||||||
|
|
||||||
|
engine, err := revisionedEngine.Get(ctx, layer.Revision, options)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.WithStack(err)
|
return nil, errors.WithStack(err)
|
||||||
}
|
}
|
||||||
|
@ -145,13 +149,13 @@ type ResponseVar struct {
|
||||||
Trailer map[string][]string `expr:"trailer"`
|
Trailer map[string][]string `expr:"trailer"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *Layer) applyResponseRules(ctx context.Context, r *http.Response, layerRevision int, options *LayerOptions) error {
|
func (l *Layer) applyResponseRules(ctx context.Context, r *http.Response, layer *store.Layer, options *LayerOptions) error {
|
||||||
rules := options.Rules.Response
|
rules := options.Rules.Response
|
||||||
if len(rules) == 0 {
|
if len(rules) == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
engine, err := l.getResponseRuleEngine(ctx, layerRevision, options)
|
engine, err := l.getResponseRuleEngine(ctx, layer, options)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.WithStack(err)
|
return errors.WithStack(err)
|
||||||
}
|
}
|
||||||
|
@ -187,8 +191,11 @@ func (l *Layer) applyResponseRules(ctx context.Context, r *http.Response, layerR
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *Layer) getResponseRuleEngine(ctx context.Context, layerRevision int, options *LayerOptions) (*rule.Engine[*ResponseVars], error) {
|
func (l *Layer) getResponseRuleEngine(ctx context.Context, layer *store.Layer, options *LayerOptions) (*rule.Engine[*ResponseVars], error) {
|
||||||
engine, err := l.responseRuleEngine.Get(ctx, layerRevision, options)
|
key := string(layer.Proxy) + "-" + string(layer.Name)
|
||||||
|
revisionedEngine := l.responseRuleEngineCache.Get(key)
|
||||||
|
|
||||||
|
engine, err := revisionedEngine.Get(ctx, layer.Revision, options)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.WithStack(err)
|
return nil, errors.WithStack(err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,28 @@
|
||||||
|
package util
|
||||||
|
|
||||||
|
import (
|
||||||
|
"forge.cadoles.com/cadoles/bouncer/internal/cache"
|
||||||
|
"forge.cadoles.com/cadoles/bouncer/internal/cache/memory"
|
||||||
|
)
|
||||||
|
|
||||||
|
type RuleEngineCache[V any, O any] struct {
|
||||||
|
cache cache.Cache[string, *RevisionedRuleEngine[V, O]]
|
||||||
|
factory RuleEngineFactoryFunc[V, O]
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *RuleEngineCache[V, O]) Get(key string) *RevisionedRuleEngine[V, O] {
|
||||||
|
revisionedRuleEngine, exists := c.cache.Get(key)
|
||||||
|
if !exists {
|
||||||
|
revisionedRuleEngine = NewRevisionedRuleEngine(c.factory)
|
||||||
|
c.cache.Set(key, revisionedRuleEngine)
|
||||||
|
}
|
||||||
|
|
||||||
|
return revisionedRuleEngine
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewInMemoryRuleEngineCache[V any, O any](factory RuleEngineFactoryFunc[V, O]) *RuleEngineCache[V, O] {
|
||||||
|
return &RuleEngineCache[V, O]{
|
||||||
|
factory: factory,
|
||||||
|
cache: memory.NewCache[string, *RevisionedRuleEngine[V, O]](),
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue