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

Commit

Permalink
Merge pull request #1554 from jclouds/issue-1492
Browse files Browse the repository at this point in the history
fix issue #1492: invalidate dynect session on ip mismatch
  • Loading branch information
Adrian Cole committed Apr 22, 2013
2 parents 8d1535a + 6eaf083 commit 630d4cf
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,15 @@ public HttpRequest filter(HttpRequest request) throws HttpException {
return builder.build();
}

private static final String IP_MISMATCH = "IP address does not match current session";

@Override
public boolean shouldRetryRequest(HttpCommand command, HttpResponse response) {
boolean retry = false; // default
try {
if (response.getStatusCode() == 401) {
closeClientButKeepContentStream(response);
byte[] data = closeClientButKeepContentStream(response);
String message = data != null ? new String(data) : null;
if (response.getStatusCode() == 401 || (message != null && message.indexOf(IP_MISMATCH) != -1)) {
logger.debug("invalidating session");
sessionCache.invalidateAll();
retry = super.shouldRetryRequest(command, response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
package org.jclouds.dynect.v3.filters;

import static com.google.common.io.Resources.getResource;
import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
import static javax.ws.rs.core.Response.Status.FORBIDDEN;
import static javax.ws.rs.core.Response.Status.UNAUTHORIZED;
import static org.easymock.EasyMock.createMock;
Expand All @@ -30,6 +32,8 @@
import static org.testng.Assert.assertSame;
import static org.testng.Assert.assertTrue;

import java.io.IOException;

import org.jclouds.domain.Credentials;
import org.jclouds.dynect.v3.domain.Session;
import org.jclouds.dynect.v3.domain.SessionCredentials;
Expand Down Expand Up @@ -95,6 +99,34 @@ public void testUnauthorizedShouldInvalidateSessionAndRetry() {
verify(creds, sessionCache, sessionApi, command);
}

@SuppressWarnings("unchecked")
@Test
public void testIPMismatchShouldInvalidateSessionAndRetry() throws IOException {
HttpCommand command = createMock(HttpCommand.class);
Supplier<Credentials> creds = createMock(Supplier.class);
LoadingCache<Credentials, Session> sessionCache = createMock(LoadingCache.class);
SessionApi sessionApi = createMock(SessionApi.class);

sessionCache.invalidateAll();
expectLastCall();
expect(command.incrementFailureCount()).andReturn(1);
expect(command.isReplayable()).andReturn(true);
expect(command.getFailureCount()).andReturn(1).atLeastOnce();

replay(creds, sessionCache, sessionApi, command);

HttpResponse response = HttpResponse.builder()
.statusCode(BAD_REQUEST.getStatusCode())
.payload(getResource("ip_mismatch.json").openStream())
.build();

SessionManager retry = new SessionManager(creds, sessionCache, sessionApi);

assertTrue(retry.shouldRetryRequest(command, response));

verify(creds, sessionCache, sessionApi, command);
}

@SuppressWarnings("unchecked")
@Test
public void testForbiddenShouldNotInvalidateSessionOrRetry() {
Expand Down
17 changes: 17 additions & 0 deletions providers/dynect/src/test/resources/ip_mismatch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"status": "failure",
"data": {},
"job_id": 305900967,
"msgs": [{
"INFO": "login: IP address does not match current session",
"SOURCE": "BLL",
"ERR_CD": "INVALID_DATA",
"LVL": "ERROR"
}, {
"INFO": "login: There was a problem with your credentials",
"SOURCE": "BLL",
"ERR_CD": null,
"LVL": "INFO"
}
]
}

0 comments on commit 630d4cf

Please sign in to comment.