1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.bremersee.dccon.repository;
18
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.List;
22 import lombok.AccessLevel;
23 import lombok.Getter;
24 import org.bremersee.data.ldaptive.LdaptiveTemplate;
25 import org.bremersee.dccon.config.DomainControllerProperties;
26 import org.bremersee.dccon.repository.cli.CommandExecutor;
27 import org.springframework.util.Assert;
28
29
30
31
32
33
34 abstract class AbstractRepository {
35
36 private static final Object KINIT_LOG = new Object();
37
38 private static final String KINIT_PASSWORD_FILE = "--password-file={}";
39
40 private static final String USE_KERBEROS = "-k";
41
42 private static final String YES = "yes";
43
44 @Getter(AccessLevel.PACKAGE)
45 private final DomainControllerProperties properties;
46
47 @Getter(AccessLevel.PACKAGE)
48 private final LdaptiveTemplate ldapTemplate;
49
50
51
52
53
54
55
56 AbstractRepository(
57 final DomainControllerProperties properties,
58 final LdaptiveTemplate ldapTemplate) {
59 Assert.notNull(properties, "Domain controller properties must not be null.");
60 this.properties = properties;
61 this.ldapTemplate = ldapTemplate;
62 }
63
64
65
66
67 void kinit() {
68 synchronized (KINIT_LOG) {
69 List<String> commands = new ArrayList<>();
70 sudo(commands);
71 commands.add(properties.getKinitBinary());
72 commands.add(KINIT_PASSWORD_FILE.replace("{}", properties.getKinitPasswordFile()));
73 commands.add(properties.getKinitAdministratorName());
74 CommandExecutor.exec(commands, properties.getSambaToolExecDir());
75 }
76 }
77
78
79
80
81
82
83 void sudo(final List<String> commands) {
84 if (properties.isUsingSudo()) {
85 commands.add(properties.getSudoBinary());
86 }
87 }
88
89
90
91
92
93
94
95 void auth(final List<String> commands) {
96 commands.add(USE_KERBEROS);
97 commands.add(YES);
98 }
99
100
101
102
103
104
105
106
107 static boolean contains(final Object value, final String query) {
108 if (value instanceof Collection) {
109
110 for (Object item : (Collection) value) {
111 if (contains(item, query)) {
112 return true;
113 }
114 }
115 return false;
116 }
117 return value != null && value.toString().toLowerCase().contains(query);
118 }
119
120 }