Skip to content

Commit 06cfe28

Browse files
committed
update Unthrotized error code to 401
Signed-off-by: Xun Zhang <xunzh@amazon.com>
1 parent 8ae85a3 commit 06cfe28

File tree

4 files changed

+60
-24
lines changed

4 files changed

+60
-24
lines changed

memory/src/main/java/org/opensearch/ml/memory/index/ConversationMetaIndex.java

+13-4
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import java.util.List;
2626
import java.util.Map;
2727

28-
import org.opensearch.OpenSearchSecurityException;
28+
import org.opensearch.OpenSearchStatusException;
2929
import org.opensearch.OpenSearchWrapperException;
3030
import org.opensearch.ResourceAlreadyExistsException;
3131
import org.opensearch.ResourceNotFoundException;
@@ -279,7 +279,10 @@ public void deleteConversation(String conversationId, ActionListener<Boolean> li
279279
listener.onFailure(e);
280280
}
281281
} else {
282-
throw new OpenSearchSecurityException("User [" + user + "] does not have access to conversation " + conversationId);
282+
throw new OpenSearchStatusException(
283+
"User [" + user + "] does not have access to conversation " + conversationId,
284+
RestStatus.UNAUTHORIZED
285+
);
283286
}
284287
}, e -> { listener.onFailure(e); }));
285288
}
@@ -383,7 +386,10 @@ public void updateConversation(String conversationId, UpdateRequest updateReques
383386
.getThreadContext()
384387
.getTransient(ConfigConstants.OPENSEARCH_SECURITY_USER_INFO_THREAD_CONTEXT);
385388
String user = User.parse(userstr) == null ? ActionConstants.DEFAULT_USERNAME_FOR_ERRORS : User.parse(userstr).getName();
386-
throw new OpenSearchSecurityException("User [" + user + "] does not have access to conversation " + conversationId);
389+
throw new OpenSearchStatusException(
390+
"User [" + user + "] does not have access to conversation " + conversationId,
391+
RestStatus.UNAUTHORIZED
392+
);
387393
}
388394
}, e -> { listener.onFailure(e); }));
389395
}
@@ -435,7 +441,10 @@ public void getConversation(String conversationId, ActionListener<ConversationMe
435441
// Otherwise you don't have permission
436442
internalListener
437443
.onFailure(
438-
new OpenSearchSecurityException("User [" + user + "] does not have access to conversation " + conversationId)
444+
new OpenSearchStatusException(
445+
"User [" + user + "] does not have access to conversation " + conversationId,
446+
RestStatus.UNAUTHORIZED
447+
)
439448
);
440449
}, e -> { internalListener.onFailure(e); });
441450
client.admin().indices().refresh(Requests.refreshRequest(META_INDEX_NAME), ActionListener.wrap(refreshResponse -> {

memory/src/main/java/org/opensearch/ml/memory/index/InteractionsIndex.java

+29-8
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import java.util.List;
2727
import java.util.Map;
2828

29-
import org.opensearch.OpenSearchSecurityException;
29+
import org.opensearch.OpenSearchStatusException;
3030
import org.opensearch.OpenSearchWrapperException;
3131
import org.opensearch.ResourceAlreadyExistsException;
3232
import org.opensearch.ResourceNotFoundException;
@@ -195,7 +195,10 @@ public void createInteraction(
195195
listener.onFailure(e);
196196
}
197197
} else {
198-
throw new OpenSearchSecurityException("User [" + user + "] does not have access to conversation " + conversationId);
198+
throw new OpenSearchStatusException(
199+
"User [" + user + "] does not have access to conversation " + conversationId,
200+
RestStatus.UNAUTHORIZED
201+
);
199202
}
200203
}, e -> { listener.onFailure(e); }));
201204
} else {
@@ -271,7 +274,10 @@ public void getInteractions(String conversationId, int from, int maxResults, Act
271274
.getThreadContext()
272275
.getTransient(ConfigConstants.OPENSEARCH_SECURITY_USER_INFO_THREAD_CONTEXT);
273276
String user = User.parse(userstr) == null ? ActionConstants.DEFAULT_USERNAME_FOR_ERRORS : User.parse(userstr).getName();
274-
throw new OpenSearchSecurityException("User [" + user + "] does not have access to conversation " + conversationId);
277+
throw new OpenSearchStatusException(
278+
"User [" + user + "] does not have access to conversation " + conversationId,
279+
RestStatus.UNAUTHORIZED
280+
);
275281
}
276282
}, e -> { listener.onFailure(e); });
277283
conversationMetaIndex.checkAccess(conversationId, accessListener);
@@ -355,7 +361,10 @@ public void getTraces(String interactionId, int from, int maxResults, ActionList
355361
String user = User.parse(userstr) == null
356362
? ActionConstants.DEFAULT_USERNAME_FOR_ERRORS
357363
: User.parse(userstr).getName();
358-
throw new OpenSearchSecurityException("User [" + user + "] does not have access to interaction " + interactionId);
364+
throw new OpenSearchStatusException(
365+
"User [" + user + "] does not have access to interaction " + interactionId,
366+
RestStatus.UNAUTHORIZED
367+
);
359368
}
360369
}, e -> { listener.onFailure(e); });
361370
conversationMetaIndex.checkAccess(conversationId, accessListener);
@@ -485,7 +494,10 @@ public void deleteConversation(String conversationId, ActionListener<Boolean> li
485494
if (access) {
486495
getAllInteractions(conversationId, resultsAtATime, searchListener);
487496
} else {
488-
throw new OpenSearchSecurityException("User [" + user + "] does not have access to conversation " + conversationId);
497+
throw new OpenSearchStatusException(
498+
"User [" + user + "] does not have access to conversation " + conversationId,
499+
RestStatus.UNAUTHORIZED
500+
);
489501
}
490502
}, e -> { listener.onFailure(e); });
491503
conversationMetaIndex.checkAccess(conversationId, accessListener);
@@ -531,7 +543,10 @@ public void searchInteractions(String conversationId, SearchRequest request, Act
531543
.getThreadContext()
532544
.getTransient(ConfigConstants.OPENSEARCH_SECURITY_USER_INFO_THREAD_CONTEXT);
533545
String user = User.parse(userstr) == null ? ActionConstants.DEFAULT_USERNAME_FOR_ERRORS : User.parse(userstr).getName();
534-
throw new OpenSearchSecurityException("User [" + user + "] does not have access to conversation " + conversationId);
546+
throw new OpenSearchStatusException(
547+
"User [" + user + "] does not have access to conversation " + conversationId,
548+
RestStatus.UNAUTHORIZED
549+
);
535550
}
536551
}, e -> { listener.onFailure(e); }));
537552
}
@@ -615,7 +630,10 @@ public void updateInteraction(String interactionId, UpdateRequest updateRequest,
615630
String user = User.parse(userstr) == null
616631
? ActionConstants.DEFAULT_USERNAME_FOR_ERRORS
617632
: User.parse(userstr).getName();
618-
throw new OpenSearchSecurityException("User [" + user + "] does not have access to interaction " + interactionId);
633+
throw new OpenSearchStatusException(
634+
"User [" + user + "] does not have access to interaction " + interactionId,
635+
RestStatus.UNAUTHORIZED
636+
);
619637
}
620638
}, e -> { listener.onFailure(e); });
621639
conversationMetaIndex.checkAccess(conversationId, accessListener);
@@ -652,7 +670,10 @@ private void checkInteractionPermission(String interactionId, Interaction intera
652670
.getThreadContext()
653671
.getTransient(ConfigConstants.OPENSEARCH_SECURITY_USER_INFO_THREAD_CONTEXT);
654672
String user = User.parse(userstr) == null ? ActionConstants.DEFAULT_USERNAME_FOR_ERRORS : User.parse(userstr).getName();
655-
throw new OpenSearchSecurityException("User [" + user + "] does not have access to interaction " + interactionId);
673+
throw new OpenSearchStatusException(
674+
"User [" + user + "] does not have access to interaction " + interactionId,
675+
RestStatus.UNAUTHORIZED
676+
);
656677
}
657678
}, e -> { internalListener.onFailure(e); });
658679
conversationMetaIndex.checkAccess(conversationId, accessListener);

memory/src/test/java/org/opensearch/ml/memory/ConversationalMemoryHandlerITTests.java

+12-8
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import java.util.function.Consumer;
2525

2626
import org.junit.Before;
27-
import org.opensearch.OpenSearchSecurityException;
27+
import org.opensearch.OpenSearchStatusException;
2828
import org.opensearch.action.LatchedActionListener;
2929
import org.opensearch.action.StepListener;
3030
import org.opensearch.client.Client;
@@ -34,6 +34,7 @@
3434
import org.opensearch.common.util.concurrent.ThreadContext.StoredContext;
3535
import org.opensearch.commons.ConfigConstants;
3636
import org.opensearch.core.action.ActionListener;
37+
import org.opensearch.core.rest.RestStatus;
3738
import org.opensearch.ml.common.conversation.ConversationMeta;
3839
import org.opensearch.ml.common.conversation.Interaction;
3940
import org.opensearch.ml.memory.index.OpenSearchConversationalMemoryHandler;
@@ -350,7 +351,10 @@ public void testDifferentUsers_DifferentConversations() {
350351
while (!contextStack.empty()) {
351352
contextStack.pop().close();
352353
}
353-
OpenSearchSecurityException e = new OpenSearchSecurityException("User was given inappropriate access controls");
354+
OpenSearchStatusException e = new OpenSearchStatusException(
355+
"User was given inappropriate access controls",
356+
RestStatus.UNAUTHORIZED
357+
);
354358
log.error(e);
355359
throw e;
356360
};
@@ -470,7 +474,7 @@ public void testDifferentUsers_DifferentConversations() {
470474
}, onFail);
471475

472476
failiid1.whenComplete(shouldHaveFailedAsString, e -> {
473-
if (e instanceof OpenSearchSecurityException
477+
if (e instanceof OpenSearchStatusException
474478
&& e.getMessage().startsWith("User [" + user2 + "] does not have access to conversation ")) {
475479
cmHandler
476480
.createInteraction(
@@ -488,7 +492,7 @@ public void testDifferentUsers_DifferentConversations() {
488492
});
489493

490494
failiid2.whenComplete(shouldHaveFailedAsString, e -> {
491-
if (e instanceof OpenSearchSecurityException
495+
if (e instanceof OpenSearchStatusException
492496
&& e.getMessage().startsWith("User [" + user2 + "] does not have access to conversation ")) {
493497
cmHandler.getConversations(10, conversations2);
494498
} else {
@@ -510,7 +514,7 @@ public void testDifferentUsers_DifferentConversations() {
510514
}, onFail);
511515

512516
failInter2.whenComplete(shouldHaveFailedAsInterList, e -> {
513-
if (e instanceof OpenSearchSecurityException
517+
if (e instanceof OpenSearchStatusException
514518
&& e.getMessage().startsWith("User [" + user2 + "] does not have access to conversation ")) {
515519
cmHandler.getInteractions(cid1.result(), 0, 10, failInter1);
516520
} else {
@@ -519,7 +523,7 @@ public void testDifferentUsers_DifferentConversations() {
519523
});
520524

521525
failInter1.whenComplete(shouldHaveFailedAsInterList, e -> {
522-
if (e instanceof OpenSearchSecurityException
526+
if (e instanceof OpenSearchStatusException
523527
&& e.getMessage().startsWith("User [" + user2 + "] does not have access to conversation ")) {
524528
contextStack.pop().restore();
525529
cmHandler.getConversations(0, 10, conversations1);
@@ -549,7 +553,7 @@ public void testDifferentUsers_DifferentConversations() {
549553
}, onFail);
550554

551555
failInter3.whenComplete(shouldHaveFailedAsInterList, e -> {
552-
if (e instanceof OpenSearchSecurityException
556+
if (e instanceof OpenSearchStatusException
553557
&& e.getMessage().startsWith("User [" + user1 + "] does not have access to conversation ")) {
554558
cmHandler
555559
.createInteraction(
@@ -567,7 +571,7 @@ public void testDifferentUsers_DifferentConversations() {
567571
});
568572

569573
failiid3.whenComplete(shouldHaveFailedAsString, e -> {
570-
if (e instanceof OpenSearchSecurityException
574+
if (e instanceof OpenSearchStatusException
571575
&& e.getMessage().startsWith("User [" + user1 + "] does not have access to conversation ")) {
572576
contextStack.pop().restore();
573577
cdl.countDown();

memory/src/test/java/org/opensearch/ml/memory/index/ConversationMetaIndexITTests.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
import org.junit.Before;
2929
import org.junit.Ignore;
30-
import org.opensearch.OpenSearchSecurityException;
30+
import org.opensearch.OpenSearchStatusException;
3131
import org.opensearch.action.LatchedActionListener;
3232
import org.opensearch.action.StepListener;
3333
import org.opensearch.action.search.SearchRequest;
@@ -39,6 +39,7 @@
3939
import org.opensearch.common.util.concurrent.ThreadContext.StoredContext;
4040
import org.opensearch.commons.ConfigConstants;
4141
import org.opensearch.core.action.ActionListener;
42+
import org.opensearch.core.rest.RestStatus;
4243
import org.opensearch.index.query.QueryBuilders;
4344
import org.opensearch.ml.common.conversation.ConversationMeta;
4445
import org.opensearch.ml.common.conversation.ConversationalIndexConstants;
@@ -388,8 +389,9 @@ public void testDifferentUsersCannotTouchOthersConversations() {
388389
}, onFail);
389390

390391
delListener.whenComplete(success -> {
391-
Exception e = new OpenSearchSecurityException(
392-
"Incorrect access was given to user [" + user2 + "] for conversation " + cid1.result()
392+
Exception e = new OpenSearchStatusException(
393+
"Incorrect access was given to user [" + user2 + "] for conversation " + cid1.result(),
394+
RestStatus.UNAUTHORIZED
393395
);
394396
while (!contextStack.empty()) {
395397
contextStack.pop().close();
@@ -398,7 +400,7 @@ public void testDifferentUsersCannotTouchOthersConversations() {
398400
log.error(e);
399401
assert (false);
400402
}, e -> {
401-
if (e instanceof OpenSearchSecurityException
403+
if (e instanceof OpenSearchStatusException
402404
&& e.getMessage().startsWith("User [" + user2 + "] does not have access to conversation ")) {
403405
contextStack.pop().restore();
404406
contextStack.pop().restore();

0 commit comments

Comments
 (0)