Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catching AccessDeniedException for Personalize calls #195

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -36,6 +38,9 @@
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;
Expand Down Expand Up @@ -96,7 +101,14 @@

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);
}
Comment on lines +106 to +108
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In order for OpenSearch to throw a 4xx error, I believe the thrown exception needs to be something that OpenSearch will recognize as a user input problem, like IllegalArgumentException.

OpenSearch doesn't know about AWS service exception classes.

throw e;

Check warning on line 109 in amazon-personalize-ranking/src/main/java/org/opensearch/search/relevance/transformer/personalizeintelligentranking/reranker/impl/AmazonPersonalizedRankerImpl.java

View check run for this annotation

Codecov / codecov/patch

amazon-personalize-ranking/src/main/java/org/opensearch/search/relevance/transformer/personalizeintelligentranking/reranker/impl/AmazonPersonalizedRankerImpl.java#L109

Added line #L109 was not covered by tests
}
catch (Exception ex) {
logger.error("Failed to re rank with Personalize.", ex);
throw ex;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -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;
}
}
Loading