Skip to content

Commit d5e60a5

Browse files
anannya03g2vinay
andauthored
Loosen error message matching for IMDS 403 responsesx (#46127)
* For Status 403, modified the retry condition * Changed retry message and modified testcase to ParameterizedTest. * Ran mvn spotless:apply --------- Co-authored-by: Vinay Gera <vigera@microsoft.com>
1 parent 669c58c commit d5e60a5

File tree

2 files changed

+31
-22
lines changed

2 files changed

+31
-22
lines changed

sdk/identity/azure-identity/src/main/java/com/azure/identity/implementation/ImdsRetryStrategy.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,11 @@ public boolean shouldRetry(HttpResponse httpResponse) {
5858
return false;
5959
}
6060
if (statusCode == 403) {
61-
return httpResponse.getHeaderValue("ResponseMessage")
62-
.contains("A socket operation was attempted to an unreachable network");
61+
String headerValue = httpResponse.getHeaderValue("ResponseMessage");
62+
if (headerValue != null) {
63+
return headerValue.contains("A socket operation was attempted to an unreachable");
64+
}
65+
return false;
6366
}
6467
if (statusCode == 410
6568
|| statusCode == 429

sdk/identity/azure-identity/src/test/java/com/azure/identity/implementation/ImdsRetryStrategyTest.java

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33

44
package com.azure.identity.implementation;
55

6-
import com.azure.core.http.HttpResponse;
76
import com.azure.core.test.http.MockHttpResponse;
87
import org.junit.jupiter.api.Assertions;
98
import org.junit.jupiter.api.Test;
9+
import org.junit.jupiter.params.ParameterizedTest;
10+
import org.junit.jupiter.params.provider.Arguments;
11+
import org.junit.jupiter.params.provider.MethodSource;
1012

1113
import java.io.IOException;
14+
import java.util.stream.Stream;
1215
import java.util.Arrays;
1316
import java.util.LinkedList;
1417
import java.util.Queue;
@@ -34,29 +37,32 @@ public void testIMDSRetry() {
3437
}
3538
}
3639

37-
@Test
38-
public void testShouldRetry() {
40+
@ParameterizedTest
41+
@MethodSource("shouldRetryInDifferentScenarios")
42+
public void testShouldRetry(String headerValue, int statusCode, boolean expectedRetry, String description) {
3943
ImdsRetryStrategy imdsRetryStrategy = new ImdsRetryStrategy();
4044

41-
HttpResponse httpResponse = new MockHttpResponse(null, 400);
42-
43-
Assertions.assertFalse(imdsRetryStrategy.shouldRetry(httpResponse),
44-
"Imds Retry Strategy should not retry on 400 status response.");
45-
46-
Assertions.assertTrue(imdsRetryStrategy.shouldRetry(new MockHttpResponse(null, 410)),
47-
"Imds Retry Strategy should retry on 410 status response.");
48-
49-
Assertions.assertTrue(imdsRetryStrategy.shouldRetry(new MockHttpResponse(null, 429)),
50-
"Imds Retry Strategy should retry on 429 status response.");
51-
52-
Assertions.assertTrue(imdsRetryStrategy.shouldRetry(new MockHttpResponse(null, 500)),
53-
"Imds Retry Strategy should retry on 429 status response.");
45+
MockHttpResponse httpResponse = new MockHttpResponse(null, statusCode);
46+
if (headerValue != null) {
47+
httpResponse.addHeader("ResponseMessage", headerValue);
48+
}
5449

55-
Assertions.assertTrue(imdsRetryStrategy.shouldRetry(new MockHttpResponse(null, 599)),
56-
"Imds Retry Strategy should retry on 429 status response.");
50+
Assertions.assertEquals(expectedRetry, imdsRetryStrategy.shouldRetry(httpResponse), description);
51+
}
5752

58-
Assertions.assertTrue(imdsRetryStrategy.shouldRetry(new MockHttpResponse(null, 404)),
59-
"Imds Retry Strategy should retry on 429 status response.");
53+
private static Stream<Arguments> shouldRetryInDifferentScenarios() {
54+
return Stream.of(Arguments.of(null, 400, false, "Imds Retry Strategy should not retry on 400 status response"),
55+
Arguments.of(null, 410, true, "Imds Retry Strategy should retry on 410 status response"),
56+
Arguments.of(null, 429, true, "Imds Retry Strategy should retry on 429 status response"),
57+
Arguments.of(null, 500, true, "Imds Retry Strategy should retry on 500 status reponse"),
58+
Arguments.of(null, 599, true, "Imds Retry Strategy should retry on 599 status response"),
59+
Arguments.of(null, 404, true, "Imds Retry Strategy should retry on 404 status response"),
60+
Arguments.of("A socket operation was attempted to an unreachable", 403, true,
61+
"Imds Retry Strategy should retry on 403 with unreachable message"),
62+
Arguments.of("Access denied", 403, false,
63+
"Imds Retry Strategy should not retry on 403 with Access Denied message"),
64+
Arguments.of(null, 403, false,
65+
"Imds Retry Strategy should not retry on 403 with no ResponseMessage header"));
6066
}
6167

6268
@Test

0 commit comments

Comments
 (0)