Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refreshing only for the forbidden or unauthorized error casea and add…
Browse files Browse the repository at this point in the history
…itional test cases to cover those scenarios

Signed-off-by: Santhosh Gandhe <1909520+san81@users.noreply.github.com>
san81 committed Jan 21, 2025
1 parent d01315c commit 0ba5ceb
Showing 2 changed files with 35 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -142,12 +142,16 @@ public void renewCredentials() {
} catch (HttpClientErrorException ex) {
this.expireTime = Instant.ofEpochMilli(0);
this.expiresInSeconds = 0;
// Try refreshing the secrets and see if that helps
// Refreshing one of the secret refreshes the entire store so we are good to trigger refresh on just one
jiraSourceConfig.getAuthenticationConfig().getOauth2Config().getAccessToken().refresh();
HttpStatus statusCode = ex.getStatusCode();
log.error("Failed to renew access token. Status code: {}, Error Message: {}",
ex.getRawStatusCode(), ex.getMessage());
throw new RuntimeException("Failed to renew access token" + ex.getMessage(), ex);
statusCode, ex.getMessage());
if (statusCode == HttpStatus.FORBIDDEN || statusCode == HttpStatus.UNAUTHORIZED) {
log.info("Trying to refresh the secrets");
// Try refreshing the secrets and see if that helps
// Refreshing one of the secret refreshes the entire store so we are good to trigger refresh on just one
jiraSourceConfig.getAuthenticationConfig().getOauth2Config().getAccessToken().refresh();
}
throw new RuntimeException("Failed to renew access token message:" + ex.getMessage(), ex);
}
}
}
Original file line number Diff line number Diff line change
@@ -15,8 +15,11 @@
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.opensearch.dataprepper.model.plugin.PluginConfigVariable;
import org.opensearch.dataprepper.plugins.source.jira.JiraSourceConfig;
import org.opensearch.dataprepper.plugins.source.jira.configuration.Oauth2Config;
import org.opensearch.dataprepper.plugins.source.jira.exception.UnAuthorizedException;
import org.opensearch.dataprepper.test.helper.ReflectivelySetField;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
@@ -52,6 +55,9 @@ public class JiraOauthConfigTest {

JiraSourceConfig jiraSourceConfig;

@Mock
PluginConfigVariable accessTokenVariable;

@BeforeEach
void setUp() {
jiraSourceConfig = createJiraConfigurationFromYaml("oauth2-auth-jira-pipeline.yaml");
@@ -89,12 +95,31 @@ void testRenewToken() throws InterruptedException {
}

@Test
void testFailedToRenewAccessToken() {
void testFailedToRenewAccessToken() throws NoSuchFieldException, IllegalAccessException {
JiraOauthConfig jiraOauthConfig = new JiraOauthConfig(jiraSourceConfig);
Oauth2Config oauth2Config = jiraSourceConfig.getAuthenticationConfig().getOauth2Config();
ReflectivelySetField.setField(Oauth2Config.class, oauth2Config, "accessToken", accessTokenVariable);
when(restTemplateMock.postForEntity(any(String.class), any(HttpEntity.class), any(Class.class)))
.thenThrow(HttpClientErrorException.class);
jiraOauthConfig.restTemplate = restTemplateMock;
assertThrows(RuntimeException.class, jiraOauthConfig::renewCredentials);
verify(oauth2Config.getAccessToken(), times(0))
.refresh();
}

@Test
void testFailedToRenewAccessToken_with_unauthorized_and_trigger_secrets_refresh()
throws NoSuchFieldException, IllegalAccessException {
JiraOauthConfig jiraOauthConfig = new JiraOauthConfig(jiraSourceConfig);
Oauth2Config oauth2Config = jiraSourceConfig.getAuthenticationConfig().getOauth2Config();
ReflectivelySetField.setField(Oauth2Config.class, oauth2Config, "accessToken", accessTokenVariable);
HttpClientErrorException unAuthorizedException = new HttpClientErrorException(HttpStatus.UNAUTHORIZED);
when(restTemplateMock.postForEntity(any(String.class), any(HttpEntity.class), any(Class.class)))
.thenThrow(unAuthorizedException);
jiraOauthConfig.restTemplate = restTemplateMock;
assertThrows(RuntimeException.class, jiraOauthConfig::renewCredentials);
verify(oauth2Config.getAccessToken(), times(1))
.refresh();
}


0 comments on commit 0ba5ceb

Please sign in to comment.