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 package com.jcabi.ssl.maven.plugin;
31
32 import com.jcabi.aspects.Immutable;
33 import com.jcabi.aspects.Loggable;
34 import com.jcabi.log.Logger;
35 import java.io.File;
36 import java.io.IOException;
37 import java.util.Properties;
38 import javax.validation.constraints.NotNull;
39 import lombok.EqualsAndHashCode;
40 import lombok.ToString;
41 import org.apache.commons.io.FileUtils;
42
43
44
45
46
47
48
49
50 @Immutable
51 @ToString
52 @EqualsAndHashCode(of = "store")
53 final class Cacerts {
54
55
56
57
58 public static final String TRUST = "javax.net.ssl.trustStore";
59
60
61
62
63 public static final String TRUST_PWD = "javax.net.ssl.trustStorePassword";
64
65
66
67
68 public static final String STD_PWD = "changeit";
69
70
71
72
73 private final transient String store;
74
75
76
77
78
79
80 public Cacerts(@NotNull final File file) throws IOException {
81 this.store = file.getAbsolutePath();
82 final File prev = new File(
83 String.format(
84 "%s/lib/security/cacerts",
85 System.getProperty("java.home")
86 )
87 );
88 FileUtils.copyFile(prev, file);
89 Logger.info(
90 this,
91 "Existing cacerts '%s' copied to '%s' (%s)",
92 prev,
93 this.store,
94 FileUtils.byteCountToDisplaySize(this.store.length())
95 );
96 }
97
98
99
100
101
102 public void imprt() throws IOException {
103 final File keystore = new File(System.getProperty(Keystore.KEY));
104 final String pwd = System.getProperty(Keystore.KEY_PWD);
105 new Keytool(new File(this.store), Cacerts.STD_PWD).imprt(keystore, pwd);
106 System.setProperty(Cacerts.TRUST, this.store);
107 System.setProperty(Cacerts.TRUST_PWD, Cacerts.STD_PWD);
108 Logger.info(
109 this,
110 "keyStore '%s' imported into trustStore '%s'",
111 keystore,
112 this.store
113 );
114 }
115
116
117
118
119
120
121 @Loggable(Loggable.DEBUG)
122 public void populate(final Properties props) {
123 final String[] names = new String[] {Cacerts.TRUST, Cacerts.TRUST_PWD};
124 for (final String name : names) {
125 final String value = System.getProperty(name);
126 if (value == null) {
127 continue;
128 }
129 props.put(name, value);
130 Logger.info(
131 this,
132 "Maven property ${%s} set to '%s'",
133 name,
134 value
135 );
136 }
137 }
138 }