mirror of https://github.com/apache/jclouds.git
Cleanup per Adrian's comments & fixed broken unit test
This commit is contained in:
parent
19cb82a26e
commit
c3da0021d8
|
@ -18,11 +18,8 @@
|
||||||
*/
|
*/
|
||||||
package org.jclouds.cloudstack.handlers;
|
package org.jclouds.cloudstack.handlers;
|
||||||
|
|
||||||
import java.io.IOException;
|
import com.google.common.base.Throwables;
|
||||||
|
import com.google.common.io.Closeables;
|
||||||
import javax.annotation.Resource;
|
|
||||||
import javax.inject.Singleton;
|
|
||||||
|
|
||||||
import org.jclouds.http.HttpCommand;
|
import org.jclouds.http.HttpCommand;
|
||||||
import org.jclouds.http.HttpErrorHandler;
|
import org.jclouds.http.HttpErrorHandler;
|
||||||
import org.jclouds.http.HttpResponse;
|
import org.jclouds.http.HttpResponse;
|
||||||
|
@ -32,8 +29,9 @@ import org.jclouds.rest.AuthorizationException;
|
||||||
import org.jclouds.rest.ResourceNotFoundException;
|
import org.jclouds.rest.ResourceNotFoundException;
|
||||||
import org.jclouds.util.Strings2;
|
import org.jclouds.util.Strings2;
|
||||||
|
|
||||||
import com.google.common.base.Throwables;
|
import javax.annotation.Resource;
|
||||||
import com.google.common.io.Closeables;
|
import javax.inject.Singleton;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
|
@ -57,6 +55,7 @@ public class CloudStackErrorHandler implements HttpErrorHandler {
|
||||||
case 400:
|
case 400:
|
||||||
exception = new IllegalArgumentException(message, exception);
|
exception = new IllegalArgumentException(message, exception);
|
||||||
break;
|
break;
|
||||||
|
case 531:
|
||||||
case 401:
|
case 401:
|
||||||
exception = new AuthorizationException(message, exception);
|
exception = new AuthorizationException(message, exception);
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -29,6 +29,8 @@ import org.jclouds.compute.ComputeServiceContext;
|
||||||
import org.jclouds.compute.ComputeServiceContextFactory;
|
import org.jclouds.compute.ComputeServiceContextFactory;
|
||||||
import org.jclouds.rest.RestContextFactory;
|
import org.jclouds.rest.RestContextFactory;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
import java.util.NoSuchElementException;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
@ -52,9 +54,11 @@ public class ApiKeyPairs {
|
||||||
* User password
|
* User password
|
||||||
* @param domain
|
* @param domain
|
||||||
* Domain name. If empty defaults to ROOT
|
* Domain name. If empty defaults to ROOT
|
||||||
|
* @throws NoSuchElementException, AuthorizationException
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public static ApiKeyPair getApiKeyPairForUser(String endpoint, String username, String password, String domain) {
|
public static ApiKeyPair loginToEndpointAsUsernameInDomainWithPasswordAndReturnApiKeyPair(
|
||||||
|
URI endpoint, String username, String password, String domain) {
|
||||||
ComputeServiceContext context = null;
|
ComputeServiceContext context = null;
|
||||||
try {
|
try {
|
||||||
context = new ComputeServiceContextFactory(setupRestProperties()).
|
context = new ComputeServiceContextFactory(setupRestProperties()).
|
||||||
|
@ -72,7 +76,7 @@ public class ApiKeyPairs {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
throw new NoSuchElementException("Unable to find API keypair for user " + username);
|
||||||
|
|
||||||
} finally {
|
} finally {
|
||||||
if (context != null)
|
if (context != null)
|
||||||
|
@ -84,7 +88,7 @@ public class ApiKeyPairs {
|
||||||
return RestContextFactory.getPropertiesFromResource("/rest.properties");
|
return RestContextFactory.getPropertiesFromResource("/rest.properties");
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Properties setupProperties(String endpoint, String username, String password, String domain) {
|
private static Properties setupProperties(URI endpoint, String username, String password, String domain) {
|
||||||
Properties overrides = new Properties();
|
Properties overrides = new Properties();
|
||||||
|
|
||||||
overrides.put(Constants.PROPERTY_TRUST_ALL_CERTS, "true");
|
overrides.put(Constants.PROPERTY_TRUST_ALL_CERTS, "true");
|
||||||
|
@ -92,7 +96,7 @@ public class ApiKeyPairs {
|
||||||
|
|
||||||
overrides.put("jclouds.cloudstack.credential-type", "passwordCredentials");
|
overrides.put("jclouds.cloudstack.credential-type", "passwordCredentials");
|
||||||
|
|
||||||
overrides.put(PROVIDER + ".endpoint", checkNotNull(endpoint, "endpoint"));
|
overrides.put(PROVIDER + ".endpoint", checkNotNull(endpoint, "endpoint").toASCIIString());
|
||||||
overrides.put(PROVIDER + ".identity",
|
overrides.put(PROVIDER + ".identity",
|
||||||
String.format("%s/%s", checkNotNull(domain, "domain"), checkNotNull(username, "username")));
|
String.format("%s/%s", checkNotNull(domain, "domain"), checkNotNull(username, "username")));
|
||||||
overrides.put(PROVIDER + ".credential", checkNotNull(password, "password"));
|
overrides.put(PROVIDER + ".credential", checkNotNull(password, "password"));
|
||||||
|
|
|
@ -35,7 +35,7 @@ import com.google.common.net.HttpHeaders;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @see KeystoneProperties#CREDENTIAL_TYPE
|
* @see CloudStackProperties#CREDENTIAL_TYPE
|
||||||
* @author Adrian Cole
|
* @author Adrian Cole
|
||||||
*/
|
*/
|
||||||
@Test(groups = "unit", testName = "PasswordAuthenticationExpectTest")
|
@Test(groups = "unit", testName = "PasswordAuthenticationExpectTest")
|
||||||
|
|
|
@ -18,12 +18,10 @@
|
||||||
*/
|
*/
|
||||||
package org.jclouds.cloudstack.features;
|
package org.jclouds.cloudstack.features;
|
||||||
|
|
||||||
import static org.jclouds.crypto.CryptoStreams.md5Hex;
|
import com.google.common.base.Function;
|
||||||
|
import com.google.common.collect.ImmutableMultimap;
|
||||||
import java.net.URI;
|
import com.google.common.collect.ImmutableSet;
|
||||||
import java.net.URLEncoder;
|
import com.google.inject.Module;
|
||||||
import java.util.Properties;
|
|
||||||
|
|
||||||
import org.jclouds.cloudstack.CloudStackContext;
|
import org.jclouds.cloudstack.CloudStackContext;
|
||||||
import org.jclouds.compute.ComputeServiceContextFactory;
|
import org.jclouds.compute.ComputeServiceContextFactory;
|
||||||
import org.jclouds.http.HttpRequest;
|
import org.jclouds.http.HttpRequest;
|
||||||
|
@ -31,10 +29,11 @@ import org.jclouds.http.HttpResponse;
|
||||||
import org.jclouds.logging.config.NullLoggingModule;
|
import org.jclouds.logging.config.NullLoggingModule;
|
||||||
import org.jclouds.rest.BaseRestClientExpectTest;
|
import org.jclouds.rest.BaseRestClientExpectTest;
|
||||||
|
|
||||||
import com.google.common.base.Function;
|
import java.net.URI;
|
||||||
import com.google.common.collect.ImmutableMultimap;
|
import java.net.URLEncoder;
|
||||||
import com.google.common.collect.ImmutableSet;
|
import java.util.Properties;
|
||||||
import com.google.inject.Module;
|
|
||||||
|
import static org.jclouds.crypto.CryptoStreams.md5Hex;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Base class for writing CloudStack Rest Client Expect tests
|
* Base class for writing CloudStack Rest Client Expect tests
|
||||||
|
@ -60,7 +59,7 @@ public abstract class BaseCloudStackRestClientExpectTest<S> extends BaseRestClie
|
||||||
.method("GET")
|
.method("GET")
|
||||||
.endpoint(
|
.endpoint(
|
||||||
URI.create("http://localhost:8080/client/api?response=json&command=login&" +
|
URI.create("http://localhost:8080/client/api?response=json&command=login&" +
|
||||||
"username=identity&password=" + md5Hex("credential")+ "&domain=ROOT"))
|
"username=identity&password=" + md5Hex("credential")+ "&domain="))
|
||||||
.headers(
|
.headers(
|
||||||
ImmutableMultimap.<String, String>builder()
|
ImmutableMultimap.<String, String>builder()
|
||||||
.put("Accept", "application/json")
|
.put("Accept", "application/json")
|
||||||
|
|
|
@ -19,12 +19,16 @@
|
||||||
package org.jclouds.cloudstack.features;
|
package org.jclouds.cloudstack.features;
|
||||||
|
|
||||||
import org.jclouds.cloudstack.domain.Account;
|
import org.jclouds.cloudstack.domain.Account;
|
||||||
|
import org.jclouds.cloudstack.domain.ApiKeyPair;
|
||||||
import org.jclouds.cloudstack.domain.LoginResponse;
|
import org.jclouds.cloudstack.domain.LoginResponse;
|
||||||
import org.jclouds.cloudstack.domain.User;
|
import org.jclouds.cloudstack.domain.User;
|
||||||
import org.jclouds.cloudstack.util.ApiKeyPairs;
|
import org.jclouds.cloudstack.util.ApiKeyPairs;
|
||||||
import org.jclouds.crypto.CryptoStreams;
|
import org.jclouds.crypto.CryptoStreams;
|
||||||
|
import org.jclouds.rest.AuthorizationException;
|
||||||
import org.testng.annotations.Test;
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
|
||||||
import static org.jclouds.cloudstack.features.GlobalAccountClientLiveTest.createTestAccount;
|
import static org.jclouds.cloudstack.features.GlobalAccountClientLiveTest.createTestAccount;
|
||||||
import static org.jclouds.cloudstack.features.GlobalUserClientLiveTest.createTestUser;
|
import static org.jclouds.cloudstack.features.GlobalUserClientLiveTest.createTestUser;
|
||||||
import static org.testng.Assert.assertEquals;
|
import static org.testng.Assert.assertEquals;
|
||||||
|
@ -54,10 +58,11 @@ public class SessionClientLiveTest extends BaseCloudStackClientLiveTest {
|
||||||
|
|
||||||
checkLoginAsTheNewUser(expectedUsername);
|
checkLoginAsTheNewUser(expectedUsername);
|
||||||
|
|
||||||
assertEquals(
|
ApiKeyPair expected = globalAdminClient.getUserClient().registerUserKeys(testUser.getId());
|
||||||
globalAdminClient.getUserClient().registerUserKeys(testUser.getId()),
|
ApiKeyPair actual = ApiKeyPairs.loginToEndpointAsUsernameInDomainWithPasswordAndReturnApiKeyPair(
|
||||||
ApiKeyPairs.getApiKeyPairForUser(endpoint, prefix + "-user", "password", "")
|
URI.create(endpoint), prefix + "-user", "password", "");
|
||||||
);
|
|
||||||
|
assertEquals(actual, expected);
|
||||||
|
|
||||||
} finally {
|
} finally {
|
||||||
if (testUser != null)
|
if (testUser != null)
|
||||||
|
@ -67,6 +72,12 @@ public class SessionClientLiveTest extends BaseCloudStackClientLiveTest {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test(expectedExceptions = AuthorizationException.class)
|
||||||
|
public void testTryToGetApiKeypairWithWrongCredentials() {
|
||||||
|
ApiKeyPairs.loginToEndpointAsUsernameInDomainWithPasswordAndReturnApiKeyPair(
|
||||||
|
URI.create(endpoint), "dummy-missing-user", "with-a-wrong-password", "");
|
||||||
|
}
|
||||||
|
|
||||||
private void checkLoginAsTheNewUser(String expectedUsername) {
|
private void checkLoginAsTheNewUser(String expectedUsername) {
|
||||||
LoginResponse response = globalAdminClient.getSessionClient()
|
LoginResponse response = globalAdminClient.getSessionClient()
|
||||||
.loginUserInDomainWithHashOfPassword(expectedUsername, "", CryptoStreams.md5Hex("password"));
|
.loginUserInDomainWithHashOfPassword(expectedUsername, "", CryptoStreams.md5Hex("password"));
|
||||||
|
|
Loading…
Reference in New Issue