Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Load scan services dynamically based on configuration properties #470

Merged
merged 3 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/main/java/com/lpvs/service/LPVSQueueService.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.lpvs.entity.enums.LPVSPullRequestStatus;
import com.lpvs.repository.LPVSPullRequestRepository;
import com.lpvs.repository.LPVSQueueRepository;
import com.lpvs.service.scan.LPVSDetectService;
import com.lpvs.util.LPVSWebhookUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
* Use of this source code is governed by a MIT license that can be
* found in the LICENSE file.
*/
package com.lpvs.service;
package com.lpvs.service.scan;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

import com.lpvs.service.LPVSGitHubConnectionService;
import com.lpvs.service.LPVSGitHubService;
import com.lpvs.service.LPVSLicenseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
Expand All @@ -23,10 +26,8 @@

import com.lpvs.entity.LPVSFile;
import com.lpvs.entity.LPVSQueue;
import com.lpvs.service.scanner.scanoss.LPVSScanossDetectService;
import com.lpvs.util.LPVSCommentUtil;

import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;

/**
Expand All @@ -36,16 +37,6 @@
@Slf4j
public class LPVSDetectService {

/**
* The type of license detection scanner.
*/
private String scannerType;

/**
* Service responsible for performing license detection using the ScanOSS scanner.
*/
private LPVSScanossDetectService scanossDetectService;

/**
* Service responsible for establishing and managing connections to the GitHub API.
*/
Expand All @@ -61,6 +52,11 @@ public class LPVSDetectService {
*/
private LPVSGitHubService gitHubService;

/**
* Service responsible for initialization of the scanner.
*/
private LPVSScanService scanService;

/**
* GitHub pull request used to trigger a single license scan (optional).
*/
Expand All @@ -82,29 +78,24 @@ public class LPVSDetectService {
* Constructs an instance of LPVSDetectService with the specified parameters.
*
* @param scannerType The type of license detection scanner.
* @param isInternal Flag indicating whether the scanner is internal or not.
* @param gitHubConnectionService Service for connecting to the GitHub API.
* @param scanossDetectService Service for license detection using ScanOSS.
* @param licenseService Service for license conflict analysis.
* @param gitHubService Service for GitHub connection and operation.
* @param scanServiceFactory Service for creating instance of the scanner.
*/
@Autowired
public LPVSDetectService(
@Value("${scanner:scanoss}") String scannerType,
@Value("${internal:false}") boolean isInternal,
LPVSGitHubConnectionService gitHubConnectionService,
LPVSScanossDetectService scanossDetectService,
LPVSLicenseService licenseService,
LPVSGitHubService gitHubService) {
this.scannerType = scannerType;
LPVSGitHubService gitHubService,
LPVSScanServiceFactory scanServiceFactory) {
this.gitHubConnectionService = gitHubConnectionService;
this.scanossDetectService = scanossDetectService;
this.licenseService = licenseService;
this.gitHubService = gitHubService;
}

/**
* Initializes the LPVSDetectService bean and logs the selected license detection scanner.
*/
@PostConstruct
private void init() {
this.scanService = scanServiceFactory.createScanService(scannerType, isInternal);
log.info("License detection scanner: " + scannerType);
}

Expand Down Expand Up @@ -166,10 +157,12 @@ public void runOneScan() {
* @throws Exception if an error occurs during the scan.
*/
public List<LPVSFile> runScan(LPVSQueue webhookConfig, String path) throws Exception {
if (scannerType.equals("scanoss")) {
scanossDetectService.runScan(webhookConfig, path);
return scanossDetectService.checkLicenses(webhookConfig);
try {
scanService.runScan(webhookConfig, path);
return scanService.checkLicenses(webhookConfig);
} catch (IllegalArgumentException | NullPointerException ex) {
log.error(ex.getMessage());
return new ArrayList<>();
}
return new ArrayList<>();
}
}
35 changes: 35 additions & 0 deletions src/main/java/com/lpvs/service/scan/LPVSScanService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Copyright (c) 2024, Samsung Electronics Co., Ltd. All rights reserved.
*
* Use of this source code is governed by a MIT license that can be
* found in the LICENSE file.
*/
package com.lpvs.service.scan;

import com.lpvs.entity.LPVSFile;
import com.lpvs.entity.LPVSQueue;

import java.util.List;

/**
* Interface representing a service for scanning files and checking licenses.
*/
public interface LPVSScanService {

/**
* Runs a scan on the specified path using the provided webhook configuration.
*
* @param webhookConfig The webhook configuration to use for the scan.
* @param path The path to the file or directory to scan.
* @throws Exception if an error occurs during the scan process.
*/
void runScan(LPVSQueue webhookConfig, String path) throws Exception;

/**
* Checks licenses for files using the provided webhook configuration.
*
* @param webhookConfig The webhook configuration to use for checking licenses.
* @return A list of LPVSFile objects representing files with detected licenses.
*/
List<LPVSFile> checkLicenses(LPVSQueue webhookConfig);
}
82 changes: 82 additions & 0 deletions src/main/java/com/lpvs/service/scan/LPVSScanServiceFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/**
* Copyright (c) 2024, Samsung Electronics Co., Ltd. All rights reserved.
*
* Use of this source code is governed by a MIT license that can be
* found in the LICENSE file.
*/
package com.lpvs.service.scan;

import com.lpvs.repository.LPVSLicenseRepository;
import com.lpvs.service.LPVSLicenseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.lang.reflect.Constructor;

/**
* Factory class for creating instances of {@link LPVSScanService}.
*/
@Service
public class LPVSScanServiceFactory {

/**
* Flag indicating whether the application is in debug mode.
*/
@Value("${debug:false}")
Boolean debug;

/**
* The service for managing licenses, providing operations related to licenses.
*/
@Autowired LPVSLicenseService licenseService;

/**
* The repository for LPVSLicense entities, allowing database interactions for licenses.
*/
@Autowired LPVSLicenseRepository lpvsLicenseRepository;

/**
* Creates a scan service based on the specified scanner type and configuration.
*
* @param scannerType The type of scanner to create.
* @param isInternal Flag indicating whether the scanner is internal or not.
* @return An instance of {@link LPVSScanService} corresponding to the specified scanner type.
* @throws IllegalArgumentException if the specified scanner type is not supported or if an error occurs during
* the creation process.
*/
public LPVSScanService createScanService(String scannerType, boolean isInternal) {
try {
Class<?> serviceClass = Class.forName(getServiceClassName(scannerType, isInternal));
Constructor<?> constructor =
serviceClass.getDeclaredConstructor(
Boolean.class, LPVSLicenseService.class, LPVSLicenseRepository.class);
return (LPVSScanService)
constructor.newInstance(debug, licenseService, lpvsLicenseRepository);
} catch (Exception e) {
throw new IllegalArgumentException(
"Error creating scan service for type: " + scannerType, e);
}
}

/**
* Gets the fully qualified class name of the scan service based on the specified scanner type and configuration.
*
* @param scannerType The type of scanner.
* @param isInternal Flag indicating whether the scanner is internal or not.
* @return The fully qualified class name of the scan service.
* @throws IllegalArgumentException if the specified scanner type is null or empty string.
*/
protected String getServiceClassName(String scannerType, boolean isInternal) {
if (scannerType != null && !scannerType.isEmpty()) {
return "com.lpvs."
+ (isInternal ? "internal." : "")
+ "service.scan.scanner.LPVS"
+ scannerType.substring(0, 1).toUpperCase()
+ scannerType.substring(1)
+ "DetectService";
} else {
throw new IllegalArgumentException("Scanner type cannot be null or empty.");
}
}
}
15 changes: 15 additions & 0 deletions src/main/java/com/lpvs/service/scan/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Copyright (c) 2024, Samsung Electronics Co., Ltd. All rights reserved.
*
* Use of this source code is governed by a MIT license that can be
* found in the LICENSE file.
*/

/**
* Provides services related to scanning operations.
* <p>
* This package contains classes and interfaces that define services for performing scanning operations,
* such as checking licenses, and managing scanning configurations.
* </p>
*/
package com.lpvs.service.scan;
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Use of this source code is governed by a MIT license that can be
* found in the LICENSE file.
*/
package com.lpvs.service.scanner.scanoss;
package com.lpvs.service.scan.scanner;

import com.google.gson.*;
import com.google.gson.reflect.TypeToken;
Expand All @@ -13,6 +13,7 @@
import com.lpvs.entity.LPVSQueue;
import com.lpvs.repository.LPVSLicenseRepository;
import com.lpvs.service.LPVSLicenseService;
import com.lpvs.service.scan.LPVSScanService;
import com.lpvs.util.LPVSWebhookUtil;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
Expand All @@ -29,12 +30,12 @@
import static com.lpvs.util.LPVSFileUtil.getScanResultsJsonFilePath;

/**
* Service class responsible for interacting with the Scanoss scanner to detect licenses in files.
* Service class responsible for interacting with the Scanoss scanner to scan licenses in files.
* It handles the initiation of the scan, processing scan results, and checking for license conflicts.
*/
@Service
@Slf4j
public class LPVSScanossDetectService {
public class LPVSScanossDetectService implements LPVSScanService {

/**
* The service for managing licenses, providing operations related to licenses.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
*/

/**
* The {@code scanoss} package contains services and utilities related to the Scanoss scanner integration.
* Scanoss is a tool used for scanning and analyzing open source software components to detect licenses,
* The package contains service related to the Scanoss scanner integration.
* Scanoss is a tool used for scanning and analyzing open source software components to scan licenses,
* vulnerabilities, and other relevant information. This package encapsulates functionality for running
* Scanoss scans, processing scan results, and handling license-related operations.
* The package also contains utility classes for handling file paths, reading scan results, and managing licenses.
*/
package com.lpvs.service.scanner.scanoss;
package com.lpvs.service.scan.scanner;
1 change: 1 addition & 0 deletions src/test/java/com/lpvs/service/LPVSQueueServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.lpvs.entity.enums.LPVSPullRequestStatus;
import com.lpvs.repository.LPVSPullRequestRepository;
import com.lpvs.repository.LPVSQueueRepository;
import com.lpvs.service.scan.LPVSDetectService;
import lombok.extern.slf4j.Slf4j;

import org.junit.jupiter.api.AfterEach;
Expand Down
Loading
Loading