1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
34
35
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
48
49
50
51
52 public KinitPasswordFileConfiguration(
53 DomainControllerProperties properties,
54 ObjectProvider<LdaptiveProperties> ldaptivePropertiesProvider) {
55 this.properties = properties;
56 this.ldaptiveProperties = ldaptivePropertiesProvider.getIfAvailable();
57 }
58
59
60
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 }