/
LDAPConfiguration.java
606 lines (542 loc) · 23.7 KB
/
LDAPConfiguration.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
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
/*
* The MIT License
*
* Copyright (c) 2004-2010, Sun Microsystems, Inc., Kohsuke Kawaguchi, Seiji Sogabe,
* Olivier Lamy
* Copyright (c) 2017 CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package jenkins.security.plugins.ldap;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import groovy.lang.Binding;
import hudson.DescriptorExtensionList;
import hudson.Extension;
import hudson.Util;
import hudson.model.AbstractDescribableImpl;
import hudson.model.Descriptor;
import hudson.security.LDAPSecurityRealm;
import hudson.security.SecurityRealm;
import hudson.util.FormValidation;
import hudson.util.Secret;
import hudson.util.spring.BeanBuilder;
import jenkins.model.Jenkins;
import org.acegisecurity.ldap.InitialDirContextFactory;
import org.acegisecurity.ldap.LdapTemplate;
import org.acegisecurity.ldap.search.FilterBasedLdapUserSearch;
import org.acegisecurity.providers.ldap.LdapAuthoritiesPopulator;
import org.apache.commons.codec.Charsets;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.input.AutoCloseInputStream;
import org.apache.commons.lang.StringUtils;
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 org.springframework.web.context.WebApplicationContext;
import javax.annotation.Nonnull;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static hudson.Util.fixEmpty;
import static hudson.Util.fixEmptyAndTrim;
import static hudson.Util.fixNull;
import static org.apache.commons.lang.StringUtils.isBlank;
import static org.apache.commons.lang.StringUtils.isNotBlank;
/**
* A configuration for one ldap connection
*/
public class LDAPConfiguration extends AbstractDescribableImpl<LDAPConfiguration> {
private static final Logger LOGGER = LDAPSecurityRealm.LOGGER;
@Restricted(NoExternalUse.class)
public static final String SECURITY_REALM_LDAPBIND_GROOVY = "LDAPBindSecurityRealm.groovy";
/**
* LDAP server name(s) separated by spaces, optionally with TCP port number, like "ldap.acme.org"
* or "ldap.acme.org:389" and/or with protcol, like "ldap://ldap.acme.org".
*/
private final String server;
/**
* The root DN to connect to. Normally something like "dc=sun,dc=com"
*/
private final String rootDN;
/**
* Allow the rootDN to be inferred? Default is false.
* If true, allow rootDN to be blank.
*/
private final boolean inhibitInferRootDN;
private String userSearchBase;
private String userSearch;
private String groupSearchBase;
private String groupSearchFilter;
private LDAPGroupMembershipStrategy groupMembershipStrategy;
/**
* If non-null, we use this and {@link #getManagerPassword()}
* when binding to LDAP.
*
* This is necessary when LDAP doesn't support anonymous access.
*/
private final String managerDN;
/**
* Password used to first bind to LDAP.
*/
private final Secret managerPasswordSecret;
private String displayNameAttributeName;
private String mailAddressAttributeName;
private Map<String,String> extraEnvVars;
/**
* Set in {@link #createApplicationContext(LDAPSecurityRealm, boolean)}
*/
private transient LDAPExtendedTemplate ldapTemplate;
private transient String id;
@DataBoundConstructor
public LDAPConfiguration(@Nonnull String server, String rootDN, boolean inhibitInferRootDN, String managerDN, Secret managerPasswordSecret) {
this.server = server.trim();
this.managerDN = fixEmpty(managerDN);
this.managerPasswordSecret = managerPasswordSecret;
this.inhibitInferRootDN = inhibitInferRootDN;
if (!inhibitInferRootDN && fixEmptyAndTrim(rootDN) == null) {
rootDN = fixNull(inferRootDN(server));
}
this.rootDN = rootDN;
this.displayNameAttributeName = LDAPSecurityRealm.DescriptorImpl.DEFAULT_DISPLAYNAME_ATTRIBUTE_NAME;
this.mailAddressAttributeName = LDAPSecurityRealm.DescriptorImpl.DEFAULT_MAILADDRESS_ATTRIBUTE_NAME;
this.userSearchBase = "";
this.userSearch = LDAPSecurityRealm.DescriptorImpl.DEFAULT_USER_SEARCH;
this.groupMembershipStrategy = new FromGroupSearchLDAPGroupMembershipStrategy("");
this.groupSearchBase = "";
}
/**
* LDAP server name(s) separated by spaces, optionally with TCP port number, like "ldap.acme.org"
* or "ldap.acme.org:389" and/or with protcol, like "ldap://ldap.acme.org".
*/
public String getServer() {
return server;
}
public String getServerUrl() {
StringBuilder buf = new StringBuilder();
boolean first = true;
for (String s : Util.fixNull(server).split("\\s+")) {
if (s.trim().length() == 0) continue;
if (first) first = false;
else buf.append(' ');
buf.append(addPrefix(s));
}
return buf.toString();
}
/**
* The root DN to connect to. Normally something like "dc=sun,dc=com"
*/
public String getRootDN() {
return rootDN;
}
public String getLDAPURL() {
return LDAPSecurityRealm.toProviderUrl(getServerUrl(), fixNull(rootDN));
}
/**
* Allow the rootDN to be inferred? Default is false.
* If true, allow rootDN to be blank.
*/
public boolean isInhibitInferRootDN() {
return inhibitInferRootDN;
}
/**
* Specifies the relative DN from {@link #rootDN the root DN}.
* This is used to narrow down the search space when doing user search.
*
* Something like "ou=people" but can be empty.
*/
public String getUserSearchBase() {
return userSearchBase;
}
/**
* Specifies the relative DN from {@link #rootDN the root DN}.
* This is used to narrow down the search space when doing user search.
*
* Something like "ou=people" but can be empty.
*/
@DataBoundSetter
public void setUserSearchBase(String userSearchBase) {
this.userSearchBase = fixNull(userSearchBase).trim();
}
/**
* Query to locate an entry that identifies the user, given the user name string.
*
* Normally "uid={0}"
*
* @see FilterBasedLdapUserSearch
*/
public String getUserSearch() {
return userSearch;
}
/**
* Query to locate an entry that identifies the user, given the user name string.
*
* Normally "uid={0}"
*
* @see FilterBasedLdapUserSearch
*/
@DataBoundSetter
public void setUserSearch(String userSearch) {
userSearch = fixEmptyAndTrim(userSearch);
this.userSearch = userSearch != null ? userSearch : LDAPSecurityRealm.DescriptorImpl.DEFAULT_USER_SEARCH;
}
/**
* This defines the organizational unit that contains groups.
*
* Normally "" to indicate the full LDAP search, but can be often narrowed down to
* something like "ou=groups"
*
* @see FilterBasedLdapUserSearch
*/
public String getGroupSearchBase() {
return groupSearchBase;
}
/**
* This defines the organizational unit that contains groups.
*
* Normally "" to indicate the full LDAP search, but can be often narrowed down to
* something like "ou=groups"
*
* @see FilterBasedLdapUserSearch
*/
@DataBoundSetter
public void setGroupSearchBase(String groupSearchBase) {
this.groupSearchBase = fixEmptyAndTrim(groupSearchBase);
}
/**
* Query to locate an entry that identifies the group, given the group name string. If non-null it will override
* the default specified by {@link LDAPSecurityRealm#GROUP_SEARCH}
*
*/
public String getGroupSearchFilter() {
return groupSearchFilter;
}
/**
* Query to locate an entry that identifies the group, given the group name string. If non-null it will override
* the default specified by {@link LDAPSecurityRealm#GROUP_SEARCH}
*
*/
@DataBoundSetter
public void setGroupSearchFilter(String groupSearchFilter) {
this.groupSearchFilter = fixEmptyAndTrim(groupSearchFilter);
}
public LDAPGroupMembershipStrategy getGroupMembershipStrategy() {
return groupMembershipStrategy;
}
@DataBoundSetter
public void setGroupMembershipStrategy(LDAPGroupMembershipStrategy groupMembershipStrategy) {
this.groupMembershipStrategy = groupMembershipStrategy == null ? new FromGroupSearchLDAPGroupMembershipStrategy("") : groupMembershipStrategy;
}
/**
* If non-null, we use this and {@link #getManagerPassword()}
* when binding to LDAP.
*
* This is necessary when LDAP doesn't support anonymous access.
*/
public String getManagerDN() {
return managerDN;
}
/**
* Password used to first bind to LDAP.
*/
public String getManagerPassword() {
return Secret.toString(managerPasswordSecret);
}
public Secret getManagerPasswordSecret() {
return managerPasswordSecret;
}
public String getDisplayNameAttributeName() {
return StringUtils.defaultString(displayNameAttributeName, LDAPSecurityRealm.DescriptorImpl.DEFAULT_DISPLAYNAME_ATTRIBUTE_NAME);
}
@DataBoundSetter
public void setDisplayNameAttributeName(String displayNameAttributeName) {
this.displayNameAttributeName = displayNameAttributeName;
}
public String getMailAddressAttributeName() {
return StringUtils.defaultString(mailAddressAttributeName, LDAPSecurityRealm.DescriptorImpl.DEFAULT_MAILADDRESS_ATTRIBUTE_NAME);
}
@DataBoundSetter
public void setMailAddressAttributeName(String mailAddressAttributeName) {
this.mailAddressAttributeName = mailAddressAttributeName;
}
public Map<String,String> getExtraEnvVars() {
return extraEnvVars == null || extraEnvVars.isEmpty()
? Collections.<String,String>emptyMap()
: Collections.unmodifiableMap(extraEnvVars);
}
@Restricted(NoExternalUse.class) //Only for migration
public void setExtraEnvVars(Map<String,String> extraEnvVars) {
this.extraEnvVars = extraEnvVars;
}
public LDAPSecurityRealm.EnvironmentProperty[] getEnvironmentProperties() {
if (extraEnvVars == null || extraEnvVars.isEmpty()) {
return new LDAPSecurityRealm.EnvironmentProperty[0];
}
LDAPSecurityRealm.EnvironmentProperty[] result = new LDAPSecurityRealm.EnvironmentProperty[extraEnvVars.size()];
int i = 0;
for (Map.Entry<String,String> entry: extraEnvVars.entrySet()) {
result[i++] = new LDAPSecurityRealm.EnvironmentProperty(entry.getKey(), entry.getValue());
}
return result;
}
@DataBoundSetter
public void setEnvironmentProperties(LDAPSecurityRealm.EnvironmentProperty[] environmentProperties) {
this.extraEnvVars = environmentProperties == null || environmentProperties.length == 0
? null
: LDAPSecurityRealm.EnvironmentProperty.toMap(Arrays.asList(environmentProperties));
}
public String getId() {
if (StringUtils.isEmpty(this.id)) {
this.id = generateId();
}
return this.id;
}
public boolean isConfiguration(String id) {
return getId().equals(id);
}
@Extension
public static final class LDAPConfigurationDescriptor extends Descriptor<LDAPConfiguration> {
//For jelly usage
public static final String DEFAULT_DISPLAYNAME_ATTRIBUTE_NAME = LDAPSecurityRealm.DescriptorImpl.DEFAULT_DISPLAYNAME_ATTRIBUTE_NAME;
public static final String DEFAULT_MAILADDRESS_ATTRIBUTE_NAME = LDAPSecurityRealm.DescriptorImpl.DEFAULT_MAILADDRESS_ATTRIBUTE_NAME;
public static final String DEFAULT_USER_SEARCH = LDAPSecurityRealm.DescriptorImpl.DEFAULT_USER_SEARCH;
@Override
public String getDisplayName() {
return "ldap";
}
public boolean noCustomBindScript() {
return !getLdapBindOverrideFile(Jenkins.getActiveInstance()).exists();
}
// note that this works better in 1.528+ (JENKINS-19124)
@SuppressFBWarnings(value = "RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE", justification = "Only on newer core versions") //TODO remove when core is bumped
public FormValidation doCheckServer(@QueryParameter String value, @QueryParameter String managerDN, @QueryParameter Secret managerPasswordSecret) {
String server = value;
String managerPassword = Secret.toString(managerPasswordSecret);
final Jenkins jenkins = Jenkins.getInstance();
if (jenkins == null) {
return FormValidation.error("Jenkins is not ready. Cannot validate the field");
}
if(!jenkins.hasPermission(Jenkins.ADMINISTER))
return FormValidation.ok();
try {
Hashtable<String,String> props = new Hashtable<String,String>();
if(managerDN!=null && managerDN.trim().length() > 0 && !"undefined".equals(managerDN)) {
props.put(Context.SECURITY_PRINCIPAL,managerDN);
}
if(managerPassword!=null && managerPassword.trim().length() > 0 && !"undefined".equals(managerPassword)) {
props.put(Context.SECURITY_CREDENTIALS,managerPassword);
}
props.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
props.put(Context.PROVIDER_URL, LDAPSecurityRealm.toProviderUrl(server, ""));
DirContext ctx = new InitialDirContext(props);
ctx.getAttributes("");
return FormValidation.ok(); // connected
} catch (NamingException e) {
// trouble-shoot
Matcher m = Pattern.compile("(ldaps?://)?([^:]+)(?:\\:(\\d+))?(\\s+(ldaps?://)?([^:]+)(?:\\:(\\d+))?)*").matcher(server.trim());
if(!m.matches())
return FormValidation.error(hudson.security.Messages.LDAPSecurityRealm_SyntaxOfServerField());
try {
InetAddress adrs = InetAddress.getByName(m.group(2));
int port = m.group(1)!=null ? 636 : 389;
if(m.group(3)!=null)
port = Integer.parseInt(m.group(3));
Socket s = new Socket(adrs,port);
s.close();
} catch (UnknownHostException x) {
return FormValidation.error(hudson.security.Messages.LDAPSecurityRealm_UnknownHost(x.getMessage()));
} catch (IOException x) {
return FormValidation.error(x, hudson.security.Messages.LDAPSecurityRealm_UnableToConnect(server, x.getMessage()));
}
// otherwise we don't know what caused it, so fall back to the general error report
// getMessage() alone doesn't offer enough
return FormValidation.error(e, hudson.security.Messages.LDAPSecurityRealm_UnableToConnect(server, e));
} catch (NumberFormatException x) {
// The getLdapCtxInstance method throws this if it fails to parse the port number
return FormValidation.error(hudson.security.Messages.LDAPSecurityRealm_InvalidPortNumber());
}
}
@SuppressFBWarnings(value = "RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE", justification = "Only on newer core versions") //TODO remove when core is bumped
public DescriptorExtensionList<LDAPGroupMembershipStrategy, Descriptor<LDAPGroupMembershipStrategy>> getGroupMembershipStrategies() {
final Jenkins jenkins = Jenkins.getInstance();
if (jenkins != null) {
return jenkins.getDescriptorList(LDAPGroupMembershipStrategy.class);
} else {
return DescriptorExtensionList.createDescriptorList((Jenkins)null, LDAPGroupMembershipStrategy.class);
}
}
}
/**
* Infer the root DN.
*
* @return null if not found.
*/
private String inferRootDN(String server) {
try {
Hashtable<String, String> props = new Hashtable<String, String>();
if (managerDN != null) {
props.put(Context.SECURITY_PRINCIPAL, managerDN);
props.put(Context.SECURITY_CREDENTIALS, getManagerPassword());
}
props.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
props.put(Context.PROVIDER_URL, LDAPSecurityRealm.toProviderUrl(getServerUrl(), ""));
DirContext ctx = new InitialDirContext(props);
Attributes atts = ctx.getAttributes("");
Attribute a = atts.get("defaultNamingContext");
if (a != null && a.get() != null) // this entry is available on Active Directory. See http://msdn2.microsoft.com/en-us/library/ms684291(VS.85).aspx
return a.get().toString();
a = atts.get("namingcontexts");
if (a == null) {
LOGGER.warning("namingcontexts attribute not found in root DSE of " + server);
return null;
}
return a.get().toString();
} catch (NamingException e) {
LOGGER.log(Level.WARNING, "Failed to connect to LDAP to infer Root DN for " + server, e);
return null;
}
}
/**
* If the given "server name" is just a host name (plus optional host name), add ldap:// prefix.
* Otherwise assume it already contains the scheme, and leave it intact.
*/
private static String addPrefix(String server) {
if (server.contains("://")) return server;
else return "ldap://" + server;
}
private String generateId() {
return generateId(server, rootDN, userSearchBase, userSearch);
}
@Restricted(NoExternalUse.class)
static String generateId(String serverUrl, String rootDN, String userSearchBase, String userSearch) {
final MessageDigest digest = DigestUtils.getMd5Digest();
digest.update(normalizeServer(serverUrl).getBytes(Charsets.UTF_8));
String userSearchBaseNormalized = normalizeUserSearchBase(rootDN, userSearchBase);
if (isNotBlank(userSearchBaseNormalized)) {
digest.update(userSearchBaseNormalized.getBytes(Charsets.UTF_8));
} else {
digest.update(new byte[]{0});
}
if (isNotBlank(userSearch)) {
digest.update(userSearch.getBytes(Charsets.UTF_8));
} else {
digest.update(LDAPConfigurationDescriptor.DEFAULT_USER_SEARCH.getBytes(Charsets.UTF_8));
}
return Base64.encodeBase64String(digest.digest());
}
private static String normalizeUserSearchBase(String rootDN, String userSearchBase) {
if (isBlank(rootDN) && isBlank(userSearchBase)) {
return "";
}
if (isBlank(rootDN)) {
return userSearchBase;
}
if (isBlank(userSearchBase)) {
return rootDN;
}
rootDN = rootDN.trim();
userSearchBase = userSearchBase.trim();
return userSearchBase + "," + rootDN;
}
@Restricted(NoExternalUse.class)
static String normalizeServer(String server) { /*package scope for testing*/
String[] urls = Util.fixNull(server).split("\\s+");
List<String> normalised = new ArrayList<>(urls.length);
for (String url : urls) {
if (isBlank(url)) {
continue;
}
url = addPrefix(url);
try {
URI uri = new URI(url);
if (uri.getPort() < 0) {
uri = new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), 389, uri.getPath(), uri.getQuery(), uri.getFragment());
}
normalised.add(uri.toString());
} catch (URISyntaxException e) {
LOGGER.warning("Unable to parse " + url + " into an URI");
}
}
Collections.sort(normalised);
return StringUtils.join(normalised, ' ');
}
@Restricted(NoExternalUse.class)
public WebApplicationContext createApplicationContext(LDAPSecurityRealm realm, boolean usePotentialUserProvidedBinding) {
Binding binding = new Binding();
binding.setVariable("instance", this);
binding.setVariable("realmInstance", realm);
final Jenkins jenkins = Jenkins.getInstance();
if (jenkins == null) {
throw new IllegalStateException("Jenkins has not been started, or was already shut down");
}
BeanBuilder builder = new BeanBuilder(jenkins.pluginManager.uberClassLoader);
try {
File override = getLdapBindOverrideFile(jenkins);
if (usePotentialUserProvidedBinding && override.exists()) {
builder.parse(new AutoCloseInputStream(new FileInputStream(override)), binding);
} else {
if (override.exists()) {
LOGGER.warning("Not loading custom " + SECURITY_REALM_LDAPBIND_GROOVY);
}
builder.parse(new AutoCloseInputStream(LDAPSecurityRealm.class.getResourceAsStream(SECURITY_REALM_LDAPBIND_GROOVY)), binding);
}
} catch (FileNotFoundException e) {
throw new IllegalStateException("Failed to load "+ SECURITY_REALM_LDAPBIND_GROOVY, e);
}
WebApplicationContext appContext = builder.createApplicationContext();
ldapTemplate = new LDAPExtendedTemplate(SecurityRealm.findBean(InitialDirContextFactory.class, appContext));
if (groupMembershipStrategy != null) {
groupMembershipStrategy.setAuthoritiesPopulator(SecurityRealm.findBean(LdapAuthoritiesPopulator.class, appContext));
}
return appContext;
}
@Restricted(NoExternalUse.class)
public LDAPExtendedTemplate getLdapTemplate() {
return ldapTemplate;
}
@Restricted(NoExternalUse.class)
public static File getLdapBindOverrideFile(Jenkins jenkins) {
return new File(jenkins.getRootDir(), SECURITY_REALM_LDAPBIND_GROOVY);
}
}