Skip to content

Commit 281e209

Browse files
committed
Merge remote-tracking branch 'origin/issue/520_improve_dsf-maven-plugin'
into develop
2 parents b062c7b + 47c0c61 commit 281e209

12 files changed

Lines changed: 670 additions & 106 deletions

File tree

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright 2018-2025 Heilbronn University of Applied Sciences
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+
package dev.dsf.maven.dev;
17+
18+
import java.io.IOException;
19+
import java.nio.file.Files;
20+
import java.nio.file.Path;
21+
import java.security.PrivateKey;
22+
import java.util.Objects;
23+
import java.util.Optional;
24+
25+
import org.slf4j.Logger;
26+
import org.slf4j.LoggerFactory;
27+
28+
import de.hsheilbronn.mi.utils.crypto.io.PemReader;
29+
import de.hsheilbronn.mi.utils.crypto.io.PemWriter;
30+
31+
public class AbstractGenerator
32+
{
33+
private static final Logger logger = LoggerFactory.getLogger(AbstractGenerator.class);
34+
35+
public static final String POSTFIX_PRIVATE_KEY = ".key";
36+
37+
private final Path baseDir;
38+
private final char[] privateKeyPassword;
39+
40+
public AbstractGenerator(Path baseDir, char[] privateKeyPassword)
41+
{
42+
Objects.requireNonNull(baseDir, "baseDir");
43+
Objects.requireNonNull(privateKeyPassword, "privateKeyPassword");
44+
45+
this.baseDir = baseDir;
46+
this.privateKeyPassword = privateKeyPassword;
47+
}
48+
49+
protected void writePrivateKey(String commonName, PrivateKey privateKey) throws RuntimeException
50+
{
51+
Path file = toPath(commonName, POSTFIX_PRIVATE_KEY);
52+
53+
try
54+
{
55+
PemWriter.writePrivateKey(privateKey).asPkcs8().encryptedAes128(privateKeyPassword).toFile(file);
56+
}
57+
catch (IOException e)
58+
{
59+
logger.error("Unable to write private-key {}: {} - {}", file.toAbsolutePath().normalize(),
60+
e.getClass().getName(), e.getMessage());
61+
throw new RuntimeException(e);
62+
}
63+
}
64+
65+
protected Optional<PrivateKey> readPrivateKey(String commonName) throws RuntimeException
66+
{
67+
Path file = toPath(commonName, POSTFIX_PRIVATE_KEY);
68+
69+
if (!Files.isReadable(file))
70+
return Optional.empty();
71+
72+
try
73+
{
74+
return Optional.of(PemReader.readPrivateKey(file, privateKeyPassword));
75+
}
76+
catch (IOException e)
77+
{
78+
logger.error("Unable to read private-key {}: {} - {}", file.toAbsolutePath().normalize(),
79+
e.getClass().getName(), e.getMessage());
80+
81+
throw new RuntimeException(e);
82+
}
83+
}
84+
85+
protected Path toPath(String id, String postFix)
86+
{
87+
return baseDir.resolve(id.replaceAll(" ", "_") + postFix);
88+
}
89+
}

dsf-maven/dsf-maven-plugin/src/main/java/dev/dsf/maven/dev/AbstractIo.java

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,36 @@
1616
package dev.dsf.maven.dev;
1717

1818
import java.io.IOException;
19+
import java.nio.file.Files;
20+
import java.nio.file.Path;
21+
import java.security.PrivateKey;
22+
import java.util.Objects;
1923

24+
import org.slf4j.Logger;
25+
import org.slf4j.LoggerFactory;
26+
27+
import de.hsheilbronn.mi.utils.crypto.io.PemWriter;
2028
import dev.dsf.maven.exception.RuntimeIOException;
2129

2230
public abstract class AbstractIo
2331
{
32+
private static final Logger logger = LoggerFactory.getLogger(AbstractIo.class);
33+
2434
protected static interface RunnableWithIoException
2535
{
2636
void run() throws IOException;
2737
}
2838

29-
protected final void toRuntimeException(RunnableWithIoException runnable)
39+
protected final Path projectBasedir;
40+
protected final char[] privateKeyPassword;
41+
42+
public AbstractIo(Path projectBasedir, char[] privateKeyPassword)
43+
{
44+
this.projectBasedir = Objects.requireNonNull(projectBasedir, "projectBasedir");
45+
this.privateKeyPassword = privateKeyPassword;
46+
}
47+
48+
protected final void toRuntimeException(RunnableWithIoException runnable) throws RuntimeIOException
3049
{
3150
try
3251
{
@@ -37,4 +56,25 @@ protected final void toRuntimeException(RunnableWithIoException runnable)
3756
throw new RuntimeIOException(e);
3857
}
3958
}
59+
60+
protected void writePrivateKey(String type, String id, PrivateKey privateKey, Path target) throws IOException
61+
{
62+
logger.info("Writing private-key encrypted ({}: {}) to {}", type, id, projectBasedir.relativize(target));
63+
64+
PemWriter.writePrivateKey(privateKey).asPkcs8().encryptedAes128(privateKeyPassword).toFile(target);
65+
}
66+
67+
protected void writePrivateKeyPlain(String type, String id, PrivateKey privateKey, Path target) throws IOException
68+
{
69+
logger.info("Writing private-key unencrypted ({}: {}) to {}", type, id, projectBasedir.relativize(target));
70+
71+
PemWriter.writePrivateKey(privateKey).asPkcs8().notEncrypted().toFile(target);
72+
}
73+
74+
protected final void writePassword(String type, String id, Path target) throws IOException
75+
{
76+
logger.info("Writing key password ({}: {}) to {}", type, id, projectBasedir.relativize(target));
77+
78+
Files.writeString(target, new String(privateKeyPassword));
79+
}
4080
}

dsf-maven/dsf-maven-plugin/src/main/java/dev/dsf/maven/dev/CertificateGenerator.java

100755100644
Lines changed: 2 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
import de.hsheilbronn.mi.utils.crypto.io.PemWriter;
5454
import de.hsheilbronn.mi.utils.crypto.keypair.KeyPairValidator;
5555

56-
public class CertificateGenerator
56+
public class CertificateGenerator extends AbstractGenerator
5757
{
5858
private static final Logger logger = LoggerFactory.getLogger(CertificateGenerator.class);
5959

@@ -109,7 +109,6 @@ public CertificateAndPrivateKey sign(CertificateAuthority ca)
109109
}
110110
}
111111

112-
public static final String POSTFIX_PRIVATE_KEY = ".key";
113112
public static final String POSTFIX_CERTIFICATE = ".crt";
114113

115114
private static final String SUBJECT_C = "DE";
@@ -121,8 +120,6 @@ public CertificateAndPrivateKey sign(CertificateAuthority ca)
121120
private static final CertificationRequestConfig CERTIFICATION_REQUEST_ISSUING_CA = new CertificationRequestConfig(
122121
CertificateAuthority::signClientServerIssuingCaCertificate, SUBJECT_CN_ISSUING_CA, null);
123122

124-
private final Path certDir;
125-
private final char[] privateKeyPassword;
126123
private final List<CertificationRequestConfig> certificationRequestConfigs = new ArrayList<>();
127124

128125
private CertificateAuthority rootCa;
@@ -132,11 +129,7 @@ public CertificateAndPrivateKey sign(CertificateAuthority ca)
132129
public CertificateGenerator(Path certDir, char[] privateKeyPassword,
133130
List<CertificationRequestConfig> certificationRequestConfigs)
134131
{
135-
Objects.requireNonNull(certDir, "certDir");
136-
Objects.requireNonNull(privateKeyPassword, "privateKeyPassword");
137-
138-
this.certDir = certDir;
139-
this.privateKeyPassword = privateKeyPassword;
132+
super(certDir, privateKeyPassword);
140133

141134
if (certificationRequestConfigs != null)
142135
this.certificationRequestConfigs.addAll(certificationRequestConfigs);
@@ -216,11 +209,6 @@ private String toHexThumbprint(X509Certificate certificate)
216209
}
217210
}
218211

219-
private Path toPath(String commonName, String postFix)
220-
{
221-
return certDir.resolve(commonName.replaceAll(" ", "_") + postFix);
222-
}
223-
224212
private Optional<X509Certificate> readCertificate(String commonName)
225213
{
226214
Path file = toPath(commonName, POSTFIX_CERTIFICATE);
@@ -241,26 +229,6 @@ private Optional<X509Certificate> readCertificate(String commonName)
241229
}
242230
}
243231

244-
private Optional<PrivateKey> readPrivateKey(String commonName)
245-
{
246-
Path file = toPath(commonName, POSTFIX_PRIVATE_KEY);
247-
248-
if (!Files.isReadable(file))
249-
return Optional.empty();
250-
251-
try
252-
{
253-
return Optional.of(PemReader.readPrivateKey(file, privateKeyPassword));
254-
}
255-
catch (IOException e)
256-
{
257-
logger.error("Unable to read private-key {}: {} - {}", file.toAbsolutePath().normalize(),
258-
e.getClass().getName(), e.getMessage());
259-
260-
throw new RuntimeException(e);
261-
}
262-
}
263-
264232
private Optional<CertificateAndPrivateKey> readCertificateAndPrivateKey(String commonName)
265233
{
266234
Optional<X509Certificate> crt = readCertificate(commonName);
@@ -333,21 +301,6 @@ private void writeCertificate(String commonName, X509Certificate crt)
333301
}
334302
}
335303

336-
private void writePrivateKey(String commonName, PrivateKey privateKey)
337-
{
338-
Path file = toPath(commonName, POSTFIX_PRIVATE_KEY);
339-
340-
try
341-
{
342-
PemWriter.writePrivateKey(privateKey).asPkcs8().encryptedAes128(privateKeyPassword).toFile(file);
343-
}
344-
catch (IOException e)
345-
{
346-
logger.error("Unable to write private-key {}: {} - {}", file.toAbsolutePath().normalize(),
347-
e.getClass().getName(), e.getMessage());
348-
throw new RuntimeException(e);
349-
}
350-
}
351304

352305
private void writeCertificateAndPrivateKey(String commonName, CertificateAndPrivateKey certificateAndPrivateKey)
353306
{

0 commit comments

Comments
 (0)