Live test for user/password based authentication

This commit is contained in:
Andrei Savu 2012-02-01 17:56:53 +02:00
parent 4a071a7d26
commit 782872ac33
4 changed files with 133 additions and 13 deletions

View File

@ -18,29 +18,81 @@
*/
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.User;
import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.compute.ComputeServiceContextFactory;
import org.jclouds.crypto.CryptoStreams;
import org.jclouds.rest.RestContextFactory;
import java.net.URI;
import java.util.Properties;
import java.util.Set;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* @author Andrei Savu
*/
public class ApiKeyPairs {
private final static String PROVIDER = "cloudstack";
/**
* Retrieve the API key pair for a given CloudStack user
*
* @param endpoint
* CloudStack API endpoint (e.g. http://72.52.126.25/client/api/)
* @param userName
* User account name
* @param password
* User password
* @param domain
* Domain name. If empty defaults to ROOT
* @param endpoint CloudStack API endpoint (e.g. http://72.52.126.25/client/api/)
* @param username User account name
* @param password User password
* @param domain Domain name. If empty defaults to ROOT
* @return
*/
public static ApiKeyPair getApiKeyPairForUser(URI endpoint, String userName, String password, String domain) {
return null;
public static ApiKeyPair getApiKeyPairForUser(String 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();
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();
}
}
}
return null;
} finally {
if (context != null)
context.close();
}
}
private static Properties setupRestProperties() {
return RestContextFactory.getPropertiesFromResource("/rest.properties");
}
private static Properties setupProperties(String 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"));
overrides.put(PROVIDER + ".identity",
String.format("%s/%s", checkNotNull(domain, "domain"), checkNotNull(username, "username")));
overrides.put(PROVIDER + ".credential", CryptoStreams.md5Hex(checkNotNull(password, "password")));
return overrides;
}
}

View File

@ -20,6 +20,7 @@ package org.jclouds.cloudstack.features;
import org.jclouds.cloudstack.CloudStackGlobalClient;
import org.jclouds.cloudstack.domain.Account;
import org.jclouds.crypto.CryptoStreams;
import org.testng.annotations.Test;
import static org.testng.Assert.assertEquals;
@ -36,7 +37,7 @@ public class GlobalAccountClientLiveTest extends BaseCloudStackClientLiveTest {
public static Account createTestAccount(CloudStackGlobalClient client, String prefix) {
return client.getAccountClient().createAccount(
prefix + "-account", Account.Type.USER, "dummy@example.com",
"First", "Last", "hashed-password");
"First", "Last", CryptoStreams.md5Hex("password"));
}
@Test

View File

@ -27,6 +27,7 @@ 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.crypto.CryptoStreams;
import org.jclouds.logging.log4j.config.Log4JLoggingModule;
import org.jclouds.sshj.config.SshjSshClientModule;
import org.testng.annotations.Test;
@ -48,7 +49,7 @@ public class GlobalUserClientLiveTest extends BaseCloudStackClientLiveTest {
public static User createTestUser(CloudStackGlobalClient client, Account account, String prefix) {
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

View File

@ -0,0 +1,66 @@
/**
* 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.User;
import org.jclouds.cloudstack.util.ApiKeyPairs;
import org.jclouds.crypto.CryptoStreams;
import org.testng.annotations.Test;
import static org.jclouds.cloudstack.features.GlobalAccountClientLiveTest.createTestAccount;
import static org.jclouds.cloudstack.features.GlobalUserClientLiveTest.createTestUser;
import static org.testng.Assert.assertEquals;
/**
* 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);
assertEquals(
globalAdminClient.getUserClient().registerUserKeys(testUser.getId()),
ApiKeyPairs.getApiKeyPairForUser(System.getProperty("test.cloudstack.endpoint"),
prefix + "-user", CryptoStreams.md5Hex("password"), "/")
);
} finally {
if (testUser != null)
globalAdminClient.getUserClient().deleteUser(testUser.getId());
if (testAccount != null)
globalAdminClient.getAccountClient().deleteAccount(testAccount.getId());
}
}
}