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.config;
18  
19  import java.io.File;
20  import java.io.FileOutputStream;
21  import java.io.IOException;
22  import java.nio.charset.StandardCharsets;
23  import lombok.extern.slf4j.Slf4j;
24  import org.bremersee.data.ldaptive.LdaptiveProperties;
25  import org.springframework.beans.factory.ObjectProvider;
26  import org.springframework.boot.context.event.ApplicationReadyEvent;
27  import org.springframework.context.annotation.Profile;
28  import org.springframework.context.event.EventListener;
29  import org.springframework.stereotype.Component;
30  import org.springframework.util.Assert;
31  
32  /**
33   * The kinit password file configuration.
34   *
35   * @author Christian Bremer
36   */
37  @Profile("cli")
38  @Component
39  @Slf4j
40  public class KinitPasswordFileConfiguration {
41  
42    private final DomainControllerProperties properties;
43  
44    private final LdaptiveProperties ldaptiveProperties;
45  
46    /**
47     * Instantiates a new kinit password file configuration.
48     *
49     * @param properties the properties
50     * @param ldaptivePropertiesProvider the provider of the ldaptive properties
51     */
52    public KinitPasswordFileConfiguration(
53        DomainControllerProperties properties,
54        ObjectProvider<LdaptiveProperties> ldaptivePropertiesProvider) {
55      this.properties = properties;
56      this.ldaptiveProperties = ldaptivePropertiesProvider.getIfAvailable();
57    }
58  
59    /**
60     * Init.
61     */
62    @EventListener(ApplicationReadyEvent.class)
63    public void init() {
64      if (ldaptiveProperties == null) {
65        log.warn("Kinit password file cannot be created because ldaptive properties are not "
66            + "present. You have to enable profile 'ldap'.");
67      } else {
68        Assert.hasText(properties.getKinitAdministratorName(),
69            "Kinit administrator name must be present.");
70        Assert.hasText(properties.getKinitPasswordFile(),
71            "Kinit password file must be specified.");
72        final File file = new File(properties.getKinitPasswordFile());
73        if (!file.exists()) {
74          try (final FileOutputStream out = new FileOutputStream(file)) {
75            out.write(ldaptiveProperties.getBindCredential().getBytes(StandardCharsets.UTF_8));
76            out.flush();
77          } catch (IOException e) {
78            log.error("Creating kinit password file failed.");
79          }
80        }
81        Assert.isTrue(file.exists(), "Kinit password file must exist.");
82      }
83    }
84  
85  }