Compare commits
14 Commits
v2024.9.27
...
v2024.10.2
Author | SHA1 | Date | |
---|---|---|---|
a686c52aed | |||
c0470ca623 | |||
c611705d45 | |||
16fa751dc7 | |||
8983a44d9e | |||
11375e546f | |||
69501f6302 | |||
382d17cc85 | |||
9bd1d0fbd7 | |||
ecacbb1cbd | |||
910f1f8ba2 | |||
be59be1795 | |||
4d6958e2f5 | |||
f3b553cb10 |
4
Makefile
4
Makefile
@ -17,8 +17,8 @@ GOTEST_ARGS ?= -short
|
|||||||
OPENWRT_DEVICE ?= 192.168.1.1
|
OPENWRT_DEVICE ?= 192.168.1.1
|
||||||
|
|
||||||
SIEGE_URLS_FILE ?= misc/siege/urls.txt
|
SIEGE_URLS_FILE ?= misc/siege/urls.txt
|
||||||
SIEGE_CONCURRENCY ?= 50
|
SIEGE_CONCURRENCY ?= 200
|
||||||
SIEGE_DURATION ?= 1M
|
SIEGE_DURATION ?= 5M
|
||||||
|
|
||||||
data/bootstrap.d/dummy.yml:
|
data/bootstrap.d/dummy.yml:
|
||||||
mkdir -p data/bootstrap.d
|
mkdir -p data/bootstrap.d
|
||||||
|
@ -60,7 +60,7 @@ Retourne `nil` si le cookie n'existe pas.
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
##### `set_cookie(ctx, cookie Cookie)`
|
##### `add_cookie(ctx, cookie Cookie)`
|
||||||
|
|
||||||
Définit un cookie sur la requête/réponse (en fonction du contexte d'utilisation).
|
Définit un cookie sur la requête/réponse (en fonction du contexte d'utilisation).
|
||||||
Voir la méthode `get_cookie()` pour voir les attributs potentiels.
|
Voir la méthode `get_cookie()` pour voir les attributs potentiels.
|
||||||
|
@ -2,6 +2,7 @@ package admin
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"expvar"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
@ -115,7 +116,9 @@ func (s *Server) run(parentCtx context.Context, addrs chan net.Addr, errs chan e
|
|||||||
router.Use(middleware.RealIP)
|
router.Use(middleware.RealIP)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
router.Use(middleware.RequestID)
|
||||||
router.Use(middleware.RequestLogger(bouncerChi.NewLogFormatter()))
|
router.Use(middleware.RequestLogger(bouncerChi.NewLogFormatter()))
|
||||||
|
router.Use(middleware.Recoverer)
|
||||||
|
|
||||||
if s.serverConfig.Sentry.DSN != "" {
|
if s.serverConfig.Sentry.DSN != "" {
|
||||||
logger.Info(ctx, "enabling sentry http middleware")
|
logger.Info(ctx, "enabling sentry http middleware")
|
||||||
@ -176,6 +179,7 @@ func (s *Server) run(parentCtx context.Context, addrs chan net.Addr, errs chan e
|
|||||||
r.HandleFunc("/profile", pprof.Profile)
|
r.HandleFunc("/profile", pprof.Profile)
|
||||||
r.HandleFunc("/symbol", pprof.Symbol)
|
r.HandleFunc("/symbol", pprof.Symbol)
|
||||||
r.HandleFunc("/trace", pprof.Trace)
|
r.HandleFunc("/trace", pprof.Trace)
|
||||||
|
r.Handle("/vars", expvar.Handler())
|
||||||
r.HandleFunc("/{name}", func(w http.ResponseWriter, r *http.Request) {
|
r.HandleFunc("/{name}", func(w http.ResponseWriter, r *http.Request) {
|
||||||
name := chi.URLParam(r, "name")
|
name := chi.URLParam(r, "name")
|
||||||
pprof.Handler(name).ServeHTTP(w, r)
|
pprof.Handler(name).ServeHTTP(w, r)
|
||||||
|
@ -2,7 +2,6 @@ package chi
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -37,12 +36,19 @@ type LogEntry struct {
|
|||||||
|
|
||||||
// Panic implements middleware.LogEntry
|
// Panic implements middleware.LogEntry
|
||||||
func (e *LogEntry) Panic(v interface{}, stack []byte) {
|
func (e *LogEntry) Panic(v interface{}, stack []byte) {
|
||||||
logger.Error(e.ctx, fmt.Sprintf("%s %s", e.method, e.path), logger.F("stack", string(stack)))
|
logger.Error(
|
||||||
|
e.ctx, "http panic",
|
||||||
|
logger.F("stack", string(stack)),
|
||||||
|
logger.F("host", e.host),
|
||||||
|
logger.F("method", e.method),
|
||||||
|
logger.F("path", e.path),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write implements middleware.LogEntry
|
// Write implements middleware.LogEntry
|
||||||
func (e *LogEntry) Write(status int, bytes int, header http.Header, elapsed time.Duration, extra interface{}) {
|
func (e *LogEntry) Write(status int, bytes int, header http.Header, elapsed time.Duration, extra interface{}) {
|
||||||
logger.Info(e.ctx, fmt.Sprintf("%s %s - %d", e.method, e.path, status),
|
logger.Info(
|
||||||
|
e.ctx, "http request",
|
||||||
logger.F("host", e.host),
|
logger.F("host", e.host),
|
||||||
logger.F("status", status),
|
logger.F("status", status),
|
||||||
logger.F("bytes", bytes),
|
logger.F("bytes", bytes),
|
||||||
|
@ -9,25 +9,35 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type RedisConfig struct {
|
type RedisConfig struct {
|
||||||
Adresses InterpolatedStringSlice `yaml:"addresses"`
|
Adresses InterpolatedStringSlice `yaml:"addresses"`
|
||||||
Master InterpolatedString `yaml:"master"`
|
Master InterpolatedString `yaml:"master"`
|
||||||
ReadTimeout InterpolatedDuration `yaml:"readTimeout"`
|
ReadTimeout InterpolatedDuration `yaml:"readTimeout"`
|
||||||
WriteTimeout InterpolatedDuration `yaml:"writeTimeout"`
|
WriteTimeout InterpolatedDuration `yaml:"writeTimeout"`
|
||||||
DialTimeout InterpolatedDuration `yaml:"dialTimeout"`
|
DialTimeout InterpolatedDuration `yaml:"dialTimeout"`
|
||||||
LockMaxRetries InterpolatedInt `yaml:"lockMaxRetries"`
|
LockMaxRetries InterpolatedInt `yaml:"lockMaxRetries"`
|
||||||
MaxRetries InterpolatedInt `yaml:"maxRetries"`
|
RouteByLatency InterpolatedBool `yaml:"routeByLatency"`
|
||||||
PingInterval InterpolatedDuration `yaml:"pingInterval"`
|
ContextTimeoutEnabled InterpolatedBool `yaml:"contextTimeoutEnabled"`
|
||||||
|
MaxRetries InterpolatedInt `yaml:"maxRetries"`
|
||||||
|
PingInterval InterpolatedDuration `yaml:"pingInterval"`
|
||||||
|
PoolSize InterpolatedInt `yaml:"poolSize"`
|
||||||
|
PoolTimeout InterpolatedDuration `yaml:"poolTimeout"`
|
||||||
|
MinIdleConns InterpolatedInt `yaml:"minIdleConns"`
|
||||||
|
MaxIdleConns InterpolatedInt `yaml:"maxIdleConns"`
|
||||||
|
ConnMaxIdleTime InterpolatedDuration `yaml:"connMaxIdleTime"`
|
||||||
|
ConnMaxLifetime InterpolatedDuration `yaml:"connMaxLifeTime"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDefaultRedisConfig() RedisConfig {
|
func NewDefaultRedisConfig() RedisConfig {
|
||||||
return RedisConfig{
|
return RedisConfig{
|
||||||
Adresses: InterpolatedStringSlice{"localhost:6379"},
|
Adresses: InterpolatedStringSlice{"localhost:6379"},
|
||||||
Master: "",
|
Master: "",
|
||||||
ReadTimeout: InterpolatedDuration(30 * time.Second),
|
ReadTimeout: InterpolatedDuration(30 * time.Second),
|
||||||
WriteTimeout: InterpolatedDuration(30 * time.Second),
|
WriteTimeout: InterpolatedDuration(30 * time.Second),
|
||||||
DialTimeout: InterpolatedDuration(30 * time.Second),
|
DialTimeout: InterpolatedDuration(30 * time.Second),
|
||||||
LockMaxRetries: 10,
|
LockMaxRetries: 10,
|
||||||
MaxRetries: 3,
|
MaxRetries: 3,
|
||||||
PingInterval: InterpolatedDuration(30 * time.Second),
|
PingInterval: InterpolatedDuration(30 * time.Second),
|
||||||
|
ContextTimeoutEnabled: true,
|
||||||
|
RouteByLatency: true,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -28,11 +28,11 @@ func NewDefaultSentryConfig() SentryConfig {
|
|||||||
Debug: false,
|
Debug: false,
|
||||||
FlushTimeout: NewInterpolatedDuration(2 * time.Second),
|
FlushTimeout: NewInterpolatedDuration(2 * time.Second),
|
||||||
AttachStacktrace: true,
|
AttachStacktrace: true,
|
||||||
SampleRate: 0.2,
|
SampleRate: 1,
|
||||||
EnableTracing: true,
|
EnableTracing: false,
|
||||||
TracesSampleRate: 0.2,
|
TracesSampleRate: 0.1,
|
||||||
ProfilesSampleRate: 0.2,
|
ProfilesSampleRate: 0.1,
|
||||||
IgnoreErrors: []string{},
|
IgnoreErrors: []string{"context canceled"},
|
||||||
SendDefaultPII: false,
|
SendDefaultPII: false,
|
||||||
ServerName: "",
|
ServerName: "",
|
||||||
Environment: "",
|
Environment: "",
|
||||||
|
@ -8,7 +8,6 @@ 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/cache"
|
"forge.cadoles.com/cadoles/bouncer/internal/cache"
|
||||||
bouncersentry "forge.cadoles.com/cadoles/bouncer/internal/sentry"
|
|
||||||
"forge.cadoles.com/cadoles/bouncer/internal/store"
|
"forge.cadoles.com/cadoles/bouncer/internal/store"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
@ -42,21 +41,11 @@ func (d *Director) rewriteRequest(r *http.Request) (*http.Request, error) {
|
|||||||
|
|
||||||
for _, p := range proxies {
|
for _, p := range proxies {
|
||||||
for _, from := range p.From {
|
for _, from := range p.From {
|
||||||
logger.Debug(
|
|
||||||
ctx, "matching request with proxy's from",
|
|
||||||
logger.F("from", from),
|
|
||||||
)
|
|
||||||
|
|
||||||
if matches := wildcard.Match(url.String(), from); !matches {
|
if matches := wildcard.Match(url.String(), from); !matches {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Debug(
|
proxyCtx := logger.With(ctx,
|
||||||
ctx, "proxy's from matched",
|
|
||||||
logger.F("from", from),
|
|
||||||
)
|
|
||||||
|
|
||||||
ctx = logger.With(ctx,
|
|
||||||
logger.F("proxy", p.Name),
|
logger.F("proxy", p.Name),
|
||||||
logger.F("host", r.Host),
|
logger.F("host", r.Host),
|
||||||
logger.F("remoteAddr", r.RemoteAddr),
|
logger.F("remoteAddr", r.RemoteAddr),
|
||||||
@ -64,7 +53,7 @@ func (d *Director) rewriteRequest(r *http.Request) (*http.Request, error) {
|
|||||||
|
|
||||||
metricProxyRequestsTotal.With(prometheus.Labels{metricLabelProxy: string(p.Name)}).Add(1)
|
metricProxyRequestsTotal.With(prometheus.Labels{metricLabelProxy: string(p.Name)}).Add(1)
|
||||||
|
|
||||||
proxyLayers, err := d.getLayers(ctx, p.Name)
|
proxyLayers, err := d.getLayers(proxyCtx, p.Name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return r, errors.WithStack(err)
|
return r, errors.WithStack(err)
|
||||||
}
|
}
|
||||||
@ -84,8 +73,8 @@ func (d *Director) rewriteRequest(r *http.Request) (*http.Request, error) {
|
|||||||
r.URL.Scheme = toURL.Scheme
|
r.URL.Scheme = toURL.Scheme
|
||||||
r.URL.Path = toURL.JoinPath(r.URL.Path).Path
|
r.URL.Path = toURL.JoinPath(r.URL.Path).Path
|
||||||
|
|
||||||
ctx = withLayers(ctx, layers)
|
proxyCtx = withLayers(proxyCtx, layers)
|
||||||
r = r.WithContext(ctx)
|
r = r.WithContext(proxyCtx)
|
||||||
|
|
||||||
return r, nil
|
return r, nil
|
||||||
}
|
}
|
||||||
@ -259,7 +248,6 @@ func (d *Director) Middleware() proxy.Middleware {
|
|||||||
}
|
}
|
||||||
|
|
||||||
handler := createMiddlewareChain(next, httpMiddlewares)
|
handler := createMiddlewareChain(next, httpMiddlewares)
|
||||||
handler = bouncersentry.SpanHandler(handler, "director.proxy")
|
|
||||||
|
|
||||||
handler.ServeHTTP(w, r)
|
handler.ServeHTTP(w, r)
|
||||||
}
|
}
|
||||||
|
@ -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)
|
||||||
)
|
)
|
||||||
|
@ -48,7 +48,7 @@ func (c *Client) Provider() *oidc.Provider {
|
|||||||
func (c *Client) Authenticate(w http.ResponseWriter, r *http.Request, sess *sessions.Session, postLoginRedirectURL string) (*oidc.IDToken, error) {
|
func (c *Client) Authenticate(w http.ResponseWriter, r *http.Request, sess *sessions.Session, postLoginRedirectURL string) (*oidc.IDToken, error) {
|
||||||
idToken, err := c.getIDToken(r, sess)
|
idToken, err := c.getIDToken(r, sess)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Warn(r.Context(), "could not retrieve idtoken", logger.CapturedE(errors.WithStack(err)))
|
logger.Warn(r.Context(), "could not retrieve idtoken", logger.E(errors.WithStack(err)))
|
||||||
|
|
||||||
c.login(w, r, sess, postLoginRedirectURL)
|
c.login(w, r, sess, postLoginRedirectURL)
|
||||||
|
|
||||||
|
@ -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)
|
||||||
}
|
}
|
||||||
|
28
internal/proxy/director/layer/util/rule_engine_cache.go
Normal file
28
internal/proxy/director/layer/util/rule_engine_cache.go
Normal file
@ -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]](),
|
||||||
|
}
|
||||||
|
}
|
@ -3,6 +3,7 @@ package proxy
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
|
"expvar"
|
||||||
"fmt"
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
"io"
|
"io"
|
||||||
@ -31,8 +32,6 @@ import (
|
|||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||||
"gitlab.com/wpetit/goweb/logger"
|
"gitlab.com/wpetit/goweb/logger"
|
||||||
|
|
||||||
bouncersentry "forge.cadoles.com/cadoles/bouncer/internal/sentry"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Server struct {
|
type Server struct {
|
||||||
@ -120,7 +119,9 @@ func (s *Server) run(parentCtx context.Context, addrs chan net.Addr, errs chan e
|
|||||||
router.Use(middleware.RealIP)
|
router.Use(middleware.RealIP)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
router.Use(middleware.RequestID)
|
||||||
router.Use(middleware.RequestLogger(bouncerChi.NewLogFormatter()))
|
router.Use(middleware.RequestLogger(bouncerChi.NewLogFormatter()))
|
||||||
|
router.Use(middleware.Recoverer)
|
||||||
|
|
||||||
if s.serverConfig.Sentry.DSN != "" {
|
if s.serverConfig.Sentry.DSN != "" {
|
||||||
logger.Info(ctx, "enabling sentry http middleware")
|
logger.Info(ctx, "enabling sentry http middleware")
|
||||||
@ -171,6 +172,7 @@ func (s *Server) run(parentCtx context.Context, addrs chan net.Addr, errs chan e
|
|||||||
r.HandleFunc("/profile", pprof.Profile)
|
r.HandleFunc("/profile", pprof.Profile)
|
||||||
r.HandleFunc("/symbol", pprof.Symbol)
|
r.HandleFunc("/symbol", pprof.Symbol)
|
||||||
r.HandleFunc("/trace", pprof.Trace)
|
r.HandleFunc("/trace", pprof.Trace)
|
||||||
|
r.Handle("/vars", expvar.Handler())
|
||||||
r.HandleFunc("/{name}", func(w http.ResponseWriter, r *http.Request) {
|
r.HandleFunc("/{name}", func(w http.ResponseWriter, r *http.Request) {
|
||||||
name := chi.URLParam(r, "name")
|
name := chi.URLParam(r, "name")
|
||||||
pprof.Handler(name).ServeHTTP(w, r)
|
pprof.Handler(name).ServeHTTP(w, r)
|
||||||
@ -193,7 +195,7 @@ func (s *Server) run(parentCtx context.Context, addrs chan net.Addr, errs chan e
|
|||||||
proxy.WithDefaultHandler(http.HandlerFunc(s.handleDefault)),
|
proxy.WithDefaultHandler(http.HandlerFunc(s.handleDefault)),
|
||||||
)
|
)
|
||||||
|
|
||||||
r.Handle("/*", bouncersentry.SpanHandler(handler, "http.proxy"))
|
r.Handle("/*", handler)
|
||||||
})
|
})
|
||||||
|
|
||||||
if err := http.Serve(listener, router); err != nil && !errors.Is(err, net.ErrClosed) {
|
if err := http.Serve(listener, router); err != nil && !errors.Is(err, net.ErrClosed) {
|
||||||
@ -230,7 +232,12 @@ func (s *Server) handleDefault(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
func (s *Server) handleError(w http.ResponseWriter, r *http.Request, status int, err error) {
|
func (s *Server) handleError(w http.ResponseWriter, r *http.Request, status int, err error) {
|
||||||
err = errors.WithStack(err)
|
err = errors.WithStack(err)
|
||||||
logger.Error(r.Context(), err.Error(), logger.CapturedE(err))
|
|
||||||
|
if errors.Is(err, context.Canceled) {
|
||||||
|
logger.Warn(r.Context(), err.Error(), logger.E(err))
|
||||||
|
} else {
|
||||||
|
logger.Error(r.Context(), err.Error(), logger.CapturedE(err))
|
||||||
|
}
|
||||||
|
|
||||||
s.renderErrorPage(w, r, err, status, http.StatusText(status))
|
s.renderErrorPage(w, r, err, status, http.StatusText(status))
|
||||||
}
|
}
|
||||||
|
@ -1,37 +0,0 @@
|
|||||||
package sentry
|
|
||||||
|
|
||||||
import (
|
|
||||||
"net/http"
|
|
||||||
|
|
||||||
"github.com/getsentry/sentry-go"
|
|
||||||
)
|
|
||||||
|
|
||||||
func SpanHandler(handler http.Handler, name string) http.Handler {
|
|
||||||
fn := func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
ctx := r.Context()
|
|
||||||
|
|
||||||
hub := sentry.GetHubFromContext(ctx)
|
|
||||||
if hub == nil {
|
|
||||||
hub = sentry.CurrentHub().Clone()
|
|
||||||
ctx = sentry.SetHubOnContext(ctx, hub)
|
|
||||||
}
|
|
||||||
|
|
||||||
options := []sentry.SpanOption{
|
|
||||||
sentry.WithOpName(name),
|
|
||||||
sentry.ContinueFromRequest(r),
|
|
||||||
sentry.WithTransactionSource(sentry.SourceURL),
|
|
||||||
}
|
|
||||||
|
|
||||||
span := sentry.StartSpan(ctx,
|
|
||||||
name,
|
|
||||||
options...,
|
|
||||||
)
|
|
||||||
defer span.Finish()
|
|
||||||
|
|
||||||
r = r.WithContext(span.Context())
|
|
||||||
|
|
||||||
handler.ServeHTTP(w, r)
|
|
||||||
}
|
|
||||||
|
|
||||||
return http.HandlerFunc(fn)
|
|
||||||
}
|
|
@ -38,9 +38,15 @@ func newRedisClient(conf config.RedisConfig) redis.UniversalClient {
|
|||||||
ReadTimeout: time.Duration(conf.ReadTimeout),
|
ReadTimeout: time.Duration(conf.ReadTimeout),
|
||||||
WriteTimeout: time.Duration(conf.WriteTimeout),
|
WriteTimeout: time.Duration(conf.WriteTimeout),
|
||||||
DialTimeout: time.Duration(conf.DialTimeout),
|
DialTimeout: time.Duration(conf.DialTimeout),
|
||||||
RouteByLatency: true,
|
RouteByLatency: bool(conf.RouteByLatency),
|
||||||
ContextTimeoutEnabled: true,
|
ContextTimeoutEnabled: bool(conf.ContextTimeoutEnabled),
|
||||||
MaxRetries: int(conf.MaxRetries),
|
MaxRetries: int(conf.MaxRetries),
|
||||||
|
PoolSize: int(conf.PoolSize),
|
||||||
|
PoolTimeout: time.Duration(conf.PoolTimeout),
|
||||||
|
MinIdleConns: int(conf.MinIdleConns),
|
||||||
|
MaxIdleConns: int(conf.MaxIdleConns),
|
||||||
|
ConnMaxIdleTime: time.Duration(conf.ConnMaxIdleTime),
|
||||||
|
ConnMaxLifetime: time.Duration(conf.ConnMaxLifetime),
|
||||||
})
|
})
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
|
@ -55,6 +55,8 @@
|
|||||||
border-radius: 5px;
|
border-radius: 5px;
|
||||||
box-shadow: 2px 2px #cccccc1c;
|
box-shadow: 2px 2px #cccccc1c;
|
||||||
color: #810000 !important;
|
color: #810000 !important;
|
||||||
|
max-width: 80%;
|
||||||
|
overflow-x: hidden;
|
||||||
}
|
}
|
||||||
|
|
||||||
.title {
|
.title {
|
||||||
@ -81,7 +83,7 @@
|
|||||||
<div id="card">
|
<div id="card">
|
||||||
<h2 class="title">Une erreur est survenue !</h2>
|
<h2 class="title">Une erreur est survenue !</h2>
|
||||||
{{ if .Debug }}
|
{{ if .Debug }}
|
||||||
<pre>{{ .Err }}</pre>
|
<pre style="overflow-x: auto">{{ .Err }}</pre>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
{{/* if a public base url is provided, show navigation link */}}
|
{{/* if a public base url is provided, show navigation link */}}
|
||||||
{{ $oidc := ( index .Layer.Options "oidc" ) }}
|
{{ $oidc := ( index .Layer.Options "oidc" ) }}
|
||||||
|
Reference in New Issue
Block a user