-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExplainControllerPolicyTest.java
More file actions
209 lines (180 loc) · 9.13 KB
/
Copy pathExplainControllerPolicyTest.java
File metadata and controls
209 lines (180 loc) · 9.13 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
package com.dbaagent.controller;
import com.dbaagent.model.ConnectionRequest;
import com.dbaagent.model.ExplainPlanAnalysis;
import com.dbaagent.provider.DatabaseProviderRegistry;
import com.dbaagent.service.AnalysisHistoryService;
import com.dbaagent.service.CredentialService;
import com.dbaagent.service.ExplainPlanService;
import com.dbaagent.model.QueryExecutionOrigin;
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 org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import java.util.Map;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
* Covers the new safety property in ExplainController:
*
* - useAnalyze=true routes the underlying query through
* QueryExecutionPolicyService.enforce(...) BEFORE running EXPLAIN ANALYZE.
* Developers can't bypass the mutation guard by piggybacking on the
* analyzer endpoint.
* - useAnalyze=false is purely plan extraction (no execution), so the
* policy gate is skipped and any connection-reader can call it.
* - Every call audits, success or blocked.
*/
@ExtendWith(MockitoExtension.class)
class ExplainControllerPolicyTest {
@Mock private ExplainPlanService explainPlanService;
@Mock private AnalysisHistoryService historyService;
@Mock private AccessControlService accessControlService;
@Mock private CredentialService credentialService;
@Mock private DatabaseProviderRegistry providerRegistry;
@Mock private QueryExecutionPolicyService queryExecutionPolicyService;
@Mock private SqlExecutionAuditService sqlExecutionAuditService;
@Mock private HttpServletRequest httpRequest;
private ExplainController controller;
@BeforeEach
void setUp() {
controller = new ExplainController(
explainPlanService,
historyService,
accessControlService,
credentialService,
providerRegistry,
queryExecutionPolicyService,
sqlExecutionAuditService
);
}
@Test
void useAnalyzeTrue_runsPolicyGateBeforeAnalyzing() {
givenConnection("conn-1", "postgres");
when(explainPlanService.analyzeQuery(eq("conn-1"), anyString(), eq(true)))
.thenReturn(new ExplainPlanAnalysis());
ResponseEntity<?> response = controller.analyzeQuery(
request("conn-1", "DELETE FROM users WHERE id = 1", true),
httpRequest
);
assertThat(response.getStatusCode().is2xxSuccessful()).isTrue();
// The policy gate is the only thing standing between an
// unprivileged user and a real DELETE — verify it ran.
verify(queryExecutionPolicyService).enforce(any(), any(), eq("postgres"));
verify(explainPlanService).analyzeQuery(eq("conn-1"), eq("DELETE FROM users WHERE id = 1"), eq(true));
verify(sqlExecutionAuditService, times(1)).record(any());
}
@Test
void useAnalyzeTrue_policyBlocksDeveloperMutation_neverHitsAnalyzer() {
givenConnection("conn-1", "postgres");
when(queryExecutionPolicyService.enforce(any(), any(), eq("postgres")))
.thenThrow(QueryExecutionPolicyException.editorMutationForbidden("DELETE"));
ResponseEntity<?> response = controller.analyzeQuery(
request("conn-1", "DELETE FROM users WHERE id = 1", true),
httpRequest
);
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.FORBIDDEN);
@SuppressWarnings("unchecked")
Map<String, Object> body = (Map<String, Object>) response.getBody();
assertThat(body).containsEntry("errorCode", QueryExecutionPolicyException.EDITOR_MUTATION_FORBIDDEN);
// Crucial: the analyzer must not have been called — otherwise the
// DELETE would have executed via EXPLAIN ANALYZE.
verify(explainPlanService, never()).analyzeQuery(anyString(), anyString(), anyBoolean());
verify(sqlExecutionAuditService, times(1)).record(any());
}
@Test
void useAnalyzeTrue_confirmationRequired_propagatesRequiresConfirmation() {
givenConnection("conn-1", "postgres");
when(queryExecutionPolicyService.enforce(any(), any(), eq("postgres")))
.thenThrow(QueryExecutionPolicyException.confirmationRequired("UPDATE", java.util.List.of("verify rows")));
ResponseEntity<?> response = controller.analyzeQuery(
request("conn-1", "UPDATE users SET active=false WHERE id=1", true),
httpRequest
);
@SuppressWarnings("unchecked")
Map<String, Object> body = (Map<String, Object>) response.getBody();
assertThat(body).containsEntry("requiresConfirmation", true);
assertThat(body).containsEntry("queryType", "UPDATE");
verify(explainPlanService, never()).analyzeQuery(anyString(), anyString(), anyBoolean());
}
@Test
void useAnalyzeTrue_mcpBearer_usesMcpExecutionContext() {
givenConnection("conn-1", "postgres");
lenient().when(httpRequest.getHeader(anyString())).thenReturn(null);
when(httpRequest.getHeader(HttpHeaders.AUTHORIZATION))
.thenReturn("Bearer dsql_mcp_public.secret");
when(accessControlService.getCurrentUsername()).thenReturn("admin");
when(accessControlService.isCurrentUserAdmin()).thenReturn(true);
when(explainPlanService.analyzeQuery(eq("conn-1"), anyString(), eq(true)))
.thenReturn(new ExplainPlanAnalysis());
controller.analyzeQuery(
request("conn-1", "CREATE TABLE t_new (id INT PRIMARY KEY)", true),
httpRequest
);
ArgumentCaptor<QueryExecutionContext> captor = ArgumentCaptor.forClass(QueryExecutionContext.class);
verify(queryExecutionPolicyService).enforce(any(), captor.capture(), eq("postgres"));
assertThat(captor.getValue().origin()).isEqualTo(QueryExecutionOrigin.MCP);
assertThat(captor.getValue().mutationMode())
.isEqualTo(QueryExecutionContext.MutationMode.MAY_MUTATE);
assertThat(captor.getValue().actorIsAdmin()).isTrue();
}
@Test
void useAnalyzeFalse_skipsPolicyGate_butStillAudits() {
// Plain EXPLAIN doesn't execute the query, so we don't need to gate
// a developer running EXPLAIN over a DELETE — the database returns
// a plan or an error, no data changes.
when(explainPlanService.analyzeQuery(eq("conn-1"), anyString(), eq(false)))
.thenReturn(new ExplainPlanAnalysis());
ResponseEntity<?> response = controller.analyzeQuery(
request("conn-1", "SELECT * FROM orders", false),
httpRequest
);
assertThat(response.getStatusCode().is2xxSuccessful()).isTrue();
verify(queryExecutionPolicyService, never()).enforce(any(), any(), anyString());
verify(credentialService, never()).getDecryptedConnection(anyString());
verify(sqlExecutionAuditService, times(1)).record(any());
}
@Test
void analyzerFailure_stillEmitsAuditFailureRow() {
when(explainPlanService.analyzeQuery(eq("conn-1"), anyString(), eq(false)))
.thenThrow(new RuntimeException("DB went away"));
ResponseEntity<?> response = controller.analyzeQuery(
request("conn-1", "SELECT * FROM orders", false),
httpRequest
);
assertThat(response.getStatusCode().is5xxServerError()).isTrue();
verify(sqlExecutionAuditService, times(1)).record(any());
}
// ─── helpers ──────────────────────────────────────────────────────────
private void givenConnection(String connectionId, String dbType) {
ConnectionRequest conn = new ConnectionRequest();
conn.setDbType(dbType);
when(credentialService.getDecryptedConnection(connectionId)).thenReturn(conn);
when(providerRegistry.getCanonicalName(dbType)).thenReturn(dbType);
}
private ExplainController.ExplainRequest request(String connectionId, String sql, boolean useAnalyze) {
ExplainController.ExplainRequest req = new ExplainController.ExplainRequest();
req.setConnectionId(connectionId);
req.setQuery(sql);
req.setUseAnalyze(useAnalyze);
return req;
}
}