Michael Pasternak has uploaded a new change for review. Change subject: sdk: implement support for user defined session authentication ......................................................................
sdk: implement support for user defined session authentication Change-Id: I8961f0aca7cc36210da8613399be44701dd087fb Signed-off-by: Michael Pasternak <[email protected]> --- M ovirt-engine-sdk-java-codegen/src/main/java/org/ovirt/engine/sdk/codegen/templates/ApiTemplate M ovirt-engine-sdk-java/src/main/java/org/ovirt/engine/sdk/Api.java M ovirt-engine-sdk-java/src/main/java/org/ovirt/engine/sdk/utils/StringUtils.java M ovirt-engine-sdk-java/src/main/java/org/ovirt/engine/sdk/web/ConnectionsPoolBuilder.java M ovirt-engine-sdk-java/src/main/java/org/ovirt/engine/sdk/web/HttpProxy.java M ovirt-engine-sdk-java/src/main/java/org/ovirt/engine/sdk/web/HttpProxyBroker.java M ovirt-engine-sdk-java/src/main/java/org/ovirt/engine/sdk/web/HttpProxyBuilder.java 7 files changed, 274 insertions(+), 51 deletions(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-engine-sdk-java refs/changes/18/12818/1 diff --git a/ovirt-engine-sdk-java-codegen/src/main/java/org/ovirt/engine/sdk/codegen/templates/ApiTemplate b/ovirt-engine-sdk-java-codegen/src/main/java/org/ovirt/engine/sdk/codegen/templates/ApiTemplate index 71d8b99..71dd5fc 100644 --- a/ovirt-engine-sdk-java-codegen/src/main/java/org/ovirt/engine/sdk/codegen/templates/ApiTemplate +++ b/ovirt-engine-sdk-java-codegen/src/main/java/org/ovirt/engine/sdk/codegen/templates/ApiTemplate @@ -39,13 +39,43 @@ * @throws IOException * Signals that an I/O exception of some sort has occurred. * @throws UnsecuredConnectionAttemptError - * Signals that attempt of connecting to SSL secured site using HTTP protocol has occurred. + * Signals that attempt of connecting to SSL secured site using + * HTTP protocol has occurred. */ public Api(String url, String username, String password) throws ClientProtocolException, ServerException, IOException, UnsecuredConnectionAttemptError { ConnectionsPool pool = new ConnectionsPoolBuilder(url, username, password).build(); HttpProxy httpProxy = new HttpProxyBuilder(pool).build(); + this.proxy = new HttpProxyBroker(httpProxy); + this.initResources(); + } + + /** + * @param url + * oVirt api url + * @param sessionid + * oVirt api sessionid to authenticate the user with + * (used as SSO solution instead of username+password) + * + * @throws ClientProtocolException + * Signals that HTTP/S protocol error has occurred. + * @throws ServerException + * Signals that an oVirt api error has occurred. + * @throws IOException + * Signals that an I/O exception of some sort has occurred. + * @throws UnsecuredConnectionAttemptError + * Signals that attempt of connecting to SSL secured site + * using HTTP protocol has occurred. + */ + public Api(String url, String sessionid) throws ClientProtocolException, ServerException, + IOException, UnsecuredConnectionAttemptError { + + ConnectionsPool pool = new ConnectionsPoolBuilder(url).build(); + HttpProxy httpProxy = new HttpProxyBuilder(pool) + .sessionid(sessionid) + .persistentAuth(true) + .build(); this.proxy = new HttpProxyBroker(httpProxy); this.initResources(); } @@ -67,7 +97,8 @@ * @throws IOException * Signals that an I/O exception of some sort has occurred. * @throws UnsecuredConnectionAttemptError - * Signals that attempt of connecting to SSL secured site using HTTP protocol has occurred. + * Signals that attempt of connecting to SSL secured site + * using HTTP protocol has occurred. */ public Api(String url, String username, String password, boolean noHostVerification) throws ClientProtocolException, ServerException, UnsecuredConnectionAttemptError, IOException { @@ -76,6 +107,39 @@ .noHostVerification(noHostVerification) .build(); HttpProxy httpProxy = new HttpProxyBuilder(pool) + .build(); + this.proxy = new HttpProxyBroker(httpProxy); + initResources(); + } + + /** + * @param url + * oVirt api url + * @param sessionid + * oVirt api sessionid to authenticate the user with + * (used as SSO solution instead of username+password) + * @param noHostVerification + * turns hostname verification off + * + * @throws ClientProtocolException + * Signals that HTTP/S protocol error has occurred. + * @throws ServerException + * Signals that an oVirt api error has occurred. + * @throws IOException + * Signals that an I/O exception of some sort has occurred. + * @throws UnsecuredConnectionAttemptError + * Signals that attempt of connecting to SSL secured site + * using HTTP protocol has occurred. + */ + public Api(String url, String sessionid, boolean noHostVerification) + throws ClientProtocolException, ServerException, UnsecuredConnectionAttemptError, IOException { + + ConnectionsPool pool = new ConnectionsPoolBuilder(url) + .noHostVerification(noHostVerification) + .build(); + HttpProxy httpProxy = new HttpProxyBuilder(pool) + .sessionid(sessionid) + .persistentAuth(true) .build(); this.proxy = new HttpProxyBroker(httpProxy); initResources(); @@ -100,7 +164,8 @@ * @throws IOException * Signals that an I/O exception of some sort has occurred. * @throws UnsecuredConnectionAttemptError - * Signals that attempt of connecting to SSL secured site using HTTP protocol has occurred. + * Signals that attempt of connecting to SSL secured site + * using HTTP protocol has occurred. */ public Api(String url, String username, String password, Boolean noHostVerification, Boolean filter) throws ClientProtocolException, ServerException, UnsecuredConnectionAttemptError, IOException { @@ -122,6 +187,9 @@ * oVirt api username * @param password * oVirt api password + * @param sessionid + * oVirt api sessionid to authenticate the user with + * (used as SSO solution instead of username+password) * @param port * oVirt api port * @param timeout @@ -142,9 +210,10 @@ * @throws IOException * Signals that an I/O exception of some sort has occurred. * @throws UnsecuredConnectionAttemptError - * Signals that attempt of connecting to SSL secured site using HTTP protocol has occurred. + * Signals that attempt of connecting to SSL secured site + * using HTTP protocol has occurred. */ - public Api(String url, String username, String password, Integer port, Integer timeout, + public Api(String url, String username, String password, String sessionid, Integer port, Integer timeout, Boolean persistentAuth, Boolean noHostVerification, Boolean filter, Boolean debug) throws ClientProtocolException, ServerException, UnsecuredConnectionAttemptError, IOException { @@ -154,6 +223,7 @@ .noHostVerification(noHostVerification) .build(); HttpProxy httpProxy = new HttpProxyBuilder(pool) + .sessionid(sessionid) .persistentAuth(persistentAuth) .filter(filter) .debug(debug) @@ -164,7 +234,7 @@ /** * Fetches /api entry point - * + * * @return API object instance */ private API getEntryPoint() throws ClientProtocolException, ServerException, IOException, @@ -186,7 +256,7 @@ /** * Enable/Disable client permissions based filtering (default is False) - * + * * @param filter */ public void setFilter(boolean filter) { @@ -195,7 +265,7 @@ /** * Enable/Disable debug mode (default is False) - * + * * @param debug */ public void setDebug(boolean debug) { @@ -204,7 +274,7 @@ /** * Enable/Disable persistent authentication (default is True) - * + * * @param persistentAuth */ public void setPersistentAuth(boolean persistentAuth) { @@ -233,6 +303,23 @@ } /** + * @param sessionid + * oVirt api sessionid to authenticate the user with + * (used as SSO solution instead of username+password) + */ + public void setSessionid(String sessionid) { + this.proxy.setSessionid(sessionid); + } + + /** + * oVirt api sessionid to authenticate the user with + * (used as SSO solution instead of username+password) + */ + public boolean isSetSessionid() { + return this.proxy.isSetSessionid(); + } + + /** * When SDK instance is no longer needed, shut down the connection * manager/httpproxy to ensure immediate deallocation of all system * resources. diff --git a/ovirt-engine-sdk-java/src/main/java/org/ovirt/engine/sdk/Api.java b/ovirt-engine-sdk-java/src/main/java/org/ovirt/engine/sdk/Api.java index a1eff67..2b24954 100644 --- a/ovirt-engine-sdk-java/src/main/java/org/ovirt/engine/sdk/Api.java +++ b/ovirt-engine-sdk-java/src/main/java/org/ovirt/engine/sdk/Api.java @@ -74,13 +74,43 @@ * @throws IOException * Signals that an I/O exception of some sort has occurred. * @throws UnsecuredConnectionAttemptError - * Signals that attempt of connecting to SSL secured site using HTTP protocol has occurred. + * Signals that attempt of connecting to SSL secured site using + * HTTP protocol has occurred. */ public Api(String url, String username, String password) throws ClientProtocolException, ServerException, IOException, UnsecuredConnectionAttemptError { ConnectionsPool pool = new ConnectionsPoolBuilder(url, username, password).build(); HttpProxy httpProxy = new HttpProxyBuilder(pool).build(); + this.proxy = new HttpProxyBroker(httpProxy); + this.initResources(); + } + + /** + * @param url + * oVirt api url + * @param sessionid + * oVirt api sessionid to authenticate the user with + * (used as SSO solution instead of username+password) + * + * @throws ClientProtocolException + * Signals that HTTP/S protocol error has occurred. + * @throws ServerException + * Signals that an oVirt api error has occurred. + * @throws IOException + * Signals that an I/O exception of some sort has occurred. + * @throws UnsecuredConnectionAttemptError + * Signals that attempt of connecting to SSL secured site + * using HTTP protocol has occurred. + */ + public Api(String url, String sessionid) throws ClientProtocolException, ServerException, + IOException, UnsecuredConnectionAttemptError { + + ConnectionsPool pool = new ConnectionsPoolBuilder(url).build(); + HttpProxy httpProxy = new HttpProxyBuilder(pool) + .sessionid(sessionid) + .persistentAuth(true) + .build(); this.proxy = new HttpProxyBroker(httpProxy); this.initResources(); } @@ -102,7 +132,8 @@ * @throws IOException * Signals that an I/O exception of some sort has occurred. * @throws UnsecuredConnectionAttemptError - * Signals that attempt of connecting to SSL secured site using HTTP protocol has occurred. + * Signals that attempt of connecting to SSL secured site + * using HTTP protocol has occurred. */ public Api(String url, String username, String password, boolean noHostVerification) throws ClientProtocolException, ServerException, UnsecuredConnectionAttemptError, IOException { @@ -111,6 +142,39 @@ .noHostVerification(noHostVerification) .build(); HttpProxy httpProxy = new HttpProxyBuilder(pool) + .build(); + this.proxy = new HttpProxyBroker(httpProxy); + initResources(); + } + + /** + * @param url + * oVirt api url + * @param sessionid + * oVirt api sessionid to authenticate the user with + * (used as SSO solution instead of username+password) + * @param noHostVerification + * turns hostname verification off + * + * @throws ClientProtocolException + * Signals that HTTP/S protocol error has occurred. + * @throws ServerException + * Signals that an oVirt api error has occurred. + * @throws IOException + * Signals that an I/O exception of some sort has occurred. + * @throws UnsecuredConnectionAttemptError + * Signals that attempt of connecting to SSL secured site + * using HTTP protocol has occurred. + */ + public Api(String url, String sessionid, boolean noHostVerification) + throws ClientProtocolException, ServerException, UnsecuredConnectionAttemptError, IOException { + + ConnectionsPool pool = new ConnectionsPoolBuilder(url) + .noHostVerification(noHostVerification) + .build(); + HttpProxy httpProxy = new HttpProxyBuilder(pool) + .sessionid(sessionid) + .persistentAuth(true) .build(); this.proxy = new HttpProxyBroker(httpProxy); initResources(); @@ -135,7 +199,8 @@ * @throws IOException * Signals that an I/O exception of some sort has occurred. * @throws UnsecuredConnectionAttemptError - * Signals that attempt of connecting to SSL secured site using HTTP protocol has occurred. + * Signals that attempt of connecting to SSL secured site + * using HTTP protocol has occurred. */ public Api(String url, String username, String password, Boolean noHostVerification, Boolean filter) throws ClientProtocolException, ServerException, UnsecuredConnectionAttemptError, IOException { @@ -157,6 +222,9 @@ * oVirt api username * @param password * oVirt api password + * @param sessionid + * oVirt api sessionid to authenticate the user with + * (used as SSO solution instead of username+password) * @param port * oVirt api port * @param timeout @@ -177,9 +245,10 @@ * @throws IOException * Signals that an I/O exception of some sort has occurred. * @throws UnsecuredConnectionAttemptError - * Signals that attempt of connecting to SSL secured site using HTTP protocol has occurred. + * Signals that attempt of connecting to SSL secured site + * using HTTP protocol has occurred. */ - public Api(String url, String username, String password, Integer port, Integer timeout, + public Api(String url, String username, String password, String sessionid, Integer port, Integer timeout, Boolean persistentAuth, Boolean noHostVerification, Boolean filter, Boolean debug) throws ClientProtocolException, ServerException, UnsecuredConnectionAttemptError, IOException { @@ -189,6 +258,7 @@ .noHostVerification(noHostVerification) .build(); HttpProxy httpProxy = new HttpProxyBuilder(pool) + .sessionid(sessionid) .persistentAuth(persistentAuth) .filter(filter) .debug(debug) @@ -199,7 +269,7 @@ /** * Fetches /api entry point - * + * * @return API object instance */ private API getEntryPoint() throws ClientProtocolException, ServerException, IOException, @@ -221,7 +291,7 @@ /** * Enable/Disable client permissions based filtering (default is False) - * + * * @param filter */ public void setFilter(boolean filter) { @@ -230,7 +300,7 @@ /** * Enable/Disable debug mode (default is False) - * + * * @param debug */ public void setDebug(boolean debug) { @@ -239,7 +309,7 @@ /** * Enable/Disable persistent authentication (default is True) - * + * * @param persistentAuth */ public void setPersistentAuth(boolean persistentAuth) { @@ -268,6 +338,23 @@ } /** + * @param sessionid + * oVirt api sessionid to authenticate the user with + * (used as SSO solution instead of username+password) + */ + public void setSessionid(String sessionid) { + this.proxy.setSessionid(sessionid); + } + + /** + * oVirt api sessionid to authenticate the user with + * (used as SSO solution instead of username+password) + */ + public boolean isSetSessionid() { + return this.proxy.isSetSessionid(); + } + + /** * When SDK instance is no longer needed, shut down the connection * manager/httpproxy to ensure immediate deallocation of all system * resources. diff --git a/ovirt-engine-sdk-java/src/main/java/org/ovirt/engine/sdk/utils/StringUtils.java b/ovirt-engine-sdk-java/src/main/java/org/ovirt/engine/sdk/utils/StringUtils.java index 9c237a7..0b6c638 100644 --- a/ovirt-engine-sdk-java/src/main/java/org/ovirt/engine/sdk/utils/StringUtils.java +++ b/ovirt-engine-sdk-java/src/main/java/org/ovirt/engine/sdk/utils/StringUtils.java @@ -122,6 +122,6 @@ * @return if string is null or empty true else false */ public static boolean isNulOrEmpty(String string) { - return string.equals(null) || "".equals(string); + return string == null || "".equals(string); } } diff --git a/ovirt-engine-sdk-java/src/main/java/org/ovirt/engine/sdk/web/ConnectionsPoolBuilder.java b/ovirt-engine-sdk-java/src/main/java/org/ovirt/engine/sdk/web/ConnectionsPoolBuilder.java index f4492b7..dec227e 100644 --- a/ovirt-engine-sdk-java/src/main/java/org/ovirt/engine/sdk/web/ConnectionsPoolBuilder.java +++ b/ovirt-engine-sdk-java/src/main/java/org/ovirt/engine/sdk/web/ConnectionsPoolBuilder.java @@ -36,6 +36,7 @@ import org.apache.http.conn.ssl.SSLSocketFactory; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.impl.conn.PoolingClientConnectionManager; +import org.ovirt.engine.sdk.utils.StringUtils; /** * Provides ConnectionsPool building services @@ -82,6 +83,16 @@ url(url); username(username); password(password); + } + + /** + * @param url + * oVirt API url + * + * @throws MalformedURLException + */ + public ConnectionsPoolBuilder(String url) throws MalformedURLException { + url(url); } /** @@ -198,9 +209,10 @@ DefaultHttpClient client = new DefaultHttpClient(createPoolingClientConnectionManager(url, port_)); - client.getCredentialsProvider().setCredentials( - new AuthScope(getHost(url), port_), - new UsernamePasswordCredentials(username, password)); + if (!StringUtils.isNulOrEmpty(username)) + client.getCredentialsProvider().setCredentials( + new AuthScope(getHost(url), port_), + new UsernamePasswordCredentials(username, password)); // FIXME: use all .ctr params diff --git a/ovirt-engine-sdk-java/src/main/java/org/ovirt/engine/sdk/web/HttpProxy.java b/ovirt-engine-sdk-java/src/main/java/org/ovirt/engine/sdk/web/HttpProxy.java index 9f716de..c3888b6 100644 --- a/ovirt-engine-sdk-java/src/main/java/org/ovirt/engine/sdk/web/HttpProxy.java +++ b/ovirt-engine-sdk-java/src/main/java/org/ovirt/engine/sdk/web/HttpProxy.java @@ -27,29 +27,32 @@ import org.apache.http.client.methods.HttpPut; import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.client.protocol.ClientContext; -import org.apache.http.cookie.Cookie; import org.apache.http.protocol.BasicHttpContext; import org.ovirt.engine.sdk.exceptions.ServerException; import org.ovirt.engine.sdk.utils.HttpHeaderUtils; import org.ovirt.engine.sdk.utils.HttpResponseHelper; +import org.ovirt.engine.sdk.utils.StringUtils; /** * Proxy in to transport layer */ public class HttpProxy { + private static final String COOKIE_HEADER = "Cookie"; private static final String CONTENT_TYPE_HEADER = "Content-type"; + private static final String PERSISTENT_AUTH_HEADER_CONTENT = "persistent-auth"; private static final String PERSISTENT_AUTH_HEADER = "Prefer"; private static final String FILTER_HEADER = "Filter"; - private static final String JSESSIONID = "JSESSIONID"; + private static final String STATIC_HEADERS[] = new String[] { "Content-type:application/xml" }; + private static int BAD_REQUEST = 400; - private static String STATIC_HEADERS[] = new String[] { "Content-type:application/xml" }; private ConnectionsPool pool; private List<Header> staticHeaders; private boolean persistentAuth = true; private boolean filter = false; private boolean debug = false; + private String sessionid; /** * @@ -57,6 +60,9 @@ * ConnectionsPool * @param persistent_auth * persistent authetication + * @param sessionid + * oVirt api sessionid to authenticate the user with + * (used as SSO solution instead of username+password) * @param insecure * flag * @param filter @@ -64,11 +70,13 @@ * @param debug * flag */ - public HttpProxy(ConnectionsPool pool, boolean persistent_auth, boolean filter, boolean debug) { + public HttpProxy(ConnectionsPool pool, boolean persistent_auth, + String sessionid, boolean filter, boolean debug) { super(); this.pool = pool; this.staticHeaders = HttpHeaderUtils.toHeaders(STATIC_HEADERS); this.persistentAuth = persistent_auth; + this.sessionid = sessionid; this.filter = filter; this.debug = debug; } @@ -114,9 +122,9 @@ * * @return {@link BasicHttpContext} */ - public BasicHttpContext getContext() { + private BasicHttpContext getContext() { BasicHttpContext context = new BasicHttpContext(); - if (this.persistentAuth) { + if (this.persistentAuth && StringUtils.isNulOrEmpty(this.sessionid)) { context.setAttribute(ClientContext.COOKIE_STORE, this.pool.getCookieStore()); } return context; @@ -153,30 +161,15 @@ // inject FILTER_HEADER request.addHeader(FILTER_HEADER, Boolean.toString(isFilter())); - // inject PERSISTENT_AUTH_HEADER if (this.persistentAuth) { - request.addHeader(PERSISTENT_AUTH_HEADER, "persistent-auth"); - String session = getJsession(); - if (session != null) { - request.addHeader(JSESSIONID, session); - } - } - } + // inject PERSISTENT_AUTH_HEADER + request.addHeader(PERSISTENT_AUTH_HEADER, PERSISTENT_AUTH_HEADER_CONTENT); - /** - * Fetches JSESSIONID from CookieStore - * - * @return JSESSIONID - */ - private String getJsession() { - if (this.pool.getCookies() != null && !this.pool.getCookies().isEmpty()) { - for (Cookie cookie : this.pool.getCookies()) { - if (cookie.getName().equals(JSESSIONID)) { - return cookie.getValue(); - } + // inject COOKIE_HEADER if JSESSION provided explicitly + if (!StringUtils.isNulOrEmpty(this.sessionid)) { + request.addHeader(COOKIE_HEADER, this.sessionid); } } - return null; } /** @@ -194,6 +187,23 @@ this.persistentAuth = persistentAuth; } + /** + * @param sessionid + * oVirt api sessionid to authenticate the user with + * (used as SSO solution instead of username+password) + */ + public void setSessionid(String sessionid) { + this.sessionid = sessionid; + } + + /** + * oVirt api sessionid to authenticate the user with + * (used as SSO solution instead of username+password) + */ + public boolean isSetSessionid() { + return !StringUtils.isNulOrEmpty(this.sessionid); + } + /** * @return Filter flag */ @@ -226,8 +236,8 @@ /** * When HttpProxy instance is no longer needed, shut down the - * connection manager to ensure immediate deallocation of all system - * resources. + * connection manager to ensure immediate deallocation of + * all system resources. */ public void shutdown() { this.pool.shutdown(); diff --git a/ovirt-engine-sdk-java/src/main/java/org/ovirt/engine/sdk/web/HttpProxyBroker.java b/ovirt-engine-sdk-java/src/main/java/org/ovirt/engine/sdk/web/HttpProxyBroker.java index 8355e2b..1789412 100644 --- a/ovirt-engine-sdk-java/src/main/java/org/ovirt/engine/sdk/web/HttpProxyBroker.java +++ b/ovirt-engine-sdk-java/src/main/java/org/ovirt/engine/sdk/web/HttpProxyBroker.java @@ -442,6 +442,23 @@ } /** + * @param sessionid + * oVirt api sessionid to authenticate the user with + * (used as SSO solution instead of username+password) + */ + public void setSessionid(String sessionid) { + this.proxy.setSessionid(sessionid); + } + + /** + * oVirt api sessionid to authenticate the user with + * (used as SSO solution instead of username+password) + */ + public boolean isSetSessionid() { + return this.proxy.isSetSessionid(); + } + + /** * @return persistent authentication flag */ public boolean isPersistentAuth() { diff --git a/ovirt-engine-sdk-java/src/main/java/org/ovirt/engine/sdk/web/HttpProxyBuilder.java b/ovirt-engine-sdk-java/src/main/java/org/ovirt/engine/sdk/web/HttpProxyBuilder.java index 628a370..0798268 100644 --- a/ovirt-engine-sdk-java/src/main/java/org/ovirt/engine/sdk/web/HttpProxyBuilder.java +++ b/ovirt-engine-sdk-java/src/main/java/org/ovirt/engine/sdk/web/HttpProxyBuilder.java @@ -23,6 +23,7 @@ private ConnectionsPool pool; private boolean persistentAuth = true; + private String sessionid; private boolean filter = false; private boolean debug = false; @@ -43,6 +44,15 @@ if (persistentAuth != null) { this.persistentAuth = persistentAuth.booleanValue(); } + return this; + } + + /** + * @param sessionid + * oVirt api sessionid to authenticate the user with (used as SSO solution instead of username+password) + */ + public HttpProxyBuilder sessionid(String sessionid) { + this.sessionid = sessionid; return this; } @@ -72,6 +82,6 @@ * builds the proxy */ public HttpProxy build() { - return new HttpProxy(pool, persistentAuth, filter, debug); + return new HttpProxy(pool, persistentAuth, sessionid, filter, debug); } } -- To view, visit http://gerrit.ovirt.org/12818 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I8961f0aca7cc36210da8613399be44701dd087fb Gerrit-PatchSet: 1 Gerrit-Project: ovirt-engine-sdk-java Gerrit-Branch: master Gerrit-Owner: Michael Pasternak <[email protected]> _______________________________________________ Engine-patches mailing list [email protected] http://lists.ovirt.org/mailman/listinfo/engine-patches
