forked from morficus/Parameterized-Remote-Trigger-Plugin
/
RemoteJenkinsServer.java
290 lines (246 loc) · 9.39 KB
/
RemoteJenkinsServer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package org.jenkinsci.plugins.ParameterizedRemoteTrigger;
import static org.apache.commons.lang.StringUtils.trimToEmpty;
import java.io.Serializable;
import javax.net.ssl.*;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.List;
import javax.annotation.CheckForNull;
import org.jenkinsci.plugins.ParameterizedRemoteTrigger.auth2.Auth2;
import org.jenkinsci.plugins.ParameterizedRemoteTrigger.auth2.Auth2.Auth2Descriptor;
import org.jenkinsci.plugins.ParameterizedRemoteTrigger.auth2.NoneAuth;
import org.jenkinsci.plugins.ParameterizedRemoteTrigger.utils.NaiveTrustManager;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.QueryParameter;
import hudson.Extension;
import hudson.model.AbstractDescribableImpl;
import hudson.model.Descriptor;
import hudson.util.FormValidation;
/**
* Holds everything regarding the remote server we wish to connect to, including validations and what not.
*
* @author Maurice W.
*
*/
public class RemoteJenkinsServer extends AbstractDescribableImpl<RemoteJenkinsServer> implements Cloneable, Serializable {
private static final long serialVersionUID = -9211781849078964416L;
/**
* Default for this class is No Authentication
*/
private static final Auth2 DEFAULT_AUTH = NoneAuth.INSTANCE;
/**
* We need to keep this for compatibility - old config deserialization!
* @deprecated since 2.3.0-SNAPSHOT - use {@link Auth2} instead.
*/
@CheckForNull
private transient List<Auth> auth;
@CheckForNull
private String displayName;
private boolean hasBuildTokenRootSupport;
private boolean trustAllCertificates;
@CheckForNull
private Auth2 auth2;
@CheckForNull
private String address;
private boolean useProxy;
@DataBoundConstructor
public RemoteJenkinsServer() {
}
/*
* see https://wiki.jenkins.io/display/JENKINS/Hint+on+retaining+backward+compatibility
*/
@SuppressWarnings("deprecation")
protected Object readResolve() {
//migrate Auth To Auth2
if(auth2 == null) {
if(auth == null || auth.size() <= 0) {
auth2 = DEFAULT_AUTH;
} else {
auth2 = Auth.authToAuth2(auth);
}
}
auth = null;
return this;
}
@DataBoundSetter
public void setTrustAllCertificates(boolean trustAllCertificates) {
this.trustAllCertificates = trustAllCertificates;
}
@DataBoundSetter
public void setDisplayName(String displayName)
{
this.displayName = trimToEmpty(displayName);
}
@DataBoundSetter
public void setHasBuildTokenRootSupport(boolean hasBuildTokenRootSupport)
{
this.hasBuildTokenRootSupport = hasBuildTokenRootSupport;
}
@DataBoundSetter
public void setUseProxy(boolean useProxy) {
this.useProxy = useProxy;
}
@DataBoundSetter
public void setAuth2(Auth2 auth2)
{
this.auth2 = (auth2 != null) ? auth2 : DEFAULT_AUTH;
}
@DataBoundSetter
public void setAddress(String address)
{
this.address = address;
}
// Getters
@CheckForNull
public String getDisplayName() {
String displayName = null;
if (this.displayName == null || this.displayName.trim().equals("")) {
if (address != null) displayName = address;
else displayName = null;
} else {
displayName = this.displayName;
}
return displayName;
}
public boolean getHasBuildTokenRootSupport() {
return hasBuildTokenRootSupport;
}
public boolean isUseProxy() {
return useProxy;
}
@CheckForNull
public Auth2 getAuth2() {
return (auth2 != null) ? auth2 : NoneAuth.INSTANCE;
}
@CheckForNull
public String getAddress() {
return address;
}
@Override
public DescriptorImpl getDescriptor() {
return (DescriptorImpl) super.getDescriptor();
}
public boolean getTrustAllCertificates() { return trustAllCertificates; }
@Extension
public static class DescriptorImpl extends Descriptor<RemoteJenkinsServer> {
public String getDisplayName() {
return "";
}
/**
* Sets the TrustManager to be a "NaiveTrustManager", allowing us to ignore untrusted certificates
* Will set the connection to null, if a key management error occurred.
*
* ATTENTION: THIS IS VERY DANGEROUS AND SHOULD ONLY BE USED IF YOU KNOW WHAT YOU DO!
* @param conn The HttpsURLConnection you want to modify.
* @param trustAllCertificates A boolean, gotten from the Remote Hosts description
*/
public void makeConnectionTrustAllCertificates(HttpsURLConnection conn, boolean trustAllCertificates)
throws NoSuchAlgorithmException, KeyManagementException {
if (trustAllCertificates) {
SSLContext ctx = SSLContext.getInstance("TLS");
ctx.init(new KeyManager[0], new TrustManager[]{new NaiveTrustManager()}, new SecureRandom());
// SSLContext.setDefault(ctx);
conn.setSSLSocketFactory(ctx.getSocketFactory());
// Trust every hostname
HostnameVerifier allHostsValid = (hostname, session) -> true;
conn.setHostnameVerifier(allHostsValid);
}
}
/**
* Validates the given address to see that it's well-formed, and is reachable.
*
* @param address
* Remote address to be validated
* @return FormValidation object
*/
@Restricted(NoExternalUse.class)
public FormValidation doCheckAddress(@QueryParameter String address, @QueryParameter boolean trustAllCertificates) {
URL host = null;
// no empty addresses allowed
if (address == null || address.trim().equals("")) {
return FormValidation.warning("The remote address can not be empty, or it must be overridden on the job configuration.");
}
// check if we have a valid, well-formed URL
try {
host = new URL(address);
host.toURI();
} catch (Exception e) {
return FormValidation.error("Malformed address (" + address + "). Remember to indicate the protocol, i.e. http, https, etc.");
}
// check that the host is reachable
try {
HttpsURLConnection conn = (HttpsURLConnection) host.openConnection();
try {
makeConnectionTrustAllCertificates(conn, trustAllCertificates);
} catch (NoSuchAlgorithmException | KeyManagementException e) {
return FormValidation.error(e, "A key management error occurred.");
}
conn.setConnectTimeout(5000);
conn.connect();
if (trustAllCertificates) {
return FormValidation.warning(
"Connection established! Accepting all certificates is potentially unsafe."
);
}
} catch (Exception e) {
return FormValidation.warning("Address looks good, but a connection could not be established.");
}
return FormValidation.ok();
}
public static List<Auth2Descriptor> getAuth2Descriptors() {
return Auth2.all();
}
public static Auth2Descriptor getDefaultAuth2Descriptor() {
return NoneAuth.DESCRIPTOR;
}
}
@Override
public RemoteJenkinsServer clone() throws CloneNotSupportedException {
RemoteJenkinsServer clone = (RemoteJenkinsServer)super.clone();
clone.auth2 = (auth2 == null) ? null : auth2.clone();
return clone;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((address == null) ? 0 : address.hashCode());
result = prime * result + ((auth2 == null) ? 0 : auth2.hashCode());
result = prime * result + ((displayName == null) ? 0 : displayName.hashCode());
result = prime * result + (hasBuildTokenRootSupport ? 1231 : 1237);
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (! (obj instanceof RemoteJenkinsServer))
return false;
RemoteJenkinsServer other = (RemoteJenkinsServer) obj;
if (address == null) {
if (other.address != null)
return false;
} else if (!address.equals(other.address))
return false;
if (auth2 == null) {
if (other.auth2 != null)
return false;
} else if (!auth2.equals(other.auth2))
return false;
if (displayName == null) {
if (other.displayName != null)
return false;
} else if (!displayName.equals(other.displayName))
return false;
if (hasBuildTokenRootSupport != other.hasBuildTokenRootSupport)
return false;
return true;
}
}