From e88d11f6a313333a11fd8585e2e19cd2b269abb7 Mon Sep 17 00:00:00 2001 From: osmboy Date: Mon, 1 Nov 2021 17:54:54 +0800 Subject: [PATCH 1/2] support keystone V3 --- dev/cosbench-keystone/META-INF/MANIFEST.MF | 1 + .../cosbench/api/keystone/KeystoneAuth.java | 28 +- .../client/keystone/KeystoneClient.java | 186 +++-------- .../client/keystone/KeystoneConstants.java | 16 +- .../client/keystone/KeystoneMapper.java | 7 +- .../client/keystone/KeystoneRequest.java | 289 ++++++++++-------- .../client/keystone/KeystoneResponse.java | 279 ++++++----------- .../keystone/handler/HttpAuthHandler.java | 13 +- 8 files changed, 316 insertions(+), 503 deletions(-) diff --git a/dev/cosbench-keystone/META-INF/MANIFEST.MF b/dev/cosbench-keystone/META-INF/MANIFEST.MF index e91744ef..d956d2af 100644 --- a/dev/cosbench-keystone/META-INF/MANIFEST.MF +++ b/dev/cosbench-keystone/META-INF/MANIFEST.MF @@ -18,5 +18,6 @@ Import-Package: com.intel.cosbench.api.auth, org.apache.http.message;version="[4.1.4,5.0.0)", org.apache.http.util;version="[4.1.4,5.0.0)", org.codehaus.jackson;version="[1.4.2,2.0.0)", + org.codehaus.jackson.annotate;version="1.4.2", org.codehaus.jackson.map;version="[1.4.2,2.0.0)", org.codehaus.jackson.map.annotate;version="[1.4.2,2.0.0)" diff --git a/dev/cosbench-keystone/src/com/intel/cosbench/api/keystone/KeystoneAuth.java b/dev/cosbench-keystone/src/com/intel/cosbench/api/keystone/KeystoneAuth.java index 96f20389..aded634e 100644 --- a/dev/cosbench-keystone/src/com/intel/cosbench/api/keystone/KeystoneAuth.java +++ b/dev/cosbench-keystone/src/com/intel/cosbench/api/keystone/KeystoneAuth.java @@ -32,7 +32,7 @@ * This class encapsulates an Openstack Keystone implementation for the * Auth-API. * - * @author ywang19, qzheng7 + * @author ywang19, qzheng7, osmboy * */ class KeystoneAuth extends NoneAuth { @@ -43,22 +43,17 @@ class KeystoneAuth extends NoneAuth { private String url; private String username; private String password; - private String userToken; /* tenant info */ - private String tenantId; private String tenantName; - - /*keystone region*/ - private String region; + /* domain info */ + private String domain; /* service info */ private String service; /* connection setting */ private int timeout; - - Logger logger = null; public KeystoneAuth() { /* empty */ @@ -67,33 +62,28 @@ public KeystoneAuth() { @Override public void init(Config config, Logger logger) { super.init(config, logger); - this.logger = logger; + url = config.get(AUTH_URL_KEY, config.get(AUTH_URL_ALTKEY, URL_DEFAULT)); username = config.get(AUTH_USERNAME_KEY, AUTH_USERNAME_DEFAULT); password = config.get(AUTH_PASSWORD_KEY, AUTH_PASSWORD_DEFAULT); - userToken = config.get(AUTH_USERTOKEN_KEY, AUTH_USERTOKEN_DEFAULT); - tenantId = config.get(AUTH_TENANT_ID_KEY, AUTH_TENANT_ID_DEFAULT); tenantName = config.get(AUTH_TENANT_NAME_KEY, config.get(AUTH_TENANT_NAME_ALTKEY, AUTH_TENANT_NAME_DEFAULT)); + domain = config.get(AUTH_DOMAIN_KEY, AUTH_DOMAIN_DEFAULT); service = config.get(AUTH_SERVICE_KEY, AUTH_SERVICE_DEFAULT); timeout = config.getInt(CONN_TIMEOUT_KEY, CONN_TIMEOUT_DEFAULT); - region = config.get(AUTH_REGION_KEY, AUTH_REGION_DEFAULT); parms.put(AUTH_URL_KEY, url); parms.put(AUTH_USERNAME_KEY, username); parms.put(AUTH_PASSWORD_KEY, password); - parms.put(AUTH_USERTOKEN_KEY, userToken); - parms.put(AUTH_TENANT_ID_KEY, tenantId); + parms.put(AUTH_DOMAIN_KEY, domain); parms.put(AUTH_TENANT_NAME_KEY, tenantName); parms.put(AUTH_SERVICE_KEY, service); parms.put(CONN_TIMEOUT_KEY, timeout); - parms.put(AUTH_REGION_KEY, AUTH_REGION_DEFAULT); - logger.debug("using auth config: {}", parms); HttpClient httpClient = HttpClientUtil.createHttpClient(timeout); - client = new KeystoneClient(logger, httpClient, url, username, password, - tenantName, timeout); + client = new KeystoneClient(httpClient, url, username, password, + tenantName, domain, timeout); logger.debug("keystone client has been initialized"); } @@ -125,7 +115,7 @@ private AuthContext createContext() { // context.put(AUTH_TOKEN_KEY, client.getKeystoneTokenId()); // context.put(STORAGE_URL_KEY, client.getServiceUrl(service)); // return context; - KeystoneAuthContext context = new KeystoneAuthContext(url, username, password, service, client.getKeystoneTokenId(), client.getServiceUrl(service,region)); + KeystoneAuthContext context = new KeystoneAuthContext(url, username, password, service, client.getKeystoneTokenId(), client.getServiceUrl(service)); return context; } diff --git a/dev/cosbench-keystone/src/com/intel/cosbench/client/keystone/KeystoneClient.java b/dev/cosbench-keystone/src/com/intel/cosbench/client/keystone/KeystoneClient.java index 5983cb14..9d2e5fce 100644 --- a/dev/cosbench-keystone/src/com/intel/cosbench/client/keystone/KeystoneClient.java +++ b/dev/cosbench-keystone/src/com/intel/cosbench/client/keystone/KeystoneClient.java @@ -17,18 +17,15 @@ package com.intel.cosbench.client.keystone; -import java.util.ArrayList; import java.util.List; import org.apache.http.client.HttpClient; -import com.intel.cosbench.client.keystone.KeystoneResponse.AccessInfo; -import com.intel.cosbench.client.keystone.KeystoneResponse.AccessInfo.ServiceInfo; -import com.intel.cosbench.client.keystone.KeystoneResponse.AccessInfo.ServiceInfo.Endpoint; -import com.intel.cosbench.client.keystone.KeystoneResponse.AccessInfo.Token; -import com.intel.cosbench.client.keystone.KeystoneResponse.AccessInfo.User; +import com.intel.cosbench.client.keystone.KeystoneResponse.ServiceInfo; +import com.intel.cosbench.client.keystone.KeystoneResponse.ServiceInfo.Endpoint; +import com.intel.cosbench.client.keystone.KeystoneResponse.TokenInfo; +import com.intel.cosbench.client.keystone.KeystoneResponse.User; import com.intel.cosbench.client.keystone.handler.*; -import com.intel.cosbench.log.Logger; /** * A client for Openstack keystone authentication service.
@@ -55,36 +52,33 @@ * order to get a new token that will be scoped with the specified tenant. *

* - * @author ywang19 - * @author qzheng + * + * @author qzheng (qing.zheng@intel.com), osmboy (lei.lei@ostorage.com.cn) */ public class KeystoneClient { - - private Logger logger; - + /* user info */ private String username; private String password; - private String userToken; - + /* tenant info */ - private String tenantId; private String tenantName; - /*targe region*/ - private String region; + /* domain info */ + private String domain; + /* authentication handler */ private AuthHandler handler; /* authentication response */ private KeystoneResponse response; - public KeystoneClient(Logger logger, HttpClient client, String url, String username, - String password, String tenantName, int timeout) { - this.logger = logger; + public KeystoneClient(HttpClient client, String url, String username, + String password, String tenantName, String domain, int timeout) { this.username = username; this.password = password; this.tenantName = tenantName; + this.domain = domain; this.handler = new HttpAuthHandler(url, timeout); } @@ -111,25 +105,24 @@ public void login() { private KeystoneRequest initRequest() { KeystoneRequest request = new KeystoneRequest(); /* user info */ - if (this.username != null && this.password != null) { - request.addCredentials(this.username, this.password); - } else if (this.userToken != null) { - request.addUserToken(this.userToken); + if (this.username != null && this.password != null && this.domain != null) { + request.addUserScope(this.username, this.password, this.domain); } else { String e = "no user info is detected in this keystone client"; throw new IllegalStateException(e); } /* tenant info */ - if (this.tenantId != null) { - request.addTenantId(this.tenantId); - } else if (this.tenantName != null) { - request.addTenantName(this.tenantName); + if (this.tenantName != null && this.domain != null) { + request.addProjectScope(this.tenantName, this.domain); + } else { + String e = "no project info is detected in keystone scope"; + throw new IllegalStateException(e); } return request; } private void validateResponse(KeystoneResponse response) { - AccessInfo info = response.getAccess(); + TokenInfo info = response.getToken(); if (info == null) { String e = "no access info is found in the auth response"; throw new KeystoneResponseException(e); @@ -139,12 +132,7 @@ private void validateResponse(KeystoneResponse response) { String e = "no user info is found in the auth response"; throw new KeystoneResponseException(e); } - Token token = info.getToken(); - if (token == null) { - String e = "no token info is found in the auth response"; - throw new KeystoneResponseException(e); - } - List catalog = info.getServiceCatalog(); + List catalog = info.getCatalog(); if (catalog == null) { String e = "no service catalog is found in the auth response"; throw new KeystoneResponseException(e); @@ -165,16 +153,6 @@ public String getUsername() { } public void setUsername(String username) { - if (username != null) { - if (this.userToken != null) { - String e = "cannot set username and userToken simultaneously"; - throw new IllegalStateException(e); - } - if (username.isEmpty()) { - String e = "username cannot be empty"; - throw new IllegalArgumentException(e); - } - } this.username = username; } @@ -183,74 +161,23 @@ public String getPassword() { } public void setPassword(String password) { - if (password != null) { - if (this.userToken != null) { - String e = "cannot set password and userToken simultaneously"; - throw new IllegalStateException(e); - } - if (password.isEmpty()) { - String e = "password cannot be empty"; - throw new IllegalArgumentException(e); - } - } this.password = password; } - public String getUserToken() { - return userToken; + public String getDomain() { + return domain; } - public void setUserToken(String userToken) { - if (userToken != null) { - if (this.username != null) { - String e = "cannot set usernmae and userToken simultaneously"; - throw new IllegalStateException(e); - } - if (this.password != null) { - String e = "cannot set password and userToken simultaneously"; - throw new IllegalStateException(e); - } - if (userToken.isEmpty()) { - String e = "userToken cannot be empty"; - throw new IllegalArgumentException(e); - } - } - this.userToken = userToken; + public void setDomain(String domain) { + + this.domain = domain; } - - public String getTenantId() { - return tenantId; - } - - public void setTenantId(String tenantId) { - if (tenantId != null) { - if (this.tenantName != null) { - String e = "cannot set tenant id and name simultaneously"; - throw new IllegalStateException(e); - } - if (tenantId.isEmpty()) { - String e = "tenant id cannot be empty"; - throw new IllegalArgumentException(e); - } - } - this.tenantId = tenantId; - } - + public String getTenantName() { return tenantName; } public void setTenantName(String tenantName) { - if (tenantName != null) { - if (this.tenantId != null) { - String e = "cannot set tenant id and name simultaneously"; - throw new IllegalStateException(e); - } - if (tenantName.isEmpty()) { - String e = "tenant name cannot be empty"; - throw new IllegalArgumentException(e); - } - } this.tenantName = tenantName; } @@ -281,7 +208,7 @@ public void setHandler(AuthHandler handler) { * @return the keystone token id */ public String getKeystoneTokenId() { - return getToken().getId(); + return getTokenInfo().getId(); } /** @@ -294,39 +221,14 @@ public String getKeystoneTokenId() { * - the name identifying the service * @return the public URL of a cloud service */ - public String getServiceUrl(String serviceName, String region) { + public String getServiceUrl(String serviceName) { ServiceInfo service = getServiceInfo(serviceName); if (service == null) return null; List endpoints = service.getEndpoints(); - - if (endpoints == null || endpoints.size() == 0) - { - logger.error("no endpoints return from keystone"); - return null; - } - - List regions = new ArrayList(); - for (Endpoint endpoint : endpoints) { - String the_region = endpoint.getRegion(); - if(the_region != null) { - regions.add(the_region); - } - } - - if (region == null || region.isEmpty()) { // no region assigned, will use the first one. - - logger.warn("Below regions are returned from keystone : " + regions.toString() + - ", but no expected region assigned in your configuration, so the first region will be used."); - return endpoints.get(0).getPublicURL(); - } - - int idx = -1; - if((idx=regions.indexOf(region)) >= 0) { - return endpoints.get(idx).getPublicURL(); - } - - return null; + if (endpoints != null && endpoints.size() > 0) + return endpoints.get(0).getUrl(); + return null; } /** @@ -340,31 +242,21 @@ public String getServiceUrl(String serviceName, String region) { * @return the information regarding a cloud service */ public ServiceInfo getServiceInfo(String serviceName) { - List catalog = getAccessInfo().getServiceCatalog(); + List catalog = getTokenInfo().getCatalog(); for (ServiceInfo service : catalog) if (serviceName != null ? serviceName.equals(service.getName()) : service.getName() == null) return service; - - List services = new ArrayList(); - for (ServiceInfo service : catalog) - services.add(service.getName()); - - logger.error("no designated service [" + serviceName + "] found, but only those services returned: " + services.toString()); - + return null; } public User getUser() { - return getAccessInfo().getUser(); - } - - public Token getToken() { - return getAccessInfo().getToken(); + return getTokenInfo().getUser(); } - private AccessInfo getAccessInfo() { - return getResponse().getAccess(); + private TokenInfo getTokenInfo() { + return getResponse().getToken(); } private KeystoneResponse getResponse() { diff --git a/dev/cosbench-keystone/src/com/intel/cosbench/client/keystone/KeystoneConstants.java b/dev/cosbench-keystone/src/com/intel/cosbench/client/keystone/KeystoneConstants.java index 44e0632c..e7def30f 100644 --- a/dev/cosbench-keystone/src/com/intel/cosbench/client/keystone/KeystoneConstants.java +++ b/dev/cosbench-keystone/src/com/intel/cosbench/client/keystone/KeystoneConstants.java @@ -24,7 +24,7 @@ public interface KeystoneConstants { // -------------------------------------------------------------------------- String AUTH_URL_KEY = "auth_url"; String AUTH_URL_ALTKEY = "url"; - String URL_DEFAULT = "http://127.0.0.1:5000/v2.0"; + String URL_DEFAULT = "http://127.0.0.1:5000/v3"; // -------------------------------------------------------------------------- // CONNECTION @@ -46,20 +46,14 @@ public interface KeystoneConstants { String AUTH_PASSWORD_KEY = "password"; String AUTH_PASSWORD_DEFAULT = ""; - - String AUTH_USERTOKEN_KEY = "usertoken"; - String AUTH_USERTOKEN_DEFAULT = ""; - - String AUTH_TENANT_ID_KEY = "tenant_id"; - String AUTH_TENANT_ID_DEFAULT = ""; - + + String AUTH_DOMAIN_KEY = "domain"; + String AUTH_DOMAIN_DEFAULT = ""; + String AUTH_TENANT_NAME_KEY = "tenant_name"; String AUTH_TENANT_NAME_ALTKEY = "tenname"; String AUTH_TENANT_NAME_DEFAULT = ""; String AUTH_SERVICE_KEY = "service"; String AUTH_SERVICE_DEFAULT = "swift"; - - String AUTH_REGION_KEY = "region"; - String AUTH_REGION_DEFAULT = "regionOne"; } diff --git a/dev/cosbench-keystone/src/com/intel/cosbench/client/keystone/KeystoneMapper.java b/dev/cosbench-keystone/src/com/intel/cosbench/client/keystone/KeystoneMapper.java index 1e4920af..9c9d6294 100644 --- a/dev/cosbench-keystone/src/com/intel/cosbench/client/keystone/KeystoneMapper.java +++ b/dev/cosbench-keystone/src/com/intel/cosbench/client/keystone/KeystoneMapper.java @@ -13,6 +13,7 @@ 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. +@author osmboy (lei.lei@ostorage.com.cn) */ package com.intel.cosbench.client.keystone; @@ -20,6 +21,7 @@ import org.codehaus.jackson.map.*; import org.codehaus.jackson.map.DeserializationConfig.Feature; import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion; +import com.intel.cosbench.client.keystone.KeystoneResponse; public class KeystoneMapper { @@ -55,14 +57,15 @@ public String toJson(Object obj) { return json; } - public T fromJson(String json, Class clazz) { - T value = null; + public KeystoneResponse fromJson(String json, Class clazz, String token) { + KeystoneResponse value; try { value = mapper.readValue(json, clazz); } catch (Exception ex) { String e = "fail to generate any obj from the given json string"; throw new KeystoneClientException(e, ex); // should never happen } + value.getToken().setId(token); return value; } diff --git a/dev/cosbench-keystone/src/com/intel/cosbench/client/keystone/KeystoneRequest.java b/dev/cosbench-keystone/src/com/intel/cosbench/client/keystone/KeystoneRequest.java index 9f4d6525..360aea25 100644 --- a/dev/cosbench-keystone/src/com/intel/cosbench/client/keystone/KeystoneRequest.java +++ b/dev/cosbench-keystone/src/com/intel/cosbench/client/keystone/KeystoneRequest.java @@ -13,28 +13,16 @@ 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. +@author osmboy (lei.lei@ostorage.com.cn) */ package com.intel.cosbench.client.keystone; -import com.intel.cosbench.client.keystone.KeystoneRequest.AuthInfo.Credentials; -import com.intel.cosbench.client.keystone.KeystoneRequest.AuthInfo.Token; - -/** - * The request that will be used when obtaining a keystone token from the - * keystone service. It contains information including the username, password, - * tenant name, tenant id and user token. Note that not all information is - * required for a request to be accepted. Some are even conflicting with others. - * This class is specially structured in a way that is compatible with the - * interface provided by the keystone service. Please refer to the keystone - * documents for more detailed information.
- * - * - * {"auth": ... } - * - * - * @author qzheng - */ +import java.util.Collections; +import java.util.List; + +import com.intel.cosbench.client.keystone.KeystoneRequest.AuthInfo.*;; + public class KeystoneRequest { private AuthInfo auth; @@ -51,26 +39,34 @@ public AuthInfo getAuth() { public void setAuth(AuthInfo auth) { this.auth = auth; } - - public void addCredentials(String username, String password) { - Credentials credentials = new Credentials(); - credentials.setUsername(username); - credentials.setPassword(password); - auth.setPasswordCredentials(credentials); - } - - public void addUserToken(String id) { - Token token = new Token(); - token.setId(id); - auth.setToken(token); - } - - public void addTenantId(String tenantId) { - auth.setTenantId(tenantId); + + public void addProjectScope(String tenantName, String id) { + // TODO Auto-generated method stub + Scope scope = new Scope(); + Project project = new Project(); + Domain domain = new Domain(); + domain.setId(id); + project.setDomain(domain); + project.setName(tenantName); + scope.setProject(project); + auth.setScope(scope); } - public void addTenantName(String tenantName) { - auth.setTenantName(tenantName); + public void addUserScope(String username, String password, String id) { + // TODO Auto-generated method stub + Identity identity = new Identity(); + Password pwd = new Password(); + User user = new User(); + Domain domain = new Domain(); + domain.setId(id); + user.setDomain(domain); + user.setName(username); + user.setPassword(password); + pwd.setUser(user); + identity.setPassword(pwd); + identity.setMethods(Collections.singletonList("password")); + auth.setIdentity(identity); + } // -------------------------------------------------------------------------- @@ -83,122 +79,159 @@ public void addTenantName(String tenantName) { * compatible with the interface provided by the keystone service.
* * - * {"passwordCredentials": ... , "token": ... , - * "tenantId": "?", "tenantName": "?"} + * { "auth": { + "identity": { + "methods": ["password"], + "password": { + "user": { + "name": "admin", + "domain": { "id": "default" }, + "password": "adminpwd" + } + } + }, + "scope": { + "project": { + "name": "demo", + "domain": { "id": "default" } + } + } + } + } * * * @author qzheng */ public static class AuthInfo { - - private Credentials passwordCredentials; - private Token token; - private String tenantId; - private String tenantName; - - public Credentials getPasswordCredentials() { - return passwordCredentials; + + private Identity identity; + private Scope scope; + + public Identity getIdentity() { + return identity; + } + + public void setIdentity(Identity identity) { + this.identity = identity; + } + + public Scope getScope() { + return scope; + } + + public void setScope(Scope scope) { + this.scope = scope; + } + + public static class Identity { + private List methods; + private Password password; + + public Password getPassword() { + return password; + } + + public void setPassword(Password password) { + this.password = password; + } + + public List getMethods() { + return methods; + } + + public void setMethods(List methods) { + this.methods = methods; + } + + } + + public static class Scope { + private Project project; + + public Project getProject() { + return project; + } + + public void setProject(Project project) { + this.project = project; + } } - - public void setPasswordCredentials(Credentials passwordCredentials) { - this.passwordCredentials = passwordCredentials; + + public static class Project { + private String name; + private Domain domain; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Domain getDomain() { + return domain; + } + + public void setDomain(Domain domain) { + this.domain = domain; + } } - - public Token getToken() { - return token; - } - - public void setToken(Token token) { - this.token = token; - } - - public String getTenantId() { - return tenantId; - } - - public void setTenantId(String tenantId) { - this.tenantId = tenantId; + + public static class Password { + private User user; + + public User getUser() { + return user; + } + + public void setUser(User user) { + this.user = user; + } + } + public static class Domain { + private String id; - public String getTenantName() { - return tenantName; - } + public String getId() { + return id; + } - public void setTenantName(String tenantName) { - this.tenantName = tenantName; + public void setId(String id) { + this.id = id; + } } + public static class User { - // ---------------------------------------------------------------------- - // AuthInfo.Credentials - // ---------------------------------------------------------------------- - - /** - * The credentials comprised of both the username and the password. This - * class is specially structured in a way that is compatible with the - * interface provided by the keystone service.
- * - * - * {"username": "?", "password": "?"} - * - * - * @author qzheng - */ - public static class Credentials { - - private String username; + private String name; + private Domain domain; private String password; - public String getUsername() { - return username; + public String getName() { + return name; } - public void setUsername(String username) { - this.username = username; + public void setName(String name) { + this.name = name; } public String getPassword() { return password; - } - - public void setPassword(String password) { - this.password = password; - } - - } - - // ---------------------------------------------------------------------- - // AuthInfo.Token - // ---------------------------------------------------------------------- - - /** - * The token meta data identified by its id. This class is specially - * structured in a way that is compatible with the interface provided by - * the keystone service.
- * - * - * {"id": "?"} - * - * - * @author qzheng - */ - public static class Token { - - private String id; + } + + public Domain getDomain() { + return domain; + } - public String getId() { - return id; - } + public void setDomain(Domain domain) { + this.domain = domain; + } - public void setId(String id) { - this.id = id; + public void setPassword(String password) { + this.password = password; } - } - + } } - // -------------------------------------------------------------------------- - // End - // -------------------------------------------------------------------------- - } diff --git a/dev/cosbench-keystone/src/com/intel/cosbench/client/keystone/KeystoneResponse.java b/dev/cosbench-keystone/src/com/intel/cosbench/client/keystone/KeystoneResponse.java index 958a85db..5be88417 100644 --- a/dev/cosbench-keystone/src/com/intel/cosbench/client/keystone/KeystoneResponse.java +++ b/dev/cosbench-keystone/src/com/intel/cosbench/client/keystone/KeystoneResponse.java @@ -13,229 +13,128 @@ 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. +@author osmboy (lei.lei@ostorage.com.cn) */ package com.intel.cosbench.client.keystone; import java.util.List; -/** - * - * { "access": ... } - * - */ + public class KeystoneResponse { - private AccessInfo access; + private TokenInfo token; - public AccessInfo getAccess() { - return access; + public TokenInfo getToken() { + return token; } - public void setAccess(AccessInfo access) { - this.access = access; + public void setToken(TokenInfo token) { + this.token = token; } - // -------------------------------------------------------------------------- - // AccessInfo - // -------------------------------------------------------------------------- - - /** - * - * { "user": ... , "token": ... , "serviceCatalog": [ ... ] } - * - */ - public static class AccessInfo { + public static class TokenInfo { + private String id; + private String name; private User user; - private Token token; - private List serviceCatalog; + private List catalog; + + public String getId() { + return id; + } + public void setId(String id) { + this.id = id; + } + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public List getCatalog() { + return catalog; + } + public void setCatalog(List catalog) { + this.catalog = catalog; + } + public User getUser() { + return user; + } + public void setUser(User user) { + this.user = user; + } - public User getUser() { - return user; - } - public void setUser(User user) { - this.user = user; - } + } + + public static class User { + private String id; + private String name; + public String getId() { + return id; + } + public void setId(String id) { + this.id = id; + } + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + + } + + public static class ServiceInfo { - public Token getToken() { - return token; - } + private String name; + private String type; + private List endpoints; - public void setToken(Token token) { - this.token = token; + public String getName() { + return name; } - public List getServiceCatalog() { - return serviceCatalog; + public void setName(String name) { + this.name = name; } - public void setServiceCatalog(List serviceCatalog) { - this.serviceCatalog = serviceCatalog; + public String getType() { + return type; } - // ---------------------------------------------------------------------- - // AccessInfo.User - // ---------------------------------------------------------------------- - - /** - * - * { "id": "?", "name": "?" } - * - */ - public static class User { - - private String id; - private String name; - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - + public void setType(String type) { + this.type = type; } - // ---------------------------------------------------------------------- - // AccessInfo.Token - // ---------------------------------------------------------------------- - - /** - * - * { "id": "?", "expires": "?" } - * - */ - public static class Token { - - private String id; - private String expires; - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public String getExpires() { - return expires; - } - - public void setExpires(String expires) { - this.expires = expires; - } + public List getEndpoints() { + return endpoints; + } + public void setEndpoints(List endpoints) { + this.endpoints = endpoints; } + + public static class Endpoint { + private String url; + private String region; + public String getUrl() { + return url; + } + public void setUrl(String url) { + this.url = url; + } + public String getRegion() { + return region; + } + public void setRegion(String region) { + this.region = region; + } - // ---------------------------------------------------------------------- - // AccessInfo.ServiceInfo - // ---------------------------------------------------------------------- - - /** - * - * {"name": "?", "type": "?", "endpoints": [ ... ] } - * - */ - public static class ServiceInfo { - - private String name; - private String type; - private List endpoints; - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getType() { - return type; - } - - public void setType(String type) { - this.type = type; - } - - public List getEndpoints() { - return endpoints; - } - - public void setEndpoints(List endpoints) { - this.endpoints = endpoints; - } - - // ------------------------------------------------------------------ - // AccessInfo.ServiceInfo.Endpoint - // ------------------------------------------------------------------ - - /** - * - * { "adminURL": "?", "internalURL": "?", "publicURL": "?" } - * - */ - public static class Endpoint { - - private String adminURL; - private String internalURL; - private String publicURL; - private String region; - - public String getAdminURL() { - return adminURL; - } - - public void setAdminURL(String adminURL) { - this.adminURL = adminURL; - } - - public String getInternalURL() { - return internalURL; - } - - public void setInternalURL(String internalURL) { - this.internalURL = internalURL; - } - - public String getPublicURL() { - return publicURL; - } - - public void setPublicURL(String publicURL) { - this.publicURL = publicURL; - } - - public String getRegion() { - return region; - } - - public void setRegion(String region) { - this.region = region; - } - - - } - } } - // -------------------------------------------------------------------------- - // End - // -------------------------------------------------------------------------- - } diff --git a/dev/cosbench-keystone/src/com/intel/cosbench/client/keystone/handler/HttpAuthHandler.java b/dev/cosbench-keystone/src/com/intel/cosbench/client/keystone/handler/HttpAuthHandler.java index b69b9490..8c359c08 100644 --- a/dev/cosbench-keystone/src/com/intel/cosbench/client/keystone/handler/HttpAuthHandler.java +++ b/dev/cosbench-keystone/src/com/intel/cosbench/client/keystone/handler/HttpAuthHandler.java @@ -12,14 +12,14 @@ 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. +limitations under the License. +@author osmboy (lei.lei@ostorage.com.cn) */ package com.intel.cosbench.client.keystone.handler; import java.io.*; import java.net.SocketTimeoutException; - import org.apache.http.*; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; @@ -32,7 +32,7 @@ public class HttpAuthHandler implements AuthHandler { - private static final String PATH = "/tokens"; + private static final String PATH = "/auth/tokens"; private HttpClient client; private String url; @@ -64,7 +64,8 @@ public KeystoneResponse POST(KeystoneRequest request) { String e = "error receiving response from the keystone"; throw new KeystoneServerException(e, ex); } - return parseResponse(response.getStatusLine(), response.getEntity()); + String token = response.getFirstHeader("X-Subject-Token").getValue(); + return parseResponse(response.getStatusLine(), response.getEntity(), token); } private void prepareRequest(HttpPost method, KeystoneRequest request) { @@ -80,7 +81,7 @@ private void prepareRequest(HttpPost method, KeystoneRequest request) { method.addHeader("Content-Type", "application/json"); // hard coded } - private KeystoneResponse parseResponse(StatusLine status, HttpEntity entity) { + private KeystoneResponse parseResponse(StatusLine status, HttpEntity entity, String token) { String json = null; int code = status.getStatusCode(); try { @@ -96,7 +97,7 @@ private KeystoneResponse parseResponse(StatusLine status, HttpEntity entity) { } finally { clearResponse(entity); } - return mapper.fromJson(json, KeystoneResponse.class); + return mapper.fromJson(json, KeystoneResponse.class, token); } private void clearResponse(HttpEntity entity) { From 2e6573ee467392ff1d16d33ae22b8e3a9f640005 Mon Sep 17 00:00:00 2001 From: Digvijay Singh Date: Mon, 22 Aug 2022 22:14:49 +0530 Subject: [PATCH 2/2] Added Support for keystone V3 and different user_domain and project_domain ids --- .../cosbench/api/keystone/KeystoneAuth.java | 14 +++++--- .../client/keystone/KeystoneClient.java | 35 +++++++++++++------ .../client/keystone/KeystoneConstants.java | 7 ++-- 3 files changed, 38 insertions(+), 18 deletions(-) diff --git a/dev/cosbench-keystone/src/com/intel/cosbench/api/keystone/KeystoneAuth.java b/dev/cosbench-keystone/src/com/intel/cosbench/api/keystone/KeystoneAuth.java index aded634e..bba41e3c 100644 --- a/dev/cosbench-keystone/src/com/intel/cosbench/api/keystone/KeystoneAuth.java +++ b/dev/cosbench-keystone/src/com/intel/cosbench/api/keystone/KeystoneAuth.java @@ -32,7 +32,7 @@ * This class encapsulates an Openstack Keystone implementation for the * Auth-API. * - * @author ywang19, qzheng7, osmboy + * @author ywang19, qzheng7, osmboy, digvijay2040 * */ class KeystoneAuth extends NoneAuth { @@ -48,7 +48,8 @@ class KeystoneAuth extends NoneAuth { private String tenantName; /* domain info */ - private String domain; + private String userdomain; + private String projectdomain; /* service info */ private String service; @@ -67,14 +68,17 @@ public void init(Config config, Logger logger) { username = config.get(AUTH_USERNAME_KEY, AUTH_USERNAME_DEFAULT); password = config.get(AUTH_PASSWORD_KEY, AUTH_PASSWORD_DEFAULT); tenantName = config.get(AUTH_TENANT_NAME_KEY, config.get(AUTH_TENANT_NAME_ALTKEY, AUTH_TENANT_NAME_DEFAULT)); - domain = config.get(AUTH_DOMAIN_KEY, AUTH_DOMAIN_DEFAULT); + userdomain = config.get(AUTH_USER_DOMAIN_KEY, AUTH_USER_DOMAIN_DEFAULT); + projectdomain = config.get(AUTH_PROJECT_DOMAIN_KEY, AUTH_PROJECT_DOMAIN_DEFAULT); service = config.get(AUTH_SERVICE_KEY, AUTH_SERVICE_DEFAULT); timeout = config.getInt(CONN_TIMEOUT_KEY, CONN_TIMEOUT_DEFAULT); parms.put(AUTH_URL_KEY, url); parms.put(AUTH_USERNAME_KEY, username); parms.put(AUTH_PASSWORD_KEY, password); - parms.put(AUTH_DOMAIN_KEY, domain); + parms.put(AUTH_USER_DOMAIN_KEY, userdomain); + parms.put(AUTH_PROJECT_DOMAIN_KEY, projectdomain); + parms.put(AUTH_TENANT_NAME_KEY, tenantName); parms.put(AUTH_SERVICE_KEY, service); parms.put(CONN_TIMEOUT_KEY, timeout); @@ -83,7 +87,7 @@ public void init(Config config, Logger logger) { HttpClient httpClient = HttpClientUtil.createHttpClient(timeout); client = new KeystoneClient(httpClient, url, username, password, - tenantName, domain, timeout); + tenantName, userdomain,projectdomain, timeout); logger.debug("keystone client has been initialized"); } diff --git a/dev/cosbench-keystone/src/com/intel/cosbench/client/keystone/KeystoneClient.java b/dev/cosbench-keystone/src/com/intel/cosbench/client/keystone/KeystoneClient.java index 9d2e5fce..ad3bf6c1 100644 --- a/dev/cosbench-keystone/src/com/intel/cosbench/client/keystone/KeystoneClient.java +++ b/dev/cosbench-keystone/src/com/intel/cosbench/client/keystone/KeystoneClient.java @@ -65,7 +65,10 @@ public class KeystoneClient { private String tenantName; /* domain info */ - private String domain; + private String userdomain; + + /* domain info */ + private String projectdomain; /* authentication handler */ private AuthHandler handler; @@ -74,11 +77,12 @@ public class KeystoneClient { private KeystoneResponse response; public KeystoneClient(HttpClient client, String url, String username, - String password, String tenantName, String domain, int timeout) { + String password, String tenantName, String userdomain,String projectdomain, int timeout) { this.username = username; this.password = password; this.tenantName = tenantName; - this.domain = domain; + this.userdomain = userdomain; + this.projectdomain = projectdomain; this.handler = new HttpAuthHandler(url, timeout); } @@ -105,15 +109,15 @@ public void login() { private KeystoneRequest initRequest() { KeystoneRequest request = new KeystoneRequest(); /* user info */ - if (this.username != null && this.password != null && this.domain != null) { - request.addUserScope(this.username, this.password, this.domain); + if (this.username != null && this.password != null && this.userdomain != null) { + request.addUserScope(this.username, this.password, this.userdomain); } else { String e = "no user info is detected in this keystone client"; throw new IllegalStateException(e); } /* tenant info */ - if (this.tenantName != null && this.domain != null) { - request.addProjectScope(this.tenantName, this.domain); + if (this.tenantName != null && this.projectdomain != null) { + request.addProjectScope(this.tenantName, this.projectdomain); } else { String e = "no project info is detected in keystone scope"; throw new IllegalStateException(e); @@ -164,13 +168,22 @@ public void setPassword(String password) { this.password = password; } - public String getDomain() { - return domain; + public String getUserDomain() { + return userdomain; + } + + public void setUserDomain(String domain) { + + this.userdomain = domain; + } + + public String getProjectDomain() { + return projectdomain; } - public void setDomain(String domain) { + public void setProjectDomain(String domain) { - this.domain = domain; + this.projectdomain = domain; } public String getTenantName() { diff --git a/dev/cosbench-keystone/src/com/intel/cosbench/client/keystone/KeystoneConstants.java b/dev/cosbench-keystone/src/com/intel/cosbench/client/keystone/KeystoneConstants.java index e7def30f..4e7e50ff 100644 --- a/dev/cosbench-keystone/src/com/intel/cosbench/client/keystone/KeystoneConstants.java +++ b/dev/cosbench-keystone/src/com/intel/cosbench/client/keystone/KeystoneConstants.java @@ -47,8 +47,11 @@ public interface KeystoneConstants { String AUTH_PASSWORD_KEY = "password"; String AUTH_PASSWORD_DEFAULT = ""; - String AUTH_DOMAIN_KEY = "domain"; - String AUTH_DOMAIN_DEFAULT = ""; + String AUTH_USER_DOMAIN_KEY = "user_domain"; + String AUTH_USER_DOMAIN_DEFAULT = ""; + + String AUTH_PROJECT_DOMAIN_KEY = "project_domain"; + String AUTH_PROJECT_DOMAIN_DEFAULT = ""; String AUTH_TENANT_NAME_KEY = "tenant_name"; String AUTH_TENANT_NAME_ALTKEY = "tenname";