Skip to content

Commit

Permalink
making it easy to read
Browse files Browse the repository at this point in the history
Signed-off-by: Santhosh Gandhe <[email protected]>
  • Loading branch information
san81 committed Jan 22, 2025
1 parent 2589f1f commit d21bd0e
Showing 1 changed file with 9 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import lombok.Getter;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -126,6 +127,7 @@ public void renewCredentials() {
String payload = String.format(payloadTemplate, "refresh_token", clientId, clientSecret, refreshToken);
HttpEntity<String> entity = new HttpEntity<>(payload, headers);

Oauth2Config oauth2Config = jiraSourceConfig.getAuthenticationConfig().getOauth2Config();
try {
ResponseEntity<Map> responseEntity = restTemplate.postForEntity(TOKEN_LOCATION, entity, Map.class);
Map<String, Object> oauthClientResponse = responseEntity.getBody();
Expand All @@ -134,10 +136,8 @@ public void renewCredentials() {
this.expiresInSeconds = (int) oauthClientResponse.get(EXPIRES_IN);
this.expireTime = Instant.ofEpochMilli(System.currentTimeMillis() + (expiresInSeconds * 1000L));
// updating config object's PluginConfigVariable so that it updates the underlying Secret store
jiraSourceConfig.getAuthenticationConfig().getOauth2Config().getAccessToken()
.setValue(this.accessToken);
jiraSourceConfig.getAuthenticationConfig().getOauth2Config().getRefreshToken()
.setValue(this.refreshToken);
oauth2Config.getAccessToken().setValue(this.accessToken);
oauth2Config.getRefreshToken().setValue(this.refreshToken);
log.info("Access Token and Refresh Token pair is now refreshed. Corresponding Secret store key updated.");
} catch (HttpClientErrorException ex) {
this.expireTime = Instant.ofEpochMilli(0);
Expand All @@ -147,13 +147,11 @@ public void renewCredentials() {
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();
this.accessToken = (String) jiraSourceConfig.getAuthenticationConfig().getOauth2Config()
.getAccessToken().getValue();
this.refreshToken = (String) jiraSourceConfig.getAuthenticationConfig()
.getOauth2Config().getRefreshToken().getValue();
// Refreshing the secrets. It should help if someone already renewed the tokens.
// Refreshing one of the secret refreshes the entire store so triggering refresh on just one
oauth2Config.getAccessToken().refresh();
this.accessToken = (String) oauth2Config.getAccessToken().getValue();
this.refreshToken = (String) oauth2Config.getRefreshToken().getValue();
this.expireTime = Instant.ofEpochMilli(System.currentTimeMillis() + (expiresInSeconds * 100L));
}
throw new RuntimeException("Failed to renew access token message:" + ex.getMessage(), ex);
Expand Down

0 comments on commit d21bd0e

Please sign in to comment.