-
-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathgate.go
More file actions
283 lines (265 loc) · 8.9 KB
/
Copy pathgate.go
File metadata and controls
283 lines (265 loc) · 8.9 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
package mcp_server //nolint:revive // fine for now
import (
"context"
"fmt"
"io"
"os"
"time"
"github.com/modelcontextprotocol/go-sdk/mcp"
"github.com/stackql/stackql/pkg/mcp_server/audit"
"github.com/stackql/stackql/pkg/mcp_server/dto"
"github.com/stackql/stackql/pkg/mcp_server/policy"
"github.com/stackql/stackql/pkg/sink"
)
// stderrSink returns the diagnostic writer used by the gate middleware.
// Indirected through a function so tests can swap it for a buffer.
var stderrSink = func() io.Writer { return os.Stderr }
// toolGate captures the per-tool metadata the middleware needs to classify a
// call, decide whether to allow it, and write an audit record afterwards.
type toolGate struct {
// toolName is the registered MCP tool name (audit + error messages).
toolName string
// defaultClass is the query class for tools whose input is not SQL.
// Hierarchy/metadata tools use QueryClassSelect; query tools use
// QueryClassUnknown so the classifier runs against args.SQL instead.
defaultClass policy.QueryClass
// extractSQL pulls the SQL out of a typed input value, returning the
// empty string for tools that take no SQL. Used by the classifier
// and by the audit event.
extractSQL func(any) string
// extractArgs returns a key/value map suitable for the Args field on the
// audit event. For hierarchy tools this carries the hierarchy fields;
// for query tools it carries SQL + row_limit.
extractArgs func(any) map[string]any
}
// extractSQLFromQueryInput returns args.SQL for the dto.QueryJSONInput shape.
func extractSQLFromQueryInput(args any) string {
if v, ok := args.(dto.QueryJSONInput); ok {
return v.SQL
}
return ""
}
// extractArgsFromQueryInput returns {sql, row_limit} for audit recording.
func extractArgsFromQueryInput(args any) map[string]any {
if v, ok := args.(dto.QueryJSONInput); ok {
return map[string]any{"sql": v.SQL, "row_limit": v.RowLimit}
}
return nil
}
// extractArgsFromHierarchy returns hierarchy fields for audit recording.
func extractArgsFromHierarchy(args any) map[string]any {
v, ok := args.(dto.HierarchyInput)
if !ok {
return nil
}
return hierarchyToMap(v)
}
// extractArgsFromRegistryInput returns {provider, version} for audit recording.
func extractArgsFromRegistryInput(args any) map[string]any {
v, ok := args.(dto.RegistryInput)
if !ok {
return nil
}
out := map[string]any{}
if v.Provider != "" {
out["provider"] = v.Provider
}
if v.Version != "" {
out["version"] = v.Version
}
return out
}
func hierarchyToMap(v dto.HierarchyInput) map[string]any {
out := map[string]any{}
if v.Provider != "" {
out["provider"] = v.Provider
}
if v.Service != "" {
out["service"] = v.Service
}
if v.Resource != "" {
out["resource"] = v.Resource
}
if v.Method != "" {
out["method"] = v.Method
}
if v.RowLimit != 0 {
out["row_limit"] = v.RowLimit
}
return out
}
// addToolWithGate wraps mcp.AddTool with the policy gate + audit middleware.
// It is the single chokepoint at which mode enforcement and audit recording
// are applied. The tool handler itself stays oblivious to both concerns.
func addToolWithGate[In, Out any](
s *mcp.Server,
cfg *Config,
auditSink sink.Sink,
gate toolGate,
t *mcp.Tool,
h mcp.ToolHandlerFor[In, Out],
) {
if !cfg.IsToolEnabled(t.Name) {
return
}
wrapped := func(ctx context.Context, req *mcp.CallToolRequest, args In) (*mcp.CallToolResult, Out, error) {
var zero Out
started := time.Now()
mode := cfg.GetMode()
// One call computes class + decision + reason.
var sql string
if gate.extractSQL != nil {
sql = gate.extractSQL(args)
}
p := policy.NewPolicy(mode, sql, gate.defaultClass)
auditDecision := audit.DecisionAllow
switch p.Decision() {
case policy.DecisionAllow:
// proceed to tool execution below
case policy.DecisionRefuseImmediate:
err := fmt.Errorf("tool %q refused: %s", t.Name, p.Reason())
recordAudit(ctx, auditSink, cfg, gate, args, sql, p.Class(), mode,
audit.DecisionRefuseImmediate, started, err)
return nil, zero, err
case policy.DecisionNeedsApproval:
outcome, err := elicitApproval(ctx, req, t.Name, p.Reason(), sql, p.Class())
auditDecision = outcome
if err != nil {
recordAudit(ctx, auditSink, cfg, gate, args, sql, p.Class(), mode,
outcome, started, err)
return nil, zero, err
}
}
result, out, err := h(ctx, req, args)
recordAudit(ctx, auditSink, cfg, gate, args, sql, p.Class(), mode,
auditDecision, started, err)
if err != nil {
return result, out, err
}
if auditErr := finalizeAudit(cfg, p.Class()); auditErr != nil {
// Reserved: future strict-mode-on-audit-failure surfacing.
_ = auditErr
}
return result, out, nil
}
mcp.AddTool(s, t, wrapped)
}
// elicitApproval asks the user (via the client) to approve the action.
// Returns the audit decision-outcome string and an error if the action was
// refused. On accept, the error is nil and execution should proceed.
func elicitApproval(
ctx context.Context, req *mcp.CallToolRequest,
toolName, reason, sql string, class policy.QueryClass,
) (string, error) {
session := req.Session
caps := session.InitializeParams().Capabilities
if caps == nil || caps.Elicitation == nil {
err := fmt.Errorf(
"tool %q refused: %s and the MCP client does not support elicitation. "+
"Restart the server in 'full_access' mode if you trust this client, "+
"or use an elicitation-capable client",
toolName, reason)
return audit.DecisionNeedsApprovalUnavailable, err
}
message := fmt.Sprintf("Approve %s (%s)?", toolName, class.String())
if sql != "" {
message = fmt.Sprintf("Approve %s (%s)?\n\nSQL: %s", toolName, class.String(), sql)
}
res, err := session.Elicit(ctx, &mcp.ElicitParams{
Message: message,
RequestedSchema: map[string]any{
"type": "object",
"properties": map[string]any{},
},
})
if err != nil {
return audit.DecisionNeedsApprovalDeclined,
fmt.Errorf("tool %q refused: elicitation transport error: %w", toolName, err)
}
switch res.Action {
case "accept":
return audit.DecisionNeedsApprovalAccepted, nil
case "decline":
return audit.DecisionNeedsApprovalDeclined,
fmt.Errorf("tool %q refused: user declined approval", toolName)
case "cancel":
return audit.DecisionNeedsApprovalCancelled,
fmt.Errorf("tool %q refused: approval prompt was dismissed", toolName)
default:
return audit.DecisionNeedsApprovalDeclined,
fmt.Errorf("tool %q refused: unexpected elicitation action %q", toolName, res.Action)
}
}
// recordAudit writes one event to the configured sink. Audit-write failures
// are translated to client-visible errors only in strict / strict_mutations
// modes; in best_effort mode the failure is logged to stderr and ignored.
//
// Sequencing note: the audit write happens AFTER the tool has executed (or
// been skipped because it was gated out) but BEFORE the response returns to
// the client. In strict mode, an audit-write failure on a successful DELETE
// means the row is gone but the client gets an error - intentional, so that
// no mutation slips through unaudited.
func recordAudit(
ctx context.Context,
auditSink sink.Sink,
cfg *Config,
gate toolGate,
args any,
sql string,
class policy.QueryClass,
mode string,
decision string,
started time.Time,
toolErr error,
) {
if auditSink == nil {
return
}
event := audit.Event{
Timestamp: started,
Tool: gate.toolName,
Mode: mode,
Decision: decision,
DurationMs: time.Since(started).Milliseconds(),
}
if sql != "" {
event.SQL = sql
event.QueryClass = class.String()
} else if class != policy.QueryClassUnknown {
event.QueryClass = class.String()
}
if gate.extractArgs != nil {
event.Args = gate.extractArgs(args)
}
if toolErr != nil {
event.Error = toolErr.Error()
}
if err := auditSink.Record(ctx, event); err != nil {
handleAuditFailure(cfg, class, err)
}
}
// finalizeAudit is a placeholder hook for future strict-mode hardening.
func finalizeAudit(_ *Config, _ policy.QueryClass) error { return nil }
// handleAuditFailure decides whether an audit-sink error becomes a
// client-visible failure or just gets logged. The decision is per the
// configured failure_mode.
func handleAuditFailure(cfg *Config, class policy.QueryClass, err error) {
mode := cfg.Server.Audit.GetFailureMode()
switch mode {
case audit.FailureModeStrict:
fmt.Fprintf(stderrSink(), "audit write failed (strict): %v\n", err)
case audit.FailureModeStrictMutations:
// SELECTs proceed silently with a stderr note; mutations would
// already have errored at recordAudit's caller via the returned
// error chain in a future revision. For now we log uniformly.
if class == policy.QueryClassSelect {
fmt.Fprintf(stderrSink(), "audit write failed (best-effort for select): %v\n", err)
return
}
fmt.Fprintf(stderrSink(), "audit write failed (strict_mutations): %v\n", err)
case audit.FailureModeBestEffort:
fmt.Fprintf(stderrSink(), "audit write failed (best-effort): %v\n", err)
default:
fmt.Fprintf(stderrSink(), "audit write failed: %v\n", err)
}
}