|
1 | 1 | package com.browserstack.automate.ci.common.proxysettings;
|
2 | 2 |
|
| 3 | +import com.browserstack.automate.ci.common.Tools; |
3 | 4 | import hudson.ProxyConfiguration;
|
| 5 | +import hudson.Util; |
4 | 6 | import jenkins.model.Jenkins;
|
5 | 7 |
|
| 8 | +import javax.annotation.Nonnull; |
| 9 | +import javax.annotation.Nullable; |
| 10 | +import java.io.PrintStream; |
6 | 11 | import java.net.InetSocketAddress;
|
7 | 12 | import java.net.Proxy;
|
| 13 | +import java.net.URL; |
| 14 | + |
| 15 | +import static com.browserstack.automate.ci.common.logger.PluginLogger.log; |
| 16 | +import static com.browserstack.automate.ci.common.logger.PluginLogger.logError; |
8 | 17 |
|
9 | 18 | public class JenkinsProxySettings {
|
10 | 19 |
|
11 | 20 | private static final ProxyConfiguration jenkinsProxy = Jenkins.getInstanceOrNull() != null ? Jenkins.getInstanceOrNull().proxy : null;
|
12 |
| - private static final String protocol = "https"; |
13 |
| - private static final String systemProxyHost = System.getProperty(protocol + ".proxyHost"); |
14 |
| - private static final int systemProxyPort = Integer.parseInt(System.getProperty(protocol + ".proxyPort", "0")); |
15 |
| - private static final String systemProxyUser = System.getProperty(protocol + ".proxyUser"); |
16 |
| - private static final String systemProxyPassword = System.getProperty(protocol + ".proxyPassword"); |
17 |
| - |
18 |
| - public static Proxy getJenkinsProxy() { |
19 |
| - if (hasSystemProxy()) { |
20 |
| - return new Proxy(Proxy.Type.HTTP, new InetSocketAddress(systemProxyHost, systemProxyPort)); |
21 |
| - } |
22 | 21 |
|
23 |
| - if (jenkinsProxy == null) return null; |
24 |
| - final String proxyHost = jenkinsProxy.name; |
25 |
| - final int proxyPort = jenkinsProxy.port; |
26 |
| - return (proxyHost != null && proxyPort != 0) ? new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)) : null; |
| 22 | + private static final String jarProxyHost = System.getProperty("https.proxyHost"); |
| 23 | + private static final int jarProxyPort = Integer.parseInt(System.getProperty("https.proxyPort", "443")); |
| 24 | + private static final String jarProxyUser = System.getProperty("https.proxyUser"); |
| 25 | + private static final String jarProxyPassword = System.getProperty("https.proxyPassword"); |
| 26 | + |
| 27 | + private static final String systemHttpProxyEnv = System.getenv("http_proxy"); |
| 28 | + private static final String systemHttpsProxyEnv = System.getenv("https_proxy"); |
| 29 | + |
| 30 | + private String finalProxyHost; |
| 31 | + private int finalProxyPort; |
| 32 | + private String finalProxyUsername; |
| 33 | + private String finalProxyPassword; |
| 34 | + |
| 35 | + private boolean hasProxy; |
| 36 | + |
| 37 | + private transient PrintStream logger; |
| 38 | + |
| 39 | + /** |
| 40 | + * Constructor for JenkinsProxySettings with Priority for Custom Proxy |
| 41 | + * |
| 42 | + * @param customProxy Custom Proxy String |
| 43 | + * @param logger Logger |
| 44 | + */ |
| 45 | + public JenkinsProxySettings(@Nonnull final String customProxy, @Nullable PrintStream logger) { |
| 46 | + this.logger = logger; |
| 47 | + decideJenkinsProxy(customProxy); |
27 | 48 | }
|
28 | 49 |
|
29 |
| - public static String getHost() { |
30 |
| - if (hasSystemProxy()) { |
31 |
| - return systemProxyHost; |
32 |
| - } |
| 50 | + /** |
| 51 | + * Constructor for JenkinsProxySettings |
| 52 | + * |
| 53 | + * @param logger Logger |
| 54 | + */ |
| 55 | + public JenkinsProxySettings(@Nullable PrintStream logger) { |
| 56 | + this.logger = logger; |
| 57 | + decideJenkinsProxy(null); |
| 58 | + } |
33 | 59 |
|
34 |
| - if (jenkinsProxy == null) return null; |
35 |
| - return jenkinsProxy.name; |
| 60 | + /** |
| 61 | + * Verifies the format of the Proxy String |
| 62 | + * |
| 63 | + * @param proxyString String |
| 64 | + * @param proxyType Type/Source of Proxy |
| 65 | + * @return |
| 66 | + */ |
| 67 | + private URL verifyAndGetProxyURL(final String proxyString, final String proxyType) { |
| 68 | + try { |
| 69 | + final URL proxyUrl = new URL(proxyString); |
| 70 | + if (proxyUrl.getHost() != null && proxyUrl.getHost().length() == 0) |
| 71 | + throw new Error("Empty host in proxy"); |
| 72 | + |
| 73 | + String userInfo = proxyUrl.getUserInfo(); |
| 74 | + if (userInfo != null) { |
| 75 | + if (userInfo.split(":").length != 2) { |
| 76 | + throw new Error("Invalid authentication params in proxy"); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + return proxyUrl; |
| 81 | + } catch (Exception e) { |
| 82 | + // TODO: Change to logDebug |
| 83 | + if (logger != null) |
| 84 | + logError(logger, String.format("Invalid Proxy String: %s, Proxy Type: %s. Error: %s", proxyString, proxyType, e.toString())); |
| 85 | + return null; |
| 86 | + } |
36 | 87 | }
|
37 | 88 |
|
38 |
| - public static int getPort() { |
39 |
| - if (hasSystemProxy()) { |
40 |
| - return systemProxyPort; |
| 89 | + /** |
| 90 | + * Decides Proxy for the Plugin. Priority: |
| 91 | + * 0. Custom Proxy passed as input |
| 92 | + * 1. `https_proxy` Environment Variable |
| 93 | + * 2. `http_proxy` Environment Variable |
| 94 | + * 3. Jenkins Proxy Configuration |
| 95 | + * 4. JAR Proxy arguments, i.e. `https.proxyHost` etc. |
| 96 | + * |
| 97 | + * @param customProxyString Custom Proxy String |
| 98 | + */ |
| 99 | + private void decideJenkinsProxy(final String customProxyString) { |
| 100 | + URL proxyUrl = null; |
| 101 | + |
| 102 | + // Verifies the custom proxy string |
| 103 | + if (customProxyString != null) { |
| 104 | + proxyUrl = verifyAndGetProxyURL(customProxyString, "ENV_VAR"); |
41 | 105 | }
|
42 | 106 |
|
43 |
| - if (jenkinsProxy == null) return 0; |
44 |
| - return jenkinsProxy.port; |
45 |
| - } |
| 107 | + // Looks for System level `https_proxy`. If not, looks for `http_proxy` |
| 108 | + if (proxyUrl == null && getSystemProxyString() != null) { |
| 109 | + proxyUrl = verifyAndGetProxyURL(getSystemProxyString(), "SYSTEM_ENV_VAR"); |
| 110 | + } |
46 | 111 |
|
47 |
| - public static String getUsername() { |
48 |
| - if (hasSystemProxy() && systemProxyUser != null && systemProxyPassword != null) { |
49 |
| - return systemProxyUser; |
| 112 | + // Looks for Jenkins Proxy |
| 113 | + if (proxyUrl == null && jenkinsProxy != null) { |
| 114 | + this.finalProxyHost = jenkinsProxy.name; |
| 115 | + this.finalProxyPort = jenkinsProxy.port; |
| 116 | + |
| 117 | + if (Util.fixEmpty(jenkinsProxy.getUserName()) != null && Util.fixEmpty(jenkinsProxy.getPassword()) != null) { |
| 118 | + this.finalProxyUsername = jenkinsProxy.getUserName(); |
| 119 | + this.finalProxyPassword = jenkinsProxy.getPassword(); |
| 120 | + } |
50 | 121 | }
|
51 | 122 |
|
52 |
| - if (jenkinsProxy == null) return null; |
53 |
| - return jenkinsProxy.getUserName(); |
| 123 | + // Looks for JAR Proxy Arguments |
| 124 | + if (proxyUrl == null && this.finalProxyHost == null && jarProxyHost != null) { |
| 125 | + this.finalProxyHost = jarProxyHost; |
| 126 | + this.finalProxyPort = jarProxyPort; |
| 127 | + |
| 128 | + if (Util.fixEmpty(jarProxyUser) != null && Util.fixEmpty(jarProxyPassword) != null) { |
| 129 | + this.finalProxyUsername = jarProxyUser; |
| 130 | + this.finalProxyPassword = jarProxyPassword; |
| 131 | + } |
| 132 | + |
| 133 | + if (this.finalProxyUsername == null) |
| 134 | + System.out.println("Username is null in case of JAR proxy..."); |
| 135 | + } |
| 136 | + |
| 137 | + // Utilises the proxyUrl set by Env Vars if Jenkins & Jar Proxy are absent |
| 138 | + if (proxyUrl != null) { |
| 139 | + this.finalProxyHost = proxyUrl.getHost(); |
| 140 | + this.finalProxyPort = proxyUrl.getPort() == -1 ? proxyUrl.getDefaultPort() : proxyUrl.getPort(); |
| 141 | + |
| 142 | + final String userInfo = proxyUrl.getUserInfo(); |
| 143 | + |
| 144 | + if (userInfo != null) { |
| 145 | + String[] userInfoArray = userInfo.split(":"); |
| 146 | + this.finalProxyUsername = userInfoArray[0]; |
| 147 | + this.finalProxyPassword = userInfoArray[1]; |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + // Logging and final boolean set |
| 152 | + if (this.finalProxyHost != null && this.finalProxyPort != 0) { |
| 153 | + this.hasProxy = true; |
| 154 | + |
| 155 | + String proxyDataToLog = "\nHost: " + this.getHost() + "\nPort: " + this.getPort(); |
| 156 | + if (this.hasAuth()) { |
| 157 | + proxyDataToLog += "\nUsername: " + this.getUsername() + "\nPassword: " + Tools.maskString(this.getPassword()); |
| 158 | + } |
| 159 | + |
| 160 | + if (logger != null) log(logger, "Proxy Selected for BrowserStack Plugin: " + proxyDataToLog); |
| 161 | + } else { |
| 162 | + this.hasProxy = false; |
| 163 | + |
| 164 | + if (logger != null) log(logger, "No Proxy Selected for BrowserStack Plugin"); |
| 165 | + } |
54 | 166 | }
|
55 | 167 |
|
56 |
| - public static String getPassword() { |
57 |
| - if (hasSystemProxy() && systemProxyUser != null && systemProxyPassword != null) { |
58 |
| - return systemProxyPassword; |
| 168 | + |
| 169 | + /** |
| 170 | + * Returns the proxy string from System level env vars. Priority: |
| 171 | + * 1. `https_proxy` |
| 172 | + * 2. `http_proxy` |
| 173 | + * If no value exists, returns null |
| 174 | + * |
| 175 | + * @return String/null |
| 176 | + */ |
| 177 | + private String getSystemProxyString() { |
| 178 | + return systemHttpsProxyEnv == null ? systemHttpProxyEnv : systemHttpsProxyEnv; |
| 179 | + } |
| 180 | + |
| 181 | + public Proxy getJenkinsProxy() { |
| 182 | + if (this.hasProxy()) { |
| 183 | + return new Proxy(Proxy.Type.HTTP, new InetSocketAddress(this.finalProxyHost, this.finalProxyPort)); |
59 | 184 | }
|
60 | 185 |
|
61 |
| - if (jenkinsProxy == null) return null; |
62 |
| - return jenkinsProxy.getPassword(); |
| 186 | + return Proxy.NO_PROXY; |
| 187 | + } |
| 188 | + |
| 189 | + public String getHost() { |
| 190 | + return this.finalProxyHost; |
| 191 | + } |
| 192 | + |
| 193 | + public int getPort() { |
| 194 | + return this.finalProxyPort; |
63 | 195 | }
|
64 | 196 |
|
65 |
| - public static ProxyConfiguration getProxyConfig() { |
66 |
| - return jenkinsProxy; |
| 197 | + public String getUsername() { |
| 198 | + if (this.finalProxyUsername != null && this.finalProxyUsername.length() != 0) |
| 199 | + return this.finalProxyUsername; |
| 200 | + |
| 201 | + return null; |
67 | 202 | }
|
68 | 203 |
|
69 |
| - public static boolean hasProxy() { |
70 |
| - return getHost() != null && getPort() != 0; |
| 204 | + public String getPassword() { |
| 205 | + if (this.finalProxyPassword != null && this.finalProxyPassword.length() != 0) |
| 206 | + return this.finalProxyPassword; |
| 207 | + |
| 208 | + return null; |
71 | 209 | }
|
72 | 210 |
|
73 |
| - public static boolean hasSystemProxy() { |
74 |
| - return systemProxyHost != null && systemProxyPort != 0; |
| 211 | + public boolean hasAuth() { |
| 212 | + return ((this.getUsername() != null) && (this.getPassword() != null)); |
75 | 213 | }
|
76 | 214 |
|
| 215 | + public boolean hasProxy() { |
| 216 | + return this.hasProxy; |
| 217 | + } |
77 | 218 | }
|
0 commit comments