/
LdapGroupProvisioner.java
525 lines (422 loc) · 20.7 KB
/
LdapGroupProvisioner.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
package edu.internet2.middleware.grouper.pspng;
/*******************************************************************************
* Copyright 2015 Internet2
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.util.*;
import org.apache.commons.lang.StringUtils;
import org.ldaptive.AttributeModification;
import org.ldaptive.AttributeModificationType;
import org.ldaptive.Connection;
import org.ldaptive.LdapAttribute;
import org.ldaptive.LdapEntry;
import org.ldaptive.ModifyRequest;
import org.ldaptive.SearchFilter;
import org.ldaptive.SearchRequest;
import org.ldaptive.SearchResult;
import org.ldaptive.io.LdifReader;
import edu.internet2.middleware.subject.Subject;
/**
* This class is the workhorse for provisioning LDAP groups from
* grouper.
*
* @author bert
*
*/
public class LdapGroupProvisioner extends LdapProvisioner<LdapGroupProvisionerConfiguration> {
public LdapGroupProvisioner(String provisionerName, LdapGroupProvisionerConfiguration config) {
super(provisionerName, config);
LOG.debug("Constructing LdapGroupProvisioner: {}", provisionerName);
}
public static Class<? extends ProvisionerConfiguration> getPropertyClass() {
return LdapGroupProvisionerConfiguration.class;
}
@Override
protected void addMembership(GrouperGroupInfo grouperGroupInfo, LdapGroup ldapGroup,
Subject subject, LdapUser ldapUser) throws PspException {
// TODO: Look in memory cache to see if change is necessary:
// a) User object's group-listing attribute
// or b) if the group-membership attribute is being fetched
if ( ldapUser == null ) {
LOG.warn("{}: Skipping adding membership to group {} because ldap user does not exist: {}",
new Object[]{getName(), grouperGroupInfo, subject});
return;
}
if ( ldapGroup == null ) {
// Create the group if it hasn't been created yet. List the user so that creation can be combined
// with membership addition
// This will normally occur when the schema requires members and group-creation is delayed until
// the this method is being called to add the first member
ldapGroup = createGroup(grouperGroupInfo, Arrays.asList(subject));
cacheGroup(grouperGroupInfo, ldapGroup);
}
else {
String membershipAttributeValue = evaluateJexlExpression(config.getMemberAttributeValueFormat(), subject, ldapUser, grouperGroupInfo, ldapGroup);
if ( membershipAttributeValue != null ) {
scheduleGroupModification(grouperGroupInfo, ldapGroup, AttributeModificationType.ADD, Arrays.asList(membershipAttributeValue));
}
}
}
protected void scheduleGroupModification(GrouperGroupInfo grouperGroupInfo, LdapGroup ldapGroup, AttributeModificationType modType, Collection<String> membershipValuesToChange) {
uncacheGroup(grouperGroupInfo, ldapGroup);
String attributeName = config.getMemberAttributeName();
for ( String value : membershipValuesToChange )
// ADD/REMOVE <value> to/from <attribute> of <group>
LOG.info("Will change LDAP: {} {} {} {} of {}",
new Object[] {modType, value,
modType == AttributeModificationType.ADD ? "to" : "from",
attributeName, ldapGroup});
scheduleLdapModification(
new ModifyRequest(
ldapGroup.getLdapObject().getDn(),
new AttributeModification(
modType,
new LdapAttribute(attributeName, membershipValuesToChange.toArray(new String[0])))));
}
@Override
protected void deleteMembership(GrouperGroupInfo grouperGroupInfo, LdapGroup ldapGroup ,
Subject subject, LdapUser ldapUser) throws PspException {
if ( ldapGroup == null ) {
LOG.warn("{}: Ignoring request to remove {} from a group that doesn't exist: {}",
new Object[]{getName(), subject.getId(), grouperGroupInfo});
return;
}
if ( ldapUser == null ) {
LOG.warn("{}: Skipping removing membership from group {} because ldap user does not exist: {}",
new Object[]{getName(), grouperGroupInfo, subject});
return;
}
// TODO: Look in memory cache to see if change is necessary:
// a) User object's group-listing attribute
// or b) if the group-membership attribute is being fetched
String membershipAttributeValue = evaluateJexlExpression(config.getMemberAttributeValueFormat(), subject, ldapUser, grouperGroupInfo, ldapGroup);
if ( membershipAttributeValue != null ) {
scheduleGroupModification(grouperGroupInfo, ldapGroup, AttributeModificationType.REMOVE, Arrays.asList(membershipAttributeValue));
}
}
/**
* Get a string set that is case-insensitive or case-sensitive
* depending on the provisioner's configuration.isMemberAttributeCaseSensitive
*/
protected Set<String> getStringSet() {
if ( config.isMemberAttributeCaseSensitive() )
return new HashSet<String>();
else
return new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
}
/**
* Get a string set that is case-insensitive or case-sensitive,
* depending on the provisioner's configuration.isMemberAttributeCaseSensitive.
*
* The returned set will contain the values provided
*/
protected Set<String> getStringSet(Collection<String> values ) {
Set<String> result = getStringSet();
if ( values != null ) {
result.addAll(values);
}
return result;
}
@Override
protected void doFullSync(
GrouperGroupInfo grouperGroupInfo, LdapGroup ldapGroup ,
Set<Subject> correctSubjects, Map<Subject, LdapUser> tsUserMap,
Set<LdapUser> correctTSUsers,
JobStatistics stats) throws PspException {
stats.totalCount.set(correctSubjects.size());
// If the group does not exist yet, then create it with all the correct members
if ( ldapGroup == null ) {
// If the schema requires member attribute, then don't do anything if there aren't any members
if ( config.areEmptyGroupsSupported() ) {
if ( correctSubjects.size() == 0 ) {
LOG.info("{}: Nothing to do because empty group already not present in ldap system", getName() );
return;
}
}
ldapGroup = createGroup(grouperGroupInfo, correctSubjects);
stats.insertCount.set(correctSubjects.size());
cacheGroup(grouperGroupInfo, ldapGroup);
return;
}
// Delete an empty group if the schema requires a membership
if ( !config.areEmptyGroupsSupported() && correctSubjects.size() == 0 ) {
LOG.info("{}: Deleting empty group because schema requires its member attribute", getName());
deleteGroup(grouperGroupInfo, ldapGroup);
// Update stats with the number of values removed by group deletion
Collection<String> membershipValues = ldapGroup.getLdapObject().getStringValues(config.getMemberAttributeName());
stats.deleteCount.set(membershipValues.size());
}
Set<String> correctMembershipValues = getStringSet();
for ( Subject correctSubject: correctSubjects ) {
String membershipAttributeValue = evaluateJexlExpression(config.getMemberAttributeValueFormat(), correctSubject, tsUserMap.get(correctSubject), grouperGroupInfo, ldapGroup);
if ( membershipAttributeValue != null ) {
correctMembershipValues.add(membershipAttributeValue);
}
}
Collection<String> currentMembershipValues = getStringSet(ldapGroup.getLdapObject().getStringValues(config.getMemberAttributeName()));
// EXTRA = CURRENT - CORRECT
{
Collection<String> extraValues = getStringSet(currentMembershipValues);
extraValues.removeAll(correctMembershipValues);
stats.deleteCount.set(extraValues.size());
LOG.info("{}: Group {} has {} extra values",
new Object[] {getName(), grouperGroupInfo, extraValues.size()});
if ( extraValues.size() > 0 )
scheduleGroupModification(grouperGroupInfo, ldapGroup, AttributeModificationType.REMOVE, extraValues);
}
// MISSING = CORRECT - CURRENT
{
Collection<String> missingValues = getStringSet(correctMembershipValues);
missingValues.removeAll(currentMembershipValues);
stats.insertCount.set(missingValues.size());
LOG.info("{}: Group {} has {} missing values",
new Object[]{getName(), grouperGroupInfo, missingValues.size()});
if ( missingValues.size() > 0 )
scheduleGroupModification(grouperGroupInfo, ldapGroup, AttributeModificationType.ADD, missingValues);
}
}
@Override
protected void doFullSync_cleanupExtraGroups(
Set<GrouperGroupInfo> groupsForThisProvisioner,
Map<GrouperGroupInfo, LdapGroup> ldapGroups,
JobStatistics stats) throws PspException {
// Grab all the DNs that match the groupsForThisProvisioner
Set<String> desiredGroupDns = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
for ( LdapGroup ldapGroup : ldapGroups.values() )
desiredGroupDns.add( ldapGroup.getLdapObject().getDn());
String filterString = config.getAllGroupSearchFilter();
if ( StringUtils.isEmpty(filterString) ) {
LOG.error("{}: Cannot cleanup extra groups without a configured all-group search filter", getName());
return;
}
String baseDn = config.getGroupSearchBaseDn();
if ( StringUtils.isEmpty(baseDn)) {
LOG.error("{}: Cannot cleanup extra groups without a configured group-search base dn", getName());
return;
}
// Get all the LDAP Groups that match the filter
List<LdapObject> searchResult
= getLdapSystem().performLdapSearchRequest(new SearchRequest(baseDn, filterString,
getLdapAttributesToFetch()));
List<LdapObject> groupsToDelete = new ArrayList<LdapObject>();
for ( LdapObject existingGroup : searchResult ) {
String existingGroupDn = existingGroup.getDn();
if ( !desiredGroupDns.contains(existingGroupDn) ) {
groupsToDelete.add(existingGroup);
}
}
LOG.info("{}: There are {} groups that we should delete", getName(), groupsToDelete.size());
int numMembershipsBeingDeleted = 0;
for ( LdapObject groupToRemove : groupsToDelete ) {
numMembershipsBeingDeleted += groupToRemove.getStringValues(config.getMemberAttributeName()).size();
getLdapSystem().performLdapDelete(groupToRemove.getDn());
}
stats.deleteCount.addAndGet(numMembershipsBeingDeleted);
}
@Override
protected LdapGroup createGroup(GrouperGroupInfo grouperGroup, Collection<Subject> initialMembers) throws PspException {
if ( !config.areEmptyGroupsSupported() && initialMembers.size() == 0 ) {
LOG.warn("Not Creating LDAP group because empty groups are not supported: {}", grouperGroup);
return null;
}
LOG.info("Creating LDAP group for GrouperGroup: {} ", grouperGroup);
String ldif = config.getGroupCreationLdifTemplate();
ldif = ldif.replaceAll("\\|\\|", "\n");
ldif = evaluateJexlExpression(ldif, null, null, grouperGroup, null);
// If initialMembers were specified, then add the ldif necessary to include them
if ( initialMembers != null && initialMembers.size() > 0 ) {
// Find all the values for the membership attribute
Collection<String> membershipValues = new HashSet<String>(initialMembers.size());
for ( Subject subject : initialMembers ) {
LdapUser ldapUser = getTargetSystemUser(subject);
if ( ldapUser != null ) {
String membershipAttributeValue = evaluateJexlExpression(config.getMemberAttributeValueFormat(), subject, ldapUser, grouperGroup, null);
if ( membershipAttributeValue != null ) {
membershipValues.add(membershipAttributeValue);
}
}
}
StringBuilder ldifForMemberships = new StringBuilder();
for ( String attributeValue : membershipValues ) {
ldifForMemberships.append(String.format("%s: %s\n", config.getMemberAttributeName(), attributeValue));
}
ldif = ldif.concat("\n");
ldif = ldif.concat(ldifForMemberships.toString());
}
Connection conn = getLdapSystem().getLdapConnection();
try {
LOG.debug("{}: LDIF for new group (with partial DN): {}", getName(), ldif.replaceAll("\\n", "||"));
Reader reader = new StringReader(ldif);
LdifReader ldifReader = new LdifReader(reader);
SearchResult ldifResult = ldifReader.read();
LdapEntry ldifEntry = ldifResult.getEntry();
// Update DN to be relative to groupCreationBaseDn
String actualDn = String.format("%s,%s", ldifEntry.getDn(),config.getGroupCreationBaseDn());
ldifEntry.setDn(actualDn);
LOG.debug("{}: Adding group: {}", getName(), ldifEntry);
performLdapAdd(ldifEntry);
// Read the group that was just created
LOG.debug("Reading group that was just added to ldap server: {}", grouperGroup);
return fetchTargetSystemGroup(grouperGroup);
} catch (PspException e) {
LOG.error("Problem while creating new group: {}", ldif, e);
throw e;
} catch ( IOException e ) {
LOG.error("IO problem while creating group: {}", ldif, e);
throw new PspException("IO problem while creating group: %s", e.getMessage());
}
finally {
conn.close();
}
}
@Override
protected Map<GrouperGroupInfo, LdapGroup> fetchTargetSystemGroups(
Collection<GrouperGroupInfo> grouperGroupsToFetch) throws PspException {
if ( grouperGroupsToFetch.size() > config.getGroupSearch_batchSize() )
throw new IllegalArgumentException("LdapGroupProvisioner.fetchTargetSystemGroups: invoked with too many groups to fetch");
// If this is a full-sync provisioner, then we want to make sure we get the member attribute of the
// group so we see all members.
String[] returnAttributes = getLdapAttributesToFetch();
if ( config.isBulkGroupSearchingEnabled() ) {
StringBuilder combinedLdapFilter = new StringBuilder();
// Start the combined ldap filter as an OR-query
combinedLdapFilter.append("(|");
for (GrouperGroupInfo grouperGroup : grouperGroupsToFetch) {
SearchFilter f = getGroupLdapFilter(grouperGroup);
String groupFilterString = f.format();
// Wrap the subject's filter in (...) if it doesn't start with (
if (groupFilterString.startsWith("("))
combinedLdapFilter.append(groupFilterString);
else
combinedLdapFilter.append('(').append(groupFilterString).append(')');
}
combinedLdapFilter.append(')');
// Actually do the search
List<LdapObject> searchResult;
LOG.debug("{}: Searching for {} groups with:: {}",
new Object[]{getName(), grouperGroupsToFetch.size(), combinedLdapFilter});
try {
searchResult = getLdapSystem().performLdapSearchRequest(
new SearchRequest(config.getGroupSearchBaseDn(),
combinedLdapFilter.toString(),
returnAttributes));
} catch (PspException e) {
LOG.error("Problem fetching groups with filter '{}' on base '{}'",
new Object[]{combinedLdapFilter, config.getGroupSearchBaseDn(), e});
throw e;
}
LOG.debug("{}: Group search returned {} groups", getName(), searchResult.size());
// Now we have a bag of LdapObjects, but we don't know which goes with which grouperGroup.
// We're going to go through the Grouper Groups and their filters and compare
// them to the Ldap data we've fetched into memory.
Map<GrouperGroupInfo, LdapGroup> result = new HashMap<GrouperGroupInfo, LdapGroup>();
Set<LdapObject> matchedFetchResults = new HashSet<LdapObject>();
// For every group we tried to bulk fetch, find the matching LdapObject that came back
for (GrouperGroupInfo groupToFetch : grouperGroupsToFetch) {
SearchFilter f = getGroupLdapFilter(groupToFetch);
for (LdapObject aFetchedLdapObject : searchResult) {
if (aFetchedLdapObject.matchesLdapFilter(f)) {
result.put(groupToFetch, new LdapGroup(aFetchedLdapObject));
matchedFetchResults.add(aFetchedLdapObject);
break;
}
}
}
Set<LdapObject> unmatchedFetchResults = new HashSet<LdapObject>(searchResult);
unmatchedFetchResults.removeAll(matchedFetchResults);
for (LdapObject unmatchedFetchResult : unmatchedFetchResults) {
LOG.warn("{}: Bulk fetch failed (returned unmatchable group data). "
+ "This can be caused by searching for a DN with escaping or by singleGroupSearchFilter ({}) that are not included "
+ "in groupSearchAttributes ({})?): {}",
new Object[]{getName(), config.getSingleGroupSearchFilter(), config.getGroupSearchAttributes(), unmatchedFetchResult.getDn()});
LOG.warn("{}: Slower fetching will be attempted", getName());
}
// We're done if everything matched up
if ( unmatchedFetchResults.size() == 0 ) {
return result;
}
else {
// Fall through to the one-by-one group searching below. This is slower, but doesn't require the
// result-matching step that just failed
}
}
// Do simple ldap searching
Map<GrouperGroupInfo, LdapGroup> result = new HashMap<GrouperGroupInfo, LdapGroup>();
for (GrouperGroupInfo grouperGroup : grouperGroupsToFetch) {
SearchFilter groupLdapFilter = getGroupLdapFilter(grouperGroup);
try {
LOG.debug("{}: Searching for group {} with:: {}",
new Object[]{getName(), grouperGroup, groupLdapFilter});
// Actually do the search
List<LdapObject> searchResult = getLdapSystem().performLdapSearchRequest(
new SearchRequest(config.getGroupSearchBaseDn(),
groupLdapFilter,
returnAttributes));
if (searchResult.size() == 1) {
LdapObject ldapObject = searchResult.iterator().next();
LOG.debug("{}: Group search returned {}", getName(), ldapObject.getDn());
result.put(grouperGroup, new LdapGroup(ldapObject));
}
else if ( searchResult.size() > 1 ){
LOG.error("{}: Search for group {} with '{}' returned multiple matches: {}",
new Object[]{getName(), grouperGroup, groupLdapFilter, searchResult});
throw new PspException("Search for ldap group returned multiple matches");
}
else if ( searchResult.size() == 0 ) {
// No match found ==> result will not include an entry for this grouperGroup
}
} catch (PspException e) {
LOG.error("{}: Problem fetching group with filter '{}' on base '{}'",
new Object[]{getName(), groupLdapFilter, config.getGroupSearchBaseDn(), e});
throw e;
}
}
return result;
}
private String[] getLdapAttributesToFetch() {
String returnAttributes[] = config.getGroupSearchAttributes();
if ( fullSyncMode ) {
returnAttributes = Arrays.copyOf(returnAttributes, returnAttributes.length + 1);
returnAttributes[returnAttributes.length-1] = config.getMemberAttributeName();
}
return returnAttributes;
}
private SearchFilter getGroupLdapFilter(GrouperGroupInfo grouperGroup) throws PspException {
String result = evaluateJexlExpression(config.getSingleGroupSearchFilter(), null, null, grouperGroup, null);
if ( StringUtils.isEmpty(result) )
throw new RuntimeException("Group searching requires singleGroupSearchFilter to be configured correctly");
// If the filter contains '||', then this filter is requesting parameter substitution
String filterPieces[] = result.split("\\|\\|");
SearchFilter filter = new SearchFilter(filterPieces[0]);
for (int i=1; i<filterPieces.length; i++)
filter.setParameter(i-1, filterPieces[i].trim());
LOG.trace("{}: Filter for group {}: {}",
new Object[] {getName(), grouperGroup, filter});
return filter;
}
@Override
protected void deleteGroup(GrouperGroupInfo grouperGroupInfo, LdapGroup ldapGroup)
throws PspException {
if ( ldapGroup == null ) {
LOG.warn("Nothing to do: Unable to delete group {} because the group wasn't found on target system", grouperGroupInfo);
return;
}
String dn = ldapGroup.getLdapObject().getDn();
LOG.info("Deleting group {} by deleting DN {}", grouperGroupInfo, dn);
getLdapSystem().performLdapDelete(dn);;
}
}