mirror of
https://github.com/Stirling-Tools/Stirling-PDF.git
synced 2025-06-23 07:55:07 +00:00

# Description of Changes This pull request introduces a comprehensive auditing system to the application, along with minor updates to existing utilities and dependencies. The most significant changes include the addition of audit-related classes and enums, updates to the `ApplicationProperties` model to support auditing configuration, and enhancements to utility methods for handling static and trackable resources. ### Audit System Implementation: * **Audit Aspect for Method Annotations**: Added `AuditAspect` to process the new `@Audited` annotation, enabling detailed logging of method execution, HTTP requests, and operation results based on configurable audit levels. (`proprietary/src/main/java/stirling/software/proprietary/audit/AuditAspect.java`) * **Audit Event Types**: Introduced `AuditEventType` enum to define standardized event types for auditing, such as authentication events, file operations, and HTTP requests. (`proprietary/src/main/java/stirling/software/proprietary/audit/AuditEventType.java`) * **Audit Levels**: Added `AuditLevel` enum to define different levels of audit logging (OFF, BASIC, STANDARD, VERBOSE), providing granular control over the amount of data logged. (`proprietary/src/main/java/stirling/software/proprietary/audit/AuditLevel.java`) ### Application Properties Update: * **Audit Configuration in `ProFeatures`**: Updated the `ProFeatures` class in `ApplicationProperties` to include support for auditing with configurable retention days, levels, and enablement flags. (`common/src/main/java/stirling/software/common/model/ApplicationProperties.java`) ### Utility Enhancements: * **Static and Trackable Resource Handling**: Extended `RequestUriUtils` methods (`isStaticResource` and `isTrackableResource`) to recognize `.txt` files as valid static and trackable resources. (`common/src/main/java/stirling/software/common/util/RequestUriUtils.java`) [[1]](diffhunk://#diff-de3599037908683f2cd8f170939547612c6fc2203e9207eb4d7966508f92bbcbR22) [[2]](diffhunk://#diff-de3599037908683f2cd8f170939547612c6fc2203e9207eb4d7966508f92bbcbR39) ### Dependency Update: * **Spring Validation Starter**: Added `spring-boot-starter-validation` to project dependencies to support validation mechanisms required for auditing features. (`proprietary/build.gradle`) Dashboard WIP    --- ## Checklist ### General - [ ] I have read the [Contribution Guidelines](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/CONTRIBUTING.md) - [ ] I have read the [Stirling-PDF Developer Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/DeveloperGuide.md) (if applicable) - [ ] I have read the [How to add new languages to Stirling-PDF](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/HowToAddNewLanguage.md) (if applicable) - [ ] I have performed a self-review of my own code - [ ] My changes generate no new warnings ### Documentation - [ ] I have updated relevant docs on [Stirling-PDF's doc repo](https://github.com/Stirling-Tools/Stirling-Tools.github.io/blob/main/docs/) (if functionality has heavily changed) - [ ] I have read the section [Add New Translation Tags](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/HowToAddNewLanguage.md#add-new-translation-tags) (for new translation tags only) ### UI Changes (if applicable) - [ ] Screenshots or videos demonstrating the UI changes are attached (e.g., as comments or direct attachments in the PR) ### Testing (if applicable) - [ ] I have tested my changes locally. Refer to the [Testing Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/DeveloperGuide.md#6-testing) for more details. --------- Co-authored-by: a <a> Co-authored-by: pixeebot[bot] <104101892+pixeebot[bot]@users.noreply.github.com>
248 lines
8.9 KiB
Java
248 lines
8.9 KiB
Java
package stirling.software.SPDF;
|
|
|
|
import java.io.IOException;
|
|
import java.net.URISyntaxException;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Path;
|
|
import java.nio.file.Paths;
|
|
import java.util.Collections;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
import java.util.Properties;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.boot.SpringApplication;
|
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
|
import org.springframework.core.env.Environment;
|
|
import org.springframework.scheduling.annotation.EnableScheduling;
|
|
|
|
import io.github.pixee.security.SystemCommand;
|
|
|
|
import jakarta.annotation.PostConstruct;
|
|
import jakarta.annotation.PreDestroy;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
import stirling.software.SPDF.UI.WebBrowser;
|
|
import stirling.software.common.configuration.AppConfig;
|
|
import stirling.software.common.configuration.ConfigInitializer;
|
|
import stirling.software.common.configuration.InstallationPathConfig;
|
|
import stirling.software.common.model.ApplicationProperties;
|
|
import stirling.software.common.util.UrlUtils;
|
|
|
|
@Slf4j
|
|
@EnableScheduling
|
|
@SpringBootApplication(
|
|
scanBasePackages = {
|
|
"stirling.software.SPDF",
|
|
"stirling.software.common",
|
|
"stirling.software.proprietary"
|
|
})
|
|
public class SPDFApplication {
|
|
|
|
private static String serverPortStatic;
|
|
private static String baseUrlStatic;
|
|
private static String contextPathStatic;
|
|
|
|
private final AppConfig appConfig;
|
|
private final Environment env;
|
|
private final ApplicationProperties applicationProperties;
|
|
private final WebBrowser webBrowser;
|
|
|
|
public SPDFApplication(
|
|
AppConfig appConfig,
|
|
Environment env,
|
|
ApplicationProperties applicationProperties,
|
|
@Autowired(required = false) WebBrowser webBrowser) {
|
|
this.appConfig = appConfig;
|
|
this.env = env;
|
|
this.applicationProperties = applicationProperties;
|
|
this.webBrowser = webBrowser;
|
|
}
|
|
|
|
public static void main(String[] args) throws IOException, InterruptedException {
|
|
SpringApplication app = new SpringApplication(SPDFApplication.class);
|
|
|
|
Properties props = new Properties();
|
|
|
|
if (Boolean.parseBoolean(System.getProperty("STIRLING_PDF_DESKTOP_UI", "false"))) {
|
|
System.setProperty("java.awt.headless", "false");
|
|
app.setHeadless(false);
|
|
props.put("java.awt.headless", "false");
|
|
props.put("spring.main.web-application-type", "servlet");
|
|
|
|
int desiredPort = 8080;
|
|
String port = UrlUtils.findAvailablePort(desiredPort);
|
|
props.put("server.port", port);
|
|
System.setProperty("server.port", port);
|
|
log.info("Desktop UI mode: Using port {}", port);
|
|
}
|
|
|
|
app.setAdditionalProfiles(getActiveProfile(args));
|
|
|
|
ConfigInitializer initializer = new ConfigInitializer();
|
|
try {
|
|
initializer.ensureConfigExists();
|
|
} catch (IOException | URISyntaxException e) {
|
|
log.error("Error initialising configuration", e);
|
|
}
|
|
Map<String, String> propertyFiles = new HashMap<>();
|
|
|
|
// External config files
|
|
Path settingsPath = Paths.get(InstallationPathConfig.getSettingsPath());
|
|
log.info("Settings file: {}", settingsPath.toString());
|
|
if (Files.exists(settingsPath)) {
|
|
propertyFiles.put(
|
|
"spring.config.additional-location", "file:" + settingsPath.toString());
|
|
} else {
|
|
log.warn("External configuration file '{}' does not exist.", settingsPath.toString());
|
|
}
|
|
|
|
Path customSettingsPath = Paths.get(InstallationPathConfig.getCustomSettingsPath());
|
|
log.info("Custom settings file: {}", customSettingsPath.toString());
|
|
if (Files.exists(customSettingsPath)) {
|
|
String existingLocation =
|
|
propertyFiles.getOrDefault("spring.config.additional-location", "");
|
|
if (!existingLocation.isEmpty()) {
|
|
existingLocation += ",";
|
|
}
|
|
propertyFiles.put(
|
|
"spring.config.additional-location",
|
|
existingLocation + "file:" + customSettingsPath.toString());
|
|
} else {
|
|
log.warn(
|
|
"Custom configuration file '{}' does not exist.",
|
|
customSettingsPath.toString());
|
|
}
|
|
Properties finalProps = new Properties();
|
|
|
|
if (!propertyFiles.isEmpty()) {
|
|
finalProps.putAll(
|
|
Collections.singletonMap(
|
|
"spring.config.additional-location",
|
|
propertyFiles.get("spring.config.additional-location")));
|
|
}
|
|
|
|
if (!props.isEmpty()) {
|
|
finalProps.putAll(props);
|
|
}
|
|
app.setDefaultProperties(finalProps);
|
|
|
|
app.run(args);
|
|
|
|
// Ensure directories are created
|
|
try {
|
|
Files.createDirectories(Path.of(InstallationPathConfig.getTemplatesPath()));
|
|
Files.createDirectories(Path.of(InstallationPathConfig.getStaticPath()));
|
|
} catch (IOException e) {
|
|
log.error("Error creating directories: {}", e.getMessage());
|
|
}
|
|
|
|
printStartupLogs();
|
|
}
|
|
|
|
@PostConstruct
|
|
public void init() {
|
|
String baseUrl = appConfig.getBaseUrl();
|
|
String contextPath = appConfig.getContextPath();
|
|
String serverPort = appConfig.getServerPort();
|
|
baseUrlStatic = baseUrl;
|
|
contextPathStatic = contextPath;
|
|
serverPortStatic = serverPort;
|
|
String url = baseUrl + ":" + getStaticPort() + contextPath;
|
|
|
|
if (webBrowser != null
|
|
&& Boolean.parseBoolean(System.getProperty("STIRLING_PDF_DESKTOP_UI", "false"))) {
|
|
webBrowser.initWebUI(url);
|
|
} else {
|
|
String browserOpenEnv = env.getProperty("BROWSER_OPEN");
|
|
boolean browserOpen = browserOpenEnv != null && "true".equalsIgnoreCase(browserOpenEnv);
|
|
if (browserOpen) {
|
|
try {
|
|
String os = System.getProperty("os.name").toLowerCase();
|
|
Runtime rt = Runtime.getRuntime();
|
|
|
|
if (os.contains("win")) {
|
|
// For Windows
|
|
SystemCommand.runCommand(rt, "rundll32 url.dll,FileProtocolHandler " + url);
|
|
} else if (os.contains("mac")) {
|
|
SystemCommand.runCommand(rt, "open " + url);
|
|
} else if (os.contains("nix") || os.contains("nux")) {
|
|
SystemCommand.runCommand(rt, "xdg-open " + url);
|
|
}
|
|
} catch (IOException e) {
|
|
log.error("Error opening browser: {}", e.getMessage());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void setServerPortStatic(String port) {
|
|
if ("auto".equalsIgnoreCase(port)) {
|
|
// Use Spring Boot's automatic port assignment (server.port=0)
|
|
SPDFApplication.serverPortStatic =
|
|
"0"; // This will let Spring Boot assign an available port
|
|
} else {
|
|
SPDFApplication.serverPortStatic = port;
|
|
}
|
|
}
|
|
|
|
@PreDestroy
|
|
public void cleanup() {
|
|
if (webBrowser != null) {
|
|
webBrowser.cleanup();
|
|
}
|
|
}
|
|
|
|
private static void printStartupLogs() {
|
|
log.info("Stirling-PDF Started.");
|
|
String url = baseUrlStatic + ":" + getStaticPort() + contextPathStatic;
|
|
log.info("Navigate to {}", url);
|
|
}
|
|
|
|
private static String[] getActiveProfile(String[] args) {
|
|
// 1. Check for explicitly passed profiles
|
|
if (args != null) {
|
|
for (String arg : args) {
|
|
if (arg.startsWith("--spring.profiles.active=")) {
|
|
String[] provided = arg.substring(arg.indexOf('=') + 1).split(",");
|
|
if (provided.length > 0) {
|
|
return provided;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// 2. Detect if SecurityConfiguration is present on classpath
|
|
if (isClassPresent(
|
|
"stirling.software.proprietary.security.configuration.SecurityConfiguration")) {
|
|
log.info("Additional features in jar");
|
|
return new String[] {"security"};
|
|
} else {
|
|
log.info("Without additional features in jar");
|
|
return new String[] {"default"};
|
|
}
|
|
}
|
|
|
|
private static boolean isClassPresent(String className) {
|
|
try {
|
|
Class.forName(className, false, SPDFApplication.class.getClassLoader());
|
|
return true;
|
|
} catch (ClassNotFoundException e) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static String getStaticBaseUrl() {
|
|
return baseUrlStatic;
|
|
}
|
|
|
|
public static String getStaticPort() {
|
|
return serverPortStatic;
|
|
}
|
|
|
|
public static String getStaticContextPath() {
|
|
return contextPathStatic;
|
|
}
|
|
}
|