Issue 429: switched swift to Storage-User and Storage-Pass auth

This commit is contained in:
Adrian Cole 2011-06-01 18:34:29 -07:00
parent 08c8430619
commit 7e00e064c0
5 changed files with 92 additions and 16 deletions

View File

@ -18,13 +18,23 @@
*/
package org.jclouds.openstack.swift.config;
import java.net.URI;
import java.util.concurrent.Future;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import org.jclouds.Constants;
import org.jclouds.http.RequiresHttp;
import org.jclouds.openstack.OpenStackAuthAsyncClient.AuthenticationResponse;
import org.jclouds.openstack.config.OpenStackAuthenticationModule.GetAuthenticationResponse;
import org.jclouds.openstack.reference.AuthHeaders;
import org.jclouds.openstack.swift.CommonSwiftAsyncClient;
import org.jclouds.openstack.swift.CommonSwiftClient;
import org.jclouds.openstack.swift.SwiftAsyncClient;
import org.jclouds.openstack.swift.SwiftClient;
import org.jclouds.rest.AsyncClientFactory;
import org.jclouds.rest.ConfiguresRestClient;
import com.google.inject.Provides;
@ -52,4 +62,28 @@ public class SwiftRestClientModule extends BaseSwiftRestClientModule<SwiftClient
CommonSwiftAsyncClient provideCommonSwiftClient(SwiftAsyncClient in) {
return in;
}
@Singleton
public static class GetAuthenticationResponseForStorage extends GetAuthenticationResponse {
@Inject
public GetAuthenticationResponseForStorage(AsyncClientFactory factory,
@Named(Constants.PROPERTY_IDENTITY) String user, @Named(Constants.PROPERTY_CREDENTIAL) String key) {
super(factory, user, key);
}
protected Future<AuthenticationResponse> authenticate() {
return client.authenticateStorage(user, key);
}
}
protected URI provideStorageUrl(AuthenticationResponse response) {
return response.getServices().get(AuthHeaders.STORAGE_URL);
}
@Override
protected void configure() {
bind(GetAuthenticationResponse.class).to(GetAuthenticationResponseForStorage.class);
super.configure();
}
}

View File

@ -93,4 +93,10 @@ public interface OpenStackAuthAsyncClient {
@ResponseParser(ParseAuthenticationResponseFromHeaders.class)
ListenableFuture<AuthenticationResponse> authenticate(@HeaderParam(AuthHeaders.AUTH_USER) String user,
@HeaderParam(AuthHeaders.AUTH_KEY) String key);
@GET
@ResponseParser(ParseAuthenticationResponseFromHeaders.class)
ListenableFuture<AuthenticationResponse> authenticateStorage(@HeaderParam(AuthHeaders.STORAGE_USER) String user,
@HeaderParam(AuthHeaders.STORAGE_PASS) String key);
}

View File

@ -24,6 +24,7 @@ import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
@ -69,25 +70,44 @@ public class OpenStackAuthenticationModule extends AbstractModule {
};
}
@Singleton
public static class GetAuthenticationResponse implements Supplier<AuthenticationResponse> {
protected final OpenStackAuthAsyncClient client;
protected final String user;
protected final String key;
@Inject
public GetAuthenticationResponse(AsyncClientFactory factory, @Named(Constants.PROPERTY_IDENTITY) String user,
@Named(Constants.PROPERTY_CREDENTIAL) String key) {
this.client = factory.create(OpenStackAuthAsyncClient.class);
this.user = user;
this.key = key;
}
@Override
public AuthenticationResponse get() {
try {
Future<AuthenticationResponse> response = authenticate();
return response.get(30, TimeUnit.SECONDS);
} catch (Exception e) {
Throwables.propagate(e);
assert false : e;
return null;
}
}
protected Future<AuthenticationResponse> authenticate() {
return client.authenticate(user, key);
}
}
@Provides
@Singleton
Supplier<AuthenticationResponse> provideAuthenticationResponseCache(final AsyncClientFactory factory,
@Named(Constants.PROPERTY_IDENTITY) final String user,
@Named(Constants.PROPERTY_CREDENTIAL) final String key) {
Supplier<AuthenticationResponse> provideAuthenticationResponseCache(
final GetAuthenticationResponse getAuthenticationResponse) {
return Suppliers.memoizeWithExpiration(new RetryOnTimeOutExceptionSupplier<AuthenticationResponse>(
new Supplier<AuthenticationResponse>() {
public AuthenticationResponse get() {
try {
Future<AuthenticationResponse> response = factory.create(OpenStackAuthAsyncClient.class)
.authenticate(user, key);
return response.get(30, TimeUnit.SECONDS);
} catch (Exception e) {
Throwables.propagate(e);
assert false : e;
return null;
}
}
}), 23, TimeUnit.HOURS);
getAuthenticationResponse), 23, TimeUnit.HOURS);
}
@Provides

View File

@ -29,6 +29,8 @@ public interface AuthHeaders {
public static final String AUTH_USER = "X-Auth-User";
public static final String AUTH_KEY = "X-Auth-Key";
public static final String STORAGE_USER = "X-Storage-User";
public static final String STORAGE_PASS = "X-Storage-Pass";
public static final String AUTH_TOKEN = "X-Auth-Token";
public static final String URL_SUFFIX = "-Url";

View File

@ -60,6 +60,20 @@ public class OpenStackAuthAsyncClientTest extends RestClientTest<OpenStackAuthAs
}
public void testAuthenticateStorage() throws SecurityException, NoSuchMethodException, IOException {
Method method = OpenStackAuthAsyncClient.class.getMethod("authenticateStorage", String.class, String.class);
HttpRequest httpRequest = processor.createRequest(method, "foo", "bar");
assertRequestLineEquals(httpRequest, "GET http://localhost:8080/v1.0 HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, "X-Storage-Pass: bar\nX-Storage-User: foo\n");
assertPayloadEquals(httpRequest, null, null, false);
assertResponseParserClassEquals(method, httpRequest, ParseAuthenticationResponseFromHeaders.class);
assertSaxResponseParserClassEquals(method, null);
assertExceptionParserClassEquals(method, MapHttp4xxCodesToExceptions.class);
}
@Override
public RestContextSpec<OpenStackAuthClient, OpenStackAuthAsyncClient> createContextSpec() {
return contextSpec("test", "http://localhost:8080", "1.0", "", "identity", "credential",