Skip to content

Commit 8ab0786

Browse files
authored
Implement new middleware interfaces for all middleware (#1422)
This provides implementations of the new interfaces for all the middleware in our codebase. This does change over the code yet, that will happen in the next PR. Some unused code is commented out to allow the larger PR to be split up.
1 parent e39040c commit 8ab0786

File tree

12 files changed

+1768
-14
lines changed

12 files changed

+1768
-14
lines changed

CLAUDE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,3 +199,4 @@ Follow conventional commit format:
199199
- Refer to the `CONTRIBUTING.md` file for guidelines on commit message format
200200
conventions.
201201
- Do not use "Conventional Commits", e.g. starting with `feat`, `fix`, `chore`, etc.
202+
- Use mockgen for creating mocks instead of generating mocks by hand.

pkg/audit/middleware.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package audit
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
7+
"github.com/stacklok/toolhive/pkg/transport/types"
8+
)
9+
10+
// Middleware type constant
11+
const (
12+
MiddlewareType = "audit"
13+
)
14+
15+
// MiddlewareParams represents the parameters for audit middleware
16+
type MiddlewareParams struct {
17+
ConfigPath string `json:"config_path,omitempty"` // Kept for backwards compatibility
18+
ConfigData *Config `json:"config_data,omitempty"` // New field for config contents
19+
Component string `json:"component,omitempty"`
20+
}
21+
22+
// Middleware wraps audit middleware functionality
23+
type Middleware struct {
24+
middleware types.MiddlewareFunction
25+
}
26+
27+
// Handler returns the middleware function used by the proxy.
28+
func (m *Middleware) Handler() types.MiddlewareFunction {
29+
return m.middleware
30+
}
31+
32+
// Close cleans up any resources used by the middleware.
33+
func (*Middleware) Close() error {
34+
// Audit middleware doesn't need cleanup
35+
return nil
36+
}
37+
38+
// CreateMiddleware factory function for audit middleware
39+
func CreateMiddleware(config *types.MiddlewareConfig, runner types.MiddlewareRunner) error {
40+
41+
var params MiddlewareParams
42+
if err := json.Unmarshal(config.Parameters, &params); err != nil {
43+
return fmt.Errorf("failed to unmarshal audit middleware parameters: %w", err)
44+
}
45+
46+
var auditConfig *Config
47+
var err error
48+
49+
if params.ConfigData != nil {
50+
// Use provided config data (preferred method)
51+
auditConfig = params.ConfigData
52+
} else if params.ConfigPath != "" {
53+
// Load config from file (backwards compatibility)
54+
auditConfig, err = LoadFromFile(params.ConfigPath)
55+
if err != nil {
56+
return fmt.Errorf("failed to load audit configuration: %w", err)
57+
}
58+
} else {
59+
// Use default config
60+
auditConfig = DefaultConfig()
61+
}
62+
63+
// Set component name if provided
64+
if params.Component != "" {
65+
auditConfig.Component = params.Component
66+
}
67+
68+
middleware, err := auditConfig.CreateMiddleware()
69+
if err != nil {
70+
return fmt.Errorf("failed to create audit middleware: %w", err)
71+
}
72+
73+
auditMw := &Middleware{middleware: middleware}
74+
runner.AddMiddleware(auditMw)
75+
return nil
76+
}

0 commit comments

Comments
 (0)