Prepare auto login.

This commit is contained in:
Michael Lipp 2025-02-24 21:21:58 +01:00
parent c6704c886f
commit d1bc335db9

View file

@ -22,8 +22,11 @@ import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import java.util.logging.Level; import java.util.logging.Level;
import static org.jdrupes.vmoperator.common.Constants.DATA_DISPLAY_LOGIN;
import static org.jdrupes.vmoperator.common.Constants.DATA_DISPLAY_PASSWORD; import static org.jdrupes.vmoperator.common.Constants.DATA_DISPLAY_PASSWORD;
import static org.jdrupes.vmoperator.common.Constants.DATA_DISPLAY_USER;
import static org.jdrupes.vmoperator.common.Constants.DATA_PASSWORD_EXPIRY; import static org.jdrupes.vmoperator.common.Constants.DATA_PASSWORD_EXPIRY;
import org.jdrupes.vmoperator.runner.qemu.commands.QmpSetDisplayPassword; import org.jdrupes.vmoperator.runner.qemu.commands.QmpSetDisplayPassword;
import org.jdrupes.vmoperator.runner.qemu.commands.QmpSetPasswordExpiry; import org.jdrupes.vmoperator.runner.qemu.commands.QmpSetPasswordExpiry;
@ -99,47 +102,41 @@ public class DisplayController extends Component {
} }
private boolean setDisplayPassword() { private boolean setDisplayPassword() {
String password; return readFromFile(DATA_DISPLAY_PASSWORD).map(password -> {
Path dpPath = configDir.resolve(DATA_DISPLAY_PASSWORD);
if (dpPath.toFile().canRead()) {
logger.finer(() -> "Found display password");
try {
password = Files.readString(dpPath);
} catch (IOException e) {
logger.log(Level.WARNING, e, () -> "Cannot read display"
+ " password: " + e.getMessage());
return false;
}
} else {
logger.finer(() -> "No display password");
return false;
}
if (Objects.equals(this.currentPassword, password)) { if (Objects.equals(this.currentPassword, password)) {
return true; return true;
} }
this.currentPassword = password; this.currentPassword = password;
logger.fine(() -> "Updating display password"); logger.fine(() -> "Updating display password");
fire(new MonitorCommand(new QmpSetDisplayPassword(protocol, password))); fire(new MonitorCommand(
new QmpSetDisplayPassword(protocol, password)));
return true; return true;
}).orElse(false);
} }
private void setPasswordExpiry() { private void setPasswordExpiry() {
Path pePath = configDir.resolve(DATA_PASSWORD_EXPIRY); readFromFile(DATA_PASSWORD_EXPIRY).ifPresent(expiry -> {
if (!pePath.toFile().canRead()) { logger.fine(() -> "Updating expiry time to " + expiry);
return; fire(
} new MonitorCommand(new QmpSetPasswordExpiry(protocol, expiry)));
logger.finer(() -> "Found expiry time"); });
String expiry;
try {
expiry = Files.readString(pePath);
} catch (IOException e) {
logger.log(Level.WARNING, e, () -> "Cannot read expiry"
+ " time: " + e.getMessage());
return;
}
logger.fine(() -> "Updating expiry time");
fire(new MonitorCommand(new QmpSetPasswordExpiry(protocol, expiry)));
} }
private Optional<String> readFromFile(String dataItem) {
Path path = configDir.resolve(dataItem);
String label = dataItem.replace('-', ' ');
if (path.toFile().canRead()) {
logger.finer(() -> "Found display user");
try {
return Optional.ofNullable(Files.readString(path));
} catch (IOException e) {
logger.log(Level.WARNING, e, () -> "Cannot read " + label + ": "
+ e.getMessage());
return Optional.empty();
}
} else {
logger.finer(() -> "No " + label);
return Optional.empty();
}
}
} }