From 488abb9bb917a2547a80bccf5e75db614bba8760 Mon Sep 17 00:00:00 2001 From: NotedSalmon Date: Thu, 26 Sep 2024 16:46:28 +0100 Subject: [PATCH] FISH-9771 static SecureRandom moved in SecurityUtils By moving the SecureRandom to outside the previous method, I am able to use the random created in the other classes such as cluster and the command. --- .../sun/enterprise/config/serverbeans/Cluster.java | 5 ++--- .../admin/cli/cluster/ImportSyncBundleCommand.java | 3 +-- .../enterprise/universal/security/SecurityUtils.java | 11 ++++++++++- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/nucleus/admin/config-api/src/main/java/com/sun/enterprise/config/serverbeans/Cluster.java b/nucleus/admin/config-api/src/main/java/com/sun/enterprise/config/serverbeans/Cluster.java index 55c1647d22d..02955650bd4 100644 --- a/nucleus/admin/config-api/src/main/java/com/sun/enterprise/config/serverbeans/Cluster.java +++ b/nucleus/admin/config-api/src/main/java/com/sun/enterprise/config/serverbeans/Cluster.java @@ -615,7 +615,6 @@ class Decorator implements CreationDecorator { public void decorate(AdminCommandContext context, final Cluster instance) throws TransactionFailure, PropertyVetoException { Logger logger = ConfigApiLoggerInfo.getLogger(); LocalStringManagerImpl localStrings = new LocalStringManagerImpl(Cluster.class); - Random random = new SecureRandom(); Transaction t = Transaction.getTransaction(instance); //check if cluster software is installed else fail , see issue 12023 final CopyConfig command = (CopyConfig) runner @@ -725,7 +724,7 @@ public void decorate(AdminCommandContext context, final Cluster instance) throws // generate a random port since user did not provide one. // better fix in future would be to walk existing clusters and pick an unused port. - TCPPORT = Integer.toString(random.nextInt(9200 - 9090) + 9090); + TCPPORT = Integer.toString(SecurityUtils.nextInt(9200 - 9090) + 9090); // hardcode all instances to use same default port. // generate mode does not support multiple instances on one machine. @@ -746,7 +745,7 @@ public void decorate(AdminCommandContext context, final Cluster instance) throws gmsListenerPortSysProp.setName(propName); if (TCPPORT == null || TCPPORT.trim().charAt(0) == '$') { String generateGmsListenerPort = Integer.toString( - random.nextInt(9200 - 9090) + 9090); + SecurityUtils.nextInt(9200 - 9090) + 9090); gmsListenerPortSysProp.setValue(generateGmsListenerPort); } else { gmsListenerPortSysProp.setValue(TCPPORT); diff --git a/nucleus/cluster/cli/src/main/java/com/sun/enterprise/admin/cli/cluster/ImportSyncBundleCommand.java b/nucleus/cluster/cli/src/main/java/com/sun/enterprise/admin/cli/cluster/ImportSyncBundleCommand.java index 17662d3f1de..7e0192f3a77 100644 --- a/nucleus/cluster/cli/src/main/java/com/sun/enterprise/admin/cli/cluster/ImportSyncBundleCommand.java +++ b/nucleus/cluster/cli/src/main/java/com/sun/enterprise/admin/cli/cluster/ImportSyncBundleCommand.java @@ -132,7 +132,6 @@ public class ImportSyncBundleCommand extends LocalInstanceCommand { private File backupDir; private static final String RENDEZVOUS_PROPERTY_NAME = "rendezvousOccurred"; - private static final Random random = new SecureRandom(); private String instanceDottedName; private String rendevousDottedName; @@ -335,7 +334,7 @@ private void writeDasProperties() throws IOException { private void backupInstanceDir() { File f = getServerDirs().getServerDir(); if (f != null && f.isDirectory()) { - setBackupDir(random.nextInt()); + setBackupDir(SecurityUtils.nextInt()); File backup = getBackupDir(); if (!f.renameTo(backup)) { logger.warning(Strings.get("import.sync.bundle.backupInstanceDirFailed", f.getAbsolutePath(), backup.getAbsolutePath())); diff --git a/nucleus/common/common-util/src/main/java/com/sun/enterprise/universal/security/SecurityUtils.java b/nucleus/common/common-util/src/main/java/com/sun/enterprise/universal/security/SecurityUtils.java index 3bae3d14541..ffb63d70104 100644 --- a/nucleus/common/common-util/src/main/java/com/sun/enterprise/universal/security/SecurityUtils.java +++ b/nucleus/common/common-util/src/main/java/com/sun/enterprise/universal/security/SecurityUtils.java @@ -49,8 +49,17 @@ */ public final class SecurityUtils { + private static SecureRandom random = new SecureRandom(); + + private static int nextInt() { + return random.nextInt(); + } + + private static int nextInt(int bound) { + return random.nextInt(bound); + } + public static String getSecureRandomHexString(int numBytes) { - SecureRandom random = new SecureRandom(); byte[] bb = new byte[numBytes]; random.nextBytes(bb); return toHexString(bb);