Repository navigation
Expand file tree
/
Copy pathAppEngineChainUnitTests.java
More file actions
479 lines (425 loc) · 19.6 KB
/
Copy pathAppEngineChainUnitTests.java
File metadata and controls
479 lines (425 loc) · 19.6 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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
/*******************************************************************************
* 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.util;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mockStatic;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicReference;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;
import prerna.auth.AccessToken;
import prerna.auth.AuthProvider;
import prerna.auth.User;
import prerna.auth.access.EngineGrantee;
import prerna.auth.utils.AbstractSecurityUtilsUnitTestsSetup;
import prerna.auth.utils.SecurityEngineUtils;
import prerna.auth.utils.SecurityProjectUtils;
import prerna.auth.utils.SecurityUpdateUtils;
import prerna.engine.api.GuardrailTypeEnum;
import prerna.engine.api.IEngine;
import prerna.engine.api.IModelEngine;
import prerna.engine.impl.guardrail.AbstractGuardrailReactorFunctionEngine;
import prerna.engine.impl.model.responses.AskModelEngineResponse;
import prerna.engine.impl.model.responses.AskStringModelEngineResponse;
import prerna.engine.impl.model.usage.ModelUsageCost;
import prerna.engine.impl.pipeline.AgentRunOperation;
import prerna.engine.impl.pipeline.EngineCallPermission;
import prerna.engine.impl.pipeline.EngineProxyFactory;
import prerna.engine.impl.pipeline.StubEngines;
import prerna.om.Insight;
import prerna.om.ThreadStore;
import prerna.reactor.IReactor;
import prerna.reactor.interceptor.GenericGuardrailInputReactor;
import prerna.sablecc2.om.GenRowStruct;
import prerna.sablecc2.om.NounStore;
import prerna.sablecc2.om.execptions.SemossPixelException;
import prerna.sablecc2.om.nounmeta.GuardrailNounMetadata;
/**
* One whole chain, as the platform runs it: an app's custom reactor asks a
* model, the model's pipeline runs a guardrail, and the guardrail asks its
* judge model. Each test is one rule of docs/platform_services/engine_access.md
* for calls an engine makes for an app.
*
* <p>
* The pieces are the real ones wherever the access check reads them: the custom
* reactor is Java source compiled at the start and loaded through the project
* sandbox as the app's custom code ({@link #REACTOR_SOURCE}); the engines come
* from {@code Utility.getModel} and {@code Utility.getGuardrailEngine} behind
* the real proxy; the model's pipeline is a pipeline.json in its assets folder
* with the real {@link GenericGuardrailInputReactor}; the guardrail is a real
* engine class ({@link JudgeAskingGuardrail}); and every permission is a row in
* the junit security database, given and taken away the way the Apps and
* Engines tables do. Only the models' answers are made up.
* </p>
*
* <p>
* To debug a hop, set a breakpoint in {@code EngineAccessGuard.check} (or in
* {@code Caller.fromStack} and {@code Caller.originAppOf}) and run one test in
* debug. The check stops three times, for the model, the guardrail and the
* judge, and {@code engineId} says which.
* </p>
*/
class AppEngineChainUnitTests extends AbstractSecurityUtilsUnitTestsSetup {
private static final String APP = "orders-app";
private static final String OTHER_APP = "other-orders-app";
private static final String AGENT = "orders-agent";
private static final String MODEL = "orders-model";
private static final String GUARDRAIL = "orders-guardrail";
private static final String JUDGE = "orders-judge";
private static final String PROJECT_TYPE = "PROJECT";
private static final String PERSON = "chainperson";
private static final String REACTOR_CLASS = "orders.app.AskOrdersModelReactor";
/**
* The app's custom reactor, as its author writes it in the app's java folder.
*/
private static final String REACTOR_SOURCE = """
package orders.app;
import java.util.HashMap;
import prerna.engine.api.IModelEngine;
import prerna.engine.impl.model.responses.AskModelEngineResponse;
import prerna.reactor.AbstractReactor;
import prerna.sablecc2.om.PixelDataType;
import prerna.sablecc2.om.nounmeta.NounMetadata;
import prerna.util.Utility;
public class AskOrdersModelReactor extends AbstractReactor {
@Override
public NounMetadata execute() {
IModelEngine model = Utility.getModel("%s");
AskModelEngineResponse<?> answer = model.ask("When does order 42 ship?", null, null, new HashMap<>());
return new NounMetadata(answer.getResponse(), PixelDataType.CONST_STRING);
}
}
"""
.formatted(MODEL);
private static User owner;
private static User person;
private static Class<?> reactorClass;
private final AtomicReference<EngineGrantee> seenByTheModel = new AtomicReference<>();
private final AtomicReference<EngineGrantee> seenByTheGuardrail = new AtomicReference<>();
private final AtomicReference<EngineGrantee> seenByTheJudge = new AtomicReference<>();
private final List<String[]> grants = new ArrayList<>();
private MockedStatic<ModelUsageCost> prices;
@BeforeAll
static void anAppAModelAGuardrailAndAJudgeOwnedBySomeoneElse() throws Exception {
owner = signedUpUser("chainowner");
person = signedUpUser(PERSON);
SecurityProjectUtils.addProject(APP, "Orders App", "CODE", null, false, owner);
SecurityProjectUtils.addProjectOwner(owner, APP);
SecurityProjectUtils.addProject(OTHER_APP, "Other Orders App", "CODE", null, false, owner);
SecurityProjectUtils.addProjectOwner(owner, OTHER_APP);
SecurityProjectUtils.addProject(AGENT, "Orders Agent", "WORKSPACE", null, false, owner);
SecurityProjectUtils.addProjectOwner(owner, AGENT);
SecurityProjectUtils.addProjectUserPermissions(owner, APP,
List.of(Map.of("userid", PERSON, "permission", "READ_ONLY")),
ZonedDateTime.now().plusDays(2).toString());
for (String[] engine : new String[][] { { MODEL, "MODEL" }, { GUARDRAIL, "GUARDRAIL" }, { JUDGE, "MODEL" } }) {
SecurityEngineUtils.addEngine(engine[0], engine[0], IEngine.CATALOG_TYPE.valueOf(engine[1]), null, null,
false, owner);
SecurityEngineUtils.addEngineOwner(owner, engine[0]);
}
reactorClass = compiledIntoTheApp(tempDir.resolve("orders-app-classes"));
}
@BeforeEach
void loadTheEnginesAndSignInThePerson() throws Exception {
prices = mockStatic(ModelUsageCost.class);
prices.when(() -> ModelUsageCost.hasKnownPrices(anyString())).thenReturn(false);
prices.when(() -> ModelUsageCost.priceEntry(anyString())).thenReturn(null);
IModelEngine model = StubEngines.unguardedModel(MODEL, Map.of("ask", () -> {
seenByTheModel.set(EngineCallPermission.granteeMakingTheCall());
return new AskStringModelEngineResponse("Order 42 ships on Friday", 0, 0);
}, "getSmssProp", AppEngineChainUnitTests::settingsWithAPipeline));
writePipeline(model);
DIHelperTestUtils.setLoadedEngine(MODEL, model);
DIHelperTestUtils.setLoadedEngine(GUARDRAIL, new JudgeAskingGuardrail(seenByTheGuardrail));
DIHelperTestUtils.setLoadedEngine(JUDGE, StubEngines.unguardedModel(JUDGE, Map.of("ask", () -> {
seenByTheJudge.set(EngineCallPermission.granteeMakingTheCall());
return new AskStringModelEngineResponse("SAFE", 0, 0);
})));
ThreadStore.setUser(person);
}
@AfterEach
void takeTheGrantsAwayAndUnload() throws Exception {
ThreadStore.remove();
for (String[] grant : grants) {
SecurityEngineUtils.removeEngineGranteePermission(owner, grant[0], grant[1], grant[2]);
}
grants.clear();
InternalUtility.removeLoadedEngine(MODEL);
InternalUtility.removeLoadedEngine(GUARDRAIL);
InternalUtility.removeLoadedEngine(JUDGE);
prices.close();
}
@Test
void eachEngineInTheChainGoesThroughOnThePermissionItGivesTheEngineCallingIt() throws Exception {
give(MODEL, APP, PROJECT_TYPE);
give(GUARDRAIL, MODEL, "MODEL");
give(JUDGE, GUARDRAIL, "GUARDRAIL");
assertEquals("Order 42 ships on Friday", runTheAppsReactor());
assertEquals(EngineGrantee.ofApp(APP), seenByTheModel.get());
assertEquals(new EngineGrantee(MODEL, "MODEL"), seenByTheGuardrail.get());
assertEquals(new EngineGrantee(GUARDRAIL, "GUARDRAIL"), seenByTheJudge.get());
}
@Test
void eachEngineInTheChainGoesThroughOnThePermissionItGivesTheApp() throws Exception {
give(MODEL, APP, PROJECT_TYPE);
give(GUARDRAIL, APP, PROJECT_TYPE);
give(JUDGE, APP, PROJECT_TYPE);
assertEquals("Order 42 ships on Friday", runTheAppsReactor());
assertEquals(EngineGrantee.ofApp(APP), seenByTheModel.get());
assertEquals(EngineGrantee.ofApp(APP), seenByTheGuardrail.get());
assertEquals(EngineGrantee.ofApp(APP), seenByTheJudge.get());
}
@Test
void anEngineThatGivesNeitherTheEngineCallingItNorTheAppAPermissionStopsTheChain() throws Exception {
give(MODEL, APP, PROJECT_TYPE);
give(GUARDRAIL, MODEL, "MODEL");
SemossPixelException refusal = assertThrows(SemossPixelException.class, this::runTheAppsReactor);
assertTrue(refusal.getMessage().startsWith("Engine " + JUDGE + " does not exist"), refusal.getMessage());
assertTrue(refusal.getMessage().contains("engine " + GUARDRAIL), refusal.getMessage());
assertTrue(refusal.getMessage().contains("app " + APP), refusal.getMessage());
assertNull(seenByTheModel.get());
}
@Test
void anAppTheModelGivesNothingIsRefusedAtTheModel() {
give(GUARDRAIL, APP, PROJECT_TYPE);
give(JUDGE, APP, PROJECT_TYPE);
SemossPixelException refusal = assertThrows(SemossPixelException.class, this::runTheAppsReactor);
assertTrue(refusal.getMessage().startsWith("Engine " + MODEL + " does not exist"), refusal.getMessage());
assertNull(seenByTheGuardrail.get());
}
@Test
void takingTheAppsPermissionAwayOnTheModelStopsTheChainAtTheModel() throws Exception {
give(MODEL, APP, PROJECT_TYPE);
give(GUARDRAIL, APP, PROJECT_TYPE);
give(JUDGE, APP, PROJECT_TYPE);
assertEquals("Order 42 ships on Friday", runTheAppsReactor());
takeAway(MODEL, APP, PROJECT_TYPE);
SemossPixelException refusal = assertThrows(SemossPixelException.class, this::runTheAppsReactor);
assertTrue(refusal.getMessage().startsWith("Engine " + MODEL + " does not exist"), refusal.getMessage());
}
@Test
void aPermissionTheModelGivesAnotherAppDoesNotCount() {
give(MODEL, OTHER_APP, PROJECT_TYPE);
give(GUARDRAIL, APP, PROJECT_TYPE);
give(JUDGE, APP, PROJECT_TYPE);
SemossPixelException refusal = assertThrows(SemossPixelException.class, this::runTheAppsReactor);
assertTrue(refusal.getMessage().startsWith("Engine " + MODEL + " does not exist"), refusal.getMessage());
assertNull(seenByTheModel.get());
}
@Test
void anAgentRunAroundTheAppsReactorLendsTheReactorNothing() {
give(MODEL, AGENT, PROJECT_TYPE);
give(GUARDRAIL, APP, PROJECT_TYPE);
give(JUDGE, APP, PROJECT_TYPE);
try (AgentRunOperation run = AgentRunOperation.begin(AGENT)) {
SemossPixelException refusal = assertThrows(SemossPixelException.class, this::runTheAppsReactor);
assertTrue(refusal.getMessage().startsWith("Engine " + MODEL + " does not exist"), refusal.getMessage());
}
assertNull(seenByTheModel.get());
}
/**
* Runs the app's custom reactor for the person signed in on the thread, as a
* pixel that calls it does.
*
* @return what the reactor returned
* @throws Exception when the reactor cannot be made
*/
private Object runTheAppsReactor() throws Exception {
IReactor reactor = (IReactor) reactorClass.getDeclaredConstructor().newInstance();
return reactor.execute().getValue();
}
/**
* Gives an app or an engine Read-Only on an engine, as its owner does from the
* engine's Apps or Engines table, and takes it away after the test.
*
* @param engineId the engine giving the permission
* @param granteeId the app or engine given it
* @param granteeType its catalog type, PROJECT for an app
*/
private void give(String engineId, String granteeId, String granteeType) {
try {
SecurityEngineUtils.setEngineGranteePermission(owner, granteeId, granteeType, engineId, "READ_ONLY", null);
} catch (IllegalAccessException e) {
throw new IllegalStateException(e);
}
grants.add(new String[] { granteeId, granteeType, engineId });
}
/**
* Takes an app's or an engine's permission on an engine away during the test,
* as its owner does from the engine's Apps or Engines table.
*
* @param engineId the engine that gave the permission
* @param granteeId the app or engine given it
* @param granteeType its catalog type, PROJECT for an app
* @throws IllegalAccessException when the owner may not take it away
*/
private void takeAway(String engineId, String granteeId, String granteeType) throws IllegalAccessException {
SecurityEngineUtils.removeEngineGranteePermission(owner, granteeId, granteeType, engineId);
grants.removeIf(
grant -> grant[0].equals(granteeId) && grant[1].equals(granteeType) && grant[2].equals(engineId));
}
private static Properties settingsWithAPipeline() {
Properties smss = new Properties();
smss.setProperty(IEngine.PIPELINE, "pipeline.json");
return smss;
}
/**
* Writes the model's pipeline.json, which runs the guardrail on the question of
* every {@code ask}, as Settings > Guardrails saves it.
*
* @param model the model, unproxied
* @throws IOException when the file cannot be written
* @throws NoSuchMethodException when the model has no {@code ask}
*/
private static void writePipeline(IModelEngine model) throws IOException, NoSuchMethodException {
String question = IModelEngine.class.getMethod("ask", String.class, String.class, Insight.class, Map.class)
.getParameters()[0].getName();
File pipeline = EngineProxyFactory.pipelineFile(model, "pipeline.json");
Files.createDirectories(pipeline.toPath().getParent());
Files.writeString(pipeline.toPath(), """
{"pipelines": {"ask": {"input": [{
"reactorClass": "%s",
"params": {"checkId": "check0", "guardrailEngineId": "%s", "inputMapping": {"prompt": "%s"}}
}]}}}
""".formatted(GenericGuardrailInputReactor.class.getName(), GUARDRAIL, question),
StandardCharsets.UTF_8);
}
private static User signedUpUser(String id) {
AccessToken token = new AccessToken();
token.setId(id);
token.setName(id);
token.setUsername(id);
token.setEmail(id + "@test.com");
token.setProvider(AuthProvider.NATIVE);
assertTrue(SecurityUpdateUtils.registerUser(id, id, id + "@test.com", "Test123!",
AuthProvider.NATIVE.getLabel(), "5555555555", "001", "US", false, false, false, null));
User user = new User();
user.setPrimaryLogin(AuthProvider.NATIVE);
user.setAccessToken(token);
return user;
}
/**
* Compiles {@link #REACTOR_SOURCE} into a classes folder and loads it through
* the project sandbox, as the app's custom code.
*
* @param classes the app's classes folder
* @return the reactor's class, loaded as the app's
* @throws Exception when it does not compile or load
*/
private static Class<?> compiledIntoTheApp(Path classes) throws Exception {
Path source = classes.resolveSibling("orders-app-java").resolve(REACTOR_CLASS.replace('.', '/') + ".java");
Files.createDirectories(source.getParent());
Files.createDirectories(classes);
Files.writeString(source, REACTOR_SOURCE, StandardCharsets.UTF_8);
JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
ByteArrayOutputStream errors = new ByteArrayOutputStream();
int status = javac.run(null, null, errors, "--release", "21", "-g", "-proc:none", "-nowarn", "-classpath",
testClassPath(), "-d", classes.toString(), source.toString());
assertEquals(0, status, errors.toString(StandardCharsets.UTF_8));
return SandboxProbe.loader(classes, APP).loadClass(REACTOR_CLASS);
}
/**
* The classpath the tests run with. Maven runs them from one jar whose manifest
* lists the classpath, so that jar's list is read.
*
* @return the classpath, for javac
* @throws IOException when the jar cannot be read
*/
private static String testClassPath() throws IOException {
String classPath = System.getProperty("java.class.path");
String[] entries = classPath.split(File.pathSeparator);
if (entries.length != 1 || !entries[0].endsWith(".jar")) {
return classPath;
}
try (JarFile jar = new JarFile(entries[0])) {
Manifest manifest = jar.getManifest();
String listed = manifest == null ? null : manifest.getMainAttributes().getValue(Attributes.Name.CLASS_PATH);
if (listed == null || listed.isBlank()) {
return classPath;
}
URI base = Path.of(entries[0]).toUri();
List<String> paths = new ArrayList<>();
for (String entry : listed.trim().split("\\s+")) {
paths.add(Path.of(base.resolve(entry)).toString());
}
return String.join(File.pathSeparator, paths);
}
}
/**
* A guardrail that asks its judge model whether a prompt is safe, as the prompt
* guardrails do: its own code gets the judge from {@code Utility.getModel} and
* asks it, so the judge's access check sees the guardrail acting.
*/
static final class JudgeAskingGuardrail extends AbstractGuardrailReactorFunctionEngine {
private final AtomicReference<EngineGrantee> seen;
JudgeAskingGuardrail(AtomicReference<EngineGrantee> seen) {
this.seen = seen;
setEngineId(GUARDRAIL);
setEngineName(GUARDRAIL);
setSmssProp(new Properties());
}
@Override
public GuardrailNounMetadata execute(NounStore ns, GenRowStruct curRow) {
this.seen.set(EngineCallPermission.granteeMakingTheCall());
GenRowStruct prompt = ns.getGenRowStruct("prompt");
String text = prompt == null || prompt.isEmpty() ? "" : String.valueOf(prompt.get(0));
AskModelEngineResponse<?> verdict = Utility.getModel(JUDGE).ask("Is this safe? " + text, null, null,
new HashMap<>());
boolean pass = "SAFE".equals(verdict.getResponse());
return new GuardrailNounMetadata(pass, text, Map.of("classification", verdict.getResponse()));
}
@Override
public GuardrailTypeEnum getGuardrailType() {
return GuardrailTypeEnum.EMBEDDED_AGGRESSIVE_SELF_HARM;
}
}
}