Skip to content

Commit

Permalink
fix(intellij): Fix intellij plugin settings (#199)
Browse files Browse the repository at this point in the history
  • Loading branch information
juancgalvis authored Aug 28, 2024
1 parent eb425d5 commit 4848d85
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 60 deletions.
5 changes: 5 additions & 0 deletions ide_extension/intellij/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 && <scan command here>"`.

## Devs

- [Developing a Plugin](https://plugins.jetbrains.com/docs/intellij/developing-plugins.html)
- [IntelliJ Platform Icons](https://intellij-icons.jetbrains.design/)
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
public class GlobalSettings implements PersistentStateComponent<GlobalSettings> {
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "";
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {
Expand All @@ -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());
Expand All @@ -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<String, String> env = FileUtils.readEnvFile(Path.of(projectPath, settings.getDotEnvFile()));
FileUtils.walkDirectory(iacDestination, content ->
DataUtils.replaceTokens(content, settings.getReplacePattern(), env));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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);
Expand All @@ -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<String, String> env = FileUtils.readEnvFile(Path.of(projectPath, settings.getDotEnvFile()));
FileUtils.walkDirectory(iacDestination, content ->
DataUtils.replaceTokens(content, settings.getReplacePattern(), env));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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());

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package co.com.bancolombia.devsecopsenginetools.utils;

import lombok.SneakyThrows;
import lombok.experimental.UtilityClass;

import java.io.BufferedReader;
Expand Down Expand Up @@ -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);
}
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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<String, String> 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, "");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
<!-- Extension points defined by the plugin.
Read more: https://plugins.jetbrains.com/docs/intellij/plugin-extension-points.html -->
<extensions defaultExtensionNs="com.intellij">
<systemProperty value="true" id="name"/>
<groupConfigurable id="DevSecOpsEngineTools.GroupConfiguration"
parentId="tools"
displayNameKey="devsecops-engine-tools.plugin.settings.displayName"
Expand All @@ -32,6 +31,7 @@
icon="/icons/logo.svg"
factoryClass="co.com.bancolombia.devsecopsenginetools.ui.tool.LogPanelLogger"/>

<errorHandler implementation="com.intellij.diagnostic.JetBrainsMarketplaceErrorReportSubmitter"/>
</extensions>

<actions>
Expand Down

0 comments on commit 4848d85

Please sign in to comment.