2024-06-25 14:03:49 +02:00
|
|
|
package rewriter
|
|
|
|
|
|
|
|
import (
|
2024-09-24 15:46:42 +02:00
|
|
|
"context"
|
2024-06-25 14:03:49 +02:00
|
|
|
"net/http"
|
2024-09-25 15:50:13 +02:00
|
|
|
"net/url"
|
2024-06-25 14:03:49 +02:00
|
|
|
|
2024-09-24 15:46:42 +02:00
|
|
|
"forge.cadoles.com/cadoles/bouncer/internal/proxy/director"
|
2024-06-25 14:03:49 +02:00
|
|
|
"forge.cadoles.com/cadoles/bouncer/internal/rule"
|
|
|
|
ruleHTTP "forge.cadoles.com/cadoles/bouncer/internal/rule/http"
|
2024-10-21 13:48:59 +02:00
|
|
|
"forge.cadoles.com/cadoles/bouncer/internal/store"
|
2024-06-25 14:03:49 +02:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2024-09-24 15:46:42 +02:00
|
|
|
type RequestVars struct {
|
|
|
|
Request RequestVar `expr:"request"`
|
|
|
|
OriginalURL URLVar `expr:"original_url"`
|
2024-06-25 14:03:49 +02:00
|
|
|
}
|
|
|
|
|
2024-09-24 15:46:42 +02:00
|
|
|
type URLVar struct {
|
|
|
|
Scheme string `expr:"scheme"`
|
|
|
|
Opaque string `expr:"opaque"`
|
|
|
|
User UserVar `expr:"user"`
|
|
|
|
Host string `expr:"host"`
|
|
|
|
Path string `expr:"path"`
|
|
|
|
RawPath string `expr:"raw_path"`
|
|
|
|
RawQuery string `expr:"raw_query"`
|
|
|
|
Fragment string `expr:"fragment"`
|
|
|
|
RawFragment string `expr:"raw_fragment"`
|
2024-06-28 10:46:38 +02:00
|
|
|
}
|
|
|
|
|
2024-09-25 15:50:13 +02:00
|
|
|
func fromURL(url *url.URL) URLVar {
|
|
|
|
return URLVar{
|
|
|
|
Scheme: url.Scheme,
|
|
|
|
Opaque: url.Opaque,
|
|
|
|
User: UserVar{
|
|
|
|
Username: url.User.Username(),
|
|
|
|
Password: func() string {
|
|
|
|
passwd, _ := url.User.Password()
|
|
|
|
return passwd
|
|
|
|
}(),
|
|
|
|
},
|
|
|
|
Host: url.Host,
|
|
|
|
Path: url.Path,
|
|
|
|
RawPath: url.RawPath,
|
|
|
|
RawQuery: url.RawQuery,
|
|
|
|
Fragment: url.Fragment,
|
|
|
|
RawFragment: url.RawFragment,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-24 15:46:42 +02:00
|
|
|
type UserVar struct {
|
2024-06-28 10:46:38 +02:00
|
|
|
Username string `expr:"username"`
|
|
|
|
Password string `expr:"password"`
|
|
|
|
}
|
|
|
|
|
2024-09-24 15:46:42 +02:00
|
|
|
type RequestVar struct {
|
2024-06-25 14:03:49 +02:00
|
|
|
Method string `expr:"method"`
|
2024-09-24 15:46:42 +02:00
|
|
|
URL URLVar `expr:"url"`
|
|
|
|
RawURL string `expr:"raw_url"`
|
2024-06-25 14:03:49 +02:00
|
|
|
Proto string `expr:"proto"`
|
2024-09-24 15:46:42 +02:00
|
|
|
ProtoMajor int `expr:"proto_major"`
|
|
|
|
ProtoMinor int `expr:"proto_minor"`
|
2024-06-25 14:03:49 +02:00
|
|
|
Header map[string][]string `expr:"header"`
|
2024-09-24 15:46:42 +02:00
|
|
|
ContentLength int64 `expr:"content_length"`
|
|
|
|
TransferEncoding []string `expr:"transfer_encoding"`
|
2024-06-25 14:03:49 +02:00
|
|
|
Host string `expr:"host"`
|
|
|
|
Trailer map[string][]string `expr:"trailer"`
|
2024-09-24 15:46:42 +02:00
|
|
|
RemoteAddr string `expr:"remote_addr"`
|
|
|
|
RequestURI string `expr:"request_uri"`
|
2024-06-25 14:03:49 +02:00
|
|
|
}
|
|
|
|
|
2024-09-25 15:50:13 +02:00
|
|
|
func fromRequest(r *http.Request) RequestVar {
|
|
|
|
return RequestVar{
|
|
|
|
Method: r.Method,
|
|
|
|
URL: fromURL(r.URL),
|
|
|
|
RawURL: r.URL.String(),
|
|
|
|
Proto: r.Proto,
|
|
|
|
ProtoMajor: r.ProtoMajor,
|
|
|
|
ProtoMinor: r.ProtoMinor,
|
|
|
|
Header: r.Header,
|
|
|
|
ContentLength: r.ContentLength,
|
|
|
|
TransferEncoding: r.TransferEncoding,
|
|
|
|
Host: r.Host,
|
|
|
|
Trailer: r.Trailer,
|
|
|
|
RemoteAddr: r.RemoteAddr,
|
|
|
|
RequestURI: r.RequestURI,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-21 13:48:59 +02:00
|
|
|
func (l *Layer) applyRequestRules(ctx context.Context, r *http.Request, layer *store.Layer, options *LayerOptions) error {
|
2024-06-25 14:03:49 +02:00
|
|
|
rules := options.Rules.Request
|
|
|
|
if len(rules) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-10-21 13:48:59 +02:00
|
|
|
engine, err := l.getRequestRuleEngine(ctx, layer, options)
|
2024-06-25 14:03:49 +02:00
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
2024-09-24 15:46:42 +02:00
|
|
|
originalURL, err := director.OriginalURL(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
vars := &RequestVars{
|
2024-09-25 15:50:13 +02:00
|
|
|
OriginalURL: fromURL(originalURL),
|
|
|
|
Request: fromRequest(r),
|
2024-06-25 14:03:49 +02:00
|
|
|
}
|
|
|
|
|
2024-09-24 15:46:42 +02:00
|
|
|
ctx = ruleHTTP.WithRequest(ctx, r)
|
|
|
|
|
|
|
|
if _, err := engine.Apply(ctx, vars); err != nil {
|
2024-06-25 14:03:49 +02:00
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-10-21 13:48:59 +02:00
|
|
|
func (l *Layer) getRequestRuleEngine(ctx context.Context, layer *store.Layer, options *LayerOptions) (*rule.Engine[*RequestVars], error) {
|
|
|
|
key := string(layer.Proxy) + "-" + string(layer.Name)
|
|
|
|
revisionedEngine := l.requestRuleEngineCache.Get(key)
|
|
|
|
|
|
|
|
engine, err := revisionedEngine.Get(ctx, layer.Revision, options)
|
2024-06-28 10:46:38 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return engine, nil
|
|
|
|
}
|
|
|
|
|
2024-09-24 15:46:42 +02:00
|
|
|
type ResponseVars struct {
|
|
|
|
OriginalURL URLVar `expr:"original_url"`
|
|
|
|
Request RequestVar `expr:"request"`
|
|
|
|
Response ResponseVar `expr:"response"`
|
2024-06-25 14:03:49 +02:00
|
|
|
}
|
|
|
|
|
2024-09-24 15:46:42 +02:00
|
|
|
type ResponseVar struct {
|
2024-06-25 14:03:49 +02:00
|
|
|
Status string `expr:"status"`
|
2024-09-24 15:46:42 +02:00
|
|
|
StatusCode int `expr:"status_code"`
|
2024-06-25 14:03:49 +02:00
|
|
|
Proto string `expr:"proto"`
|
2024-09-24 15:46:42 +02:00
|
|
|
ProtoMajor int `expr:"proto_major"`
|
|
|
|
ProtoMinor int `expr:"proto_minor"`
|
2024-06-25 14:03:49 +02:00
|
|
|
Header map[string][]string `expr:"header"`
|
2024-09-24 15:46:42 +02:00
|
|
|
ContentLength int64 `expr:"content_length"`
|
|
|
|
TransferEncoding []string `expr:"transfer_encoding"`
|
2024-06-25 14:03:49 +02:00
|
|
|
Uncompressed bool `expr:"uncompressed"`
|
|
|
|
Trailer map[string][]string `expr:"trailer"`
|
|
|
|
}
|
|
|
|
|
2024-10-21 13:48:59 +02:00
|
|
|
func (l *Layer) applyResponseRules(ctx context.Context, r *http.Response, layer *store.Layer, options *LayerOptions) error {
|
2024-06-28 10:46:38 +02:00
|
|
|
rules := options.Rules.Response
|
2024-06-25 14:03:49 +02:00
|
|
|
if len(rules) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-10-21 13:48:59 +02:00
|
|
|
engine, err := l.getResponseRuleEngine(ctx, layer, options)
|
2024-06-25 14:03:49 +02:00
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
2024-09-24 15:46:42 +02:00
|
|
|
originalURL, err := director.OriginalURL(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
vars := &ResponseVars{
|
2024-09-25 15:50:13 +02:00
|
|
|
OriginalURL: fromURL(originalURL),
|
|
|
|
Request: fromRequest(r.Request),
|
2024-09-24 15:46:42 +02:00
|
|
|
Response: ResponseVar{
|
2024-06-25 14:03:49 +02:00
|
|
|
Proto: r.Proto,
|
|
|
|
ProtoMajor: r.ProtoMajor,
|
|
|
|
ProtoMinor: r.ProtoMinor,
|
|
|
|
Header: r.Header,
|
|
|
|
ContentLength: r.ContentLength,
|
|
|
|
TransferEncoding: r.TransferEncoding,
|
|
|
|
Trailer: r.Trailer,
|
|
|
|
Status: r.Status,
|
|
|
|
StatusCode: r.StatusCode,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-09-24 15:46:42 +02:00
|
|
|
ctx = ruleHTTP.WithResponse(ctx, r)
|
2024-09-25 15:50:13 +02:00
|
|
|
ctx = ruleHTTP.WithRequest(ctx, r.Request)
|
2024-09-24 15:46:42 +02:00
|
|
|
|
|
|
|
if _, err := engine.Apply(ctx, vars); err != nil {
|
2024-06-25 14:03:49 +02:00
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-06-28 10:46:38 +02:00
|
|
|
|
2024-10-21 13:48:59 +02:00
|
|
|
func (l *Layer) getResponseRuleEngine(ctx context.Context, layer *store.Layer, options *LayerOptions) (*rule.Engine[*ResponseVars], error) {
|
|
|
|
key := string(layer.Proxy) + "-" + string(layer.Name)
|
|
|
|
revisionedEngine := l.responseRuleEngineCache.Get(key)
|
|
|
|
|
|
|
|
engine, err := revisionedEngine.Get(ctx, layer.Revision, options)
|
2024-06-28 10:46:38 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return engine, nil
|
|
|
|
}
|