allow use of tenantId when specified numeric

This commit is contained in:
Adrian Cole 2012-05-20 14:39:32 -07:00
parent a91a23f745
commit 94404841ca
8 changed files with 201 additions and 2 deletions

View File

@ -0,0 +1,76 @@
/**
* 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.openstack.nova.v1_1;
import static org.testng.Assert.assertEquals;
import java.net.URI;
import java.util.Properties;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse;
import org.jclouds.openstack.keystone.v2_0.config.KeystoneProperties;
import org.jclouds.openstack.nova.v1_1.internal.BaseNovaClientExpectTest;
import org.jclouds.openstack.nova.v1_1.parse.ParseServerListTest;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableSet;
/**
*
* @see KeystoneProperties#CREDENTIAL_TYPE
* @author Adrian Cole
*/
@Test(groups = "unit", testName = "AccessKeyAndSecretKeyAndTenantIdAuthenticationExpectTest")
public class AccessKeyAndSecretKeyAndTenantIdAuthenticationExpectTest extends BaseNovaClientExpectTest {
/**
* this reflects the properties that a user would pass to createContext
*/
@Override
protected Properties setupProperties() {
Properties contextProperties = super.setupProperties();
contextProperties.setProperty("jclouds.keystone.credential-type", "authenticateApiAccessKeyCredentials");
return contextProperties;
}
public void testListServersWhenResponseIs2xx() throws Exception {
HttpRequest listServers = HttpRequest
.builder()
.method("GET")
.endpoint(URI.create("https://az-1.region-a.geo-1.compute.hpcloudsvc.com/v1.1/3456/servers"))
.headers(
ImmutableMultimap.<String, String> builder().put("Accept", "application/json")
.put("X-Auth-Token", authToken).build()).build();
HttpResponse listServersResponse = HttpResponse.builder().statusCode(200)
.payload(payloadFromResource("/server_list.json")).build();
NovaClient clientWhenServersExist = requestsSendResponses(keystoneAuthWithAccessKeyAndSecretKeyAndTenantId,
responseWithKeystoneAccess, listServers, listServersResponse);
assertEquals(clientWhenServersExist.getConfiguredZones(), ImmutableSet.of("az-1.region-a.geo-1"));
assertEquals(clientWhenServersExist.getServerClientForZone("az-1.region-a.geo-1").listServers().toString(),
new ParseServerListTest().expected().toString());
}
}

View File

@ -0,0 +1,75 @@
/**
* 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.openstack.nova.v1_1;
import static org.testng.Assert.assertEquals;
import java.net.URI;
import java.util.Properties;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse;
import org.jclouds.openstack.keystone.v2_0.config.KeystoneProperties;
import org.jclouds.openstack.nova.v1_1.internal.BaseNovaClientExpectTest;
import org.jclouds.openstack.nova.v1_1.parse.ParseServerListTest;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableSet;
/**
*
* @see KeystoneProperties#CREDENTIAL_TYPE
* @author Adrian Cole
*/
@Test(groups = "unit", testName = "AccessKeyAndSecretKeyAuthenticationExpectTest")
public class AccessKeyAndSecretKeyAuthenticationExpectTest extends BaseNovaClientExpectTest {
/**
* this reflects the properties that a user would pass to createContext
*/
@Override
protected Properties setupProperties() {
Properties contextProperties = super.setupProperties();
contextProperties.setProperty("jclouds.keystone.credential-type", "authenticateApiAccessKeyCredentials");
return contextProperties;
}
public void testListServersWhenResponseIs2xx() throws Exception {
HttpRequest listServers = HttpRequest
.builder()
.method("GET")
.endpoint(URI.create("https://az-1.region-a.geo-1.compute.hpcloudsvc.com/v1.1/3456/servers"))
.headers(
ImmutableMultimap.<String, String> builder().put("Accept", "application/json")
.put("X-Auth-Token", authToken).build()).build();
HttpResponse listServersResponse = HttpResponse.builder().statusCode(200)
.payload(payloadFromResource("/server_list.json")).build();
NovaClient clientWhenServersExist = requestsSendResponses(keystoneAuthWithAccessKeyAndSecretKey,
responseWithKeystoneAccess, listServers, listServersResponse);
assertEquals(clientWhenServersExist.getConfiguredZones(), ImmutableSet.of("az-1.region-a.geo-1"));
assertEquals(clientWhenServersExist.getServerClientForZone("az-1.region-a.geo-1").listServers().toString(),
new ParseServerListTest().expected().toString());
}
}

View File

@ -43,6 +43,8 @@ public class BaseNovaExpectTest<T> extends BaseRestClientExpectTest<T> {
protected HttpRequest extensionsOfNovaRequest; protected HttpRequest extensionsOfNovaRequest;
protected HttpResponse extensionsOfNovaResponse; protected HttpResponse extensionsOfNovaResponse;
protected HttpResponse unmatchedExtensionsOfNovaResponse; protected HttpResponse unmatchedExtensionsOfNovaResponse;
protected HttpRequest keystoneAuthWithAccessKeyAndSecretKeyAndTenantId;
protected String identityWithTenantId;
public BaseNovaExpectTest() { public BaseNovaExpectTest() {
provider = "openstack-nova"; provider = "openstack-nova";
@ -50,10 +52,13 @@ public class BaseNovaExpectTest<T> extends BaseRestClientExpectTest<T> {
credential); credential);
keystoneAuthWithAccessKeyAndSecretKey = KeystoneFixture.INSTANCE.initialAuthWithAccessKeyAndSecretKey(identity, keystoneAuthWithAccessKeyAndSecretKey = KeystoneFixture.INSTANCE.initialAuthWithAccessKeyAndSecretKey(identity,
credential); credential);
keystoneAuthWithAccessKeyAndSecretKeyAndTenantId = KeystoneFixture.INSTANCE.initialAuthWithAccessKeyAndSecretKeyAndTenantId(identity,
credential);
authToken = KeystoneFixture.INSTANCE.getAuthToken(); authToken = KeystoneFixture.INSTANCE.getAuthToken();
responseWithKeystoneAccess = KeystoneFixture.INSTANCE.responseWithAccess(); responseWithKeystoneAccess = KeystoneFixture.INSTANCE.responseWithAccess();
// now, createContext arg will need tenant prefix // now, createContext arg will need tenant prefix
identityWithTenantId = KeystoneFixture.INSTANCE.getTenantId() + ":" + identity;
identity = KeystoneFixture.INSTANCE.getTenantName() + ":" + identity; identity = KeystoneFixture.INSTANCE.getTenantName() + ":" + identity;
extensionsOfNovaRequest = HttpRequest extensionsOfNovaRequest = HttpRequest

View File

@ -68,4 +68,15 @@ public interface AuthenticationAsyncClient {
// still use tenantName // still use tenantName
ListenableFuture<Access> authenticateTenantWithCredentials(@PayloadParam("tenantName") String tenantId, ListenableFuture<Access> authenticateTenantWithCredentials(@PayloadParam("tenantName") String tenantId,
ApiAccessKeyCredentials apiAccessKeyCredentials); ApiAccessKeyCredentials apiAccessKeyCredentials);
/**
* @see AuthenticationClient#authenticateTenantWithCredentials(String,ApiAccessKeyCredentials)
*/
@POST
@SelectJson("access")
@Consumes(MediaType.APPLICATION_JSON)
@Path("/tokens")
@MapBinder(BindAuthToJsonPayload.class)
ListenableFuture<Access> authenticateTenantWithTenantIdAndCredentials(@PayloadParam("tenantId") String tenantId,
ApiAccessKeyCredentials apiAccessKeyCredentials);
} }

View File

@ -50,4 +50,11 @@ public interface AuthenticationClient {
* @return access with token * @return access with token
*/ */
Access authenticateTenantWithCredentials(String tenantId, ApiAccessKeyCredentials passwordCredentials); Access authenticateTenantWithCredentials(String tenantId, ApiAccessKeyCredentials passwordCredentials);
/**
* Authenticate to generate a token.
*
* @return access with token
*/
Access authenticateTenantWithTenantIdAndCredentials(String tenantId, ApiAccessKeyCredentials passwordCredentials);
} }

View File

@ -79,6 +79,8 @@ public class BindAuthToJsonPayload extends BindToJsonPayload implements MapBinde
// still use tenantName // still use tenantName
if (!Strings.isNullOrEmpty((String) postParams.get("tenantName"))) if (!Strings.isNullOrEmpty((String) postParams.get("tenantName")))
builder.put("tenantName", postParams.get("tenantName")); builder.put("tenantName", postParams.get("tenantName"));
else if (!Strings.isNullOrEmpty((String) postParams.get("tenantId")))
builder.put("tenantId", postParams.get("tenantId"));
return super.bindToRequest(request, ImmutableMap.of("auth", builder.build())); return super.bindToRequest(request, ImmutableMap.of("auth", builder.build()));
} }

View File

@ -51,7 +51,13 @@ public class AuthenticateApiAccessKeyCredentials implements Function<Credentials
ApiAccessKeyCredentials apiAccessKeyCredentials = ApiAccessKeyCredentials.createWithAccessKeyAndSecretKey( ApiAccessKeyCredentials apiAccessKeyCredentials = ApiAccessKeyCredentials.createWithAccessKeyAndSecretKey(
usernameOrAccessKey, passwordOrSecretKey); usernameOrAccessKey, passwordOrSecretKey);
return client.authenticateTenantWithCredentials(tenantId, apiAccessKeyCredentials); Access access;
if(tenantId.matches("[0-9]*")) {
access = client.authenticateTenantWithTenantIdAndCredentials(tenantId, apiAccessKeyCredentials);
} else {
access = client.authenticateTenantWithCredentials(tenantId, apiAccessKeyCredentials);
}
return access;
} }
@Override @Override

View File

@ -40,9 +40,13 @@ import com.google.common.net.HttpHeaders;
public enum KeystoneFixture { public enum KeystoneFixture {
INSTANCE; INSTANCE;
public String getTenantName(){ public String getTenantId(){
return "12346637803162"; return "12346637803162";
} }
public String getTenantName(){
return "adrian@jclouds.org";
}
public HttpRequest initialAuthWithUsernameAndPassword(String username, String password){ public HttpRequest initialAuthWithUsernameAndPassword(String username, String password){
return HttpRequest return HttpRequest
@ -69,6 +73,19 @@ public enum KeystoneFixture {
"{\"auth\":{\"apiAccessKeyCredentials\":{\"accessKey\":\"%s\",\"secretKey\":\"%s\"},\"tenantName\":\"%s\"}}", "{\"auth\":{\"apiAccessKeyCredentials\":{\"accessKey\":\"%s\",\"secretKey\":\"%s\"},\"tenantName\":\"%s\"}}",
accessKey, secretKey, getTenantName()), "application/json")).build(); accessKey, secretKey, getTenantName()), "application/json")).build();
} }
public HttpRequest initialAuthWithAccessKeyAndSecretKeyAndTenantId(String accessKey, String secretKey){
return HttpRequest
.builder()
.method("POST")
.endpoint(URI.create("http://localhost:5000/v2.0/tokens"))
.headers(ImmutableMultimap.of(HttpHeaders.ACCEPT, "application/json"))
.payload(
payloadFromStringWithContentType(
format(
"{\"auth\":{\"apiAccessKeyCredentials\":{\"accessKey\":\"%s\",\"secretKey\":\"%s\"},\"tenantId\":\"%s\"}}",
accessKey, secretKey, getTenantId()), "application/json")).build();
}
public String getAuthToken(){ public String getAuthToken(){
return "Auth_4f173437e4b013bee56d1007"; return "Auth_4f173437e4b013bee56d1007";