Skip to content

Commit

Permalink
adding support for specifying a Proxy to use for the connection
Browse files Browse the repository at this point in the history
  • Loading branch information
davydotcom committed Jun 14, 2024
1 parent 0bdd5c9 commit 1e2a8b6
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 7 deletions.
12 changes: 12 additions & 0 deletions sdk/src/main/java/org/ovirt/engine/sdk4/ConnectionBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public abstract class ConnectionBuilder {
protected String user;
protected String password;
protected String token;

protected ProxyInfo proxyInfo;
protected boolean insecure = false;
protected boolean kerberos = false;
protected int timeout = 0;
Expand Down Expand Up @@ -210,6 +212,16 @@ public ConnectionBuilder ssoUrl(String ssoUrl) {
return this;
}

/**
* Set proxy information if using a Proxy
* @param proxyInfo
* @return
*/
public ConnectionBuilder proxy(ProxyInfo proxyInfo) {
this.proxyInfo = proxyInfo;
return this;
}

/**
* Set SSO revoke url
*
Expand Down
85 changes: 85 additions & 0 deletions sdk/src/main/java/org/ovirt/engine/sdk4/ProxyInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package org.ovirt.engine.sdk4;

public class ProxyInfo {
protected String host;
private Integer proxyPort;
protected String user;
protected String password;
private String proxyDomain;
private String proxyWorkstation;

private ProxyType proxyType;


public ProxyInfo(String host, Integer proxyPort, String user, String password, String proxyDomain, ProxyType proxyType) {
this.host = host;
this.proxyPort = proxyPort;
this.user = user;
this.password = password;
this.proxyDomain = proxyDomain;
this.proxyType = proxyType;
}

public String getHost() {
return host;
}

public void setHost(String host) {
this.host = host;
}


public String getUser() {
return user;
}

public void setUser(String user) {
this.user = user;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public Integer getProxyPort() {
return proxyPort;
}

public void setProxyPort(Integer proxyPort) {
this.proxyPort = proxyPort;
}

public String getProxyDomain() {
return proxyDomain;
}

public void setProxyDomain(String proxyDomain) {
this.proxyDomain = proxyDomain;
}

public ProxyType getProxyType() {
return proxyType;
}

public void setProxyType(ProxyType proxyType) {
this.proxyType = proxyType;
}

public String getProxyWorkstation() {
return proxyWorkstation;
}

public void setProxyWorkstation(String proxyWorkstation) {
this.proxyWorkstation = proxyWorkstation;
}


//create an enum for proxy type
public enum ProxyType {
HTTP, SOCKS
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.Principal;
import java.security.cert.CertificateException;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;

import org.apache.http.auth.AuthSchemeProvider;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import java.net.Proxy;
import org.apache.http.HttpHost;
import org.apache.http.auth.*;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.AuthSchemes;
import org.apache.http.client.config.CookieSpecs;
Expand All @@ -31,12 +31,15 @@
import org.apache.http.impl.auth.SPNegoSchemeFactory;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.ProxyAuthenticationStrategy;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.protocol.HttpContext;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.SSLContexts;
import org.ovirt.engine.sdk4.ConnectionBuilder;
import org.ovirt.engine.sdk4.Error;
import org.ovirt.engine.sdk4.HttpClient;
import org.ovirt.engine.sdk4.ProxyInfo;

public class ConnectionBuilder45 extends ConnectionBuilder {

Expand Down Expand Up @@ -93,6 +96,22 @@ public String getPassword() {
clientBuilder.disableContentCompression();
}

if(proxyInfo != null) {
if(proxyInfo.getProxyType() == ProxyInfo.ProxyType.HTTP) {
if(proxyInfo.getHost() != null && proxyInfo.getProxyPort() != null) {
clientBuilder.setProxy(new HttpHost(proxyInfo.getHost(), proxyInfo.getProxyPort()));
if(proxyInfo.getUser() != null) { //authenticated proxy
NTCredentials ntCreds = new NTCredentials(proxyInfo.getUser(), proxyInfo.getPassword(), proxyInfo.getProxyWorkstation(), proxyInfo.getProxyDomain());
credsProvider.setCredentials(new AuthScope(proxyInfo.getHost(), proxyInfo.getProxyPort()), ntCreds);
clientBuilder.setDefaultCredentialsProvider(credsProvider);
clientBuilder.setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy());
}
}
} else if(proxyInfo.getProxyType() == ProxyInfo.ProxyType.SOCKS) {
//this has to be handled in the Connection Factory createSocket
}
}

return new HttpClient45(clientBuilder.build());
}

Expand All @@ -102,7 +121,20 @@ private Registry createConnectionSocketFactoryRegistry() {

// Create SSL/TLS or plain connection:
if (HTTP_PROTOCOL.equals(protocol)) {
ConnectionSocketFactory plainsf = PlainConnectionSocketFactory.getSocketFactory();
ConnectionSocketFactory plainsf = new PlainConnectionSocketFactory() {
@Override
public Socket createSocket(final HttpContext context) throws IOException {
if (proxyInfo != null && proxyInfo.getProxyType() == ProxyInfo.ProxyType.SOCKS) {
if (proxyInfo.getHost() != null && proxyInfo.getProxyPort() != null) {
InetSocketAddress socksaddr = new InetSocketAddress(proxyInfo.getHost(), proxyInfo.getProxyPort());
Proxy proxy = new Proxy(Proxy.Type.SOCKS, socksaddr);
return new Socket(proxy);
}
}
return super.createSocket(context);
}
};

registry = RegistryBuilder.<ConnectionSocketFactory>create()
.register(HTTP_PROTOCOL, plainsf)
.build();
Expand All @@ -113,7 +145,19 @@ else if (HTTPS_PROTOCOL.equals(protocol)) {
if (this.insecure) {
SSLContext sslcontext = SSLContext.getInstance("TLS");
sslcontext.init(null, new TrustManager[]{noCaTrustManager}, null);
sslsf = new SSLConnectionSocketFactory(sslcontext, NoopHostnameVerifier.INSTANCE);
sslsf = new SSLConnectionSocketFactory(sslcontext, NoopHostnameVerifier.INSTANCE) {
@Override
public Socket createSocket(final HttpContext context) throws IOException {
if(proxyInfo != null && proxyInfo.getProxyType() == ProxyInfo.ProxyType.SOCKS) {
if(proxyInfo.getHost() != null && proxyInfo.getProxyPort() != null) {
InetSocketAddress socksaddr = new InetSocketAddress(proxyInfo.getHost(), proxyInfo.getProxyPort());
Proxy proxy = new Proxy(Proxy.Type.SOCKS, socksaddr);
return new Socket(proxy);
}
}
return super.createSocket(context);
}
};
}
else {
SSLContextBuilder sslContextBuilder = SSLContexts.custom();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public class HttpConnection implements Connection {
private String ssoTokenName = null;
private String ssoUrl = null;
private String ssoRevokeUrl = null;

private Map<String, String> headers = null;


Expand Down

0 comments on commit 1e2a8b6

Please sign in to comment.