-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruleshttp.go
More file actions
442 lines (401 loc) · 15 KB
/
ruleshttp.go
File metadata and controls
442 lines (401 loc) · 15 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
// Package ruleshttp provides an http.RoundTripper that uses the Expr expression
// language (github.com/expr-lang/expr) to enforce rules on HTTP
// requests and responses.
//
// Two hook types are supported:
//
// - pre_request: rules evaluated against the outgoing request before it
// is sent. The default decision is deny; the first rule whose match
// expression is true and whose authorize expression is true allows the
// request to proceed.
//
// - pre_response: rules evaluated against the incoming response after it
// is received. The same logic applies.
//
// If no rules are configured for a phase, that phase denies all traffic.
//
// Configuration is loaded from a YAML file via [NewFromFile] or built
// programmatically with a [Config] value passed to [New].
package ruleshttp
import (
"bytes"
"errors"
"fmt"
"io"
"log/slog"
"net/http"
"os"
"github.com/expr-lang/expr"
"github.com/expr-lang/expr/vm"
"gopkg.in/yaml.v3"
)
// ErrDenied is returned when a request or response is denied by rules.
// Callers can use errors.Is to detect this case.
var ErrDenied = errors.New("ruleshttp: denied")
// RequestEnv is the environment exposed to pre_request Expr expressions.
// Field names are used directly in expression strings, e.g. Method == "GET".
type RequestEnv struct {
// Method is the HTTP method (GET, POST, …).
Method string `yaml:"method"`
// Scheme is the URL scheme (http or https).
Scheme string `yaml:"scheme"`
// Path is the URL path component.
Path string `yaml:"path"`
// Host is the request host (from the Host header or URL).
Host string `yaml:"host"`
// Headers contains canonicalized header names mapped to all their values.
// Keys follow Go's http.CanonicalHeaderKey format, e.g. "Content-Type".
Headers map[string][]string `yaml:"headers"`
// Body is the full request body decoded as a UTF-8 string.
Body string `yaml:"body"`
// Query contains URL query parameters mapped to all their values.
Query map[string][]string `yaml:"query"`
}
// ResponseEnv is the environment exposed to pre_response Expr expressions.
type ResponseEnv struct {
// StatusCode is the numeric HTTP status code, e.g. 200.
StatusCode int `yaml:"status_code"`
// Headers contains canonicalized response header names mapped to all their values.
Headers map[string][]string `yaml:"headers"`
// Body is the full response body decoded as a UTF-8 string.
Body string `yaml:"body"`
// Request is the original request environment.
Request RequestEnv `yaml:"request"`
}
// Rule is a named pair of match and authorize Expr expressions.
// A rule applies to a request when match returns true.
// It allows the request when authorize returns true.
type Rule struct {
Name string `yaml:"name"`
Match string `yaml:"match"`
Authorize string `yaml:"authorize"`
}
// Config holds the pre_request and pre_response rule lists loaded from YAML.
type Config struct {
PreRequest []Rule `yaml:"pre_request"`
PreResponse []Rule `yaml:"pre_response"`
}
// parseConfig parses YAML bytes into a Config and validates it.
func parseConfig(data []byte) (Config, error) {
var cfg Config
if err := yaml.Unmarshal(data, &cfg); err != nil {
return Config{}, fmt.Errorf("ruleshttp: parsing config: %w", err)
}
if len(cfg.PreRequest) == 0 && len(cfg.PreResponse) == 0 {
return Config{}, fmt.Errorf("ruleshttp: no rules set")
}
for i, r := range cfg.PreRequest {
if r.Match == "" {
return Config{}, fmt.Errorf("ruleshttp: pre_request[%d] %q: match must not be empty (use ALL to match all)", i, r.Name)
}
if r.Authorize == "" {
return Config{}, fmt.Errorf("ruleshttp: pre_request[%d] %q: authorize must not be empty (use ALL to allow all matches)", i, r.Name)
}
}
for i, r := range cfg.PreResponse {
if r.Match == "" {
return Config{}, fmt.Errorf("ruleshttp: pre_response[%d] %q: match must not be empty (use ALL to match all)", i, r.Name)
}
if r.Authorize == "" {
return Config{}, fmt.Errorf("ruleshttp: pre_response[%d] %q: authorize must not be empty (use ALL to allow all matches)", i, r.Name)
}
}
return cfg, nil
}
// compiledRule pairs compiled Expr programs for match and authorize
// conditions with the rule's human-readable name for error messages.
type compiledRule struct {
name string
match *vm.Program
authorize *vm.Program
}
// Transport is an http.RoundTripper that evaluates Expr expressions before
// sending requests and before returning responses.
type Transport struct {
wrapped http.RoundTripper
preRequest []compiledRule
preResponse []compiledRule
logger *slog.Logger
logAllows bool
logDenials bool
}
// Option configures a Transport. If multiple logger options are provided,
// the last one takes effect.
type Option func(*Transport)
// WithRoundTripper sets the underlying RoundTripper used to make real HTTP
// calls. Defaults to http.DefaultTransport when not provided.
func WithRoundTripper(rt http.RoundTripper) Option {
return func(t *Transport) {
t.wrapped = rt
}
}
// WithLogger attaches a [slog.Logger] that records only non-rule errors
// (transport failures, body read errors) at Error level. Rule outcomes
// (allows and denials) are not logged.
//
// Every log entry includes the fields:
// - err – the error returned by RoundTrip, if any
// - pre_request_allowed – whether the pre_request rules passed
// - pre_request_allowed_rule – name of the rule that allowed the request
// - pre_response_allowed – whether the pre_response rules passed
// - pre_response_allowed_rule – name of the rule that allowed the response
func WithLogger(logger *slog.Logger) Option {
return func(t *Transport) {
t.logger = logger
t.logAllows = false
t.logDenials = false
}
}
// WithDenialLogger attaches a [slog.Logger] that records rule denials at
// Info level in addition to non-rule errors at Error level.
func WithDenialLogger(logger *slog.Logger) Option {
return func(t *Transport) {
t.logger = logger
t.logAllows = false
t.logDenials = true
}
}
// WithAllowLogger attaches a [slog.Logger] that records rule allows at
// Info level in addition to non-rule errors at Error level.
func WithAllowLogger(logger *slog.Logger) Option {
return func(t *Transport) {
t.logger = logger
t.logAllows = true
t.logDenials = false
}
}
// WithAllLogger attaches a [slog.Logger] that records every roundtrip —
// rule allows and denials at Info level, non-rule errors at Error level.
func WithAllLogger(logger *slog.Logger) Option {
return func(t *Transport) {
t.logger = logger
t.logAllows = true
t.logDenials = true
}
}
// New creates a Transport from cfg. It compiles all Expr expressions eagerly
// so that configuration errors are surfaced immediately rather than at
// request-time.
func New(cfg Config, opts ...Option) (*Transport, error) {
// Because rules are default deny this is likely an error. It otherwise creates a Transport that denies all requests.
if len(cfg.PreRequest) == 0 && len(cfg.PreResponse) == 0 {
return nil, fmt.Errorf("ruleshttp: no rules set")
}
t := &Transport{wrapped: http.DefaultTransport}
for _, o := range opts {
o(t)
}
for i, rule := range cfg.PreRequest {
matchProg, err := expr.Compile(resolveAlias(rule.Match), expr.Env(RequestEnv{}), expr.AsBool())
if err != nil {
return nil, fmt.Errorf("ruleshttp: pre_request[%d] %q match compile error: %w", i, rule.Name, err)
}
authProg, err := expr.Compile(resolveAlias(rule.Authorize), expr.Env(RequestEnv{}), expr.AsBool())
if err != nil {
return nil, fmt.Errorf("ruleshttp: pre_request[%d] %q authorize compile error: %w", i, rule.Name, err)
}
t.preRequest = append(t.preRequest, compiledRule{name: rule.Name, match: matchProg, authorize: authProg})
}
for i, rule := range cfg.PreResponse {
matchProg, err := expr.Compile(resolveAlias(rule.Match), expr.Env(ResponseEnv{}), expr.AsBool())
if err != nil {
return nil, fmt.Errorf("ruleshttp: pre_response[%d] %q match compile error: %w", i, rule.Name, err)
}
authProg, err := expr.Compile(resolveAlias(rule.Authorize), expr.Env(ResponseEnv{}), expr.AsBool())
if err != nil {
return nil, fmt.Errorf("ruleshttp: pre_response[%d] %q authorize compile error: %w", i, rule.Name, err)
}
t.preResponse = append(t.preResponse, compiledRule{name: rule.Name, match: matchProg, authorize: authProg})
}
return t, nil
}
// NewFromFile reads a YAML config file at path and creates a Transport from it.
// It parses and validates the YAML, then calls [New].
func NewFromFile(path string, opts ...Option) (*Transport, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("ruleshttp: reading config %q: %w", path, err)
}
cfg, err := parseConfig(data)
if err != nil {
return nil, err
}
return New(cfg, opts...)
}
// roundtripLogEntry is the structured log record emitted once per RoundTrip.
// JSON tags match the slog output keys so tests can unmarshal directly into
// this type instead of using map[string]any.
type roundtripLogEntry struct {
Level string `json:"level"`
Err *string `json:"err"`
PreRequestAllowed bool `json:"pre_request_allowed"`
PreRequestAllowedRule string `json:"pre_request_allowed_rule"`
PreResponseAllowed bool `json:"pre_response_allowed"`
PreResponseAllowedRule string `json:"pre_response_allowed_rule"`
}
// logArgs returns the key-value pairs for a slog call. Level and Err are
// omitted — Level is controlled by the caller (Info vs Error) and Err is
// passed as-is so slog can render nil as JSON null.
func (e roundtripLogEntry) logArgs(err error) []any {
return []any{
"err", err,
"pre_request_allowed", e.PreRequestAllowed,
"pre_request_allowed_rule", e.PreRequestAllowedRule,
"pre_response_allowed", e.PreResponseAllowed,
"pre_response_allowed_rule", e.PreResponseAllowedRule,
}
}
// RoundTrip implements http.RoundTripper.
//
// When pre_request rules are configured, they are evaluated in order; the
// first rule whose match expression is true and whose authorize expression
// is true allows the request. If no rule allows, the request is rejected
// with ErrDenied. An empty rules list denies all traffic.
// The same logic applies to pre_response rules.
//
// If a logger is configured via [WithLogger], a single wide log entry is
// emitted after the roundtrip completes.
func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error) {
resp, entry, err := t.roundtrip(req)
if t.logger != nil {
denied := errors.Is(err, ErrDenied)
args := entry.logArgs(err)
switch {
case err != nil && !denied:
// Transport or internal error: always log at Error.
t.logger.Error("ruleshttp roundtrip", args...)
case denied && t.logDenials:
t.logger.Info("ruleshttp roundtrip", args...)
case !denied && t.logAllows:
t.logger.Info("ruleshttp roundtrip", args...)
}
}
return resp, err
}
// roundtrip executes the full request/response cycle and returns the response,
// any error, and per-phase rule outcomes for logging.
func (t *Transport) roundtrip(req *http.Request) (*http.Response, roundtripLogEntry, error) {
var entry roundtripLogEntry
reqEnv, err := buildRequestEnv(req)
if err != nil {
return nil, entry, err
}
allowed, rule, err := evalRules(t.preRequest, reqEnv)
if err != nil {
return nil, entry, fmt.Errorf("ruleshttp: pre_request: %w", err)
}
entry.PreRequestAllowed = allowed
entry.PreRequestAllowedRule = rule
if !allowed {
return nil, entry, fmt.Errorf("%w: pre_request denied %s %s", ErrDenied, req.Method, req.URL.Path)
}
resp, err := t.wrapped.RoundTrip(req)
if err != nil {
return nil, entry, err
}
origBody := resp.Body
respEnv, err := buildResponseEnv(resp, reqEnv)
if err != nil {
origBody.Close()
return nil, entry, err
}
// buildResponseEnv replaced resp.Body with a buffered copy;
// close the original (now drained) body.
origBody.Close()
allowed, rule, err = evalRules(t.preResponse, respEnv)
if err != nil {
return nil, entry, fmt.Errorf("ruleshttp: pre_response: %w", err)
}
entry.PreResponseAllowed = allowed
entry.PreResponseAllowedRule = rule
if !allowed {
return nil, entry, fmt.Errorf("%w: pre_response denied status %d %s %s",
ErrDenied, resp.StatusCode, req.Method, req.URL.Path)
}
return resp, entry, nil
}
// evalRules evaluates each rule in order. For each rule, it runs the
// match expression then the authorize expression. The first rule that both
// matches and authorizes short-circuits and returns true.
//
// An empty rules slice denies all traffic (returns false).
func evalRules(rules []compiledRule, env any) (bool, string, error) {
for _, r := range rules {
matched, err := expr.Run(r.match, env)
if err != nil {
return false, "", fmt.Errorf("rule %q match: %w", r.name, err)
}
if !matched.(bool) {
continue
}
authorized, err := expr.Run(r.authorize, env)
if err != nil {
return false, "", fmt.Errorf("rule %q authorize: %w", r.name, err)
}
if authorized.(bool) {
return true, r.name, nil
}
}
return false, "", nil
}
// resolveAlias substitutes well-known shorthand tokens before Expr compilation.
// ANY expands to "true", conveying "applies to any request" in match expressions.
// ALL expands to "true", conveying "authorize all matches" in authorize expressions.
func resolveAlias(s string) string {
switch s {
case "ANY", "ALL":
return "true"
}
return s
}
// CheckRequest evaluates the pre_request rules against env and returns
// whether the request is allowed, the name of the matching rule, and any
// expression evaluation error.
func (t *Transport) CheckRequest(env RequestEnv) (bool, string, error) {
return evalRules(t.preRequest, env)
}
// CheckResponse evaluates the pre_response rules against env and returns
// whether the response is allowed, the name of the matching rule, and any
// expression evaluation error.
func (t *Transport) CheckResponse(env ResponseEnv) (bool, string, error) {
return evalRules(t.preResponse, env)
}
// buildRequestEnv constructs a RequestEnv from req. If the request has a
// body it is fully read and restored so the wrapped transport can re-read it.
func buildRequestEnv(req *http.Request) (RequestEnv, error) {
env := RequestEnv{
Method: req.Method,
Scheme: req.URL.Scheme,
Path: req.URL.Path,
Host: req.Host,
Headers: map[string][]string(req.Header),
Query: map[string][]string(req.URL.Query()),
}
if req.Body != nil {
body, err := io.ReadAll(req.Body)
if err != nil {
return RequestEnv{}, fmt.Errorf("ruleshttp: reading request body: %w", err)
}
req.Body = io.NopCloser(bytes.NewReader(body))
env.Body = string(body)
}
return env, nil
}
// buildResponseEnv constructs a ResponseEnv from resp. The response body is
// fully read and restored so the caller can still read it.
func buildResponseEnv(resp *http.Response, reqEnv RequestEnv) (ResponseEnv, error) {
env := ResponseEnv{
StatusCode: resp.StatusCode,
Headers: map[string][]string(resp.Header),
Request: reqEnv,
}
if resp.Body != nil {
body, err := io.ReadAll(resp.Body)
if err != nil {
return ResponseEnv{}, fmt.Errorf("ruleshttp: reading response body: %w", err)
}
resp.Body = io.NopCloser(bytes.NewReader(body))
env.Body = string(body)
}
return env, nil
}