From 7f70b0b7c81d3801205a0b90259a0facc0837c93 Mon Sep 17 00:00:00 2001 From: Prashant Mishra Date: Mon, 28 Aug 2023 14:00:52 -0700 Subject: [PATCH] Catching AccessDeniedException for Personalize calls --- .../impl/AmazonPersonalizedRankerImpl.java | 14 +++++++++- .../AmazonPersonalizeRankerImplTests.java | 26 +++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/amazon-personalize-ranking/src/main/java/org/opensearch/search/relevance/transformer/personalizeintelligentranking/reranker/impl/AmazonPersonalizedRankerImpl.java b/amazon-personalize-ranking/src/main/java/org/opensearch/search/relevance/transformer/personalizeintelligentranking/reranker/impl/AmazonPersonalizedRankerImpl.java index cb8cac1..44234bf 100644 --- a/amazon-personalize-ranking/src/main/java/org/opensearch/search/relevance/transformer/personalizeintelligentranking/reranker/impl/AmazonPersonalizedRankerImpl.java +++ b/amazon-personalize-ranking/src/main/java/org/opensearch/search/relevance/transformer/personalizeintelligentranking/reranker/impl/AmazonPersonalizedRankerImpl.java @@ -7,8 +7,10 @@ */ package org.opensearch.search.relevance.transformer.personalizeintelligentranking.reranker.impl; +import com.amazonaws.AmazonServiceException; import com.amazonaws.services.personalizeruntime.model.GetPersonalizedRankingRequest; import com.amazonaws.services.personalizeruntime.model.GetPersonalizedRankingResult; +import com.amazonaws.services.personalizeruntime.model.InvalidInputException; import com.amazonaws.services.personalizeruntime.model.PredictedItem; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -36,6 +38,9 @@ public class AmazonPersonalizedRankerImpl implements PersonalizedRanker { private static final Logger logger = LogManager.getLogger(AmazonPersonalizedRankerImpl.class); private final PersonalizeIntelligentRankerConfiguration rankerConfig; private final PersonalizeClient personalizeClient; + private static final String INSUFFCIENT_PUT_METRIC_PERMISSION_FORMAT = + "Insufficient privileges for calling personalize campaign. Please ensure that the supplied role is configured correctly."; + private static final String ACCESS_DENIED_EXCEPTION_ERROR_CODE = "AccessDeniedException"; public AmazonPersonalizedRankerImpl(PersonalizeIntelligentRankerConfiguration config, PersonalizeClient client) { this.rankerConfig = config; @@ -96,7 +101,14 @@ public SearchHits rerank(SearchHits hits, PersonalizeRequestParameters requestPa SearchHits personalizedHits = combineScores(hits, result); return personalizedHits; - } catch (Exception ex) { + } catch (AmazonServiceException e) { + logger.error("Exception while calling personalize campaign: {}", e.getMessage()); + if (ACCESS_DENIED_EXCEPTION_ERROR_CODE.equals(e.getErrorCode())) { + throw new InvalidInputException(INSUFFCIENT_PUT_METRIC_PERMISSION_FORMAT); + } + throw e; + } + catch (Exception ex) { logger.error("Failed to re rank with Personalize.", ex); throw ex; } diff --git a/amazon-personalize-ranking/src/test/java/org/opensearch/search/relevance/transformer/personalizeintelligentranking/ranker/impl/AmazonPersonalizeRankerImplTests.java b/amazon-personalize-ranking/src/test/java/org/opensearch/search/relevance/transformer/personalizeintelligentranking/ranker/impl/AmazonPersonalizeRankerImplTests.java index 32b4a61..ba60c43 100644 --- a/amazon-personalize-ranking/src/test/java/org/opensearch/search/relevance/transformer/personalizeintelligentranking/ranker/impl/AmazonPersonalizeRankerImplTests.java +++ b/amazon-personalize-ranking/src/test/java/org/opensearch/search/relevance/transformer/personalizeintelligentranking/ranker/impl/AmazonPersonalizeRankerImplTests.java @@ -8,6 +8,9 @@ package org.opensearch.search.relevance.transformer.personalizeintelligentranking.ranker.impl; +import com.amazonaws.AmazonServiceException; +import com.amazonaws.services.personalizeruntime.model.InvalidInputException; +import org.junit.Assert; import org.mockito.Mockito; import org.opensearch.OpenSearchParseException; import org.opensearch.search.SearchHit; @@ -39,6 +42,8 @@ public class AmazonPersonalizeRankerImplTests extends OpenSearchTestCase { private String region = "us-west-2"; private double weight = 0.25; private int numOfHits = 10; + private static final String ACCESS_DENIED_EXCEPTION_ERROR_CODE = "AccessDeniedException"; + public void testReRank() throws IOException { PersonalizeIntelligentRankerConfiguration rankerConfig = @@ -303,4 +308,25 @@ public void testReRankWithWeightAsNeitherZeroOrOneWithNullItemIdField() throws I assertNotEquals(rerankedDocumentIdsWhenWeightIsOne, rerankedDocumentIds); assertNotEquals(rerankedDocumentIdsWhenWeightIsZero, rerankedDocumentIds); } + + public void testReRankWithaccessDeniedException() throws IOException { + + PersonalizeIntelligentRankerConfiguration rankerConfig = + new PersonalizeIntelligentRankerConfiguration(personalizeCampaign, iamRoleArn, recipe, itemIdField, region, weight); + PersonalizeClient client = Mockito.mock(PersonalizeClient.class); + Mockito.when(client.getPersonalizedRanking(any())).thenThrow(buildErrorMsg(ACCESS_DENIED_EXCEPTION_ERROR_CODE)); + + PersonalizeRequestParameters requestParameters = new PersonalizeRequestParameters(); + requestParameters.setUserId("28"); + SearchHits responseHits = SearchTestUtil.getSampleSearchHitsForPersonalize(numOfHits); + AmazonPersonalizedRankerImpl ranker = new AmazonPersonalizedRankerImpl(rankerConfig, client); + Assert.assertThrows(InvalidInputException.class, () -> ranker.rerank(responseHits, requestParameters)); + } + + + private AmazonServiceException buildErrorMsg(String errorMessage) { + AmazonServiceException amazonServiceException = new AmazonServiceException("Error"); + amazonServiceException.setErrorCode(errorMessage); + return amazonServiceException; + } }