Skip to content

Commit ce63ee6

Browse files
committed
More KeyTool class usages
Signed-off-by: David Matějček <[email protected]>
1 parent 4fea0fa commit ce63ee6

File tree

7 files changed

+114
-174
lines changed

7 files changed

+114
-174
lines changed

appserver/itest-tools/src/main/java/org/glassfish/main/itest/tools/GlassFishTestEnvironment.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import org.glassfish.main.itest.tools.asadmin.Asadmin;
5656
import org.glassfish.main.itest.tools.asadmin.AsadminResult;
5757
import org.glassfish.main.itest.tools.asadmin.StartServ;
58+
import org.glassfish.main.jdke.security.KeyTool;
5859

5960
import static java.net.http.HttpResponse.BodyHandlers.ofString;
6061
import static org.glassfish.embeddable.GlassFishVariable.JAVA_HOME;
@@ -81,7 +82,6 @@ public class GlassFishTestEnvironment {
8182

8283
private static final File ASADMIN = findAsadmin();
8384
private static final File STARTSERV = findStartServ();
84-
private static final File KEYTOOL = findKeyTool();
8585
private static final File JARSIGNER = findJarSigner();
8686
private static final File PASSWORD_FILE_FOR_UPDATE = findPasswordFile("password_update.txt");
8787
private static final File PASSWORD_FILE = findPasswordFile("password.txt");
@@ -150,11 +150,6 @@ public static StartServ getStartServInTopLevelBin() {
150150
}
151151

152152

153-
public static KeyTool getKeyTool() {
154-
return new KeyTool(KEYTOOL);
155-
}
156-
157-
158153
public static JarSigner getJarSigner() {
159154
return new JarSigner(JARSIGNER);
160155
}
@@ -177,13 +172,21 @@ public static Path getDomain1Directory() {
177172

178173
public static KeyStore getDomain1KeyStore() {
179174
Path keystore = getDomain1Directory().resolve(Paths.get("config", "keystore.jks"));
180-
return KeyTool.loadKeyStore(keystore.toFile(), "changeit".toCharArray());
175+
try {
176+
return new KeyTool(keystore.toFile(), "changeit".toCharArray()).loadKeyStore();
177+
} catch (IOException e) {
178+
throw new IllegalStateException(e);
179+
}
181180
}
182181

183182

184183
public static KeyStore getDomain1TrustStore() {
185184
Path cacerts = getDomain1Directory().resolve(Paths.get("config", "cacerts.jks"));
186-
return KeyTool.loadKeyStore(cacerts.toFile(), "changeit".toCharArray());
185+
try {
186+
return new KeyTool(cacerts.toFile(), "changeit".toCharArray()).loadKeyStore();
187+
} catch (IOException e) {
188+
throw new IllegalStateException(e);
189+
}
187190
}
188191

189192

appserver/itest-tools/src/main/java/org/glassfish/main/itest/tools/KeyTool.java

Lines changed: 0 additions & 110 deletions
This file was deleted.

appserver/tests/application/src/test/java/org/glassfish/main/test/app/security/jmac/https/JmacHttpsTest.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2023, 2025 Contributors to the Eclipse Foundation
2+
* Copyright (c) 2023, 2025 Contributors to the Eclipse Foundation.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -36,9 +36,9 @@
3636
import javax.net.ssl.X509TrustManager;
3737

3838
import org.glassfish.main.itest.tools.GlassFishTestEnvironment;
39-
import org.glassfish.main.itest.tools.KeyTool;
4039
import org.glassfish.main.itest.tools.TestUtilities;
4140
import org.glassfish.main.itest.tools.asadmin.Asadmin;
41+
import org.glassfish.main.jdke.security.KeyTool;
4242
import org.jboss.shrinkwrap.api.ShrinkWrap;
4343
import org.jboss.shrinkwrap.api.exporter.ZipExporter;
4444
import org.jboss.shrinkwrap.api.spec.JavaArchive;
@@ -63,34 +63,31 @@
6363
import static org.junit.jupiter.api.Assertions.assertTrue;
6464

6565
public class JmacHttpsTest {
66-
private static final String MYKS_PASSWORD = UUID.randomUUID().toString();
66+
private static final char[] MYKS_PASSWORD = UUID.randomUUID().toString().toCharArray();
6767

6868
private static final Logger LOG = System.getLogger(JmacHttpsTest.class.getName());
6969

7070
private static final String APP_NAME = "security-jmac-https";
7171
private static final String AUTH_MODULE_NAME = "httpsTestAuthModule";
7272

7373
private static final Asadmin ASADMIN = GlassFishTestEnvironment.getAsadmin();
74-
private static final KeyTool KEYTOOL = GlassFishTestEnvironment.getKeyTool();
7574

7675
@TempDir
7776
private static File tempDir;
7877
private static File myKeyStore;
7978
private static File warFile;
8079
private static File loginModuleFile;
80+
private static KeyTool myKeyStoreTool;
8181

8282

8383
@BeforeAll
8484
public static void prepareDeployment() throws Exception {
8585
myKeyStore = new File(tempDir, "httpstest.jks");
86-
KEYTOOL.exec("-genkey", "-alias", "httpstest", "-keyalg", "RSA", "-dname",
87-
"CN=HTTPSTEST,OU=Eclipse GlassFish Tests",
88-
"-validity", "7", "-keypass", MYKS_PASSWORD, "-keystore", myKeyStore.getAbsolutePath(), "-storepass",
89-
MYKS_PASSWORD);
86+
myKeyStoreTool = new KeyTool(myKeyStore, MYKS_PASSWORD);
87+
myKeyStoreTool.generateKeyPair("httpstest", "CN=HTTPSTEST,OU=Eclipse GlassFish Tests", "RSA", 7);
9088

91-
KEYTOOL.exec("-importkeystore", "-srckeystore", myKeyStore.getAbsolutePath(), "-srcstorepass", MYKS_PASSWORD,
92-
"-destkeystore", GlassFishTestEnvironment.getDomain1Directory().resolve(Paths.get("config", "cacerts.jks"))
93-
.toFile().getAbsolutePath(), "-deststorepass", "changeit");
89+
File cacertsFile = getDomain1Directory().resolve(Paths.get("config", "cacerts.jks")).toFile();
90+
myKeyStoreTool.copyCertificate("httpstest", cacertsFile, "changeit".toCharArray());
9491

9592
// Default is false, required to set the client certificate to the context.
9693
ASADMIN.exec("set", "configs.config.server-config.network-config.protocols.protocol.http-listener-2.ssl.client-auth-enabled=true");
@@ -137,7 +134,7 @@ void test() throws Exception {
137134
HttpsURLConnection connection = openConnection(true, 8181, "/" + APP_NAME + "/index.jsp");
138135
SSLContext sslContext = SSLContext.getInstance("TLSv1.3");
139136
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
140-
keyManagerFactory.init(KeyTool.loadKeyStore(myKeyStore, MYKS_PASSWORD.toCharArray()), MYKS_PASSWORD.toCharArray());
137+
keyManagerFactory.init(myKeyStoreTool.loadKeyStore(), MYKS_PASSWORD);
141138
sslContext.init(keyManagerFactory.getKeyManagers(), new TrustManager[] {new TestTrustManager()}, null);
142139
SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
143140
connection.setSSLSocketFactory(sslSocketFactory);

appserver/tests/application/src/test/java/org/glassfish/main/test/app/signedear/SignedEarDeploymentTest.java

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2024 Contributors to the Eclipse Foundation.
2+
* Copyright (c) 2024, 2025 Contributors to the Eclipse Foundation.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -22,8 +22,8 @@
2222

2323
import org.glassfish.main.itest.tools.GlassFishTestEnvironment;
2424
import org.glassfish.main.itest.tools.JarSigner;
25-
import org.glassfish.main.itest.tools.KeyTool;
2625
import org.glassfish.main.itest.tools.asadmin.Asadmin;
26+
import org.glassfish.main.jdke.security.KeyTool;
2727
import org.glassfish.main.test.app.signedear.api.ExampleRemote;
2828
import org.glassfish.main.test.app.signedear.impl.ExampleBean;
2929
import org.jboss.shrinkwrap.api.ShrinkWrap;
@@ -41,8 +41,8 @@
4141
/**
4242
* Integration test for deployment EAR application that contains signed
4343
* shared libraries and modules.
44-
*
45-
* <p>This integration test checks the correctness of the classloading
44+
* <p>
45+
* This integration test checks the correctness of the classloading
4646
* of generated classes.
4747
*/
4848
public class SignedEarDeploymentTest {
@@ -54,22 +54,16 @@ public class SignedEarDeploymentTest {
5454
private static final String KEYSTORE_PASSWORD = UUID.randomUUID().toString();
5555

5656
private static final Asadmin ASADMIN = GlassFishTestEnvironment.getAsadmin();
57-
private static final KeyTool KEYTOOL = GlassFishTestEnvironment.getKeyTool();
5857
private static final JarSigner JARSIGNER = GlassFishTestEnvironment.getJarSigner();
5958

6059
@TempDir
6160
private static File tempDir;
6261
private static File earFile;
6362

6463
@BeforeAll
65-
public static void prepareDeployment() {
64+
public static void prepareDeployment() throws Exception {
6665
File keyStore = new File(tempDir, "signtest.jks");
67-
68-
// Generate a key pair (a public key and associated private key).
69-
KEYTOOL.exec("-genkeypair", "-alias", "signtest", "-keyalg", "RSA", "-dname",
70-
"CN=SIGNTEST, OU=Eclipse Glassfish Tests, O=Eclipse Foundation, L=Brussels, ST=Belgium, C=Belgium",
71-
"-validity", "7", "-keypass", KEYSTORE_PASSWORD, "-keystore", keyStore.getAbsolutePath(),
72-
"-storepass", KEYSTORE_PASSWORD);
66+
new KeyTool(keyStore, KEYSTORE_PASSWORD.toCharArray()).generateKeyPair("signtest", "CN=SIGNTEST", "RSA", 7);
7367

7468
// Create shared library.
7569
JavaArchive apiArchive = ShrinkWrap.create(JavaArchive.class)
@@ -83,8 +77,7 @@ public static void prepareDeployment() {
8377
"-keypass", KEYSTORE_PASSWORD, apiFile.getAbsolutePath(), "signtest");
8478

8579
// Create EAR EJB module.
86-
JavaArchive implArchive = ShrinkWrap.create(JavaArchive.class)
87-
.addClass(ExampleBean.class);
80+
JavaArchive implArchive = ShrinkWrap.create(JavaArchive.class).addClass(ExampleBean.class);
8881
File implFile = new File(tempDir, "impl.jar");
8982
implArchive.as(ZipExporter.class).exportTo(implFile);
9083
LOG.log(Level.INFO, implArchive.toString(true));

nucleus/admin/server-mgmt/src/main/java/com/sun/enterprise/admin/servermgmt/cli/CreateDomainCommand.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import com.sun.enterprise.admin.servermgmt.DomainException;
2828
import com.sun.enterprise.admin.servermgmt.DomainsManager;
2929
import com.sun.enterprise.admin.servermgmt.KeystoreManager;
30-
import com.sun.enterprise.admin.servermgmt.RepositoryManager;
3130
import com.sun.enterprise.admin.servermgmt.domain.DomainBuilder;
3231
import com.sun.enterprise.admin.servermgmt.pe.PEDomainsManager;
3332
import com.sun.enterprise.admin.util.CommandModelData.ParamModelData;
@@ -77,7 +76,7 @@ public final class CreateDomainCommand extends CLICommand {
7776
private static final String ADMIN_PORT = "adminport";
7877
private static final String ADMIN_PASSWORD = "password";
7978
private static final String MASTER_PASSWORD = "masterpassword";
80-
private static final String DEFAULT_MASTER_PASSWORD = RepositoryManager.DEFAULT_MASTER_PASSWORD;
79+
private static final String DEFAULT_MASTER_PASSWORD = KeystoreManager.DEFAULT_MASTER_PASSWORD;
8180
private static final String SAVE_MASTER_PASSWORD = "savemasterpassword";
8281
private static final String INSTANCE_PORT = "instanceport";
8382
private static final String DOMAIN_PROPERTIES = "domainproperties";

0 commit comments

Comments
 (0)