View Javadoc
1   /*
2    * Copyright 2019 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * The abstract repository.
31   *
32   * @author Christian Bremer
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     * Instantiates a new abstract repository.
52     *
53     * @param properties the properties
54     * @param ldapTemplate the ldap template
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     * Calls linux command {@code kinit} for authentication.
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     * Calls linux command {@code sudo}.
80     *
81     * @param commands the commands
82     */
83    void sudo(final List<String> commands) {
84      if (properties.isUsingSudo()) {
85        commands.add(properties.getSudoBinary());
86      }
87    }
88  
89    /**
90     * Adds the use kerberos option of the linux command {@code samba-tool} to the list of commands.
91     * This requires a successful authentication with {@code kinit}, see {@link #kinit()}.
92     *
93     * @param commands the commands
94     */
95    void auth(final List<String> commands) {
96      commands.add(USE_KERBEROS);
97      commands.add(YES);
98    }
99  
100   /**
101    * Checks whether the given value contains the given query.
102    *
103    * @param value the value
104    * @param query the query
105    * @return {@code true} if the value contains the query, otherwise {@code false}
106    */
107   static boolean contains(final Object value, final String query) {
108     if (value instanceof Collection) {
109       //noinspection rawtypes
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 }