Skip to content

Commit

Permalink
chore: Add support for global bin scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
rsenden committed Jan 22, 2024
1 parent 6bcc6dd commit 3c7355f
Show file tree
Hide file tree
Showing 25 changed files with 83 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ public static final InputStream getResourceInputStream(String resourcePath) {
}

public static final void copyResource(String resourcePath, Path destinationFilePath, CopyOption... options) {
var parent = destinationFilePath.getParent();
try {
Files.createDirectories(parent);
} catch (IOException e) {
throw new RuntimeException(String.format("Error creating directory %s", parent), e);
}
try ( InputStream in = getResourceInputStream(resourcePath) ) {
Files.copy( in, destinationFilePath, options);
} catch ( IOException e ) {
Expand All @@ -50,11 +56,6 @@ public static final void copyResource(String resourcePath, Path destinationFileP
}

public static final void copyResourceToDir(String resourcePath, Path destinationPath, CopyOption... options) {
try {
Files.createDirectories(destinationPath);
} catch (IOException e) {
throw new RuntimeException(String.format("Error creating directory %s", destinationPath), e);
}
String fileName = Paths.get(resourcePath).getFileName().toString();
copyResource(resourcePath, destinationPath.resolve(fileName), options);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.Consumer;
Expand Down Expand Up @@ -82,6 +83,29 @@ public final boolean isSingular() {
protected abstract void postInstall(ToolInstallationResult installationResult);
protected abstract String getDefaultArtifactType();

protected final void copyBinResource(ToolInstallationResult installationResult, String resourceFile) {
var fullResourceFile = getFullResourceFile(installationResult, resourceFile);
FileUtils.copyResourceToDir(fullResourceFile, installationResult.getInstallationDescriptor().getBinPath(), StandardCopyOption.REPLACE_EXISTING);
}

@SneakyThrows
protected final void copyGlobalBinResource(ToolInstallationResult installationResult, String resourceFile) {
if ( installDir==null ) {
// We only install global bin scripts if no explicit tool installation directory was specified
var fullResourceFile = getFullResourceFile(installationResult, resourceFile);
var destFilePath = getBasePath().resolve("bin").resolve(Path.of(resourceFile).getFileName());
FileUtils.copyResource(fullResourceFile, destFilePath, StandardCopyOption.REPLACE_EXISTING);
String content = new String(Files.readAllBytes(destFilePath), "ASCII");
content = content.replace("{{binDir}}", installationResult.getInstallationDescriptor().getBinDir());
Files.write(destFilePath, content.getBytes("ASCII"));
ToolInstaller.updateFilePermissions(destFilePath);
}
}

private String getFullResourceFile(ToolInstallationResult installationResult, String resourceFile) {
return String.format("com/fortify/cli/tool/%s/%s", installationResult.getToolName().replace("-", "_"), resourceFile);
}

private final ArrayNode install() {
try ( var progressWriter = progressWriterFactory.create() ) {
var preparer = new ToolInstallationPreparer();
Expand Down Expand Up @@ -109,13 +133,17 @@ private final Path getTargetPath(ToolInstaller toolInstaller) {
toolInstaller.getProgressWriter().writeWarning("WARN: --install-dir option is deprecated");
result = this.installDir.toPath();
} else {
var basePath = this.baseDir!=null
? this.baseDir.toPath()
: Path.of(System.getProperty("user.home"),"fortify", "tools");
var basePath = getBasePath();
result = basePath.resolve(String.format("%s/%s", getToolName(), toolInstaller.getToolVersion()));
}
return result.normalize().toAbsolutePath();
}

private Path getBasePath() {
return this.baseDir!=null
? this.baseDir.toPath()
: Path.of(System.getProperty("user.home"),"fortify", "tools");
}

private final class ToolInstallationPreparer implements Consumer<ToolInstaller> {
@Getter private final ArrayNode toolInstallationOutputDescriptors = OBJECTMAPPER.createArrayNode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,9 @@ private static final void updateBinPermissions(Path binPath) throws IOException
}
}

// TODO Move this method to FileUtils or similar, as it's also used by AbstractToolInstallCommand
@SneakyThrows
private static final void updateFilePermissions(Path p) {
public static final void updateFilePermissions(Path p) {
try {
Files.setPosixFilePermissions(p, binPermissions);
} catch ( UnsupportedOperationException e ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
import java.nio.file.Files;

import com.fortify.cli.common.output.cli.mixin.OutputHelperMixins;
import com.fortify.cli.common.util.FileUtils;
import com.fortify.cli.tool._common.cli.cmd.AbstractToolInstallCommand;
import com.fortify.cli.tool._common.helper.ToolInstallationHelper;
import com.fortify.cli.tool._common.helper.ToolInstaller.ToolInstallationResult;

import lombok.Getter;
Expand All @@ -38,9 +36,10 @@ protected String getDefaultArtifactType() {
@Override @SneakyThrows
protected void postInstall(ToolInstallationResult installationResult) {
var installationDescriptor = installationResult.getInstallationDescriptor();
var binPath = installationDescriptor.getBinPath();
FileUtils.copyResourceToDir(ToolInstallationHelper.getToolResourceFile(getToolName(), "extra-files/bin/FortifyBugTrackerUtility"), binPath);
FileUtils.copyResourceToDir(ToolInstallationHelper.getToolResourceFile(getToolName(), "extra-files/bin/FortifyBugTrackerUtility.bat"), binPath);
copyBinResource(installationResult, "extra-files/bin/FortifyBugTrackerUtility");
copyBinResource(installationResult, "extra-files/bin/FortifyBugTrackerUtility.bat");
copyGlobalBinResource(installationResult, "extra-files/global_bin/FortifyBugTrackerUtility");
copyGlobalBinResource(installationResult, "extra-files/global_bin/FortifyBugTrackerUtility.bat");

var jarFiles = Files.find(installationDescriptor.getInstallPath(), 1,
(p,a)->p.toFile().getName().matches("FortifyBugTrackerUtility.*\\.jar"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,7 @@ protected void postInstall(ToolInstallationResult installationResult) {
Path installPath = installationDescriptor.getInstallPath();
Path binPath = installationDescriptor.getBinPath();
FileUtils.moveFiles(installPath, binPath, "debricked(\\.exe)?");
copyGlobalBinResource(installationResult, "extra-files/global_bin/debricked");
copyGlobalBinResource(installationResult, "extra-files/global_bin/debricked.bat");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import com.fortify.cli.common.output.cli.mixin.OutputHelperMixins;
import com.fortify.cli.common.util.FileUtils;
import com.fortify.cli.tool._common.cli.cmd.AbstractToolInstallCommand;
import com.fortify.cli.tool._common.helper.ToolInstallationHelper;
import com.fortify.cli.tool._common.helper.ToolInstaller.ToolInstallationResult;

import lombok.Getter;
Expand All @@ -43,8 +42,13 @@ protected void postInstall(ToolInstallationResult installationResult) {
Path binPath = installationDescriptor.getBinPath();
FileUtils.moveFiles(installPath, binPath, "fcli(_completion)?(\\.exe)?");
if ( Files.exists(installPath.resolve("fcli.jar")) ) {
FileUtils.copyResourceToDir(ToolInstallationHelper.getToolResourceFile(getToolName(), "extra-files/bin/fcli"), binPath);
FileUtils.copyResourceToDir(ToolInstallationHelper.getToolResourceFile(getToolName(), "extra-files/bin/fcli.bat"), binPath);
copyBinResource(installationResult, "extra-files/jar/bin/fcli");
copyBinResource(installationResult, "extra-files/jar/bin/fcli.bat");
copyGlobalBinResource(installationResult, "extra-files/jar/global_bin/fcli");
copyGlobalBinResource(installationResult, "extra-files/jar/global_bin/fcli.bat");
} else {
copyGlobalBinResource(installationResult, "extra-files/native/global_bin/fcli");
copyGlobalBinResource(installationResult, "extra-files/native/global_bin/fcli.bat");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,8 @@
*******************************************************************************/
package com.fortify.cli.tool.fod_uploader.cli.cmd;

import java.nio.file.Path;

import com.fortify.cli.common.output.cli.mixin.OutputHelperMixins;
import com.fortify.cli.common.util.FileUtils;
import com.fortify.cli.tool._common.cli.cmd.AbstractToolInstallCommand;
import com.fortify.cli.tool._common.helper.ToolInstallationHelper;
import com.fortify.cli.tool._common.helper.ToolInstaller.ToolInstallationResult;

import lombok.Getter;
Expand All @@ -37,9 +33,9 @@ protected String getDefaultArtifactType() {

@Override @SneakyThrows
protected void postInstall(ToolInstallationResult installationResult) {
var installationDescriptor = installationResult.getInstallationDescriptor();
Path binPath = installationDescriptor.getBinPath();
FileUtils.copyResourceToDir(ToolInstallationHelper.getToolResourceFile(getToolName(), "extra-files/bin/FoDUpload"), binPath);
FileUtils.copyResourceToDir(ToolInstallationHelper.getToolResourceFile(getToolName(), "extra-files/bin/FoDUpload.bat"), binPath);
copyBinResource(installationResult, "extra-files/bin/FoDUpload");
copyBinResource(installationResult, "extra-files/bin/FoDUpload.bat");
copyGlobalBinResource(installationResult, "extra-files/global_bin/FoDUpload");
copyGlobalBinResource(installationResult, "extra-files/global_bin/FoDUpload.bat");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ protected String getDefaultArtifactType() {
@Override @SneakyThrows
protected void postInstall(ToolInstallationResult installationResult) {
var installationDescriptor = installationResult.getInstallationDescriptor();
// Updating bin permissions is handled by parent class
updateClientAuthToken(installationDescriptor.getInstallPath());
copyGlobalBinResource(installationResult, "extra-files/global_bin/scancentral");
copyGlobalBinResource(installationResult, "extra-files/global_bin/scancentral.bat");
}

private void updateClientAuthToken(Path installPath) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,8 @@
*******************************************************************************/
package com.fortify.cli.tool.vuln_exporter.cli.cmd;

import java.nio.file.Path;

import com.fortify.cli.common.output.cli.mixin.OutputHelperMixins;
import com.fortify.cli.common.util.FileUtils;
import com.fortify.cli.tool._common.cli.cmd.AbstractToolInstallCommand;
import com.fortify.cli.tool._common.helper.ToolInstallationHelper;
import com.fortify.cli.tool._common.helper.ToolInstaller.ToolInstallationResult;

import lombok.Getter;
Expand All @@ -37,9 +33,9 @@ protected String getDefaultArtifactType() {

@Override @SneakyThrows
protected void postInstall(ToolInstallationResult installationResult) {
var installationDescriptor = installationResult.getInstallationDescriptor();
Path binPath = installationDescriptor.getBinPath();
FileUtils.copyResourceToDir(ToolInstallationHelper.getToolResourceFile(getToolName(), "extra-files/bin/FortifyVulnerabilityExporter"), binPath);
FileUtils.copyResourceToDir(ToolInstallationHelper.getToolResourceFile(getToolName(), "extra-files/bin/FortifyVulnerabilityExporter.bat"), binPath);
copyBinResource(installationResult, "extra-files/bin/FortifyVulnerabilityExporter");
copyBinResource(installationResult, "extra-files/bin/FortifyVulnerabilityExporter.bat");
copyGlobalBinResource(installationResult, "extra-files/global_bin/FortifyVulnerabilityExporter");
copyGlobalBinResource(installationResult, "extra-files/global_bin/FortifyVulnerabilityExporter.bat");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
{{binDir}}/FortifyBugTrackerUtility "$@"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{binDir}}\FortifyBugTrackerUtility.bat %*
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
{{binDir}}/debricked "$@"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{binDir}}\debricked.exe %*
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
{{binDir}}/fcli "$@"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{binDir}}\fcli.bat %*
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
{{binDir}}/fcli "$@"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{binDir}}\fcli.exe %*
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
{{binDir}}/FoDUpload "$@"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{binDir}}\FoDUpload.bat %*
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
{{binDir}}/scancentral "$@"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{binDir}}\scancentral.bat %*
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
{{binDir}}/FortifyVulnerabilityExporter "$@"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{binDir}}\FortifyVulnerabilityExporter.bat %*

0 comments on commit 3c7355f

Please sign in to comment.