Skip to content

Commit

Permalink
update password key generation, remove dependence on the machine conf…
Browse files Browse the repository at this point in the history
…iguration which causes problems in slurm
  • Loading branch information
ZihengSun committed Jan 3, 2025
1 parent 69b0f35 commit 1dc0b50
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 36 deletions.
7 changes: 7 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
"mainClass": "com.gw.GeoweaverCLI",
"args": "list --process"
},
{
"type": "java",
"name": "reset password",
"request": "launch",
"mainClass": "com.gw.GeoweaverCLI",
"args": "resetpassword"
},
{
"type": "java",
"name": "Import",
Expand Down
79 changes: 43 additions & 36 deletions src/main/java/com/gw/utils/BaseTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@
import java.nio.file.Paths;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Date;
import java.util.List;
import java.util.Optional;
Expand All @@ -52,11 +54,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import oshi.SystemInfo;
import oshi.hardware.CentralProcessor;
import oshi.hardware.ComputerSystem;
import oshi.hardware.HardwareAbstractionLayer;
import oshi.software.os.OperatingSystem;


/**
* Class BaseTool.java
Expand Down Expand Up @@ -106,45 +104,54 @@ public BaseTool() {}

public String getLocalhostIdentifier() throws Exception {

String keystr = null;
String keystr = null;
Path keyFilePath = Paths.get(System.getProperty("user.home"), "geoweaver", ".key");

try {
try {

SystemInfo systemInfo = new SystemInfo();
OperatingSystem operatingSystem = systemInfo.getOperatingSystem();
HardwareAbstractionLayer hardwareAbstractionLayer = systemInfo.getHardware();
CentralProcessor centralProcessor = hardwareAbstractionLayer.getProcessor();
ComputerSystem computerSystem = hardwareAbstractionLayer.getComputerSystem();

String vendor = operatingSystem.getManufacturer();
String processorSerialNumber = computerSystem.getSerialNumber();
String uuid = computerSystem.getHardwareUUID();
String processorIdentifier = centralProcessor.getProcessorIdentifier().getIdentifier();
int processors = centralProcessor.getLogicalProcessorCount();

String delimiter = "-";

keystr =
String.format("%08x", vendor.hashCode())
+ delimiter
+ String.format("%08x", processorSerialNumber.hashCode())
+ delimiter
+ String.format("%08x", uuid.hashCode())
+ delimiter
+ String.format("%08x", processorIdentifier.hashCode())
+ delimiter
+ processors;
// Check if the key file exists, if not, generate and save a new key
if (!Files.exists(keyFilePath)) {
String randomKey = generateRandomKey();
Files.createDirectories(keyFilePath.getParent()); // Ensure the directory exists
Files.write(keyFilePath, randomKey.getBytes());
}

} catch (Exception e) {
// Read the key from the file
String savedKey = Files.readString(keyFilePath).trim();

e.printStackTrace();
// Use the saved key as the basis for the password
keystr = hashString(savedKey);

keystr = "GeoweaverWorkflowManagementSoftwareForAll";
}
} catch (Exception e) {

e.printStackTrace();

return keystr;
keystr = "GeoweaverWorkflowManagementSoftwareForAll";
}

return keystr;
}

private String generateRandomKey() {
SecureRandom secureRandom = new SecureRandom();
byte[] randomBytes = new byte[32]; // 256-bit key
secureRandom.nextBytes(randomBytes);
return Base64.getEncoder().encodeToString(randomBytes);
}

private String hashString(String input) throws NoSuchAlgorithmException {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] hash = digest.digest(input.getBytes());
StringBuilder hexString = new StringBuilder();
for (byte b : hash) {
String hex = Integer.toHexString(0xff & b);
if (hex.length() == 1) hexString.append('0');
hexString.append(hex);
}
return hexString.toString();
}


public static boolean isPortInUse(String host, int port) {
try (Socket socket = new Socket()) {
socket.connect(new InetSocketAddress(host, port), 200);
Expand Down

0 comments on commit 1dc0b50

Please sign in to comment.