Skip to content

Commit

Permalink
FISH-9771 static SecureRandom moved in SecurityUtils
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
NotedSalmon committed Sep 26, 2024
1 parent d714132 commit 488abb9
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,6 @@ class Decorator implements CreationDecorator<Cluster> {
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
Expand Down Expand Up @@ -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.
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 488abb9

Please sign in to comment.