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.util.UUID;
|
|
|
|
|
2023-08-13 01:14:14 +01:00
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
import jakarta.annotation.PostConstruct;
|
2024-07-05 21:48:33 +02:00
|
|
|
import lombok.extern.slf4j.Slf4j;
|
2024-10-14 22:34:41 +01:00
|
|
|
import stirling.software.SPDF.config.interfaces.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
|
2024-07-05 21:48:33 +02:00
|
|
|
@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
|
|
|
|
2024-05-18 23:47:05 +02:00
|
|
|
@Autowired private ApplicationProperties applicationProperties;
|
2023-08-26 17:30:49 +01:00
|
|
|
|
2024-07-05 21:48:33 +02:00
|
|
|
@Autowired private DatabaseBackupInterface databaseBackupHelper;
|
2024-05-12 19:58:34 +02:00
|
|
|
|
2023-08-26 17:30:49 +01:00
|
|
|
@PostConstruct
|
2024-07-05 21:48:33 +02:00
|
|
|
public void init() throws IllegalArgumentException, IOException {
|
|
|
|
if (databaseBackupHelper.hasBackup() && !userService.hasUsers()) {
|
|
|
|
databaseBackupHelper.importDatabase();
|
|
|
|
} else if (!userService.hasUsers()) {
|
2024-05-18 23:47:05 +02:00
|
|
|
initializeAdminUser();
|
2024-07-05 21:48:33 +02:00
|
|
|
} else {
|
|
|
|
databaseBackupHelper.exportDatabase();
|
2024-05-18 23:47:05 +02:00
|
|
|
}
|
|
|
|
initializeInternalApiUser();
|
|
|
|
}
|
2023-08-26 22:33:23 +01:00
|
|
|
|
2024-07-05 21:48:33 +02:00
|
|
|
private void initializeAdminUser() throws IOException {
|
2024-05-18 23:47:05 +02:00
|
|
|
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 {
|
2024-05-19 10:52:11 +02:00
|
|
|
userService.saveUser(initialUsername, initialPassword, Role.ADMIN.getRoleId());
|
2024-07-05 21:48:33 +02:00
|
|
|
log.info("Admin user created: " + initialUsername);
|
2024-05-18 23:47:05 +02:00
|
|
|
} catch (IllegalArgumentException e) {
|
2024-07-05 21:48:33 +02:00
|
|
|
log.error("Failed to initialize security setup", e);
|
2024-05-18 23:47:05 +02:00
|
|
|
System.exit(1);
|
2023-12-30 19:11:27 +00:00
|
|
|
}
|
2024-05-18 23:47:05 +02:00
|
|
|
} else {
|
|
|
|
createDefaultAdminUser();
|
2023-12-30 19:11:27 +00:00
|
|
|
}
|
2024-05-18 23:47:05 +02:00
|
|
|
}
|
|
|
|
|
2024-07-05 21:48:33 +02:00
|
|
|
private void createDefaultAdminUser() throws IllegalArgumentException, IOException {
|
2024-05-18 23:47:05 +02:00
|
|
|
String defaultUsername = "admin";
|
|
|
|
String defaultPassword = "stirling";
|
|
|
|
if (!userService.findByUsernameIgnoreCase(defaultUsername).isPresent()) {
|
|
|
|
userService.saveUser(defaultUsername, defaultPassword, Role.ADMIN.getRoleId(), true);
|
2024-07-05 21:48:33 +02:00
|
|
|
log.info("Default admin user created: " + defaultUsername);
|
2024-05-18 23:47:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-05 21:48:33 +02:00
|
|
|
private void initializeInternalApiUser() throws IllegalArgumentException, IOException {
|
2024-04-14 23:07:03 +02:00
|
|
|
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());
|
2024-07-05 21:48:33 +02:00
|
|
|
log.info("Internal API user created: " + Role.INTERNAL_API_USER.getRoleId());
|
2023-08-26 17:30:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|