mirror of https://github.com/apache/jclouds.git
Merge pull request #343 from andreisavu/login-api
Retrieve API keys using user + password authentication
This commit is contained in:
commit
c25c04289d
|
@ -18,17 +18,15 @@
|
||||||
*/
|
*/
|
||||||
package org.jclouds.cloudstack.functions;
|
package org.jclouds.cloudstack.functions;
|
||||||
|
|
||||||
import java.io.File;
|
import com.google.common.base.Function;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
|
||||||
import javax.inject.Singleton;
|
|
||||||
|
|
||||||
import org.jclouds.cloudstack.domain.LoginResponse;
|
import org.jclouds.cloudstack.domain.LoginResponse;
|
||||||
import org.jclouds.cloudstack.features.SessionClient;
|
import org.jclouds.cloudstack.features.SessionClient;
|
||||||
import org.jclouds.crypto.CryptoStreams;
|
import org.jclouds.crypto.CryptoStreams;
|
||||||
import org.jclouds.domain.Credentials;
|
import org.jclouds.domain.Credentials;
|
||||||
|
|
||||||
import com.google.common.base.Function;
|
import javax.inject.Inject;
|
||||||
|
import javax.inject.Singleton;
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
@Singleton
|
@Singleton
|
||||||
public class LoginWithPasswordCredentials implements Function<Credentials, LoginResponse> {
|
public class LoginWithPasswordCredentials implements Function<Credentials, LoginResponse> {
|
||||||
|
@ -42,7 +40,8 @@ public class LoginWithPasswordCredentials implements Function<Credentials, Login
|
||||||
@Override
|
@Override
|
||||||
public LoginResponse apply(Credentials input) {
|
public LoginResponse apply(Credentials input) {
|
||||||
String username = input.identity;
|
String username = input.identity;
|
||||||
String domain = "ROOT";
|
String domain = ""; // empty = ROOT domain
|
||||||
|
|
||||||
// domain may be present
|
// domain may be present
|
||||||
if (username.indexOf('/') != -1) {
|
if (username.indexOf('/') != -1) {
|
||||||
File domainUsername = new File(username);
|
File domainUsername = new File(username);
|
||||||
|
|
|
@ -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;
|
||||||
|
|
|
@ -18,29 +18,89 @@
|
||||||
*/
|
*/
|
||||||
package org.jclouds.cloudstack.util;
|
package org.jclouds.cloudstack.util;
|
||||||
|
|
||||||
|
import com.google.common.collect.ImmutableSet;
|
||||||
|
import com.google.inject.Module;
|
||||||
|
import org.jclouds.Constants;
|
||||||
|
import org.jclouds.cloudstack.CloudStackClient;
|
||||||
|
import org.jclouds.cloudstack.domain.Account;
|
||||||
import org.jclouds.cloudstack.domain.ApiKeyPair;
|
import org.jclouds.cloudstack.domain.ApiKeyPair;
|
||||||
|
import org.jclouds.cloudstack.domain.User;
|
||||||
|
import org.jclouds.compute.ComputeServiceContext;
|
||||||
|
import org.jclouds.compute.ComputeServiceContextFactory;
|
||||||
|
import org.jclouds.rest.RestContextFactory;
|
||||||
|
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
|
import java.util.NoSuchElementException;
|
||||||
|
import java.util.Properties;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import static com.google.common.base.Preconditions.checkNotNull;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Andrei Savu
|
* @author Andrei Savu
|
||||||
*/
|
*/
|
||||||
public class ApiKeyPairs {
|
public class ApiKeyPairs {
|
||||||
|
|
||||||
|
private final static String PROVIDER = "cloudstack";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve the API key pair for a given CloudStack user
|
* Retrieve the API key pair for a given CloudStack user
|
||||||
*
|
*
|
||||||
* @param endpoint
|
* @param endpoint
|
||||||
* CloudStack API endpoint (e.g. http://72.52.126.25/client/api/)
|
* CloudStack API endpoint (e.g. http://72.52.126.25/client/api/)
|
||||||
* @param userName
|
* @param username
|
||||||
* User account name
|
* User account name
|
||||||
* @param password
|
* @param password
|
||||||
* 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(URI endpoint, String userName, String password, String domain) {
|
public static ApiKeyPair loginToEndpointAsUsernameInDomainWithPasswordAndReturnApiKeyPair(
|
||||||
return null;
|
URI endpoint, String username, String password, String domain) {
|
||||||
|
ComputeServiceContext context = null;
|
||||||
|
try {
|
||||||
|
context = new ComputeServiceContextFactory(setupRestProperties()).
|
||||||
|
createContext(PROVIDER, ImmutableSet.<Module>of(), setupProperties(endpoint, username, password, domain));
|
||||||
|
|
||||||
|
CloudStackClient client = CloudStackClient.class.cast(context.getProviderSpecificContext().getApi());
|
||||||
|
Set<Account> listOfAccounts = client.getAccountClient().listAccounts();
|
||||||
|
|
||||||
|
domain = (domain.equals("") || domain.equals("/")) ? "ROOT" : domain;
|
||||||
|
for (Account account : listOfAccounts) {
|
||||||
|
for (User user : account.getUsers()) {
|
||||||
|
if (user.getName().equals(username) && user.getDomain().equals(domain)) {
|
||||||
|
return ApiKeyPair.builder().apiKey(user.getApiKey())
|
||||||
|
.secretKey(user.getSecretKey()).build();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw new NoSuchElementException("Unable to find API keypair for user " + username);
|
||||||
|
|
||||||
|
} finally {
|
||||||
|
if (context != null)
|
||||||
|
context.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Properties setupRestProperties() {
|
||||||
|
return RestContextFactory.getPropertiesFromResource("/rest.properties");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Properties setupProperties(URI endpoint, String username, String password, String domain) {
|
||||||
|
Properties overrides = new Properties();
|
||||||
|
|
||||||
|
overrides.put(Constants.PROPERTY_TRUST_ALL_CERTS, "true");
|
||||||
|
overrides.put(Constants.PROPERTY_RELAX_HOSTNAME, "true");
|
||||||
|
|
||||||
|
overrides.put("jclouds.cloudstack.credential-type", "passwordCredentials");
|
||||||
|
|
||||||
|
overrides.put(PROVIDER + ".endpoint", checkNotNull(endpoint, "endpoint").toASCIIString());
|
||||||
|
overrides.put(PROVIDER + ".identity",
|
||||||
|
String.format("%s/%s", checkNotNull(domain, "domain"), checkNotNull(username, "username")));
|
||||||
|
overrides.put(PROVIDER + ".credential", checkNotNull(password, "password"));
|
||||||
|
|
||||||
|
return overrides;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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")
|
||||||
|
|
|
@ -20,6 +20,7 @@ package org.jclouds.cloudstack.features;
|
||||||
|
|
||||||
import org.jclouds.cloudstack.CloudStackGlobalClient;
|
import org.jclouds.cloudstack.CloudStackGlobalClient;
|
||||||
import org.jclouds.cloudstack.domain.Account;
|
import org.jclouds.cloudstack.domain.Account;
|
||||||
|
import org.jclouds.crypto.CryptoStreams;
|
||||||
import org.testng.annotations.Test;
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
import static org.testng.Assert.assertEquals;
|
import static org.testng.Assert.assertEquals;
|
||||||
|
@ -36,7 +37,7 @@ public class GlobalAccountClientLiveTest extends BaseCloudStackClientLiveTest {
|
||||||
public static Account createTestAccount(CloudStackGlobalClient client, String prefix) {
|
public static Account createTestAccount(CloudStackGlobalClient client, String prefix) {
|
||||||
return client.getAccountClient().createAccount(
|
return client.getAccountClient().createAccount(
|
||||||
prefix + "-account", Account.Type.USER, "dummy@example.com",
|
prefix + "-account", Account.Type.USER, "dummy@example.com",
|
||||||
"First", "Last", "hashed-password");
|
"First", "Last", CryptoStreams.md5Hex("password"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
@ -27,6 +27,7 @@ import org.jclouds.cloudstack.domain.ApiKeyPair;
|
||||||
import org.jclouds.cloudstack.domain.User;
|
import org.jclouds.cloudstack.domain.User;
|
||||||
import org.jclouds.compute.ComputeServiceContext;
|
import org.jclouds.compute.ComputeServiceContext;
|
||||||
import org.jclouds.compute.ComputeServiceContextFactory;
|
import org.jclouds.compute.ComputeServiceContextFactory;
|
||||||
|
import org.jclouds.crypto.CryptoStreams;
|
||||||
import org.jclouds.logging.log4j.config.Log4JLoggingModule;
|
import org.jclouds.logging.log4j.config.Log4JLoggingModule;
|
||||||
import org.jclouds.sshj.config.SshjSshClientModule;
|
import org.jclouds.sshj.config.SshjSshClientModule;
|
||||||
import org.testng.annotations.Test;
|
import org.testng.annotations.Test;
|
||||||
|
@ -48,7 +49,7 @@ public class GlobalUserClientLiveTest extends BaseCloudStackClientLiveTest {
|
||||||
|
|
||||||
public static User createTestUser(CloudStackGlobalClient client, Account account, String prefix) {
|
public static User createTestUser(CloudStackGlobalClient client, Account account, String prefix) {
|
||||||
return client.getUserClient().createUser(prefix + "-user",
|
return client.getUserClient().createUser(prefix + "-user",
|
||||||
account.getName(), "dummy2@example.com", "md5-password", "First", "Last");
|
account.getName(), "dummy2@example.com", CryptoStreams.md5Hex("password"), "First", "Last");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
@ -18,13 +18,7 @@
|
||||||
*/
|
*/
|
||||||
package org.jclouds.cloudstack.features;
|
package org.jclouds.cloudstack.features;
|
||||||
|
|
||||||
import static org.jclouds.crypto.CryptoStreams.md5Hex;
|
import com.google.common.collect.ImmutableMultimap;
|
||||||
import static org.testng.Assert.assertEquals;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.net.URI;
|
|
||||||
import java.net.URLEncoder;
|
|
||||||
|
|
||||||
import org.jclouds.cloudstack.CloudStackContext;
|
import org.jclouds.cloudstack.CloudStackContext;
|
||||||
import org.jclouds.cloudstack.domain.Account;
|
import org.jclouds.cloudstack.domain.Account;
|
||||||
import org.jclouds.cloudstack.domain.LoginResponse;
|
import org.jclouds.cloudstack.domain.LoginResponse;
|
||||||
|
@ -32,11 +26,16 @@ import org.jclouds.http.HttpRequest;
|
||||||
import org.jclouds.http.HttpResponse;
|
import org.jclouds.http.HttpResponse;
|
||||||
import org.testng.annotations.Test;
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
import com.google.common.collect.ImmutableMultimap;
|
import java.io.IOException;
|
||||||
|
import java.net.URI;
|
||||||
|
import java.net.URLEncoder;
|
||||||
|
|
||||||
|
import static org.jclouds.crypto.CryptoStreams.md5Hex;
|
||||||
|
import static org.testng.Assert.assertEquals;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests behavior of {@code SessionClient}
|
* Tests behavior of {@code SessionClient}
|
||||||
*
|
*
|
||||||
* @author Andrei Savu
|
* @author Andrei Savu
|
||||||
*/
|
*/
|
||||||
@Test(groups = "live", singleThreaded = true, testName = "SessionClientExpectTest")
|
@Test(groups = "live", singleThreaded = true, testName = "SessionClientExpectTest")
|
||||||
|
@ -44,42 +43,54 @@ public class SessionClientExpectTest extends BaseCloudStackRestClientExpectTest<
|
||||||
|
|
||||||
@SuppressWarnings("deprecation")
|
@SuppressWarnings("deprecation")
|
||||||
public void testLoginWhenResponseIs2xxIncludesJSessionId() throws IOException {
|
public void testLoginWhenResponseIs2xxIncludesJSessionId() throws IOException {
|
||||||
String domain = "Partners/jCloud";
|
String domain = "Partners/jCloud";
|
||||||
String user = "jcloud";
|
String user = "jcloud";
|
||||||
String password = "jcl0ud";
|
String password = "jcl0ud";
|
||||||
String md5password = md5Hex(password);
|
String md5password = md5Hex(password);
|
||||||
|
|
||||||
HttpRequest request = HttpRequest.builder()
|
HttpRequest request = HttpRequest.builder()
|
||||||
.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="+user+"&password=" + md5password+ "&domain=" + URLEncoder.encode(domain)))
|
"username=" + user + "&password=" + md5password + "&domain=" + URLEncoder.encode(domain)))
|
||||||
.headers(
|
.headers(
|
||||||
ImmutableMultimap.<String, String>builder()
|
ImmutableMultimap.<String, String>builder()
|
||||||
.put("Accept", "application/json")
|
.put("Accept", "application/json")
|
||||||
.build())
|
.build())
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
String jSessionId = "90DD65D13AEAA590ECCA312D150B9F6D";
|
String jSessionId = "90DD65D13AEAA590ECCA312D150B9F6D";
|
||||||
SessionClient client = requestSendsResponse(request,
|
SessionClient client = requestSendsResponse(request,
|
||||||
HttpResponse.builder()
|
HttpResponse.builder()
|
||||||
.statusCode(200)
|
.statusCode(200)
|
||||||
.headers(
|
.headers(
|
||||||
ImmutableMultimap.<String, String>builder()
|
ImmutableMultimap.<String, String>builder()
|
||||||
.put("Set-Cookie", "JSESSIONID="+jSessionId+"; Path=/client")
|
.put("Set-Cookie", "JSESSIONID=" + jSessionId + "; Path=/client")
|
||||||
.build())
|
.build())
|
||||||
.payload(payloadFromResource("/loginresponse.json"))
|
.payload(payloadFromResource("/loginresponse.json"))
|
||||||
.build());
|
.build());
|
||||||
|
|
||||||
assertEquals(client.loginUserInDomainWithHashOfPassword(user, domain, md5password).toString(),
|
assertEquals(client.loginUserInDomainWithHashOfPassword(user, domain, md5password).toString(),
|
||||||
|
LoginResponse.builder().timeout(1800).lastName("Kiran").registered(false).username("jcloud").firstName("Vijay")
|
||||||
LoginResponse.builder().timeout(1800).lastName("Kiran").registered(false).username("jcloud").firstName("Vijay")
|
.domainId(11).accountType(Account.Type.DOMAIN_ADMIN).userId(19).sessionKey(
|
||||||
.domainId(11).accountType(Account.Type.DOMAIN_ADMIN).userId(19).sessionKey(
|
"uYT4/MNiglgAKiZRQkvV8QP8gn0=").jSessionId(jSessionId).accountName("jcloud").build().toString());
|
||||||
"uYT4/MNiglgAKiZRQkvV8QP8gn0=").jSessionId(jSessionId).accountName("jcloud").build().toString());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO: logout.
|
|
||||||
|
|
||||||
|
public void testLogout() throws IOException {
|
||||||
|
HttpRequest request = HttpRequest.builder()
|
||||||
|
.method("GET")
|
||||||
|
.endpoint(
|
||||||
|
URI.create("http://localhost:8080/client/api?response=json&command=logout&sessionkey=dummy-session-key"))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
SessionClient client = requestSendsResponse(request,
|
||||||
|
HttpResponse.builder()
|
||||||
|
.statusCode(200)
|
||||||
|
.payload(payloadFromResource("/logoutresponse.json"))
|
||||||
|
.build());
|
||||||
|
|
||||||
|
client.logoutUser("dummy-session-key");
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected SessionClient clientFrom(CloudStackContext context) {
|
protected SessionClient clientFrom(CloudStackContext context) {
|
||||||
|
|
|
@ -0,0 +1,91 @@
|
||||||
|
/**
|
||||||
|
* Licensed to jclouds, Inc. (jclouds) under one or more
|
||||||
|
* contributor license agreements. See the NOTICE file
|
||||||
|
* distributed with this work for additional information
|
||||||
|
* regarding copyright ownership. jclouds licenses this file
|
||||||
|
* to you under the Apache License, Version 2.0 (the
|
||||||
|
* "License"); you may not use this file except in compliance
|
||||||
|
* with the License. You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing,
|
||||||
|
* software distributed under the License is distributed on an
|
||||||
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
* KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*/
|
||||||
|
package org.jclouds.cloudstack.features;
|
||||||
|
|
||||||
|
import org.jclouds.cloudstack.domain.Account;
|
||||||
|
import org.jclouds.cloudstack.domain.ApiKeyPair;
|
||||||
|
import org.jclouds.cloudstack.domain.LoginResponse;
|
||||||
|
import org.jclouds.cloudstack.domain.User;
|
||||||
|
import org.jclouds.cloudstack.util.ApiKeyPairs;
|
||||||
|
import org.jclouds.crypto.CryptoStreams;
|
||||||
|
import org.jclouds.rest.AuthorizationException;
|
||||||
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
|
||||||
|
import static org.jclouds.cloudstack.features.GlobalAccountClientLiveTest.createTestAccount;
|
||||||
|
import static org.jclouds.cloudstack.features.GlobalUserClientLiveTest.createTestUser;
|
||||||
|
import static org.testng.Assert.assertEquals;
|
||||||
|
import static org.testng.Assert.assertNotNull;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests behavior of {@code SessionClient}
|
||||||
|
*
|
||||||
|
* @author Andrei Savu
|
||||||
|
*/
|
||||||
|
@Test(groups = "live", singleThreaded = true, testName = "SessionClientLiveTest")
|
||||||
|
public class SessionClientLiveTest extends BaseCloudStackClientLiveTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCreateContextUsingUserAndPasswordAuthentication() {
|
||||||
|
assert globalAdminEnabled;
|
||||||
|
|
||||||
|
Account testAccount = null;
|
||||||
|
User testUser = null;
|
||||||
|
String prefix = this.prefix + "-session";
|
||||||
|
try {
|
||||||
|
testAccount = createTestAccount(globalAdminClient, prefix);
|
||||||
|
testUser = createTestUser(globalAdminClient, testAccount, prefix);
|
||||||
|
|
||||||
|
String expectedUsername = prefix + "-user";
|
||||||
|
assertEquals(testUser.getName(), expectedUsername);
|
||||||
|
|
||||||
|
checkLoginAsTheNewUser(expectedUsername);
|
||||||
|
|
||||||
|
ApiKeyPair expected = globalAdminClient.getUserClient().registerUserKeys(testUser.getId());
|
||||||
|
ApiKeyPair actual = ApiKeyPairs.loginToEndpointAsUsernameInDomainWithPasswordAndReturnApiKeyPair(
|
||||||
|
URI.create(endpoint), prefix + "-user", "password", "");
|
||||||
|
|
||||||
|
assertEquals(actual, expected);
|
||||||
|
|
||||||
|
} finally {
|
||||||
|
if (testUser != null)
|
||||||
|
globalAdminClient.getUserClient().deleteUser(testUser.getId());
|
||||||
|
if (testAccount != null)
|
||||||
|
globalAdminClient.getAccountClient().deleteAccount(testAccount.getId());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expectedExceptions = AuthorizationException.class)
|
||||||
|
public void testTryToGetApiKeypairWithWrongCredentials() {
|
||||||
|
ApiKeyPairs.loginToEndpointAsUsernameInDomainWithPasswordAndReturnApiKeyPair(
|
||||||
|
URI.create(endpoint), "dummy-missing-user", "with-a-wrong-password", "");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void checkLoginAsTheNewUser(String expectedUsername) {
|
||||||
|
LoginResponse response = globalAdminClient.getSessionClient()
|
||||||
|
.loginUserInDomainWithHashOfPassword(expectedUsername, "", CryptoStreams.md5Hex("password"));
|
||||||
|
|
||||||
|
assertNotNull(response);
|
||||||
|
assertNotNull(response.getSessionKey());
|
||||||
|
assertNotNull(response.getJSessionId());
|
||||||
|
|
||||||
|
client.getSessionClient().logoutUser(response.getSessionKey());
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1 @@
|
||||||
|
{ "logoutresponse" : { "description" : "success" } }
|
Loading…
Reference in New Issue