Skip to content

Commit 4463dda

Browse files
alparishAlekhya Parisha
andauthored
Move token refresh inside retry operations for Office 365 connector (#5766)
Signed-off-by: Alekhya Parisha <aparisha@amazon.com> Co-authored-by: Alekhya Parisha <aparisha@amazon.com>
1 parent 1306b47 commit 4463dda

2 files changed

Lines changed: 61 additions & 12 deletions

File tree

  • data-prepper-plugins/saas-source-plugins/microsoft-office365-source/src

data-prepper-plugins/saas-source-plugins/microsoft-office365-source/src/main/java/org/opensearch/dataprepper/plugins/source/microsoft_office365/Office365RestClient.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public void startSubscriptions() {
7575
log.info("Starting Office 365 subscriptions for audit logs");
7676
try {
7777
HttpHeaders headers = new HttpHeaders();
78-
headers.setBearerAuth(authConfig.getAccessToken());
78+
7979
headers.setContentType(MediaType.APPLICATION_JSON);
8080

8181
// TODO: Only start the subscriptions only if the call commented
@@ -103,6 +103,7 @@ public void startSubscriptions() {
103103

104104
RetryHandler.executeWithRetry(() -> {
105105
try {
106+
headers.setBearerAuth(authConfig.getAccessToken());
106107
ResponseEntity<String> response = restTemplate.exchange(
107108
url,
108109
HttpMethod.POST,
@@ -155,12 +156,13 @@ public AuditLogsResponse searchAuditLogs(final String contentType,
155156
endTime.toString());
156157

157158
final HttpHeaders headers = new HttpHeaders();
158-
headers.setBearerAuth(authConfig.getAccessToken());
159159

160160
return searchCallLatencyTimer.record(() -> {
161161
try {
162162
return RetryHandler.executeWithRetry(
163163
() -> {
164+
headers.setBearerAuth(authConfig.getAccessToken());
165+
164166
ResponseEntity<List<Map<String, Object>>> response = restTemplate.exchange(
165167
url,
166168
HttpMethod.GET,
@@ -205,19 +207,19 @@ public String getAuditLog(final String contentId) {
205207
contentId);
206208

207209
final HttpHeaders headers = new HttpHeaders();
208-
headers.setBearerAuth(authConfig.getAccessToken());
209210

210211
return auditLogFetchLatencyTimer.record(() -> {
211212
try {
212-
String response = RetryHandler.executeWithRetry(() ->
213-
restTemplate.exchange(
214-
url,
215-
HttpMethod.GET,
216-
new HttpEntity<>(headers),
217-
String.class
218-
).getBody(),
219-
authConfig::renewCredentials
220-
);
213+
String response = RetryHandler.executeWithRetry(() -> {
214+
headers.setBearerAuth(authConfig.getAccessToken());
215+
216+
return restTemplate.exchange(
217+
url,
218+
HttpMethod.GET,
219+
new HttpEntity<>(headers),
220+
String.class
221+
).getBody();
222+
}, authConfig::renewCredentials);
221223
auditLogRequestsSuccessCounter.increment();
222224
return response;
223225
} catch (Exception e) {

data-prepper-plugins/saas-source-plugins/microsoft-office365-source/src/test/java/org/opensearch/dataprepper/plugins/source/microsoft_office365/Office365RestClientTest.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import java.util.HashMap;
3535
import java.util.List;
3636
import java.util.Map;
37+
import java.util.ArrayList;
3738

3839
import static org.junit.jupiter.api.Assertions.assertEquals;
3940
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
@@ -42,6 +43,7 @@
4243
import static org.mockito.ArgumentMatchers.any;
4344
import static org.mockito.ArgumentMatchers.anyString;
4445
import static org.mockito.ArgumentMatchers.eq;
46+
import static org.mockito.ArgumentMatchers.argThat;
4547
import static org.mockito.Mockito.times;
4648
import static org.mockito.Mockito.verify;
4749
import static org.mockito.Mockito.when;
@@ -242,4 +244,49 @@ void testGetAuditLogFailure() {
242244
() -> office365RestClient.getAuditLog("test-content-id"));
243245
assertEquals("Failed to fetch audit log", exception.getMessage());
244246
}
247+
248+
@Test
249+
void testTokenRenewal() {
250+
// Setup
251+
Instant startTime = Instant.now().minus(1, ChronoUnit.HOURS);
252+
Instant endTime = Instant.now();
253+
254+
List<String> tokensUsed = new ArrayList<>();
255+
List<String> requestTokens = new ArrayList<>();
256+
257+
when(authConfig.getTenantId()).thenReturn("test-tenant-id");
258+
when(authConfig.getAccessToken()).thenAnswer(invocation -> {
259+
String token = "token-" + tokensUsed.size();
260+
tokensUsed.add(token);
261+
return token;
262+
});
263+
264+
when(restTemplate.exchange(
265+
anyString(),
266+
eq(HttpMethod.GET),
267+
argThat(request -> {
268+
requestTokens.add(request.getHeaders().getFirst("Authorization"));
269+
return true;
270+
}),
271+
any(ParameterizedTypeReference.class)
272+
)).thenAnswer(invocation -> {
273+
if (requestTokens.size() == 1) {
274+
throw new HttpClientErrorException(HttpStatus.UNAUTHORIZED); // First request fails with 401
275+
}
276+
return new ResponseEntity<>(Collections.singletonList(new HashMap<>()), HttpStatus.OK); // Second request succeeds
277+
});
278+
279+
// Execute
280+
office365RestClient.searchAuditLogs(
281+
"Audit.AzureActiveDirectory",
282+
startTime,
283+
endTime,
284+
null
285+
);
286+
287+
// Verify
288+
assertEquals(2, requestTokens.size(), "Should have made two requests");
289+
assertEquals("Bearer token-0", requestTokens.get(0), "First request should use token-0");
290+
assertEquals("Bearer token-1", requestTokens.get(1), "Second request should use token-1");
291+
}
245292
}

0 commit comments

Comments
 (0)