Skip to content
This repository was archived by the owner on Mar 25, 2018. It is now read-only.

Commit f03ab58

Browse files
author
Adrian Cole
committed
Merge pull request #1555 from jclouds/issue-1492-1.6.x
fix issue #1492: invalidate dynect session on ip mismatch (1.6.x)
2 parents 00b9fbe + 874d835 commit f03ab58

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

providers/dynect/src/main/java/org/jclouds/dynect/v3/filters/SessionManager.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,15 @@ public HttpRequest filter(HttpRequest request) throws HttpException {
101101
return builder.build();
102102
}
103103

104+
private static final String IP_MISMATCH = "IP address does not match current session";
105+
104106
@Override
105107
public boolean shouldRetryRequest(HttpCommand command, HttpResponse response) {
106108
boolean retry = false; // default
107109
try {
108-
if (response.getStatusCode() == 401) {
109-
closeClientButKeepContentStream(response);
110+
byte[] data = closeClientButKeepContentStream(response);
111+
String message = data != null ? new String(data) : null;
112+
if (response.getStatusCode() == 401 || (message != null && message.indexOf(IP_MISMATCH) != -1)) {
110113
logger.debug("invalidating session");
111114
sessionCache.invalidateAll();
112115
retry = super.shouldRetryRequest(command, response);

providers/dynect/src/test/java/org/jclouds/dynect/v3/filters/SessionManagerTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
*/
1919
package org.jclouds.dynect.v3.filters;
2020

21+
import static com.google.common.io.Resources.getResource;
22+
import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
2123
import static javax.ws.rs.core.Response.Status.FORBIDDEN;
2224
import static javax.ws.rs.core.Response.Status.UNAUTHORIZED;
2325
import static org.easymock.EasyMock.createMock;
@@ -30,6 +32,8 @@
3032
import static org.testng.Assert.assertSame;
3133
import static org.testng.Assert.assertTrue;
3234

35+
import java.io.IOException;
36+
3337
import org.jclouds.domain.Credentials;
3438
import org.jclouds.dynect.v3.domain.Session;
3539
import org.jclouds.dynect.v3.domain.SessionCredentials;
@@ -95,6 +99,34 @@ public void testUnauthorizedShouldInvalidateSessionAndRetry() {
9599
verify(creds, sessionCache, sessionApi, command);
96100
}
97101

102+
@SuppressWarnings("unchecked")
103+
@Test
104+
public void testIPMismatchShouldInvalidateSessionAndRetry() throws IOException {
105+
HttpCommand command = createMock(HttpCommand.class);
106+
Supplier<Credentials> creds = createMock(Supplier.class);
107+
LoadingCache<Credentials, Session> sessionCache = createMock(LoadingCache.class);
108+
SessionApi sessionApi = createMock(SessionApi.class);
109+
110+
sessionCache.invalidateAll();
111+
expectLastCall();
112+
expect(command.incrementFailureCount()).andReturn(1);
113+
expect(command.isReplayable()).andReturn(true);
114+
expect(command.getFailureCount()).andReturn(1).atLeastOnce();
115+
116+
replay(creds, sessionCache, sessionApi, command);
117+
118+
HttpResponse response = HttpResponse.builder()
119+
.statusCode(BAD_REQUEST.getStatusCode())
120+
.payload(getResource("ip_mismatch.json").openStream())
121+
.build();
122+
123+
SessionManager retry = new SessionManager(creds, sessionCache, sessionApi);
124+
125+
assertTrue(retry.shouldRetryRequest(command, response));
126+
127+
verify(creds, sessionCache, sessionApi, command);
128+
}
129+
98130
@SuppressWarnings("unchecked")
99131
@Test
100132
public void testForbiddenShouldNotInvalidateSessionOrRetry() {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"status": "failure",
3+
"data": {},
4+
"job_id": 305900967,
5+
"msgs": [{
6+
"INFO": "login: IP address does not match current session",
7+
"SOURCE": "BLL",
8+
"ERR_CD": "INVALID_DATA",
9+
"LVL": "ERROR"
10+
}, {
11+
"INFO": "login: There was a problem with your credentials",
12+
"SOURCE": "BLL",
13+
"ERR_CD": null,
14+
"LVL": "INFO"
15+
}
16+
]
17+
}

0 commit comments

Comments
 (0)