Repository navigation
Expand file tree
/
Copy pathAgentEngineChainUnitTests.java
More file actions
374 lines (338 loc) · 14.1 KB
/
Copy pathAgentEngineChainUnitTests.java
File metadata and controls
374 lines (338 loc) · 14.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
/*******************************************************************************
* 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.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotNull;
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.mock;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.when;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
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.IEngine;
import prerna.engine.api.IModelEngine;
import prerna.engine.impl.model.Room;
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.StubEngines;
import prerna.om.ThreadStore;
import prerna.project.api.IProject;
import prerna.reactor.agent.AgentUseAccess;
import prerna.sablecc2.om.execptions.SemossPixelException;
/**
* What an agent's run may use, end to end: the agent's run asks models and
* reads skills with the permissions engines and projects gave the agent, on top
* of the person's own access, and only for a person who may use the agent. A
* sub-agent's run uses the sub-agent's permissions, never its parent's. Each
* test is one use case of docs/platform_services/engine_access.md for agent
* runs.
*
* <p>
* The pieces are the real ones wherever the access check reads them: the run
* begins through {@link AgentUseAccess#beginAgentRun}, as the agent runner and
* the room namer begin it; the models and the skill are behind the real engine
* proxy ({@link StubEngines}); and every permission is a row in the junit
* security database, given and taken away the way the Apps and Agents tables
* do. Only the models' answers are made up.
* </p>
*/
class AgentEngineChainUnitTests extends AbstractSecurityUtilsUnitTestsSetup {
private static final String AGENT = "chain-agent";
private static final String SUB_AGENT = "chain-sub-agent";
private static final String SKILL = "chain-skill";
private static final String AGENT_MODEL = "chain-agent-model";
private static final String SUB_AGENT_MODEL = "chain-sub-agent-model";
private static final String PERSONS_MODEL = "chain-persons-model";
private static final String PROJECT_TYPE = "PROJECT";
private static final String PERSON = "agentchainperson";
private static User owner;
private static User person;
private final AtomicReference<EngineGrantee> seenByTheAgentModel = new AtomicReference<>();
private final AtomicReference<EngineGrantee> seenByTheSubAgentModel = new AtomicReference<>();
private final AtomicReference<EngineGrantee> seenByThePersonsModel = new AtomicReference<>();
private final List<String[]> engineGrants = new ArrayList<>();
private final List<String[]> projectGrants = new ArrayList<>();
private MockedStatic<ModelUsageCost> prices;
private IModelEngine agentModel;
private IModelEngine subAgentModel;
private IModelEngine personsModel;
private IProject skill;
@BeforeAll
static void twoAgentsASkillAndThreeModelsOwnedBySomeoneElse() throws Exception {
owner = signedUpUser("agentchainowner");
person = signedUpUser(PERSON);
for (String[] project : new String[][] { { AGENT, "WORKSPACE" }, { SUB_AGENT, "WORKSPACE" },
{ SKILL, "SKILL" } }) {
SecurityProjectUtils.addProject(project[0], project[0], project[1], null, false, owner);
SecurityProjectUtils.addProjectOwner(owner, project[0]);
}
SecurityProjectUtils.addProjectUserPermissions(owner, AGENT,
List.of(Map.of("userid", PERSON, "permission", "READ_ONLY")),
ZonedDateTime.now().plusDays(2).toString());
for (String model : List.of(AGENT_MODEL, SUB_AGENT_MODEL, PERSONS_MODEL)) {
SecurityEngineUtils.addEngine(model, model, IEngine.CATALOG_TYPE.MODEL, null, null, false, owner);
SecurityEngineUtils.addEngineOwner(owner, model);
}
SecurityEngineUtils.addEngineUserPermissions(owner, PERSONS_MODEL,
List.of(Map.<String, Object>of("userid", PERSON, "permission", "READ_ONLY")));
}
@BeforeEach
void signInThePerson() {
prices = mockStatic(ModelUsageCost.class);
prices.when(() -> ModelUsageCost.hasKnownPrices(anyString())).thenReturn(false);
prices.when(() -> ModelUsageCost.priceEntry(anyString())).thenReturn(null);
agentModel = model(AGENT_MODEL, seenByTheAgentModel);
subAgentModel = model(SUB_AGENT_MODEL, seenByTheSubAgentModel);
personsModel = model(PERSONS_MODEL, seenByThePersonsModel);
skill = StubEngines.project(SKILL);
ThreadStore.setUser(person);
}
@AfterEach
void takeTheGrantsAway() throws Exception {
ThreadStore.remove();
for (String[] grant : engineGrants) {
SecurityEngineUtils.removeEngineGranteePermission(owner, grant[0], grant[1], grant[2]);
}
engineGrants.clear();
for (String[] grant : projectGrants) {
SecurityProjectUtils.removeProjectGranteePermission(owner, grant[0], grant[1], grant[2]);
}
projectGrants.clear();
prices.close();
}
@Test
void thePersonsOwnAccessNeedsNoAgentAndNoGrant() {
assertEquals("answer", ask(personsModel));
assertNull(seenByThePersonsModel.get());
assertThrows(SemossPixelException.class, () -> ask(agentModel));
}
@Test
void anAgentsRunAsksAModelTheAgentWasGivenAndNothingElseCanAskIt() {
give(AGENT_MODEL, AGENT);
assertThrows(SemossPixelException.class, () -> ask(agentModel));
try (AgentRunOperation run = AgentUseAccess.beginAgentRun(person, null, AGENT)) {
assertNotNull(run);
assertEquals("answer", ask(agentModel));
assertEquals(EngineGrantee.ofApp(AGENT), seenByTheAgentModel.get());
}
assertThrows(SemossPixelException.class, () -> ask(agentModel));
}
@Test
void aPersonWhoCannotViewTheAgentGetsNoneOfItsAccess() {
give(SUB_AGENT_MODEL, SUB_AGENT);
try (AgentRunOperation run = AgentUseAccess.beginAgentRun(person, null, SUB_AGENT)) {
assertNull(run);
assertThrows(SemossPixelException.class, () -> ask(subAgentModel));
}
}
@Test
void aReadOnlyGrantLetsTheAgentAskTheModelButNotChangeIt() {
give(AGENT_MODEL, AGENT);
try (AgentRunOperation run = AgentUseAccess.beginAgentRun(person, null, AGENT)) {
assertEquals("answer", ask(agentModel));
SemossPixelException refusal = assertThrows(SemossPixelException.class, agentModel::delete);
assertTrue(refusal.getMessage().contains("agent " + AGENT), refusal.getMessage());
}
}
@Test
void anAgentsRunReadsASkillTheAgentWasGiven() {
giveOnProject(SKILL, AGENT);
assertThrows(SemossPixelException.class, skill::getProjectName);
try (AgentRunOperation run = AgentUseAccess.beginAgentRun(person, null, AGENT)) {
assertDoesNotThrow(skill::getProjectName);
}
}
@Test
void aSubAgentsRunUsesTheSubAgentsGrantsAndNeverItsParents() {
give(AGENT_MODEL, AGENT);
giveOnProject(SUB_AGENT, AGENT);
give(SUB_AGENT_MODEL, SUB_AGENT);
try (AgentRunOperation run = AgentUseAccess.beginAgentRun(person, roomStartedUnder(AGENT), SUB_AGENT)) {
assertNotNull(run);
assertEquals("answer", ask(subAgentModel));
assertEquals(EngineGrantee.ofApp(SUB_AGENT), seenByTheSubAgentModel.get());
assertThrows(SemossPixelException.class, () -> ask(agentModel));
}
try (AgentRunOperation run = AgentUseAccess.beginAgentRun(person, null, AGENT)) {
assertEquals("answer", ask(agentModel));
assertThrows(SemossPixelException.class, () -> ask(subAgentModel));
}
}
@Test
void aSubAgentItsParentWasNotGivenGetsNoneOfItsAccess() {
give(SUB_AGENT_MODEL, SUB_AGENT);
try (AgentRunOperation run = AgentUseAccess.beginAgentRun(person, roomStartedUnder(AGENT), SUB_AGENT)) {
assertNull(run);
assertThrows(SemossPixelException.class, () -> ask(subAgentModel));
}
}
@Test
void aThreadTheRunStartsAsksAsThePersonOnly() throws Exception {
give(AGENT_MODEL, AGENT);
AtomicReference<Throwable> onTheThread = new AtomicReference<>();
try (AgentRunOperation run = AgentUseAccess.beginAgentRun(person, null, AGENT)) {
Thread thread = Thread.ofVirtual().start(() -> {
ThreadStore.setUser(person);
try {
ask(agentModel);
} catch (Throwable e) {
onTheThread.set(e);
} finally {
ThreadStore.remove();
}
});
thread.join();
assertEquals("answer", ask(agentModel));
}
assertInstanceOf(SemossPixelException.class, onTheThread.get());
}
@Test
void takingTheAgentsPermissionAwayStopsItsNextCall() throws Exception {
give(AGENT_MODEL, AGENT);
try (AgentRunOperation run = AgentUseAccess.beginAgentRun(person, null, AGENT)) {
assertEquals("answer", ask(agentModel));
takeAway(AGENT_MODEL, AGENT);
assertThrows(SemossPixelException.class, () -> ask(agentModel));
}
}
/**
* Asks a model the way a run does.
*
* @param model the model, behind the engine proxy
* @return its answer
*/
private static Object ask(IModelEngine model) {
return model.ask("When does order 42 ship?", null, null, new HashMap<>()).getResponse();
}
/**
* A model behind the engine proxy that answers every ask, noting the app, agent
* or engine whose permission let the ask through.
*
* @param id the model
* @param seen where the model notes who made the call
* @return the model
*/
private static IModelEngine model(String id, AtomicReference<EngineGrantee> seen) {
return StubEngines.model(id, Map.of("ask", () -> {
seen.set(EngineCallPermission.granteeMakingTheCall());
return new AskStringModelEngineResponse("answer", 0, 0);
}));
}
/**
* A sub-agent's room that records the agents it was started under, as a spawn
* records them.
*
* @param agents the agents, the first agent first
* @return the room
*/
private static Room roomStartedUnder(String... agents) {
Room room = mock(Room.class);
when(room.getOptionsMap()).thenReturn(Map.of(AgentUseAccess.ROOM_OPTION_AGENT_CHAIN, List.of(agents)));
return room;
}
/**
* Gives an agent Read-Only on a model, as the model's owner does from its Apps
* and Agents table, and takes it away after the test.
*
* @param modelId the model
* @param agentId the agent
*/
private void give(String modelId, String agentId) {
try {
SecurityEngineUtils.setEngineGranteePermission(owner, agentId, PROJECT_TYPE, modelId, "READ_ONLY", null);
} catch (IllegalAccessException e) {
throw new IllegalStateException(e);
}
engineGrants.add(new String[] { agentId, PROJECT_TYPE, modelId });
}
/**
* Takes an agent's permission on a model away during the test.
*
* @param modelId the model
* @param agentId the agent
* @throws IllegalAccessException when the owner may not take it away
*/
private void takeAway(String modelId, String agentId) throws IllegalAccessException {
SecurityEngineUtils.removeEngineGranteePermission(owner, agentId, PROJECT_TYPE, modelId);
engineGrants.removeIf(grant -> grant[0].equals(agentId) && grant[2].equals(modelId));
}
/**
* Gives an agent Read-Only on a skill or another agent, as the project's owner
* does from its Apps and Agents table, and takes it away after the test.
*
* @param projectId the skill or agent giving the permission
* @param agentId the agent given it
*/
private void giveOnProject(String projectId, String agentId) {
try {
SecurityProjectUtils.setProjectGranteePermission(owner, agentId, PROJECT_TYPE, projectId, "READ_ONLY",
null);
} catch (IllegalAccessException e) {
throw new IllegalStateException(e);
}
projectGrants.add(new String[] { agentId, PROJECT_TYPE, projectId });
}
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;
}
}