From f8ebb675b637d6a087113dd3cf93d9beec1fa349 Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Tue, 31 Jan 2012 11:14:44 -0800 Subject: [PATCH] fixed structure of auth request and organized tenantId to precede username --- .../v2_0/binders/BindAuthToJsonPayload.java | 8 +++---- .../config/KeyStoneAuthenticationModule.java | 23 ++++++++++++------- .../BaseKeyStoneRestClientExpectTest.java | 6 ++--- 3 files changed, 22 insertions(+), 15 deletions(-) diff --git a/common/openstack/src/main/java/org/jclouds/openstack/keystone/v2_0/binders/BindAuthToJsonPayload.java b/common/openstack/src/main/java/org/jclouds/openstack/keystone/v2_0/binders/BindAuthToJsonPayload.java index 2e14a272ea..533817a807 100644 --- a/common/openstack/src/main/java/org/jclouds/openstack/keystone/v2_0/binders/BindAuthToJsonPayload.java +++ b/common/openstack/src/main/java/org/jclouds/openstack/keystone/v2_0/binders/BindAuthToJsonPayload.java @@ -59,9 +59,9 @@ public class BindAuthToJsonPayload extends BindToJsonPayload implements MapBinde protected void addCredentialsInArgsOrNull(GeneratedHttpRequest gRequest, Builder builder) { for (Object arg : gRequest.getArgs()) { if (arg instanceof PasswordCredentials) { - builder.put("auth", ImmutableMap.of("passwordCredentials", PasswordCredentials.class.cast(arg))); + builder.put("passwordCredentials", PasswordCredentials.class.cast(arg)); } else if (arg instanceof ApiAccessKeyCredentials) { - builder.put("auth", ImmutableMap.of("apiAccessKeyCredentials", ApiAccessKeyCredentials.class.cast(arg))); + builder.put("apiAccessKeyCredentials", ApiAccessKeyCredentials.class.cast(arg)); } } } @@ -74,10 +74,10 @@ public class BindAuthToJsonPayload extends BindToJsonPayload implements MapBinde checkState(gRequest.getArgs() != null, "args should be initialized at this point"); Builder builder = ImmutableMap. builder(); + addCredentialsInArgsOrNull(gRequest, builder); if (Strings.emptyToNull(postParams.get("tenantId")) != null) builder.put("tenantId", postParams.get("tenantId")); - addCredentialsInArgsOrNull(gRequest, builder); - return super.bindToRequest(request, builder.build()); + return super.bindToRequest(request, ImmutableMap.of("auth", builder.build())); } } diff --git a/common/openstack/src/main/java/org/jclouds/openstack/keystone/v2_0/config/KeyStoneAuthenticationModule.java b/common/openstack/src/main/java/org/jclouds/openstack/keystone/v2_0/config/KeyStoneAuthenticationModule.java index faabcdcceb..74bed2c0d3 100644 --- a/common/openstack/src/main/java/org/jclouds/openstack/keystone/v2_0/config/KeyStoneAuthenticationModule.java +++ b/common/openstack/src/main/java/org/jclouds/openstack/keystone/v2_0/config/KeyStoneAuthenticationModule.java @@ -31,12 +31,15 @@ import javax.inject.Singleton; import org.jclouds.Constants; import org.jclouds.concurrent.RetryOnTimeOutExceptionFunction; import org.jclouds.domain.Credentials; +import org.jclouds.http.HttpRetryHandler; import org.jclouds.http.RequiresHttp; +import org.jclouds.http.annotation.ClientError; import org.jclouds.location.Provider; import org.jclouds.openstack.Authentication; import org.jclouds.openstack.keystone.v2_0.ServiceAsyncClient; import org.jclouds.openstack.keystone.v2_0.domain.Access; import org.jclouds.openstack.keystone.v2_0.domain.PasswordCredentials; +import org.jclouds.openstack.keystone.v2_0.handlers.RetryOnRenew; import org.jclouds.rest.AsyncClientFactory; import com.google.common.base.Function; @@ -87,9 +90,9 @@ public class KeyStoneAuthenticationModule extends AbstractModule { @Provides @Provider - protected Credentials provideAuthenticationCredentials(@Named(Constants.PROPERTY_IDENTITY) String user, - @Named(Constants.PROPERTY_CREDENTIAL) String key) { - return new Credentials(user, key); + protected Credentials provideAuthenticationCredentials(@Named(Constants.PROPERTY_IDENTITY) String userOrApiKey, + @Named(Constants.PROPERTY_CREDENTIAL) String keyOrSecretKey) { + return new Credentials(userOrApiKey, keyOrSecretKey); } @Singleton @@ -104,14 +107,14 @@ public class KeyStoneAuthenticationModule extends AbstractModule { @Override public Access apply(Credentials input) { // TODO: nice error messages, etc. - Iterable usernameTenantId = Splitter.on(':').split(input.identity); - String username = Iterables.get(usernameTenantId, 0); - String tenantId = Iterables.get(usernameTenantId, 1); + Iterable tenantIdUsername = Splitter.on(':').split(input.identity); + String tenantId = Iterables.get(tenantIdUsername, 0); + String username = Iterables.get(tenantIdUsername, 1); PasswordCredentials passwordCredentials = PasswordCredentials.createWithUsernameAndPassword(username, input.credential); try { - return factory.create(ServiceAsyncClient.class) - .authenticateTenantWithCredentials(tenantId, passwordCredentials).get(); + return factory.create(ServiceAsyncClient.class).authenticateTenantWithCredentials(tenantId, + passwordCredentials).get(); } catch (Exception e) { throw Throwables.propagate(e); } @@ -126,12 +129,16 @@ public class KeyStoneAuthenticationModule extends AbstractModule { } } + // TODO: what is the timeout of the session token? modify default accordingly + // PROPERTY_SESSION_INTERVAL is default to 60 seconds, but we have this here at 23 hours for now. @Provides @Singleton public LoadingCache provideAccessCache2(Function getAccess) { return CacheBuilder.newBuilder().expireAfterWrite(23, TimeUnit.HOURS).build(CacheLoader.from(getAccess)); } + // Temporary conversion of a cache to a supplier until there is a single-element cache + // http://code.google.com/p/guava-libraries/issues/detail?id=872 @Provides @Singleton protected Supplier provideAccessSupplier(final LoadingCache cache, diff --git a/common/openstack/src/test/java/org/jclouds/openstack/keystone/v2_0/internal/BaseKeyStoneRestClientExpectTest.java b/common/openstack/src/test/java/org/jclouds/openstack/keystone/v2_0/internal/BaseKeyStoneRestClientExpectTest.java index 5924752008..ee45b8f308 100644 --- a/common/openstack/src/test/java/org/jclouds/openstack/keystone/v2_0/internal/BaseKeyStoneRestClientExpectTest.java +++ b/common/openstack/src/test/java/org/jclouds/openstack/keystone/v2_0/internal/BaseKeyStoneRestClientExpectTest.java @@ -36,8 +36,8 @@ import com.google.common.net.HttpHeaders; public class BaseKeyStoneRestClientExpectTest extends BaseRestClientExpectTest { public BaseKeyStoneRestClientExpectTest() { - // username:tenantId - identity = "user@jclouds.org:12346637803162"; + // tenantId:username + identity = "12346637803162:user@jclouds.org"; credential = "Password1234"; } @@ -48,7 +48,7 @@ public class BaseKeyStoneRestClientExpectTest extends BaseRestClientExpectTes .headers(ImmutableMultimap.of(HttpHeaders.ACCEPT, "application/json")) .payload( payloadFromStringWithContentType( - "{\"tenantId\":\"12346637803162\",\"auth\":{\"passwordCredentials\":{\"username\":\"user@jclouds.org\",\"password\":\"Password1234\"}}}", + "{\"auth\":{\"passwordCredentials\":{\"username\":\"user@jclouds.org\",\"password\":\"Password1234\"},\"tenantId\":\"12346637803162\"}}", "application/json")).build(); protected String authToken = "Auth_4f173437e4b013bee56d1007";