Repository navigation
Expand file tree
/
Copy pathMCPResource.java
More file actions
378 lines (349 loc) · 13.1 KB
/
Copy pathMCPResource.java
File metadata and controls
378 lines (349 loc) · 13.1 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
/*******************************************************************************
* Copyright 2015 Defense Health Agency (DHA)
*
* If your use of this software does not include any GPLv2 components:
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ----------------------------------------------------------------------------
* If your use of this software includes any GPLv2 components:
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*******************************************************************************/
package prerna.semoss.web.services.local;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.time.ZoneId;
import java.util.HashSet;
import java.util.HexFormat;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.ReentrantLock;
import java.util.regex.Pattern;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.ThreadContext;
import jakarta.annotation.security.PermitAll;
import jakarta.inject.Singleton;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.sse.Sse;
import jakarta.ws.rs.sse.SseEventSink;
import prerna.auth.AccessToken;
import prerna.auth.User;
import prerna.mcp.MCPReaper;
import prerna.om.Insight;
import prerna.om.InsightStore;
import prerna.util.Constants;
import prerna.util.Utility;
import prerna.web.services.util.WebUtility;
@Singleton
@Path("/ext/mcp/{toolbox_id}")
@PermitAll
public class MCPResource {
private static final Logger classLogger = LogManager.getLogger(MCPResource.class);
private static final long MAX_IDLE_TIMEOUT_MINUTES = 30;
public static final String MCP_AUTH_KEY = "MCP_AUTH_KEY";
private static final Map<String, Insight> INSIGHT_MAP = new ConcurrentHashMap<>();
private static final Map<String, ReentrantLock> SESSION_LOCKS = new ConcurrentHashMap<>();
// the toolbox id is placed inside every pixel the toolbox runs; engine and
// project ids use only these characters, while the path segment check alone
// would still allow quotes
private static final Pattern TOOLBOX_ID_PATTERN = Pattern.compile("[A-Za-z0-9_-]{1,128}");
private static final ExecutorService SSE_EXECUTOR = Executors
.newThreadPerTaskExecutor(Thread.ofVirtual().name("mcp-sse-", 0).factory());
/**
* Handle streamable HTTP connection
*
* @param toolbox_id
* @param access
* @param request
* @param response
*/
@POST
@Path("/comms")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public void commsHttp(@PathParam("toolbox_id") String toolbox_id, @QueryParam("access_key") String access,
@Context HttpServletRequest request, @Context HttpServletResponse response) {
if (!isValidToolboxId(toolbox_id)) {
rejectToolboxId(response);
return;
}
classLogger.info("Running tool via streamable http... {}", toolbox_id);
// Get the input stream from the async context
try {
final InputStream is = request.getInputStream();
// Initialize session
HttpSession session = request.getSession();
String cacheKey = insightCacheKey(session, request.getHeader("Authorization"));
addAuthKeyToSession(session, cacheKey);
String sessionId = session.getId();
Insight insight = getInsight(session, cacheKey);
int sessionTimeoutSeconds = session.getMaxInactiveInterval();
long idleTimeoutMinutes = (sessionTimeoutSeconds <= 0) ? MAX_IDLE_TIMEOUT_MINUTES
: Math.min(MAX_IDLE_TIMEOUT_MINUTES, sessionTimeoutSeconds / 60L);
MCPReaper reaper = new MCPReaper(insight, sessionId, is, response, toolbox_id,
request.getRequestURL().toString(), ThreadContext.getImmutableContext(), idleTimeoutMinutes);
reaper.run();
} catch (IOException e) {
if (!WebUtility.handleStreamingException(e, classLogger, toolbox_id, null, null)) {
classLogger.error("Error running tool via streamable http.... {}", toolbox_id, e);
}
}
}
/**
* Handle SSE connection for backward compatibility
*
* @param toolbox_id
* @param access
* @param eventSink
* @param sse
* @param is
* @param request
*/
@POST
@Path("/comms")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.SERVER_SENT_EVENTS)
public void commsSse(@PathParam("toolbox_id") String toolbox_id, @QueryParam("access_key") String access,
@Context SseEventSink eventSink, @Context Sse sse, InputStream is, @Context HttpServletRequest request,
@Context HttpServletResponse response) {
if (!isValidToolboxId(toolbox_id)) {
rejectToolboxId(response);
try {
eventSink.close();
} catch (IOException e) {
classLogger.warn("Unable to close the SSE stream of a refused MCP request", e);
}
return;
}
classLogger.info("Running tool via SSE... {}", toolbox_id);
// Initialize session
HttpSession session = request.getSession();
String cacheKey = insightCacheKey(session, request.getHeader("Authorization"));
addAuthKeyToSession(session, cacheKey);
String sessionId = session.getId();
Insight insight = getInsight(session, cacheKey);
try {
response.setContentType(MediaType.SERVER_SENT_EVENTS);
response.setHeader("X-Content-Type-Options", "nosniff");
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Connection", "keep-alive");
response.setCharacterEncoding("UTF-8");
response.flushBuffer();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
MCPReaper reaper = new MCPReaper(insight, sessionId, reader, eventSink, sse, toolbox_id,
request.getRequestURL().toString(), ThreadContext.getImmutableContext());
SSE_EXECUTOR.submit(reaper);
} catch (IOException e) {
if (!WebUtility.handleStreamingException(e, classLogger, toolbox_id, null, null)) {
classLogger.error("Error running tool via sse.... {}", toolbox_id, e);
}
}
}
/**
* Returns the insight cache key for a caller: the signed in user's login plus a
* digest of the Authorization header, or the session id when there is no
* header. The same header sent by two people never shares an insight, and the
* header itself, a bearer secret, is never kept.
*
* @param session the caller's session
* @param authorization the Authorization header, or null
* @return the cache key
*/
public static String insightCacheKey(HttpSession session, String authorization) {
if (authorization == null) {
return session.getId();
}
User user = (User) session.getAttribute(Constants.SESSION_USER);
AccessToken login = user == null ? null : user.getPrimaryLoginToken();
String owner = login == null || login.getId() == null || login.getProvider() == null
? "session " + session.getId()
: login.getProvider().getLabel() + " " + login.getId();
try {
byte[] digest = MessageDigest.getInstance("SHA-256")
.digest(authorization.getBytes(StandardCharsets.UTF_8));
return owner + " " + HexFormat.of().formatHex(digest);
} catch (NoSuchAlgorithmException e) {
throw new IllegalStateException("SHA-256 is not available", e);
}
}
/**
* Returns whether a toolbox id is safe to place in the toolbox's pixels.
*
* @param toolboxId the toolbox id from the request path
* @return true when the id has only the characters engine and project ids use
*/
private static boolean isValidToolboxId(String toolboxId) {
return toolboxId != null && TOOLBOX_ID_PATTERN.matcher(toolboxId).matches();
}
/**
* Answers 400 for a toolbox id that is not safe to use, before any stream
* starts.
*
* @param response the response to answer on
*/
private static void rejectToolboxId(HttpServletResponse response) {
classLogger.warn("Refused an MCP request for an invalid toolbox id");
try {
response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid toolbox id");
} catch (IOException e) {
classLogger.warn("Unable to answer an MCP request for an invalid toolbox id", e);
}
}
/**
* Adds an insight cache key to a set in the session in a thread-safe manner, so
* the cached insight is cleared when the session ends.
*
* @param session The user's HttpSession
* @param cacheKey The cache key from {@link #insightCacheKey}
*/
private static void addAuthKeyToSession(HttpSession session, String cacheKey) {
if (cacheKey != null && !cacheKey.equals(session.getId())) {
String sessionId = session.getId();
ReentrantLock sessionLock = SESSION_LOCKS.computeIfAbsent(sessionId, ignored -> new ReentrantLock());
sessionLock.lock();
try {
Set<String> mcpKeys = (Set<String>) session.getAttribute(MCP_AUTH_KEY);
if (mcpKeys == null) {
mcpKeys = new HashSet<>();
session.setAttribute(MCP_AUTH_KEY, mcpKeys);
}
mcpKeys.add(cacheKey);
} finally {
sessionLock.unlock();
}
}
}
/**
* Remove a key from the mcpThread cache
*
* @param key
*/
public static void clearInsight(String key) {
if (key != null) {
Insight removedInsight = INSIGHT_MAP.remove(key);
if (removedInsight != null) {
MCPReaper.clearInsightLock(removedInsight);
classLogger.info("Removed cached insight from MCP thread");
}
}
}
/**
* Remove the session lock entry created for MCP auth-key writes.
*
* @param sessionId http session id
*/
public static void clearSessionLock(String sessionId) {
if (sessionId != null) {
SESSION_LOCKS.remove(sessionId);
}
}
/**
* Remove an insight lock entry from MCP reaper lock cache.
*
* @param insightId insight identifier
*/
public static void clearInsightLock(String insightId) {
MCPReaper.clearInsightLock(insightId);
}
/**
* Clears MCP session-scoped cache and lock state.
*
* @param sessionId http session id
*/
public static void clearSessionState(String sessionId) {
clearInsight(sessionId);
clearSessionLock(sessionId);
}
/**
* Returns the cached insight for a cache key, opening one for the session when
* there is none or the cached one has closed.
*
* @param session the caller's session
* @param cacheKey the key from {@link #insightCacheKey}
* @return the insight to run in
*/
private Insight getInsight(HttpSession session, String cacheKey) {
String key = cacheKey;
// fast path: avoid compute lock on cache hit
Insight existing = INSIGHT_MAP.get(key);
if (existing != null && InsightStore.getInstance().get(existing.getInsightId()) != null) {
return existing;
}
return INSIGHT_MAP.compute(key, (k, e) -> {
if (e != null && InsightStore.getInstance().get(e.getInsightId()) != null) {
return e;
}
return initSession(session);
});
}
/**
*
* @param session
* @return
*/
private Insight initSession(HttpSession session) {
User user = (User) session.getAttribute(Constants.SESSION_USER);
String insightId = (String) session.getAttribute(Constants.INSIGHT);
String sessionId = session.getId();
Insight insight = null;
// insight id could be null
if (insightId == null) {
Set<String> sessionInsights = InsightStore.getInstance().getInsightIDsForSession(sessionId);
if (sessionInsights == null || sessionInsights.isEmpty()) {
// need to make a new insight here
insight = new Insight();
InsightStore.getInstance().put(insight);
insightId = insight.getInsightId();
InsightStore.getInstance().addToSessionHash(sessionId, insightId);
} else {
// pull the insight id from the session set
insightId = sessionInsights.iterator().next();
insight = InsightStore.getInstance().get(insightId);
}
// get the zone id
ZoneId zoneId = ZoneId.of(Utility.getApplicationZoneId());
user.setZoneId(zoneId);
session.setAttribute(Constants.INSIGHT, insightId);
} else {
insight = InsightStore.getInstance().get(insightId);
}
// set the user
insight.setUser(user);
return insight;
}
}