forked from jenkinsci/email-ext-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMailAccount.java
More file actions
345 lines (300 loc) · 12.4 KB
/
MailAccount.java
File metadata and controls
345 lines (300 loc) · 12.4 KB
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
package hudson.plugins.emailext;
import com.cloudbees.plugins.credentials.CredentialsMatchers;
import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.CredentialsScope;
import com.cloudbees.plugins.credentials.SystemCredentialsProvider;
import com.cloudbees.plugins.credentials.common.StandardListBoxModel;
import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials;
import com.cloudbees.plugins.credentials.domains.DomainRequirement;
import com.cloudbees.plugins.credentials.domains.HostnamePortRequirement;
import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Extension;
import hudson.Util;
import hudson.model.AbstractDescribableImpl;
import hudson.model.Descriptor;
import hudson.model.Item;
import hudson.model.Queue;
import hudson.model.queue.Tasks;
import hudson.security.ACL;
import hudson.util.FormValidation;
import hudson.util.FormValidation.Kind;
import hudson.util.ListBoxModel;
import hudson.util.Secret;
import java.util.Collections;
import java.util.List;
import jenkins.model.Jenkins;
import jenkins.security.FIPS140;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.QueryParameter;
public class MailAccount extends AbstractDescribableImpl<MailAccount> {
private String address;
private String smtpHost;
private String smtpPort = "25";
private transient String smtpUsername;
private transient Secret smtpPassword;
private String credentialsId;
private boolean useSsl;
private boolean useTls;
private String advProperties;
private boolean defaultAccount;
private boolean useOAuth2;
@Deprecated
public MailAccount(JSONObject jo) {
address = Util.nullify(jo.optString("address", null));
smtpHost = Util.nullify(jo.optString("smtpHost", null));
smtpPort = Util.nullify(jo.optString("smtpPort", null));
if (jo.optBoolean("auth", false)) {
credentialsId = Util.nullify(jo.optString("credentialsId", null));
}
useSsl = jo.optBoolean("useSsl", false);
useTls = jo.optBoolean("useTls", false);
advProperties = Util.nullify(jo.optString("advProperties", null));
}
@DataBoundConstructor
public MailAccount() {}
public boolean isValid() {
return isFromAddressValid() && isSmtpServerValid() && isSecureAuthWhenFIPS();
}
public boolean isSecureAuthWhenFIPS() {
// when in FIPS mode if we are using authentication we must also use TLS or SSL to protect the password
return !(credentialsId != null && FIPS140.useCompliantAlgorithms() && !(useSsl || useTls));
}
public boolean isFromAddressValid() {
return isDefaultAccount() || StringUtils.isNotBlank(address);
}
public boolean isSmtpServerValid() {
return true;
// Note: having no SMTP server is fine, it means localhost.
// More control could be implemented here when not null though,
// like checking the value looks like an FQDN or IP address.
}
public boolean isSmtpAuthValid() {
return true;
// Note: we either have credentials or not, there isn't
// two pieces to look at
}
public boolean isDefaultAccount() {
return defaultAccount;
}
@DataBoundSetter
void setDefaultAccount(boolean defaultAccount) {
this.defaultAccount = defaultAccount;
}
@Override
public MailAccountDescriptor getDescriptor() {
return (MailAccountDescriptor) Jenkins.get().getDescriptor(getClass());
}
@Extension
public static class MailAccountDescriptor extends Descriptor<MailAccount> {
@NonNull
@Override
public String getDisplayName() {
return "";
}
@SuppressWarnings({"lgtm[jenkins/csrf]", "unused"}) // Used by stapler
public ListBoxModel doFillCredentialsIdItems(@AncestorInPath Item item, @QueryParameter String credentialsId) {
final StandardListBoxModel result = new StandardListBoxModel();
if (item == null) {
if (!Jenkins.get().hasPermission(Jenkins.ADMINISTER)) {
return result.includeCurrentValue(credentialsId);
}
} else {
if (!item.hasPermission(Item.EXTENDED_READ) && !item.hasPermission(CredentialsProvider.USE_ITEM)) {
return result.includeCurrentValue(credentialsId);
}
}
return result.includeEmptyValue()
.includeMatchingAs(
item instanceof Queue.Task t ? Tasks.getAuthenticationOf(t) : ACL.SYSTEM,
item,
StandardUsernamePasswordCredentials.class,
Collections.emptyList(),
CredentialsMatchers.instanceOf(StandardUsernamePasswordCredentials.class))
.includeCurrentValue(credentialsId);
}
@SuppressWarnings("lgtm[jenkins/csrf]")
public FormValidation doCheckCredentialsId(
@AncestorInPath Item item,
@QueryParameter String value,
@QueryParameter boolean useSsl,
@QueryParameter boolean useTls) {
if (item == null) {
if (!Jenkins.get().hasPermission(Jenkins.ADMINISTER)) {
return FormValidation.ok();
}
} else {
if (!item.hasPermission(Item.EXTENDED_READ) && !item.hasPermission(CredentialsProvider.USE_ITEM)) {
return FormValidation.ok();
}
}
if (StringUtils.isBlank(value)) {
return FormValidation.ok();
}
// do this after the authentication check so we do not reveal the FIPS mode of the controller.
FormValidation insecureAuthValidation;
if (useSsl || useTls) {
insecureAuthValidation = FormValidation.ok();
} else {
if (FIPS140.useCompliantAlgorithms()) {
insecureAuthValidation =
FormValidation.error("Authentication requires either TLS or SSL to be enabled");
} else {
insecureAuthValidation = FormValidation.warning(
"For security when using authentication it is recommended to enable either TLS or SSL");
}
}
if (CredentialsProvider.listCredentials(
StandardUsernamePasswordCredentials.class,
item,
item instanceof Queue.Task t ? Tasks.getAuthenticationOf2(t) : ACL.SYSTEM2,
null,
CredentialsMatchers.withId(value))
.isEmpty()) {
if (insecureAuthValidation.kind == Kind.OK) {
return FormValidation.error("Cannot find currently selected credentials");
}
return FormValidation.aggregate(List.of(
insecureAuthValidation, FormValidation.error("Cannot find currently selected credentials")));
}
return insecureAuthValidation;
}
}
public String getAddress() {
return address;
}
@DataBoundSetter
public void setAddress(String address) {
this.address = Util.fixEmptyAndTrim(address);
}
public String getSmtpHost() {
return smtpHost;
}
@DataBoundSetter
public void setSmtpHost(String smtpHost) {
this.smtpHost = Util.fixEmptyAndTrim(smtpHost);
}
public String getSmtpPort() {
return smtpPort;
}
@DataBoundSetter
public void setSmtpPort(String smtpPort) {
smtpPort = Util.fixEmptyAndTrim(smtpPort);
if (smtpPort != null) {
try {
int port = Integer.parseInt(smtpPort);
if (port < 1 || port > 65535) {
throw new IllegalArgumentException("SMTP port must be between 1 and 65535");
}
} catch (NumberFormatException e) {
throw new IllegalArgumentException("SMTP port must be a valid number");
}
}
this.smtpPort = smtpPort;
}
@Deprecated
public String getSmtpUsername() {
return smtpUsername;
}
@DataBoundSetter
public void setSmtpUsername(String smtpUsername) {
this.smtpUsername = Util.fixEmptyAndTrim(smtpUsername);
}
@Deprecated
public Secret getSmtpPassword() {
return smtpPassword;
}
@DataBoundSetter
public void setSmtpPassword(Secret smtpPassword) {
this.smtpPassword = smtpPassword;
}
public void setSmtpPassword(String smtpPassword) {
this.smtpPassword = Secret.fromString(smtpPassword);
}
public String getCredentialsId() {
if (StringUtils.isBlank(credentialsId) && StringUtils.isNotBlank(smtpUsername) && smtpPassword != null) {
migrateCredentials();
}
return credentialsId;
}
@DataBoundSetter
public void setCredentialsId(String credentialsId) {
this.credentialsId = Util.fixEmptyAndTrim(credentialsId);
}
public boolean isUseSsl() {
return useSsl;
}
@DataBoundSetter
public void setUseSsl(boolean useSsl) {
this.useSsl = useSsl;
}
public boolean isUseTls() {
return useTls;
}
@DataBoundSetter
public void setUseTls(boolean useTls) {
this.useTls = useTls;
}
public boolean isUseOAuth2() {
return useOAuth2;
}
@DataBoundSetter
public void setUseOAuth2(boolean useOAuth2) {
this.useOAuth2 = useOAuth2;
}
public String getAdvProperties() {
return advProperties;
}
@DataBoundSetter
public void setAdvProperties(String advProperties) {
this.advProperties = advProperties;
}
private Object readResolve() {
if (StringUtils.isBlank(credentialsId) && StringUtils.isNotBlank(smtpUsername) && smtpPassword != null) {
migrateCredentials();
}
return this;
}
private void migrateCredentials() {
DomainRequirement domainRequirement = null;
if (StringUtils.isNotBlank(smtpHost) && StringUtils.isNotBlank(smtpPort)) {
domainRequirement = new HostnamePortRequirement(smtpHost, Integer.parseInt(smtpPort));
}
final List<StandardUsernamePasswordCredentials> credentials = CredentialsMatchers.filter(
CredentialsProvider.lookupCredentials(
StandardUsernamePasswordCredentials.class, Jenkins.get(), ACL.SYSTEM2, domainRequirement),
CredentialsMatchers.withUsername(smtpUsername));
for (final StandardUsernamePasswordCredentials cred : credentials) {
if (smtpPassword.getPlainText().equals(Secret.toString(cred.getPassword()))) {
// If some credentials have the same username/password, use those.
credentialsId = cred.getId();
break;
}
}
if (StringUtils.isBlank(credentialsId)) {
// If we couldn't find any existing credentials,
// create new credentials with the principal and secret and use it.
final StandardUsernamePasswordCredentials newCredentials;
try {
newCredentials = new UsernamePasswordCredentialsImpl(
CredentialsScope.GLOBAL,
null,
"Migrated from email-ext username/password",
smtpUsername,
smtpPassword.getPlainText());
} catch (Descriptor.FormException e) {
// safe to ignore as too short password should happen only in FIPS mode
// and migrating from no FIPS to FIPS is not supported, but only fresh start
throw new RuntimeException("Password used for email-ext server configuration could not be migrated", e);
}
SystemCredentialsProvider.getInstance().getCredentials().add(newCredentials);
credentialsId = newCredentials.getId();
}
smtpUsername = null;
smtpPassword = null;
}
}