Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

122 lines
4.6 KiB
Java
Raw Normal View History

2023-08-13 01:14:14 +01:00
package stirling.software.SPDF.config.security;
2023-08-27 00:39:22 +01:00
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.UUID;
import org.simpleyaml.configuration.file.YamlFile;
import org.simpleyaml.configuration.implementation.SimpleYamlImplementation;
import org.simpleyaml.configuration.implementation.snakeyaml.lib.DumperOptions;
2023-08-13 01:14:14 +01:00
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import stirling.software.SPDF.config.DatabaseBackupInterface;
2023-08-26 17:30:49 +01:00
import stirling.software.SPDF.model.ApplicationProperties;
2023-08-13 01:14:14 +01:00
import stirling.software.SPDF.model.Role;
2023-12-30 19:11:27 +00:00
2023-08-13 01:14:14 +01:00
@Component
@Slf4j
2023-08-27 11:59:08 +01:00
public class InitialSecuritySetup {
2023-08-13 01:14:14 +01:00
2023-08-26 17:30:49 +01:00
@Autowired private UserService userService;
2023-09-02 00:05:50 +01:00
@Autowired private ApplicationProperties applicationProperties;
2023-08-26 17:30:49 +01:00
@Autowired private DatabaseBackupInterface databaseBackupHelper;
2023-08-26 17:30:49 +01:00
@PostConstruct
public void init() throws IllegalArgumentException, IOException {
if (databaseBackupHelper.hasBackup() && !userService.hasUsers()) {
databaseBackupHelper.importDatabase();
} else if (!userService.hasUsers()) {
initializeAdminUser();
} else {
databaseBackupHelper.exportDatabase();
}
initializeInternalApiUser();
}
2023-08-26 22:33:23 +01:00
@PostConstruct
public void initSecretKey() throws IOException {
String secretKey = applicationProperties.getAutomaticallyGenerated().getKey();
if (!isValidUUID(secretKey)) {
secretKey = UUID.randomUUID().toString(); // Generating a random UUID as the secret key
saveKeyToConfig(secretKey);
}
}
2024-05-18 19:38:39 +01:00
private void initializeAdminUser() throws IOException {
String initialUsername =
applicationProperties.getSecurity().getInitialLogin().getUsername();
String initialPassword =
applicationProperties.getSecurity().getInitialLogin().getPassword();
if (initialUsername != null
&& !initialUsername.isEmpty()
&& initialPassword != null
&& !initialPassword.isEmpty()
&& !userService.findByUsernameIgnoreCase(initialUsername).isPresent()) {
try {
userService.saveUser(initialUsername, initialPassword, Role.ADMIN.getRoleId());
log.info("Admin user created: " + initialUsername);
} catch (IllegalArgumentException e) {
log.error("Failed to initialize security setup", e);
System.exit(1);
2023-12-30 19:11:27 +00:00
}
} else {
createDefaultAdminUser();
2023-12-30 19:11:27 +00:00
}
}
private void createDefaultAdminUser() throws IllegalArgumentException, IOException {
String defaultUsername = "admin";
String defaultPassword = "stirling";
if (!userService.findByUsernameIgnoreCase(defaultUsername).isPresent()) {
userService.saveUser(defaultUsername, defaultPassword, Role.ADMIN.getRoleId(), true);
log.info("Default admin user created: " + defaultUsername);
}
}
private void initializeInternalApiUser() throws IllegalArgumentException, IOException {
if (!userService.usernameExistsIgnoreCase(Role.INTERNAL_API_USER.getRoleId())) {
2023-12-25 12:58:49 +00:00
userService.saveUser(
Role.INTERNAL_API_USER.getRoleId(),
UUID.randomUUID().toString(),
Role.INTERNAL_API_USER.getRoleId());
2023-12-24 17:12:32 +00:00
userService.addApiKeyToUser(Role.INTERNAL_API_USER.getRoleId());
log.info("Internal API user created: " + Role.INTERNAL_API_USER.getRoleId());
2023-08-26 17:30:49 +01:00
}
}
private void saveKeyToConfig(String key) throws IOException {
2023-08-26 22:33:23 +01:00
Path path = Paths.get("configs", "settings.yml"); // Target the configs/settings.yml
2023-08-26 17:30:49 +01:00
final YamlFile settingsYml = new YamlFile(path.toFile());
DumperOptions yamlOptionssettingsYml =
((SimpleYamlImplementation) settingsYml.getImplementation()).getDumperOptions();
yamlOptionssettingsYml.setSplitLines(false);
settingsYml.loadWithComments();
2023-08-26 17:30:49 +01:00
settingsYml
.path("AutomaticallyGenerated.key")
.set(key)
.comment("# Automatically Generated Settings (Do Not Edit Directly)");
settingsYml.save();
2023-08-26 17:30:49 +01:00
}
2024-05-05 13:33:17 +01:00
private boolean isValidUUID(String uuid) {
if (uuid == null) {
return false;
}
try {
UUID.fromString(uuid);
return true;
} catch (IllegalArgumentException e) {
return false;
}
}
2023-08-26 17:30:49 +01:00
}