-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExplainController.java
More file actions
372 lines (338 loc) · 15.2 KB
/
Copy pathExplainController.java
File metadata and controls
372 lines (338 loc) · 15.2 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
package com.dbaagent.controller;
import com.dbaagent.model.AnalysisHistory;
import com.dbaagent.model.ConnectionRequest;
import com.dbaagent.model.ExplainPlanAnalysis;
import com.dbaagent.model.QueryExecutionOrigin;
import com.dbaagent.model.QueryRequest;
import com.dbaagent.provider.DatabaseProviderRegistry;
import com.dbaagent.service.AnalysisHistoryService;
import com.dbaagent.service.ClientContext;
import com.dbaagent.service.CredentialService;
import com.dbaagent.service.ExplainPlanService;
import com.dbaagent.service.McpTokenService;
import com.dbaagent.service.QueryExecutionContext;
import com.dbaagent.service.QueryExecutionPolicyException;
import com.dbaagent.service.QueryExecutionPolicyService;
import com.dbaagent.service.SqlExecutionAuditService;
import com.dbaagent.service.security.AccessControlService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;
import java.sql.SQLSyntaxErrorException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* REST API for SQL execution plan analysis.
*
* Two safety properties matter on this controller:
*
* 1. `useAnalyze=true` actually runs the underlying statement inside
* EXPLAIN ANALYZE — so for any mutating statement, that's a real
* database write. We route those through QueryExecutionPolicyService
* with `QueryExecutionContext.forSqlSurface(...)` so MCP bearers
* keep the MCP DROP/TRUNCATE block and Editor JWT callers keep the
* Editor DROP TABLE block. Role, WHERE, and confirmation gates that
* protect /api/connections/{id}/query protect this path too.
*
* 2. Every call — success, blocked, or failed — emits a SecurityEvent so
* audit dashboards can see CLI/MCP/Editor traffic with one filter.
*/
@RestController
@RequestMapping("/explain")
@RequiredArgsConstructor
@Slf4j
public class ExplainController {
private final ExplainPlanService explainPlanService;
private final AnalysisHistoryService historyService;
private final AccessControlService accessControlService;
private final CredentialService credentialService;
private final DatabaseProviderRegistry providerRegistry;
private final QueryExecutionPolicyService queryExecutionPolicyService;
private final SqlExecutionAuditService sqlExecutionAuditService;
/**
* Analyze a query's execution plan. With `useAnalyze=true` the query is
* actually executed (Postgres/MySQL EXPLAIN ANALYZE semantics), so the
* mutation policy applies to the underlying statement.
*/
@PostMapping("/analyze")
public ResponseEntity<?> analyzeQuery(
@RequestBody ExplainRequest request,
HttpServletRequest httpRequest
) {
ClientContext client = ClientContext.fromRequest(httpRequest);
String connectionId = request.getConnectionId();
QueryRequest auditQueryRequest = buildAuditQueryRequest(request, httpRequest);
ConnectionRequest connectionRequest = null;
try {
accessControlService.assertCanUseChatEditor(connectionId);
log.info("EXPLAIN analysis requested for connection: {} (useAnalyze={})", connectionId, request.isUseAnalyze());
// ANALYZE actually executes the SQL. Route the underlying
// statement through the same policy gate /connections/{id}/query
// uses so a developer can't bypass the mutation guard by sending
// useAnalyze=true with `DELETE FROM users`, and MCP callers keep
// the DROP/TRUNCATE block.
if (request.isUseAnalyze()) {
connectionRequest = credentialService.getDecryptedConnection(connectionId);
String dbType = providerRegistry.getCanonicalName(connectionRequest.getDbType());
queryExecutionPolicyService.enforce(
auditQueryRequest,
QueryExecutionContext.forSqlSurface(
McpTokenService.isMcpAuthorizationHeader(
httpRequest.getHeader(HttpHeaders.AUTHORIZATION)
),
accessControlService.getCurrentUsername(),
accessControlService.isCurrentUserAdmin(),
Boolean.TRUE.equals(request.getMutationConfirmed())
),
dbType
);
}
ExplainPlanAnalysis analysis = explainPlanService.analyzeQuery(
connectionId,
request.getQuery(),
request.isUseAnalyze()
);
sqlExecutionAuditService.record(SqlExecutionAuditService.AuditRecord.executed()
.operation("analyze-plan")
.connectionId(connectionId)
.connectionRequest(connectionRequest)
.queryRequest(auditQueryRequest)
.explainResult(analysis)
.useAnalyze(request.isUseAnalyze())
.httpRequest(httpRequest)
.client(client));
return ResponseEntity.ok(analysis);
} catch (QueryExecutionPolicyException e) {
sqlExecutionAuditService.record(SqlExecutionAuditService.AuditRecord.blocked(e.getMessage())
.operation("analyze-plan")
.connectionId(connectionId)
.connectionRequest(connectionRequest)
.queryRequest(auditQueryRequest)
.useAnalyze(request.isUseAnalyze())
.httpRequest(httpRequest)
.client(client));
return ResponseEntity.status(e.getHttpStatus()).body(Map.of(
"message", e.getMessage(),
"errorCode", e.getErrorCode(),
"queryType", e.getQueryType(),
"requiresConfirmation", e.isRequiresConfirmation(),
"warnings", e.getWarnings()
));
} catch (org.springframework.web.server.ResponseStatusException e) {
throw e;
} catch (Exception e) {
String reason = e instanceof ResponseStatusException rse ? rse.getReason() : e.getMessage();
sqlExecutionAuditService.record(SqlExecutionAuditService.AuditRecord.failed(reason)
.operation("analyze-plan")
.connectionId(connectionId)
.connectionRequest(connectionRequest)
.queryRequest(auditQueryRequest)
.useAnalyze(request.isUseAnalyze())
.httpRequest(httpRequest)
.client(client));
if (e instanceof ResponseStatusException responseStatusException) {
return ResponseEntity.status(responseStatusException.getStatusCode())
.body(Map.of("message", responseStatusException.getReason()));
}
log.error("Error analyzing query execution plan", e);
HttpStatus status = isSqlSyntaxError(e) || e instanceof IllegalArgumentException
? HttpStatus.BAD_REQUEST
: HttpStatus.INTERNAL_SERVER_ERROR;
String message = e.getMessage() != null ? e.getMessage() : "Failed to analyze query";
return ResponseEntity.status(status).body(Map.of("message", message));
}
}
/**
* Build a QueryRequest used only for audit + policy classification.
* Carries the user's mutationConfirmed flag through so admin-confirmed
* ANALYZE runs aren't stuck on the confirmation gate.
*/
private QueryRequest buildAuditQueryRequest(ExplainRequest request, HttpServletRequest httpRequest) {
QueryRequest qr = new QueryRequest();
qr.setQuery(request.getQuery());
boolean mcpBearer = McpTokenService.isMcpAuthorizationHeader(
httpRequest.getHeader(HttpHeaders.AUTHORIZATION)
);
qr.setExecutionOrigin(mcpBearer ? QueryExecutionOrigin.MCP : QueryExecutionOrigin.EDITOR);
qr.setMutationConfirmed(Boolean.TRUE.equals(request.getMutationConfirmed()));
return qr;
}
/**
* Save analysis to history
*/
@PostMapping("/history")
public ResponseEntity<HistoryResponse> saveHistory(
@RequestBody SaveHistoryRequest request
) {
try {
accessControlService.assertCanUseChatEditor(request.getConnectionId());
log.info("Saving analysis history for connection: {}", request.getConnectionId());
AnalysisHistory history = historyService.saveAnalysis(
request.getConnectionId(),
request.getQuery(),
request.getUseAnalyze(),
request.getAnalysisData(),
request.getPerformanceScore(),
request.getIssueCount(),
request.getUserId()
);
HistoryResponse response = convertToResponse(history);
return ResponseEntity.ok(response);
} catch (org.springframework.web.server.ResponseStatusException e) {
throw e;
} catch (Exception e) {
if (e instanceof ResponseStatusException responseStatusException) {
return ResponseEntity.status(responseStatusException.getStatusCode()).build();
}
log.error("Error saving analysis history", e);
return ResponseEntity.internalServerError().build();
}
}
private boolean isSqlSyntaxError(Throwable error) {
Throwable current = error;
while (current != null) {
if (current instanceof BadSqlGrammarException || current instanceof SQLSyntaxErrorException) {
return true;
}
current = current.getCause();
}
return false;
}
/**
* Get analysis history for a connection
*/
@GetMapping("/history/{connectionId}")
public ResponseEntity<List<HistoryResponse>> getHistory(
@PathVariable String connectionId
) {
try {
accessControlService.assertCanUseChatEditor(connectionId);
log.info("Fetching analysis history for connection: {}", connectionId);
List<AnalysisHistory> histories = historyService.getRecentHistory(connectionId);
List<HistoryResponse> responses = histories.stream()
.map(this::convertToResponse)
.collect(Collectors.toList());
return ResponseEntity.ok(responses);
} catch (org.springframework.web.server.ResponseStatusException e) {
throw e;
} catch (Exception e) {
if (e instanceof ResponseStatusException responseStatusException) {
return ResponseEntity.status(responseStatusException.getStatusCode()).build();
}
log.error("Error fetching analysis history", e);
return ResponseEntity.internalServerError().build();
}
}
/**
* Delete a specific history entry
*/
@DeleteMapping("/history/{id}")
public ResponseEntity<Map<String, String>> deleteHistory(
@PathVariable String id
) {
try {
accessControlService.assertCanAccessAnalysisHistory(id);
log.info("Deleting analysis history: {}", id);
historyService.deleteHistory(id);
return ResponseEntity.ok(Map.of("message", "History deleted successfully"));
} catch (org.springframework.web.server.ResponseStatusException e) {
throw e;
} catch (Exception e) {
if (e instanceof ResponseStatusException responseStatusException) {
return ResponseEntity.status(responseStatusException.getStatusCode())
.body(Map.of("message", responseStatusException.getReason()));
}
log.error("Error deleting analysis history", e);
return ResponseEntity.internalServerError().build();
}
}
/**
* Delete all history for a connection
*/
@DeleteMapping("/history/connection/{connectionId}")
public ResponseEntity<Map<String, String>> deleteAllHistory(
@PathVariable String connectionId
) {
try {
accessControlService.assertCanUseChatEditor(connectionId);
log.info("Deleting all history for connection: {}", connectionId);
historyService.deleteAllForConnection(connectionId);
return ResponseEntity.ok(Map.of("message", "All history deleted successfully"));
} catch (org.springframework.web.server.ResponseStatusException e) {
throw e;
} catch (Exception e) {
if (e instanceof ResponseStatusException responseStatusException) {
return ResponseEntity.status(responseStatusException.getStatusCode())
.body(Map.of("message", responseStatusException.getReason()));
}
log.error("Error deleting all history", e);
return ResponseEntity.internalServerError().build();
}
}
/**
* Convert AnalysisHistory entity to response DTO
*/
private HistoryResponse convertToResponse(AnalysisHistory history) {
Map<String, Object> analysisData = historyService.getAnalysisDataAsMap(history);
HistoryResponse response = new HistoryResponse();
response.setId(history.getId());
response.setConnectionId(history.getConnectionId());
response.setUserId(history.getUserId());
response.setQuery(history.getQuery());
response.setUseAnalyze(history.getUseAnalyze());
response.setAnalysisData(analysisData);
response.setPerformanceScore(history.getPerformanceScore());
response.setIssueCount(history.getIssueCount());
response.setTimestamp(history.getCreatedAt().toString());
return response;
}
/**
* Request model for EXPLAIN analysis
*/
@Data
public static class ExplainRequest {
private String connectionId;
private String query;
private boolean useAnalyze = false; // If true, use EXPLAIN ANALYZE (actually executes query)
// When useAnalyze=true with a mutating statement, the policy gate
// returns requiresConfirmation on the first call; client resends
// with mutationConfirmed=true.
private Boolean mutationConfirmed = Boolean.FALSE;
}
/**
* Request model for saving analysis history
*/
@Data
public static class SaveHistoryRequest {
private String connectionId;
private String userId;
private String query;
private Boolean useAnalyze;
private Map<String, Object> analysisData;
private Integer performanceScore;
private Integer issueCount;
}
/**
* Response model for analysis history
*/
@Data
public static class HistoryResponse {
private String id;
private String connectionId;
private String userId;
private String query;
private Boolean useAnalyze;
private Map<String, Object> analysisData;
private Integer performanceScore;
private Integer issueCount;
private String timestamp;
}
}