Skip to content

Commit 73b45a0

Browse files
Replace unsafe String casts with instanceof check in AtlassianOauthConfig
Signed-off-by: Srikanth Padakanti <srikanth_padakanti@apple.com>
1 parent b85efd3 commit 73b45a0

2 files changed

Lines changed: 74 additions & 6 deletions

File tree

data-prepper-plugins/saas-source-plugins/atlassian-commons/src/main/java/org/opensearch/dataprepper/plugins/source/atlassian/rest/auth/AtlassianOauthConfig.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ public class AtlassianOauthConfig implements AtlassianAuthConfig {
6666

6767
public AtlassianOauthConfig(AtlassianSourceConfig atlassianSourceConfig) {
6868
this.atlassianSourceConfig = atlassianSourceConfig;
69-
this.accessToken = (String) atlassianSourceConfig.getAuthenticationConfig().getOauth2Config()
70-
.getAccessToken().getValue();
71-
this.refreshToken = (String) atlassianSourceConfig.getAuthenticationConfig()
72-
.getOauth2Config().getRefreshToken().getValue();
69+
this.accessToken = resolveStringValue(atlassianSourceConfig.getAuthenticationConfig().getOauth2Config()
70+
.getAccessToken().getValue());
71+
this.refreshToken = resolveStringValue(atlassianSourceConfig.getAuthenticationConfig()
72+
.getOauth2Config().getRefreshToken().getValue());
7373
this.clientId = atlassianSourceConfig.getAuthenticationConfig().getOauth2Config().getClientId();
7474
this.clientSecret = atlassianSourceConfig.getAuthenticationConfig().getOauth2Config().getClientSecret();
7575
}
@@ -150,8 +150,8 @@ public void renewCredentials() {
150150
// Refreshing the secrets. It should help if someone already renewed the tokens.
151151
// Refreshing one of the secret refreshes the entire store so triggering refresh on just one
152152
oauth2Config.getAccessToken().refresh();
153-
this.accessToken = (String) oauth2Config.getAccessToken().getValue();
154-
this.refreshToken = (String) oauth2Config.getRefreshToken().getValue();
153+
this.accessToken = resolveStringValue(oauth2Config.getAccessToken().getValue());
154+
this.refreshToken = resolveStringValue(oauth2Config.getRefreshToken().getValue());
155155
this.expireTime = Instant.now().plusSeconds(10);
156156
}
157157
throw new RuntimeException("Failed to renew access token message:" + ex.getMessage(), ex);
@@ -180,4 +180,11 @@ public void initCredentials() {
180180
this.cloudId = getAtlassianAccountCloudId();
181181
this.url = OAuth2_URL + atlassianSourceConfig.getOauth2UrlContext() + SLASH + this.cloudId + SLASH;
182182
}
183+
184+
private String resolveStringValue(final Object value) {
185+
if (value instanceof String) {
186+
return (String) value;
187+
}
188+
return value != null ? value.toString() : null;
189+
}
183190
}

data-prepper-plugins/saas-source-plugins/atlassian-commons/src/test/java/org/opensearch/dataprepper/plugins/source/atlassian/rest/auth/AtlassianOauthConfigTest.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,4 +188,65 @@ void testFailedToGetCloudId() {
188188
}
189189
}
190190

191+
@Test
192+
void testConstructor_handles_non_string_getValue() throws NoSuchFieldException, IllegalAccessException {
193+
Oauth2Config oauth2Config = confluenceSourceConfig.getAuthenticationConfig().getOauth2Config();
194+
PluginConfigVariable nonStringAccessToken = new PluginConfigVariable() {
195+
@Override
196+
public Object getValue() { return 12345; }
197+
@Override
198+
public void setValue(Object updatedValue) {}
199+
@Override
200+
public void refresh() {}
201+
@Override
202+
public boolean isUpdatable() { return false; }
203+
};
204+
PluginConfigVariable nonStringRefreshToken = new PluginConfigVariable() {
205+
@Override
206+
public Object getValue() { return 67890; }
207+
@Override
208+
public void setValue(Object updatedValue) {}
209+
@Override
210+
public void refresh() {}
211+
@Override
212+
public boolean isUpdatable() { return false; }
213+
};
214+
ReflectivelySetField.setField(Oauth2Config.class, oauth2Config, "accessToken", nonStringAccessToken);
215+
ReflectivelySetField.setField(Oauth2Config.class, oauth2Config, "refreshToken", nonStringRefreshToken);
216+
217+
AtlassianOauthConfig jiraOauthConfig = new AtlassianOauthConfig(confluenceSourceConfig);
218+
219+
assertEquals("12345", jiraOauthConfig.getAccessToken());
220+
assertEquals("67890", jiraOauthConfig.getRefreshToken());
221+
}
222+
223+
@Test
224+
void testRenewCredentials_handles_non_string_getValue_after_refresh()
225+
throws NoSuchFieldException, IllegalAccessException {
226+
AtlassianOauthConfig jiraOauthConfig = new AtlassianOauthConfig(confluenceSourceConfig);
227+
Oauth2Config oauth2Config = confluenceSourceConfig.getAuthenticationConfig().getOauth2Config();
228+
229+
PluginConfigVariable nonStringAccessToken = new PluginConfigVariable() {
230+
@Override
231+
public Object getValue() { return 99999; }
232+
@Override
233+
public void setValue(Object updatedValue) {}
234+
@Override
235+
public void refresh() {}
236+
@Override
237+
public boolean isUpdatable() { return true; }
238+
};
239+
ReflectivelySetField.setField(Oauth2Config.class, oauth2Config, "accessToken", nonStringAccessToken);
240+
ReflectivelySetField.setField(Oauth2Config.class, oauth2Config, "refreshToken", accessTokenVariable);
241+
when(accessTokenVariable.getValue()).thenReturn("refreshed_token");
242+
243+
HttpClientErrorException unauthorizedException = new HttpClientErrorException(HttpStatus.UNAUTHORIZED);
244+
when(restTemplateMock.postForEntity(any(String.class), any(HttpEntity.class), any(Class.class)))
245+
.thenThrow(unauthorizedException);
246+
jiraOauthConfig.restTemplate = restTemplateMock;
247+
248+
assertThrows(RuntimeException.class, jiraOauthConfig::renewCredentials);
249+
assertEquals("99999", jiraOauthConfig.getAccessToken());
250+
}
251+
191252
}

0 commit comments

Comments
 (0)