-
Notifications
You must be signed in to change notification settings - Fork 188
/
OpenDJController.java
executable file
·1021 lines (884 loc) · 34.6 KB
/
OpenDJController.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
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2010-2019 Evolveum
*
* 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.
*/
package com.evolveum.midpoint.test.ldap;
import static org.testng.AssertJUnit.assertEquals;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.Validate;
import org.opends.messages.Message;
import org.opends.messages.MessageBuilder;
import org.opends.server.config.ConfigException;
import org.opends.server.core.AddOperation;
import org.opends.server.core.BindOperation;
import org.opends.server.core.DeleteOperation;
import org.opends.server.core.ModifyDNOperation;
import org.opends.server.core.ModifyOperation;
import org.opends.server.protocols.internal.InternalClientConnection;
import org.opends.server.protocols.internal.InternalSearchOperation;
import org.opends.server.types.Attribute;
import org.opends.server.types.AttributeValue;
import org.opends.server.types.ByteString;
import org.opends.server.types.DN;
import org.opends.server.types.DereferencePolicy;
import org.opends.server.types.DirectoryEnvironmentConfig;
import org.opends.server.types.DirectoryException;
import org.opends.server.types.Entry;
import org.opends.server.types.InitializationException;
import org.opends.server.types.LDIFImportConfig;
import org.opends.server.types.ResultCode;
import org.opends.server.types.SearchResultEntry;
import org.opends.server.types.SearchScope;
import org.opends.server.util.ChangeRecordEntry;
import org.opends.server.util.EmbeddedUtils;
import org.opends.server.util.LDIFException;
import org.opends.server.util.LDIFReader;
import org.opends.server.util.ModifyChangeRecordEntry;
import org.opends.server.util.ModifyDNChangeRecordEntry;
import org.testng.AssertJUnit;
import com.evolveum.midpoint.test.util.MidPointAsserts;
import com.evolveum.midpoint.util.MiscUtil;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
/**
* This class controls embedded OpenDJ instance.
*
* It is used in Unit tests. It configures and starts and stops the instance. It
* can even manage a "template" configuration of OpenDJ and copy it to working
* instance configuration.
*
* @author Radovan Semancik
*/
public class OpenDJController extends AbstractResourceController {
private String DATA_TEMPLATE_DIR = "test-data";
private String SERVER_ROOT = "target/test-data/opendj";
private String LDAP_SUFFIX = "dc=example,dc=com";
public static final String DEFAULT_TEMPLATE_NAME = "opendj.template";
public static final String RI_TEMPLATE_NAME = "opendj.template.ri";
public static final String OBJECT_CLASS_INETORGPERSON_NAME = "inetOrgPerson";
public static final String RESOURCE_OPENDJ_PRIMARY_IDENTIFIER_LOCAL_NAME = "entryUUID";
public static final String RESOURCE_OPENDJ_SECONDARY_IDENTIFIER_LOCAL_NAME = "dn";
protected File serverRoot = new File(SERVER_ROOT);
protected File configFile = null;
protected File templateRoot;
private static final Trace LOGGER = TraceManager.getTrace(OpenDJController.class);
protected InternalClientConnection internalConnection;
public OpenDJController() {
init();
}
public OpenDJController(String serverRoot) {
SERVER_ROOT = serverRoot;
this.serverRoot = new File(serverRoot);
init();
}
/**
* Initialize
*
*/
private void init() {
if (!serverRoot.exists()) {
serverRoot.mkdirs();
}
if (configFile == null) {
configFile = new File(serverRoot, "config/config.ldif");
}
}
/**
* Get the value of serverRoot.
*
* The top directory of working OpenDS installation. The OpenDS placed in
* this directory will be used during the tests.
*
* @return the value of serverRoot
*/
public File getServerRoot() {
return this.serverRoot;
}
/**
* Set the value of serverRoot
*
* The top directory of working OpenDS installation. The OpenDS placed in
* this directory will be used during the tests.
*
* @param serverRoot
* new value of serverRoot
*/
public void setServerRoot(File serverRoot) {
this.serverRoot = serverRoot;
}
/**
* Get the value of configFile
*
* File name of primary OpenDS configuration file. Normally
* <serverRoot>/config/config.ldif
*
* @return the value of configFile
*/
public File getConfigFile() {
return configFile;
}
/**
* Set the value of configFile
*
* File name of primary OpenDS configuration file. Normally
* <serverRoot>/config/config.ldif
*
* @param configFile
* new value of configFile
*/
public void setConfigFile(File configFile) {
this.configFile = configFile;
}
/**
* Get the value of templateServerRoot
*
* The top directory of template OpenDS installation. All the files from
* this directory will be copied to the working OpenDS directory
* (serverRoot). This usually happens before the tests.
*
* @return the value of templateServerRoot
*/
public File getTemplateServerRoot() {
return templateRoot;
}
public String getSuffix() {
return LDAP_SUFFIX;
}
public String getSuffixPeople() {
return "ou=People,"+LDAP_SUFFIX;
}
public String getAccountDn(String username) {
return "uid="+username+","+getSuffixPeople();
}
/**
* Get the value of internalConnection
*
* The connection to the OpenDS instance. It can be used to fetch and
* manipulate the data.
*
* @return the value of internelConnection
*/
public InternalClientConnection getInternalConnection() {
Validate.notNull(internalConnection, "Not connected");
return internalConnection;
}
/**
* Refresh working OpenDS installation from the template.
*
* The existing working OpenDS installation (in serverRoot) will be
* discarded and replaced by a fresh known-state setup (from
* templateServerRoot).
*
* @throws IOException
* @throws URISyntaxException
*/
public void refreshFromTemplate(String templateName) throws IOException, URISyntaxException {
deleteDirectory(serverRoot);
extractTemplate(serverRoot, templateName);
}
/**
* Extract template from class
*/
private void extractTemplate(File dst, String templateName) throws IOException, URISyntaxException {
LOGGER.info("Extracting OpenDJ template....");
if (!dst.exists()) {
LOGGER.debug("Creating target dir {}", dst.getPath());
dst.mkdirs();
}
templateRoot = new File(DATA_TEMPLATE_DIR, templateName);
String templateRootPath = DATA_TEMPLATE_DIR + "/" + templateName; // templateRoot.getPath does not work on Windows, as it puts "\" into the path name (leading to problems with getSystemResource)
// Determing if we need to extract from JAR or directory
if (templateRoot.isDirectory()) {
LOGGER.trace("Need to do directory copy.");
MiscUtil.copyDirectory(templateRoot, dst);
return;
}
LOGGER.debug("Try to localize OpenDJ Template in JARs as " + templateRootPath);
URL srcUrl = ClassLoader.getSystemResource(templateRootPath);
LOGGER.debug("srcUrl " + srcUrl);
// sample:
// file:/C:/.m2/repository/test-util/1.9-SNAPSHOT/test-util-1.9-SNAPSHOT.jar!/test-data/opendj.template
// output:
// /C:/.m2/repository/test-util/1.9-SNAPSHOT/test-util-1.9-SNAPSHOT.jar
//
// beware that in the URL there can be spaces encoded as %20, e.g.
// file:/C:/Documents%20and%20Settings/user/.m2/repository/com/evolveum/midpoint/infra/test-util/2.1-SNAPSHOT/test-util-2.1-SNAPSHOT.jar!/test-data/opendj.template
//
if (srcUrl.getPath().contains("!/")) {
URI srcFileUri = new URI(srcUrl.getPath().split("!/")[0]); // e.g. file:/C:/Documents%20and%20Settings/user/.m2/repository/com/evolveum/midpoint/infra/test-util/2.1-SNAPSHOT/test-util-2.1-SNAPSHOT.jar
File srcFile = new File(srcFileUri);
JarFile jar = new JarFile(srcFile);
LOGGER.debug("Extracting OpenDJ from JAR file {} to {}", srcFile.getPath(), dst.getPath());
Enumeration<JarEntry> entries = jar.entries();
JarEntry e;
byte buf[] = new byte[655360];
while (entries.hasMoreElements()) {
e = entries.nextElement();
// skip other files
if (!e.getName().contains(templateRootPath)) {
continue;
}
// prepare destination file
String filepath = e.getName().substring(templateRootPath.length());
File dstFile = new File(dst, filepath);
// test if directory
if (e.isDirectory()) {
LOGGER.debug("Create directory: {}", dstFile.getAbsolutePath());
dstFile.mkdirs();
continue;
}
LOGGER.debug("Extract {} to {}", filepath, dstFile.getAbsolutePath());
// Find file on classpath
InputStream is = ClassLoader.getSystemResourceAsStream(e.getName());
// InputStream is = jar.getInputStream(e); //old way
// Copy content
OutputStream out = new FileOutputStream(dstFile);
int len;
while ((len = is.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.close();
is.close();
}
jar.close();
} else {
try {
File file = new File(srcUrl.toURI());
File[] files = file.listFiles();
for (File subFile : files) {
if (subFile.isDirectory()) {
MiscUtil.copyDirectory(subFile, new File(dst, subFile.getName()));
} else {
MiscUtil.copyFile(subFile, new File(dst, subFile.getName()));
}
}
} catch (Exception ex) {
throw new IOException(ex);
}
}
LOGGER.debug("OpenDJ Extracted");
}
/**
* Start the embedded OpenDJ directory server using files copied from the default
* template.
*/
public InternalClientConnection startCleanServer() throws IOException, URISyntaxException {
return startCleanServer(DEFAULT_TEMPLATE_NAME);
}
/**
* Start the embedded OpenDJ directory server using files copied from the
* template with referential integrity plugin turned on.
*/
public InternalClientConnection startCleanServerRI() throws IOException, URISyntaxException {
return startCleanServer(RI_TEMPLATE_NAME);
}
/**
* Start the embedded OpenDJ directory server using files copied from the specified
* template.
*/
public InternalClientConnection startCleanServer(String templateName) throws IOException, URISyntaxException {
refreshFromTemplate(templateName);
return start();
}
/**
* Start the embedded OpenDJ directory server.
*
* Configuration and databases from serverRoot location will be used.
*
* @return
*/
public InternalClientConnection start() {
LOGGER.info("Starting OpenDJ server");
DirectoryEnvironmentConfig envConfig = new DirectoryEnvironmentConfig();
try {
envConfig.setServerRoot(serverRoot);
envConfig.setConfigFile(configFile);
// envConfig.setDisableConnectionHandlers(true);
} catch (InitializationException ex) {
ex.printStackTrace();
throw new RuntimeException("OpenDJ initialization failed", ex);
}
// Check if the server is already running
if (EmbeddedUtils.isRunning()) {
throw new RuntimeException("Server already running");
} else {
try {
EmbeddedUtils.startServer(envConfig);
} catch (ConfigException ex) {
LOGGER.error("Possible OpenDJ misconfiguration: " + ex.getMessage(), ex);
throw new RuntimeException("OpenDJ startup failed", ex);
} catch (InitializationException ex) {
LOGGER.error("OpenDJ startup failed", ex);
throw new RuntimeException("OpenDJ startup failed", ex);
}
}
internalConnection = InternalClientConnection.getRootConnection();
if (internalConnection == null) {
LOGGER.error("OpenDJ cannot get internal connection (null)");
throw new RuntimeException("OpenDS cannot get internal connection (null)");
}
LOGGER.info("OpenDJ server started");
return internalConnection;
}
/**
* Stop the embedded OpenDS server.
*
*/
public void stop() {
if (EmbeddedUtils.isRunning()) {
LOGGER.debug("Stopping OpenDJ server");
EmbeddedUtils.stopServer(this.getClass().getName(), Message.EMPTY);
LOGGER.info("OpenDJ server is stopped");
} else {
LOGGER.warn("Attempt to stop OpenDJ server that is already stopped.");
}
}
public boolean isRunning() {
return EmbeddedUtils.isRunning();
}
public void assumeRunning() {
if (!isRunning()) {
start();
}
}
public void assumeStopped() {
if (isRunning()) {
stop();
}
}
/**
* Delete a directory and its contents.
*
* @param dir
* The name of the directory to delete.
* @throws IOException
* If the directory could not be deleted.
*/
public static void deleteDirectory(File dir) throws IOException {
if (dir.isDirectory()) {
// Recursively delete sub-directories and files.
for (String child : dir.list()) {
deleteDirectory(new File(dir, child));
}
}
dir.delete();
}
public Set<String> asSet(List<Attribute> attributes) {
// Just blindly get the fist one now.
// There is most likely just one anyway.
// TODO: improve that later
// Attribute attr = attributes.get(0);
Set<String> result = new HashSet<>();
// TODO find newer OpenDS jar
// Iterator<AttributeValue> iterator = attr.iterator();
// while (iterator.hasNext()) {
// result.add(iterator.next().toString());
// }
return result;
}
// Generic utility methods
public Entry searchByEntryUuid(String entryUuid) throws DirectoryException {
InternalSearchOperation op = getInternalConnection().processSearch(
"dc=example,dc=com", SearchScope.WHOLE_SUBTREE, DereferencePolicy.NEVER_DEREF_ALIASES, 100,
100, false, "(entryUUID=" + entryUuid + ")", getSearchAttributes());
LinkedList<SearchResultEntry> searchEntries = op.getSearchEntries();
if (searchEntries == null || searchEntries.isEmpty()) {
return null;
}
if (searchEntries.size() > 1) {
AssertJUnit.fail("Multiple matches for Entry UUID "+entryUuid+": "+searchEntries);
}
return searchEntries.get(0);
}
public Entry searchAndAssertByEntryUuid(String entryUuid) throws DirectoryException {
Entry entry = searchByEntryUuid(entryUuid);
if (entry == null) {
AssertJUnit.fail("Entry UUID "+entryUuid+" not found");
}
return entry;
}
public Entry searchSingle(String filter) throws DirectoryException {
InternalSearchOperation op = getInternalConnection().processSearch(
getSuffix(), SearchScope.WHOLE_SUBTREE, DereferencePolicy.NEVER_DEREF_ALIASES, 100,
100, false, filter, getSearchAttributes());
if (op.getEntriesSent() == 0) {
return null;
} else if (op.getEntriesSent() > 1) {
AssertJUnit.fail("Found too many entries ("+op.getEntriesSent()+") for filter "+filter);
}
return op.getSearchEntries().get(0);
}
public Entry searchByUid(String string) throws DirectoryException {
return searchSingle("(uid=" + string + ")");
}
public Entry fetchEntry(String dn) throws DirectoryException {
Validate.notNull(dn);
InternalSearchOperation op = getInternalConnection().processSearch(
dn, SearchScope.BASE_OBJECT, DereferencePolicy.NEVER_DEREF_ALIASES, 100,
100, false, "(objectclass=*)", getSearchAttributes());
if (op.getEntriesSent() == 0) {
return null;
} else if (op.getEntriesSent() > 1) {
AssertJUnit.fail("Found too many entries ("+op.getEntriesSent()+") for dn "+dn);
}
return op.getSearchEntries().get(0);
}
public Entry fetchAndAssertEntry(String dn, String objectClass) throws DirectoryException {
Entry entry = fetchEntry(dn);
AssertJUnit.assertNotNull("No entry for DN "+dn, entry);
assertDn(entry, dn);
assertObjectClass(entry, objectClass);
return entry;
}
private LinkedHashSet<String> getSearchAttributes() {
LinkedHashSet<String> attrs = new LinkedHashSet<>();
attrs.add("*");
attrs.add("ds-pwp-account-disabled");
attrs.add("createTimestamp");
attrs.add("modifyTimestamp");
return attrs;
}
public boolean isAccountEnabled(Entry ldapEntry) {
String pwpAccountDisabled = getAttributeValue(ldapEntry, "ds-pwp-account-disabled");
if (pwpAccountDisabled != null && pwpAccountDisabled.equalsIgnoreCase("true")) {
return false;
}
return true;
}
public static String getAttributeValue(Entry response, String name) {
return getAttributeValue(response, name, null);
}
public static String getAttributeValue(Entry response, String name, String option) {
List<Attribute> attrs = response.getAttribute(name.toLowerCase());
Attribute attribute = findAttribute(name, option, attrs);
if (attribute == null) {
return null;
}
return getAttributeValue(attribute);
}
private static Attribute findAttribute(String name, String option, List<Attribute> attributes) {
if (attributes == null || attributes.size() == 0) {
return null;
}
for (Attribute attr : attributes) {
if (option == null) {
if (attr.getOptions() == null || attr.getOptions().isEmpty()) {
return attr;
}
} else {
if (attr.getOptions() != null && attr.getOptions().contains(option)) {
return attr;
}
}
}
return null;
}
public static String getAttributeValue(Attribute attribute) {
return attribute.iterator().next().getValue().toString();
}
public static byte[] getAttributeValueBinary(Entry response, String name) {
List<Attribute> attrs = response.getAttribute(name.toLowerCase());
if (attrs == null || attrs.size() == 0) {
return null;
}
assertEquals("Too many attributes for name "+name+": ",
1, attrs.size());
Attribute attribute = attrs.get(0);
ByteString value = attribute.iterator().next().getValue();
return value.toByteArray();
}
public static Collection<String> getAttributeValues(Entry response, String name) {
List<Attribute> attrs = response.getAttribute(name.toLowerCase());
if (attrs == null || attrs.size() == 0) {
return null;
}
assertEquals("Too many attributes for name "+name+": ",
1, attrs.size());
Attribute attribute = attrs.get(0);
Collection<String> values = new ArrayList<>(attribute.size());
Iterator<AttributeValue> iterator = attribute.iterator();
while (iterator.hasNext()) {
AttributeValue attributeValue = iterator.next();
values.add(attributeValue.getValue().toString());
}
return values;
}
public static String getDn(Entry response) {
DN dn = response.getDN();
return dn.toString();
}
public static void assertDn(Entry response, String expected) throws DirectoryException {
DN actualDn = response.getDN();
if (actualDn.compareTo(DN.decode(expected)) != 0) {
AssertJUnit.fail("Wrong DN, expected "+expected+" but was "+actualDn.toString());
}
}
public void assertNoEntry(String dn) throws DirectoryException {
Entry entry = fetchEntry(dn);
if (entry != null) {
AssertJUnit.fail("Found entry for dn "+dn+" while not expecting it: "+entry);
}
}
public static void assertObjectClass(Entry response, String expected) throws DirectoryException {
Collection<String> objectClassValues = getAttributeValues(response, "objectClass");
AssertJUnit.assertTrue("Wrong objectclass for entry "+getDn(response)+", expected "+expected+" but got "+objectClassValues,
objectClassValues.contains(expected));
}
public static void assertNoObjectClass(Entry response, String unexpected) throws DirectoryException {
Collection<String> objectClassValues = getAttributeValues(response, "objectClass");
AssertJUnit.assertFalse("Unexpected objectclass for entry "+getDn(response)+": "+unexpected+", got "+objectClassValues,
objectClassValues.contains(unexpected));
}
public void assertUniqueMember(Entry groupEntry, String accountDn) throws DirectoryException {
Collection<String> members = getAttributeValues(groupEntry, "uniqueMember");
assertContainsDn("No member "+accountDn+" in group "+getDn(groupEntry),
members, accountDn);
}
public static void assertContainsDn(String message, Collection<String> actualValues, String expectedValue) throws DirectoryException {
AssertJUnit.assertNotNull(message+", expected "+expectedValue+", got null", actualValues);
DN expectedDn = DN.decode(expectedValue);
for (String actualValue: actualValues) {
DN actualDn = DN.decode(actualValue);
if (actualDn.compareTo(expectedDn)==0) {
return;
}
}
AssertJUnit.fail(message+", expected "+expectedValue+", got "+actualValues);
}
public void assertUniqueMember(String groupDn, String accountDn) throws DirectoryException {
Entry groupEntry = fetchEntry(groupDn);
assertUniqueMember(groupEntry, accountDn);
}
public void assertNoUniqueMember(String groupDn, String accountDn) throws DirectoryException {
Entry groupEntry = fetchEntry(groupDn);
assertNoUniqueMember(groupEntry, accountDn);
}
public void assertNoUniqueMember(Entry groupEntry, String accountDn) {
Collection<String> members = getAttributeValues(groupEntry, "uniqueMember");
MidPointAsserts.assertNotContainsCaseIgnore("Member "+accountDn+" in group "+getDn(groupEntry),
members, accountDn);
}
public static void assertAttribute(Entry response, String name, String... values) {
List<Attribute> attrs = response.getAttribute(name.toLowerCase());
if (attrs == null || attrs.size() == 0) {
if (values.length == 0) {
return;
} else {
AssertJUnit.fail("Attribute "+name+" does not have any value");
}
}
assertEquals("Too many \"attributes\" for "+name+": ",
1, attrs.size());
Attribute attribute = response.getAttribute(name.toLowerCase()).get(0);
if (values.length != attribute.size()) {
AssertJUnit.fail("Wrong number of values for attribute "+name+", expected "+values.length+" values but got "+attribute.size()+" values: "+attribute);
}
for (String value: values) {
boolean found = false;
Iterator<AttributeValue> iterator = attribute.iterator();
List<String> attrVals = new ArrayList<>();
while (iterator.hasNext()) {
AttributeValue attributeValue = iterator.next();
String attrVal = attributeValue.toString();
attrVals.add(attrVal);
if (attrVal.equals(value)) {
found = true;
}
}
if (!found) {
AssertJUnit.fail("Attribute "+name+" does not contain value "+value+", it has values: "+attrVals);
}
}
}
public static void assertNoAttribute(Entry response, String name) {
List<Attribute> attrs = response.getAttribute(name.toLowerCase());
if (attrs == null || attrs.size() == 0) {
return;
}
assertEquals("Too many \"attributes\" for "+name+": ",
1, attrs.size());
Attribute attribute = response.getAttribute(name.toLowerCase()).get(0);
if (attribute.size() == 0) {
return;
}
if (attribute.isEmpty()) {
return;
}
AssertJUnit.fail("Attribute "+name+" exists while not expecting it: "+attribute);
}
public static void assertAttributeLang(Entry entry, String attributeName, String expectedOrigValue, String... params) {
List<Attribute> attrs = entry.getAttribute(attributeName.toLowerCase());
if (attrs == null || attrs.size() == 0) {
AssertJUnit.fail("Attribute "+attributeName+" does not have any value");
}
Map<String,String> expectedLangs = MiscUtil.paramsToMap(params);
List<String> langsSeen = new ArrayList<>();
for (Attribute attr : attrs) {
if (attr.size() == 0) {
throw new IllegalArgumentException("No values in attribute "+attributeName+": "+attr);
}
if (attr.size() > 1) {
throw new IllegalArgumentException("Too many values in attribute "+attributeName+": "+attr);
}
String attrValue = attr.iterator().next().toString();
if (attr.getOptions() == null || attr.getOptions().isEmpty()) {
assertEquals("Wrong orig value in attribute '"+attributeName+" in entry "+entry.getDN(), expectedOrigValue, attrValue);
} else if (attr.getOptions().size() == 1) {
String option = attr.getOptions().iterator().next();
if (!option.startsWith("lang-")) {
throw new IllegalArgumentException("Non-lang option "+option+" in attribute "+attributeName+": "+attr);
}
String lang = option.substring("lang-".length());
String expectedValue = expectedLangs.get(lang);
assertEquals("Wrong "+option+" value in attribute '"+attributeName+" in entry "+entry.getDN(), expectedValue, attrValue);
langsSeen.add(lang);
} else {
throw new IllegalArgumentException("More than one option in attribute "+attributeName+": "+attr);
}
}
for (Map.Entry<String, String> expectedLangEntry : expectedLangs.entrySet()) {
if (!langsSeen.contains(expectedLangEntry.getKey())) {
AssertJUnit.fail("No lang "+expectedLangEntry.getKey()+" in attribute "+attributeName+" in entry "+entry.getDN()+"; expected "+expectedLangEntry.getValue());
}
}
}
public void assertActive(Entry response, boolean active) {
assertEquals("Unexpected activation of entry "+response, active, isAccountEnabled(response));
}
public Entry addEntryFromLdifFile(File file) throws IOException, LDIFException {
return addEntryFromLdifFile(file.getPath());
}
public Entry addEntryFromLdifFile(String filename) throws IOException, LDIFException {
LDIFImportConfig importConfig = new LDIFImportConfig(filename);
LDIFReader ldifReader = new LDIFReader(importConfig);
Entry ldifEntry = ldifReader.readEntry();
addEntry(ldifEntry);
return ldifEntry;
}
public List<Entry> addEntriesFromLdifFile(File file) throws IOException, LDIFException {
return addEntriesFromLdifFile(file.getPath());
}
public List<Entry> addEntriesFromLdifFile(String filename) throws IOException, LDIFException {
List<Entry> retval = new ArrayList<>();
LDIFImportConfig importConfig = new LDIFImportConfig(filename);
LDIFReader ldifReader = new LDIFReader(importConfig);
for (;;) {
Entry ldifEntry = ldifReader.readEntry();
if (ldifEntry == null) {
break;
}
addEntry(ldifEntry);
retval.add(ldifEntry);
}
return retval;
}
public void addEntry(Entry ldapEntry) {
AddOperation addOperation = getInternalConnection().processAdd(ldapEntry);
if (ResultCode.SUCCESS != addOperation.getResultCode()) {
throw new RuntimeException("LDAP operation error: "+addOperation.getResultCode()+": "+addOperation.getErrorMessage());
}
}
public Entry addEntry(String ldif) throws IOException, LDIFException {
LDIFImportConfig importConfig = new LDIFImportConfig(IOUtils.toInputStream(ldif, "utf-8"));
LDIFReader ldifReader = new LDIFReader(importConfig);
Entry ldifEntry = ldifReader.readEntry();
addEntry(ldifEntry);
return ldifEntry;
}
public ChangeRecordEntry executeRenameChange(File file) throws LDIFException, IOException{
return executeRenameChange(file.getPath());
}
public ChangeRecordEntry executeRenameChange(String filename) throws LDIFException, IOException{
LDIFImportConfig importConfig = new LDIFImportConfig(filename);
LDIFReader ldifReader = new LDIFReader(importConfig);
ChangeRecordEntry entry = ldifReader.readChangeRecord(false);
if (!(entry instanceof ModifyDNChangeRecordEntry)){
throw new LDIFException(new MessageBuilder("Could not execute rename..Bad change").toMessage());
}
ModifyDNOperation modifyOperation = getInternalConnection().processModifyDN((ModifyDNChangeRecordEntry)entry);
if (ResultCode.SUCCESS != modifyOperation.getResultCode()) {
throw new RuntimeException("LDAP operation error: "+modifyOperation.getResultCode()+": "+modifyOperation.getErrorMessage());
}
return entry;
}
public ChangeRecordEntry executeLdifChange(File file) throws IOException, LDIFException {
LDIFImportConfig importConfig = new LDIFImportConfig(file.getPath());
LDIFReader ldifReader = new LDIFReader(importConfig);
ChangeRecordEntry entry = ldifReader.readChangeRecord(false);
ModifyOperation modifyOperation = getInternalConnection()
.processModify((ModifyChangeRecordEntry) entry);
if (ResultCode.SUCCESS != modifyOperation.getResultCode()) {
throw new RuntimeException("LDAP operation error: "+modifyOperation.getResultCode()+": "+modifyOperation.getErrorMessage());
}
return entry;
}
public ChangeRecordEntry executeLdifChange(String ldif) throws IOException, LDIFException {
InputStream ldifInputStream = IOUtils.toInputStream(ldif, "UTF-8");
LDIFImportConfig importConfig = new LDIFImportConfig(ldifInputStream);
LDIFReader ldifReader = new LDIFReader(importConfig);
ChangeRecordEntry entry = ldifReader.readChangeRecord(false);
ModifyOperation modifyOperation = getInternalConnection()
.processModify((ModifyChangeRecordEntry) entry);
if (ResultCode.SUCCESS != modifyOperation.getResultCode()) {
throw new RuntimeException("LDAP operation error: "+modifyOperation.getResultCode()+": "+modifyOperation.getErrorMessage());
}
return entry;
}
public ChangeRecordEntry modifyReplace(String entryDn, String attributeName, String value) throws IOException, LDIFException {
String ldif = "dn: " + entryDn + "\nchangetype: modify\nreplace: "+attributeName+"\n"+attributeName+": " + value;
return executeLdifChange(ldif);
}
public ChangeRecordEntry modifyAdd(String entryDn, String attributeName, String value) throws IOException, LDIFException {
String ldif = "dn: " + entryDn + "\nchangetype: modify\nadd: "+attributeName+"\n"+attributeName+": " + value;
return executeLdifChange(ldif);
}
public ChangeRecordEntry modifyDelete(String entryDn, String attributeName, String value) throws IOException, LDIFException {
String ldif = "dn: " + entryDn + "\nchangetype: modify\ndelete: "+attributeName+"\n"+attributeName+": " + value;
return executeLdifChange(ldif);
}
public void delete(String entryDn) {
DeleteOperation deleteOperation = getInternalConnection().processDelete(entryDn);
if (ResultCode.SUCCESS != deleteOperation.getResultCode()) {
throw new RuntimeException("LDAP operation error: "+deleteOperation.getResultCode()+": "+deleteOperation.getErrorMessage());
}
}
public String dumpEntries() throws DirectoryException {
InternalSearchOperation op = getInternalConnection().processSearch(
LDAP_SUFFIX, SearchScope.WHOLE_SUBTREE, DereferencePolicy.NEVER_DEREF_ALIASES, 100,
100, false, "(objectclass=*)", getSearchAttributes());
StringBuilder sb = new StringBuilder();
for (SearchResultEntry searchEntry: op.getSearchEntries()) {
sb.append(toHumanReadableLdifoid(searchEntry));
sb.append("\n");
}
return sb.toString();
}
public String dumpTree() throws DirectoryException {
StringBuilder sb = new StringBuilder();
sb.append(LDAP_SUFFIX).append("\n");
dumpTreeLevel(sb, LDAP_SUFFIX, 1);
return sb.toString();
}
private void dumpTreeLevel(StringBuilder sb, String dn, int indent) throws DirectoryException {
InternalSearchOperation op = getInternalConnection().processSearch(
dn, SearchScope.SINGLE_LEVEL, DereferencePolicy.NEVER_DEREF_ALIASES, 100,
100, false, "(objectclass=*)", getSearchAttributes());
for (SearchResultEntry searchEntry: op.getSearchEntries()) {
ident(sb, indent);
sb.append(searchEntry.getDN().getRDN());
sb.append("\n");
dumpTreeLevel(sb, searchEntry.getDN().toString(), indent + 1);
}
}
private void ident(StringBuilder sb, int indent) {
for(int i=0; i < indent; i++) {
sb.append(" ");
}
}
public String toHumanReadableLdifoid(Entry entry) {
StringBuilder sb = new StringBuilder();
sb.append("dn: ").append(entry.getDN()).append("\n");
for (Attribute attribute: entry.getAttributes()) {
for (AttributeValue val: attribute) {
sb.append(attribute.getName());
sb.append(": ");
sb.append(val);
sb.append("\n");
}
}
return sb.toString();
}
public Collection<String> getGroupUniqueMembers(String groupDn) throws DirectoryException {
Entry groupEntry = fetchEntry(groupDn);
if (groupEntry == null) {
throw new IllegalArgumentException(groupDn + " was not found");
}
return getAttributeValues(groupEntry, "uniqueMember");
}
/*
dn: <group>
changetype: modify
delete: uniqueMember
uniqueMember: <member>
*/
public ChangeRecordEntry removeGroupUniqueMember(String groupDn, String memberDn) throws IOException, LDIFException {
String ldif = "dn: " + groupDn + "\nchangetype: modify\ndelete: uniqueMember\nuniqueMember: " + memberDn;
return executeLdifChange(ldif);
}
public ChangeRecordEntry addGroupUniqueMember(String groupDn, String memberDn) throws IOException, LDIFException {
String ldif = "dn: " + groupDn + "\nchangetype: modify\nadd: uniqueMember\nuniqueMember: " + memberDn;
return executeLdifChange(ldif);
}
public ChangeRecordEntry addGroupUniqueMembers(String groupDn, List<String> memberDns) throws IOException, LDIFException {
if (memberDns.isEmpty()) {
return null; // garbage in garbage out, sorry
}
StringBuilder sb = new StringBuilder();
sb.append("dn: ").append(groupDn).append("\nchangetype: modify\nadd: uniqueMember");
for (String memberDn : memberDns) {
sb.append("\nuniqueMember: ").append(memberDn);
}
return executeLdifChange(sb.toString());
}
public boolean checkPassword(String entryDn, String password) throws DirectoryException {
InternalClientConnection conn = new InternalClientConnection(DN.decode(entryDn));
BindOperation op = conn.processSimpleBind(entryDn, password);
if (op.getResultCode() == ResultCode.SUCCESS) {
return true;
} else {
LOGGER.error("Bind error: {} ({})", op.getAuthFailureReason(), op.getResultCode());
return false;
}
}
public void assertPassword(String entryDn, String password) throws DirectoryException {
if (!checkPassword(entryDn, password)) {