Skip to content

Commit adfd5f4

Browse files
committed
Reducing complexity and duplications around keystores
- types and passwords are not respected everywhere (still) - we need to have control over this - also deleted some unused code, especially constants Signed-off-by: David Matějček <[email protected]>
1 parent f92a82c commit adfd5f4

File tree

14 files changed

+203
-337
lines changed

14 files changed

+203
-337
lines changed

appserver/security/core-ee/src/main/java/com/sun/enterprise/security/ee/J2EESecurityManager.java

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/*
2+
* Copyright (c) 2025 Contributors to the Eclipse Foundation.
23
* Copyright (c) 1997, 2020 Oracle and/or its affiliates. All rights reserved.
34
*
45
* This program and the accompanying materials are made available under the
@@ -23,36 +24,21 @@
2324
import java.net.SocketPermission;
2425
import java.util.PropertyPermission;
2526

27+
import static org.glassfish.embeddable.GlassFishVariable.KEYSTORE_PASSWORD;
28+
import static org.glassfish.embeddable.GlassFishVariable.TRUSTSTORE_PASSWORD;
29+
2630
/**
2731
* Java 2 security manager that enforces code security.
2832
*
2933
* @author Harish Prabandham
3034
*/
35+
@Deprecated(since = "7.1.0", forRemoval = true)
3136
public class J2EESecurityManager extends java.rmi.RMISecurityManager {
3237

3338
private CachedPermissionImpl connectPerm;
34-
3539
private PermissionCache cache;
40+
private boolean cacheEnabled;
3641

37-
private boolean cacheEnabled = false;
38-
39-
public J2EESecurityManager() {
40-
}
41-
42-
/*
43-
* public void checkAccess(ThreadGroup t) { Class[] clss = getClassContext(); for(int i=1; i < clss.length; ++i) { //
44-
* IASRI 4660742 System.out.println(clss[i] + " : " + clss[i].getProtectionDomain()); // START OF IASRI 4660742
45-
* _logger.log(Level.FINE,clss[i] + " : " + clss[i].getProtectionDomain()); // END OF IASRI 4660742 }
46-
*
47-
* System.out.flush();
48-
*
49-
* // JDK 1.1. implementation... Class[] clss = getClassContext(); for(int i=1; i < clss.length; ++i) {
50-
* checkIfInContainer(clss[i]); } }
51-
*
52-
* // JDK 1.1. implementation... private void checkIfInContainer(Class clazz) { Class[] parents =
53-
* clazz.getDeclaredClasses(); for(int i=0; i < parents.length; ++i) { if(parents[i] == com.sun.ejb.Container.class)
54-
* throw new SecurityException("Got it...."); } }
55-
*/
5642

5743
@Override
5844
public void checkAccess(ThreadGroup t) {
@@ -106,7 +92,8 @@ private boolean checkConnectPermission() {
10692
}
10793

10894
private boolean checkProperty(String key) {
109-
if (key.equals("javax.net.ssl.keyStorePassword") || key.equals("javax.net.ssl.trustStorePassword")) {
95+
if (KEYSTORE_PASSWORD.getSystemPropertyName().equals(key)
96+
|| TRUSTSTORE_PASSWORD.getSystemPropertyName().equals(key)) {
11097
SSLUtils.checkPermission(key);
11198
}
11299
if (cacheEnabled()) {

appserver/web/web-glue/src/main/java/com/sun/enterprise/web/connector/coyote/PECoyoteConnector.java

Lines changed: 11 additions & 39 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
* Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
44
*
55
* This program and the accompanying materials are made available under the
@@ -60,6 +60,11 @@
6060
import org.glassfish.web.admin.monitor.RequestProbeProvider;
6161
import org.glassfish.web.util.IntrospectionUtils;
6262

63+
import static org.glassfish.embeddable.GlassFishVariable.KEYSTORE_FILE;
64+
import static org.glassfish.embeddable.GlassFishVariable.KEYSTORE_TYPE;
65+
import static org.glassfish.embeddable.GlassFishVariable.TRUSTSTORE_FILE;
66+
import static org.glassfish.embeddable.GlassFishVariable.TRUSTSTORE_TYPE;
67+
6368
public class PECoyoteConnector extends Connector {
6469

6570
private static final String DEFAULT_KEYSTORE_TYPE = "JKS";
@@ -1328,51 +1333,18 @@ private void configureSSL(NetworkListener listener) {
13281333
* Configures this connector with its keystore and truststore.
13291334
*/
13301335
private void configureKeysAndCerts() {
1331-
1332-
/*
1333-
* Keystore
1334-
*/
1335-
String prop = System.getProperty("javax.net.ssl.keyStore");
1336-
String keyStoreType = System.getProperty("javax.net.ssl.keyStoreType",DEFAULT_KEYSTORE_TYPE);
1336+
String prop = System.getProperty(KEYSTORE_FILE.getSystemPropertyName());
1337+
String keyStoreType = System.getProperty(KEYSTORE_TYPE.getSystemPropertyName(), DEFAULT_KEYSTORE_TYPE);
13371338
if (prop != null) {
1338-
// PE
13391339
setKeystoreFile(prop);
13401340
setKeystoreType(keyStoreType);
13411341
}
13421342

1343-
/*
1344-
* Get keystore password from password.conf file.
1345-
* Notice that JSSE, the underlying SSL implementation in PE,
1346-
* currently does not support individual key entry passwords
1347-
* that are different from the keystore password.
1348-
*
1349-
String ksPasswd = null;
1350-
try {
1351-
ksPasswd = PasswordConfReader.getKeyStorePassword();
1352-
} catch (IOException ioe) {
1353-
// Ignore
1354-
}
1355-
if (ksPasswd == null) {
1356-
ksPasswd = System.getProperty("javax.net.ssl.keyStorePassword");
1357-
}
1358-
if (ksPasswd != null) {
1359-
try {
1360-
connector.setKeystorePass(ksPasswd);
1361-
} catch (Exception e) {
1362-
_logger.log(Level.SEVERE,
1363-
"pewebcontainer.http_listener_keystore_password_exception",
1364-
e);
1365-
}
1366-
}*/
1367-
1368-
/*
1369-
* Truststore
1370-
*/
1371-
prop = System.getProperty("javax.net.ssl.trustStore");
1343+
prop = System.getProperty(TRUSTSTORE_FILE.getSystemPropertyName());
1344+
keyStoreType = System.getProperty(TRUSTSTORE_TYPE.getSystemPropertyName(), DEFAULT_TRUSTSTORE_TYPE);
13721345
if (prop != null) {
1373-
// PE
13741346
setTruststore(prop);
1375-
setTruststoreType(DEFAULT_TRUSTSTORE_TYPE);
1347+
setTruststoreType(keyStoreType);
13761348
}
13771349
}
13781350

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

Lines changed: 16 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@
3030
import com.sun.enterprise.util.io.ServerDirs;
3131

3232
import java.io.File;
33-
import java.io.FileInputStream;
3433
import java.io.IOException;
35-
import java.security.KeyStore;
3634
import java.time.Duration;
3735
import java.util.List;
3836
import java.util.function.Supplier;
@@ -42,6 +40,7 @@
4240
import org.glassfish.api.ActionReport.ExitCode;
4341
import org.glassfish.api.admin.CommandException;
4442
import org.glassfish.main.jdke.i18n.LocalStringsImpl;
43+
import org.glassfish.main.jdke.security.KeyTool;
4544

4645
import static com.sun.enterprise.admin.cli.CLIConstants.DEFAULT_ADMIN_PORT;
4746
import static com.sun.enterprise.admin.cli.CLIConstants.DEFAULT_HOSTNAME;
@@ -50,11 +49,7 @@
5049
import static java.util.logging.Level.FINER;
5150

5251
/**
53-
* A class that's supposed to capture all the behavior common to operation on a "local" server. It's getting fairly
54-
* complicated thus the "section headers" comments. This class plays two roles,
55-
* <UL>
56-
* <LI>a place for putting common code - which are final methods. A parent class that is communicating with its own
57-
* unknown sub-classes. These are non-final methods
52+
* A class that's supposed to capture all the behavior common to operation on a "local" server.
5853
*
5954
* @author Byron Nevins
6055
*/
@@ -210,62 +205,45 @@ protected final String readFromMasterPasswordFile() {
210205
}
211206

212207
protected final boolean verifyMasterPassword(String mpv) {
213-
//issue : 14971, should ideally use javax.net.ssl.keyStore and
214-
//javax.net.ssl.keyStoreType system props here but they are
215-
//unavailable to asadmin start-domain hence falling back to
216-
//cacerts.jks instead of keystore.jks. Since the truststore
217-
//is less-likely to be Non-JKS
218-
219208
return loadAndVerifyKeystore(getJKS(), mpv);
220209
}
221210

222211
protected boolean loadAndVerifyKeystore(File jks, String mpv) {
223-
FileInputStream fis = null;
224212
try {
225-
fis = new FileInputStream(jks);
226-
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
227-
ks.load(fis, mpv.toCharArray());
213+
new KeyTool(jks, mpv.toCharArray()).loadKeyStore();
228214
return true;
229215
} catch (Exception e) {
230-
if (logger.isLoggable(FINER)) {
231-
logger.finer(e.getMessage());
232-
}
216+
logger.log(FINER, e.getMessage(), e);
233217
return false;
234-
} finally {
235-
try {
236-
if (fis != null) {
237-
fis.close();
238-
}
239-
} catch (IOException ioe) {
240-
// ignore, I know ...
241-
}
242218
}
243219
}
244220

245221
/**
246-
* Get the master password, either from a password file or by asking the user.
222+
* @return the master password, either from a password file or by asking the user.
247223
*/
248224
protected final String getMasterPassword() throws CommandException {
249225
// Sets the password into the launcher info.
250226
// Yes, returning master password as a string is not right ...
251-
final int RETRIES = 3;
252-
long t0 = now();
227+
final int countOfRetries = 3;
228+
final long start = System.currentTimeMillis();
253229
String mpv = passwords.get(CLIConstants.MASTER_PASSWORD);
254-
if (mpv == null) { //not specified in the password file
255-
mpv = "changeit"; //optimization for the default case -- see 9592
230+
if (mpv == null) {
231+
// not specified in the password file
232+
// optimization for the default case
233+
mpv = "changeit";
256234
if (!verifyMasterPassword(mpv)) {
257235
mpv = readFromMasterPasswordFile();
258236
if (!verifyMasterPassword(mpv)) {
259-
mpv = retry(RETRIES);
237+
mpv = retry(countOfRetries);
260238
}
261239
}
262-
} else { // the passwordfile contains AS_ADMIN_MASTERPASSWORD, use it
240+
} else {
241+
// the passwordfile contains AS_ADMIN_MASTERPASSWORD, use it
263242
if (!verifyMasterPassword(mpv)) {
264-
mpv = retry(RETRIES);
243+
mpv = retry(countOfRetries);
265244
}
266245
}
267-
long t1 = now();
268-
logger.log(FINER, "Time spent in master password extraction: {0} msec", (t1 - t0)); //TODO
246+
logger.log(FINER, "Time spent in master password extraction: {0} ms", (System.currentTimeMillis() - start));
269247
return mpv;
270248
}
271249

@@ -481,9 +459,4 @@ private File getUniquePath(File f) {
481459
}
482460
return f;
483461
}
484-
485-
private long now() {
486-
// it's just *so* ugly to call this directly!
487-
return System.currentTimeMillis();
488-
}
489462
}

nucleus/common/common-util/src/main/java/com/sun/enterprise/security/store/AsadminSecurityUtil.java

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2022 Contributors to the Eclipse Foundation
2+
* Copyright (c) 2022, 2025 Contributors to the Eclipse Foundation
33
* Copyright (c) 2010, 2021 Oracle and/or its affiliates. All rights reserved.
44
*
55
* This program and the accompanying materials are made available under the
@@ -20,13 +20,10 @@
2020
import com.sun.enterprise.util.CULoggerInfo;
2121
import com.sun.enterprise.util.io.FileUtils;
2222

23-
import java.io.BufferedInputStream;
2423
import java.io.Console;
2524
import java.io.File;
26-
import java.io.FileInputStream;
2725
import java.io.FileNotFoundException;
2826
import java.io.IOException;
29-
import java.io.InputStream;
3027
import java.nio.file.Path;
3128
import java.security.KeyStore;
3229
import java.security.KeyStoreException;
@@ -36,9 +33,10 @@
3633
import java.util.logging.Logger;
3734

3835
import org.glassfish.main.jdke.i18n.LocalStringsImpl;
36+
import org.glassfish.main.jdke.security.KeyTool;
3937

40-
import static com.sun.enterprise.util.SystemPropertyConstants.CLIENT_TRUSTSTORE_PASSWORD_PROPERTY;
41-
import static com.sun.enterprise.util.SystemPropertyConstants.KEYSTORE_PROPERTY;
38+
import static org.glassfish.embeddable.GlassFishVariable.KEYSTORE_FILE;
39+
import static org.glassfish.embeddable.GlassFishVariable.TRUSTSTORE_PASSWORD;
4240

4341
/**
4442
* Various utility methods related to certificate-based security.
@@ -111,16 +109,6 @@ public synchronized static AsadminSecurityUtil getInstance(final boolean isPromp
111109
return getInstance(null, isPromptable);
112110
}
113111

114-
115-
116-
/**
117-
* @return the master password for the keystore and truststore, as set by the system property
118-
* (defaulted if the property is not set).
119-
*/
120-
public static char[] getAsadminTruststorePassword() {
121-
return System.getProperty(CLIENT_TRUSTSTORE_PASSWORD_PROPERTY, "changeit").toCharArray();
122-
}
123-
124112
private AsadminSecurityUtil(final char[] commandLineMasterPassword, final boolean isPromptable) {
125113
try {
126114
init(commandLineMasterPassword, isPromptable);
@@ -178,10 +166,8 @@ private void init(final char[] commandLineMasterPassword, final boolean isPrompt
178166
}
179167
} catch (IOException ex) {
180168
if (ex.getCause() instanceof UnrecoverableKeyException) {
181-
/*
182-
* The password did not allow access to the keystore. Prompt
183-
* the user if possible.
184-
*/
169+
// The password did not allow access to the keystore.
170+
// Prompt the user if possible.
185171
if (!isPromptable) {
186172
throw ex;
187173
}
@@ -222,15 +208,11 @@ private AsadminTruststore openTruststore(final char[] password)
222208
*/
223209
private KeyStore openKeystore(final char[] candidateMasterPassword)
224210
throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException {
225-
final KeyStore permanentKS = KeyStore.getInstance("JKS");
226-
227-
try (InputStream keyStoreStream = asadminKeyStoreStream()) {
228-
if (keyStoreStream == null) {
229-
return null;
230-
}
231-
permanentKS.load(keyStoreStream, candidateMasterPassword);
232-
return permanentKS;
211+
final File keystoreFile = getAsadminKeyStoreFile();
212+
if (keystoreFile == null) {
213+
return null;
233214
}
215+
return new KeyTool(keystoreFile, candidateMasterPassword).loadKeyStore();
234216
}
235217

236218
/**
@@ -246,19 +228,19 @@ private char[] chooseMasterPassword(final char[] commandMasterPassword) {
246228
/**
247229
* Returns an open stream to the keystore.
248230
*
249-
* @return stream to the keystore
231+
* @return keystore file or null
250232
* @throws FileNotFoundException
251233
*/
252-
private InputStream asadminKeyStoreStream() throws FileNotFoundException {
253-
String location = System.getProperty(KEYSTORE_PROPERTY);
234+
private File getAsadminKeyStoreFile() throws FileNotFoundException {
235+
String location = System.getProperty(KEYSTORE_FILE.getSystemPropertyName());
254236
if (location == null) {
255237
return null;
256238
}
257-
return new BufferedInputStream(new FileInputStream(location));
239+
return new File(location);
258240
}
259241

260242
private char[] defaultMasterPassword() {
261-
return System.getProperty(CLIENT_TRUSTSTORE_PASSWORD_PROPERTY, "changeit").toCharArray();
243+
return System.getProperty(TRUSTSTORE_PASSWORD.getSystemPropertyName(), "changeit").toCharArray();
262244
}
263245

264246
}

0 commit comments

Comments
 (0)