diff --git a/ide_extension/intellij/README.md b/ide_extension/intellij/README.md index 941eef36..0849ca57 100644 --- a/ide_extension/intellij/README.md +++ b/ide_extension/intellij/README.md @@ -42,3 +42,8 @@ Error running scan IaC command: java.io.IOException: Cannot run program "docker" In this case we recommend you to run the scan after a command that loads your env, for example if you use zsh, you can change the command to `zsh -c "source ~/.zshrc && "`. + +## Devs + +- [Developing a Plugin](https://plugins.jetbrains.com/docs/intellij/developing-plugins.html) +- [IntelliJ Platform Icons](https://intellij-icons.jetbrains.design/) \ No newline at end of file diff --git a/ide_extension/intellij/src/main/java/co/com/bancolombia/devsecopsenginetools/configuration/GlobalSettings.java b/ide_extension/intellij/src/main/java/co/com/bancolombia/devsecopsenginetools/configuration/GlobalSettings.java index 94db13fe..969dbd16 100644 --- a/ide_extension/intellij/src/main/java/co/com/bancolombia/devsecopsenginetools/configuration/GlobalSettings.java +++ b/ide_extension/intellij/src/main/java/co/com/bancolombia/devsecopsenginetools/configuration/GlobalSettings.java @@ -15,7 +15,7 @@ public class GlobalSettings implements PersistentStateComponent { public static final String DEFAULT_IAC_SCAN_COMMAND = "docker run --rm -v {projectPath}/dev-sec-ops/iac:/iac {image} devsecops-engine-tools --platform_devops local --remote_config_repo docker_default_remote_config --tool engine_iac --folder_path /iac"; public static final String DEFAULT_IMAGE_SCAN_COMMAND = "echo \"coming soon\""; - public static final String DEFAULT_IMAGE = "bancolombia/devsecops-engine-tools:1.7.41"; + public static final String DEFAULT_IMAGE = "bancolombia/devsecops-engine-tools:1.8.6"; private String scanIacCommand; private String scanImageCommand; diff --git a/ide_extension/intellij/src/main/java/co/com/bancolombia/devsecopsenginetools/configuration/ProjectSettings.java b/ide_extension/intellij/src/main/java/co/com/bancolombia/devsecopsenginetools/configuration/ProjectSettings.java index 381ecb61..22b01561 100644 --- a/ide_extension/intellij/src/main/java/co/com/bancolombia/devsecopsenginetools/configuration/ProjectSettings.java +++ b/ide_extension/intellij/src/main/java/co/com/bancolombia/devsecopsenginetools/configuration/ProjectSettings.java @@ -40,7 +40,7 @@ public void fillIfDefaults(Project project) { // calculable values String projectPath = project.getBasePath() == null ? "" : project.getBasePath(); if (Paths.get(projectPath, "build.gradle").toFile().exists()) { - preBuildScript = "gradle build"; + preBuildScript = "gradlew build"; buildContextPath = "build/libs"; } else { preBuildScript = ""; diff --git a/ide_extension/intellij/src/main/java/co/com/bancolombia/devsecopsenginetools/tasks/ScanIacTask.java b/ide_extension/intellij/src/main/java/co/com/bancolombia/devsecopsenginetools/tasks/ScanIacTask.java index 4f825e1b..8054c316 100644 --- a/ide_extension/intellij/src/main/java/co/com/bancolombia/devsecopsenginetools/tasks/ScanIacTask.java +++ b/ide_extension/intellij/src/main/java/co/com/bancolombia/devsecopsenginetools/tasks/ScanIacTask.java @@ -1,11 +1,8 @@ package co.com.bancolombia.devsecopsenginetools.tasks; import co.com.bancolombia.devsecopsenginetools.configuration.GlobalSettings; -import co.com.bancolombia.devsecopsenginetools.configuration.ProjectSettings; -import co.com.bancolombia.devsecopsenginetools.configuration.ProjectSettingsUtils; import co.com.bancolombia.devsecopsenginetools.ui.tool.LogPanelLogger; import co.com.bancolombia.devsecopsenginetools.utils.Commands; -import co.com.bancolombia.devsecopsenginetools.utils.DataUtils; import co.com.bancolombia.devsecopsenginetools.utils.FileUtils; import co.com.bancolombia.devsecopsenginetools.utils.docker.DockerLatestImage; import co.com.bancolombia.devsecopsenginetools.utils.http.HttpClient; @@ -16,11 +13,6 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.Map; - import static java.util.Objects.requireNonNull; public class ScanIacTask extends Task.Backgroundable { @@ -35,7 +27,7 @@ public ScanIacTask(@Nullable Project project, @NlsContexts.ProgressTitle @NotNul public void run(@NotNull ProgressIndicator progressIndicator) { try { LogPanelLogger.clear(); - prepareFiles(requireNonNull(myProject)); + FileUtils.copyIaCFiles(requireNonNull(myProject)); String command = getCommand(); LogPanelLogger.info("Running scan IaC command: " + command); Commands.runCommand(command, LogPanelLogger.getAppender()); @@ -56,23 +48,4 @@ public void run(@NotNull ProgressIndicator progressIndicator) { .replace("{projectPath}", requireNonNull(myProject.getBasePath())) .replace("{image}", image); } - - private void prepareFiles(Project project) throws IOException { - String projectPath = project.getBasePath() != null ? project.getBasePath() : ""; - Path iacDestination = Path.of(projectPath, "build", "dev-sec-ops", "iac"); - Files.createDirectories(iacDestination); - FileUtils.deleteDirectory(iacDestination); - ProjectSettings settings = ProjectSettingsUtils.getProjectSettings(project); - for (String source : settings.getIacDirectory().split(",")) { - Path iacSource = Path.of(projectPath, source); - LogPanelLogger.info("Copying IaC files from " + iacSource + " to " + iacDestination); - FileUtils.copyDirectory(iacSource, iacDestination); - } - if (settings.isReplaceTokens()) { - LogPanelLogger.info("Replacing tokens in IaC files"); - Map env = FileUtils.readEnvFile(Path.of(projectPath, settings.getDotEnvFile())); - FileUtils.walkDirectory(iacDestination, content -> - DataUtils.replaceTokens(content, settings.getReplacePattern(), env)); - } - } } diff --git a/ide_extension/intellij/src/main/java/co/com/bancolombia/devsecopsenginetools/tasks/ScanImageTask.java b/ide_extension/intellij/src/main/java/co/com/bancolombia/devsecopsenginetools/tasks/ScanImageTask.java index cd271d82..a9dcf70f 100644 --- a/ide_extension/intellij/src/main/java/co/com/bancolombia/devsecopsenginetools/tasks/ScanImageTask.java +++ b/ide_extension/intellij/src/main/java/co/com/bancolombia/devsecopsenginetools/tasks/ScanImageTask.java @@ -5,7 +5,6 @@ import co.com.bancolombia.devsecopsenginetools.configuration.ProjectSettingsUtils; import co.com.bancolombia.devsecopsenginetools.ui.tool.LogPanelLogger; import co.com.bancolombia.devsecopsenginetools.utils.Commands; -import co.com.bancolombia.devsecopsenginetools.utils.DataUtils; import co.com.bancolombia.devsecopsenginetools.utils.FileUtils; import com.intellij.openapi.progress.ProgressIndicator; import com.intellij.openapi.progress.Task; @@ -15,10 +14,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.io.IOException; -import java.nio.file.Files; import java.nio.file.Path; -import java.util.Map; import static java.util.Objects.requireNonNull; @@ -35,7 +31,7 @@ public ScanImageTask(@Nullable Project project, @NlsContexts.ProgressTitle @NotN public void run(@NotNull ProgressIndicator progressIndicator) { try { LogPanelLogger.clear(); - prepareFiles(requireNonNull(myProject)); + FileUtils.copyIaCFiles(requireNonNull(myProject)); GlobalSettings settings = GlobalSettings.getInstance(); String scanCommand = settings.getScanImageCommand().replace("{image}", settings.getDevSecOpsImage()); ProjectSettings projectSettings = ProjectSettingsUtils.getProjectSettings(myProject); @@ -56,27 +52,8 @@ public void run(@NotNull ProgressIndicator progressIndicator) { LogPanelLogger.info("Running scan Image command: " + scanCommand); Commands.runCommand(scanCommand, LogPanelLogger.getAppender()); } catch (Exception ex) { - LogPanelLogger.error("Error running scan IaC command: ", ex); + LogPanelLogger.error("Error running scan Image command: ", ex); } completable.complete(); } - - private void prepareFiles(Project project) throws IOException { - String projectPath = project.getBasePath() != null ? project.getBasePath() : ""; - Path iacDestination = Path.of(projectPath, "build", "dev-sec-ops", "iac"); - FileUtils.deleteDirectory(iacDestination); - Files.createDirectories(iacDestination); - ProjectSettings settings = ProjectSettingsUtils.getProjectSettings(project); - for (String source : settings.getIacDirectory().split(",")) { - Path iacSource = Path.of(projectPath, source); - LogPanelLogger.info("Copying IaC files from " + iacSource + " to " + iacDestination); - FileUtils.copyDirectory(iacSource, iacDestination); - } - if (settings.isReplaceTokens()) { - LogPanelLogger.info("Replacing tokens in IaC files"); - Map env = FileUtils.readEnvFile(Path.of(projectPath, settings.getDotEnvFile())); - FileUtils.walkDirectory(iacDestination, content -> - DataUtils.replaceTokens(content, settings.getReplacePattern(), env)); - } - } } diff --git a/ide_extension/intellij/src/main/java/co/com/bancolombia/devsecopsenginetools/ui/configuration/GlobalConfiguration.java b/ide_extension/intellij/src/main/java/co/com/bancolombia/devsecopsenginetools/ui/configuration/GlobalConfiguration.java index f85cee08..179e327d 100644 --- a/ide_extension/intellij/src/main/java/co/com/bancolombia/devsecopsenginetools/ui/configuration/GlobalConfiguration.java +++ b/ide_extension/intellij/src/main/java/co/com/bancolombia/devsecopsenginetools/ui/configuration/GlobalConfiguration.java @@ -94,6 +94,7 @@ public void apply() { // image globalSettings.setDevSecOpsImage(dockerImage.getText()); globalSettings.setScanImageCommand(scanImageCommand.getText()); + globalSettings.setCheckForLatestImage(checkForLatestImageCheckBox.isSelected()); // variables globalSettings.setAzureDevOpsOrganization(azureOrganization.getText()); globalSettings.setAzureDevOpsProject(azureProject.getText()); @@ -113,8 +114,9 @@ private void loadConfig() { globalSettings = GlobalSettings.getInstance(); if (globalSettings != null) { - dockerImage.setText(globalSettings.getDevSecOpsImage()); scanIacCommand.setText(globalSettings.getScanIacCommand()); + + dockerImage.setText(globalSettings.getDevSecOpsImage()); scanImageCommand.setText(globalSettings.getScanImageCommand()); checkForLatestImageCheckBox.setSelected(globalSettings.isCheckForLatestImage()); diff --git a/ide_extension/intellij/src/main/java/co/com/bancolombia/devsecopsenginetools/utils/Commands.java b/ide_extension/intellij/src/main/java/co/com/bancolombia/devsecopsenginetools/utils/Commands.java index 4857b0ef..ac8bf140 100644 --- a/ide_extension/intellij/src/main/java/co/com/bancolombia/devsecopsenginetools/utils/Commands.java +++ b/ide_extension/intellij/src/main/java/co/com/bancolombia/devsecopsenginetools/utils/Commands.java @@ -1,5 +1,6 @@ package co.com.bancolombia.devsecopsenginetools.utils; +import lombok.SneakyThrows; import lombok.experimental.UtilityClass; import java.io.BufferedReader; @@ -27,9 +28,11 @@ public static void runCommand(String command, Appender appender, ProcessBuilder String[] cmd = DataUtils.splitCommand(current); processBuilder.command(cmd); Process process = processBuilder.start(); - printOutput(appender, process.getInputStream()); - printOutput(appender, process.getErrorStream()); + Thread infoLog = printOutputAsync(appender, process.getInputStream()); + Thread errorLog = printOutputAsync(appender, process.getErrorStream()); int exitVal = process.waitFor(); + infoLog.join(); + errorLog.join(); if (exitVal != 0) { throw new IOException("Error running command: " + current + ", exit code: " + exitVal); } @@ -38,7 +41,14 @@ public static void runCommand(String command, Appender appender, ProcessBuilder appender.success("Command executed successfully in " + formatDuration(duration)); } - private static void printOutput(Appender appender, InputStream is) throws IOException { + private static Thread printOutputAsync(Appender appender, InputStream is) throws InterruptedException { + Thread thread = new Thread(() -> printOutput(appender, is)); + thread.start(); + return thread; + } + + @SneakyThrows + private static void printOutput(Appender appender, InputStream is) { BufferedReader reader = new BufferedReader(new InputStreamReader(is)); String line; while ((line = reader.readLine()) != null) { diff --git a/ide_extension/intellij/src/main/java/co/com/bancolombia/devsecopsenginetools/utils/FileUtils.java b/ide_extension/intellij/src/main/java/co/com/bancolombia/devsecopsenginetools/utils/FileUtils.java index 31c8b1a8..3585d731 100644 --- a/ide_extension/intellij/src/main/java/co/com/bancolombia/devsecopsenginetools/utils/FileUtils.java +++ b/ide_extension/intellij/src/main/java/co/com/bancolombia/devsecopsenginetools/utils/FileUtils.java @@ -1,6 +1,9 @@ package co.com.bancolombia.devsecopsenginetools.utils; +import co.com.bancolombia.devsecopsenginetools.configuration.ProjectSettings; +import co.com.bancolombia.devsecopsenginetools.configuration.ProjectSettingsUtils; import co.com.bancolombia.devsecopsenginetools.ui.tool.LogPanelLogger; +import com.intellij.openapi.project.Project; import lombok.experimental.UtilityClass; import org.apache.commons.io.file.PathUtils; @@ -89,6 +92,25 @@ public static ResourceBundle getProperties() { return ResourceBundle.getBundle("plugin"); } + public static void copyIaCFiles(Project project) throws IOException { + String projectPath = project.getBasePath() != null ? project.getBasePath() : ""; + Path iacDestination = Path.of(projectPath, "build", "dev-sec-ops", "iac"); + Files.createDirectories(iacDestination); + FileUtils.deleteDirectory(iacDestination); + ProjectSettings settings = ProjectSettingsUtils.getProjectSettings(project); + for (String source : settings.getIacDirectory().split(",")) { + Path iacSource = Path.of(projectPath, source); + LogPanelLogger.info("Copying IaC files from " + iacSource + " to " + iacDestination); + FileUtils.copyDirectory(iacSource, iacDestination); + } + if (settings.isReplaceTokens()) { + LogPanelLogger.info("Replacing tokens in IaC files"); + Map env = FileUtils.readEnvFile(Path.of(projectPath, settings.getDotEnvFile())); + FileUtils.walkDirectory(iacDestination, content -> + DataUtils.replaceTokens(content, settings.getReplacePattern(), env)); + } + } + public static String findDockerfile(String projectPath) { String path = findDockerfile(new File(projectPath)) .map(File::getAbsolutePath).orElse("").replace(projectPath, ""); diff --git a/ide_extension/intellij/src/main/resources/META-INF/plugin.xml b/ide_extension/intellij/src/main/resources/META-INF/plugin.xml index 483f4dde..78f24e0a 100644 --- a/ide_extension/intellij/src/main/resources/META-INF/plugin.xml +++ b/ide_extension/intellij/src/main/resources/META-INF/plugin.xml @@ -7,7 +7,6 @@ - +