JCLOUDS-656: Parse Swift authentication headers case-insensitively

Also ensure they are added to the result object with the normalized
capitalization expected by downstream code.

Follow-up to 375cb207.
This commit is contained in:
Fabian Lange 2014-08-06 18:16:32 +02:00 committed by Andrew Phillips
parent 5d3b293b69
commit aa174a5d73
2 changed files with 26 additions and 10 deletions

View File

@ -20,7 +20,6 @@ import static com.google.common.base.Preconditions.checkNotNull;
import static org.jclouds.http.HttpUtils.releasePayload;
import static org.jclouds.http.Uris.uriBuilder;
import static org.jclouds.openstack.reference.AuthHeaders.AUTH_TOKEN;
import static org.jclouds.openstack.reference.AuthHeaders.URL_SUFFIX;
import java.net.URI;
import java.util.Map.Entry;
@ -31,6 +30,7 @@ import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse;
import org.jclouds.logging.Logger;
import org.jclouds.openstack.domain.AuthenticationResponse;
import org.jclouds.openstack.reference.AuthHeaders;
import org.jclouds.rest.InvocationContext;
import com.google.common.annotations.VisibleForTesting;
@ -58,8 +58,14 @@ public class ParseAuthenticationResponseFromHeaders implements Function<HttpResp
// HTTP headers are case insensitive (RFC 2616) so we must allow for that when looking an header names for the URL keyword
Builder<String, URI> builder = ImmutableMap.builder();
for (Entry<String, String> entry : from.getHeaders().entries()) {
if (entry.getKey().toLowerCase().endsWith(URL_SUFFIX.toLowerCase()))
builder.put(entry.getKey(), getURI(entry.getValue()));
String header = entry.getKey();
if (header.equalsIgnoreCase(AuthHeaders.STORAGE_URL)) {
builder.put(AuthHeaders.STORAGE_URL, getURI(entry.getValue()));
} else if (header.equalsIgnoreCase(AuthHeaders.SERVER_MANAGEMENT_URL)) {
builder.put(AuthHeaders.SERVER_MANAGEMENT_URL, getURI(entry.getValue()));
} else if (header.equalsIgnoreCase(AuthHeaders.CDN_MANAGEMENT_URL)) {
builder.put(AuthHeaders.CDN_MANAGEMENT_URL, getURI(entry.getValue()));
}
}
AuthenticationResponse response = new AuthenticationResponse(checkNotNull(from.getFirstHeaderOrNull(AUTH_TOKEN),
AUTH_TOKEN), builder.build());

View File

@ -23,9 +23,9 @@ import java.net.URI;
import org.jclouds.Constants;
import org.jclouds.http.HttpResponse;
import org.jclouds.openstack.domain.AuthenticationResponse;
import org.jclouds.openstack.reference.AuthHeaders;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableMap;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
@ -53,11 +53,16 @@ public class ParseAuthenticationResponseFromHeadersTest {
HttpResponse response = HttpResponse.builder().statusCode(204).message("No Content")
.addHeader("X-Auth-Token", "token")
.addHeader("X-Storage-Token", "token")
.addHeader("X-Storage-Url", "http://127.0.0.1:8080/v1/token").build();
.addHeader("X-Storage-Url", "http://127.0.0.1:8080/v1/token")
.addHeader("X-CDN-Management-Url", "http://127.0.0.1:8080/cdn-mgmt")
.addHeader("X-Server-Management-Url", "http://127.0.0.1:8080/srv-mgmt")
.build();
AuthenticationResponse md = parser.apply(response);
assertEquals(md, new AuthenticationResponse("token", ImmutableMap.<String, URI> of("X-Storage-Url",
URI.create("http://fooman:8080/v1/token"))));
assertEquals(md.getAuthToken(), "token");
assertEquals(md.getServices().get(AuthHeaders.STORAGE_URL), URI.create("http://fooman:8080/v1/token"));
assertEquals(md.getServices().get(AuthHeaders.CDN_MANAGEMENT_URL), URI.create("http://fooman:8080/cdn-mgmt"));
assertEquals(md.getServices().get(AuthHeaders.SERVER_MANAGEMENT_URL), URI.create("http://fooman:8080/srv-mgmt"));
}
public void testHandleHeadersCaseInsensitively() {
@ -67,9 +72,14 @@ public class ParseAuthenticationResponseFromHeadersTest {
HttpResponse response = HttpResponse.builder().statusCode(204).message("No Content")
.addHeader("x-auth-token", "token")
.addHeader("x-storage-token", "token")
.addHeader("x-storage-url", "http://127.0.0.1:8080/v1/token").build();
.addHeader("x-storage-url", "http://127.0.0.1:8080/v1/token")
.addHeader("x-cdn-management-url", "http://127.0.0.1:8080/cdn-mgmt")
.addHeader("x-server-management-url", "http://127.0.0.1:8080/srv-mgmt")
.build();
AuthenticationResponse md = parser.apply(response);
assertEquals(md, new AuthenticationResponse("token", ImmutableMap.<String, URI> of("x-storage-url",
URI.create("http://fooman:8080/v1/token"))));
assertEquals(md.getAuthToken(), "token");
assertEquals(md.getServices().get(AuthHeaders.STORAGE_URL), URI.create("http://fooman:8080/v1/token"));
assertEquals(md.getServices().get(AuthHeaders.CDN_MANAGEMENT_URL), URI.create("http://fooman:8080/cdn-mgmt"));
assertEquals(md.getServices().get(AuthHeaders.SERVER_MANAGEMENT_URL), URI.create("http://fooman:8080/srv-mgmt"));
}
}