Skip to content

Commit

Permalink
Merge pull request #489 from psmf22/tool_debricked
Browse files Browse the repository at this point in the history
feat: add tool config update command (update toolversions from zip bundle)
  • Loading branch information
rsenden authored Jan 10, 2024
2 parents 8d09303 + 6ba5e36 commit 896167c
Show file tree
Hide file tree
Showing 30 changed files with 743 additions and 191 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Comparator;
Expand Down Expand Up @@ -92,7 +93,7 @@ public static final void extractZip(File zipFile, Path targetDir) throws IOExcep
Files.createDirectories(resolvedPath);
} else {
Files.createDirectories(resolvedPath.getParent());
Files.copy(zipIn, resolvedPath);
Files.copy(zipIn, resolvedPath, StandardCopyOption.REPLACE_EXISTING);
}
}
}
Expand All @@ -109,7 +110,7 @@ public static final void extractTarGZ(File tgzFile, Path targetDir) throws IOExc
if(entry.isDirectory()) {
Files.createDirectories(extractTo);
} else {
Files.copy(tar, extractTo);
Files.copy(tar, extractTo, StandardCopyOption.REPLACE_EXISTING);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@
import java.util.Set;
import java.util.stream.Stream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
Expand All @@ -37,7 +34,10 @@
import com.fortify.cli.common.util.FcliDataHelper;
import com.fortify.cli.common.util.FileUtils;
import com.fortify.cli.common.util.StringUtils;
import com.fortify.cli.tool._common.helper.OsAndArchHelper;
import com.fortify.cli.tool._common.helper.SignatureHelper;
import com.fortify.cli.tool._common.helper.ToolHelper;
import com.fortify.cli.tool._common.helper.ToolVersionArtifactDescriptor;
import com.fortify.cli.tool._common.helper.ToolVersionCombinedDescriptor;
import com.fortify.cli.tool._common.helper.ToolVersionDownloadDescriptor;
import com.fortify.cli.tool._common.helper.ToolVersionInstallDescriptor;
Expand All @@ -49,12 +49,13 @@
import picocli.CommandLine.Option;

public abstract class AbstractToolInstallCommand extends AbstractOutputCommand implements IJsonNodeSupplier, IActionCommandResultSupplier {
private static final Logger LOG = LoggerFactory.getLogger(AbstractToolInstallCommand.class);
private static final Set<PosixFilePermission> binPermissions = PosixFilePermissions.fromString("rwxr-xr-x");
@Getter @Option(names={"-v", "--version"}, required = true, descriptionKey="fcli.tool.install.version", defaultValue = "default")
private String version;
@Getter @Option(names={"-d", "--install-dir"}, required = false, descriptionKey="fcli.tool.install.install-dir")
private File installDir;
@Getter @Option(names={"--type"}, required = false, descriptionKey="fcli.tool.install.type")
private String type;
@Mixin private CommonOptionMixins.RequireConfirmation requireConfirmation;
@Getter @Option(names={"--on-digest-mismatch"}, required = false, descriptionKey="fcli.tool.install.on-digest-mismatch", defaultValue = "fail")
private DigestMismatchAction onDigestMismatch;
Expand All @@ -66,7 +67,7 @@ private static enum DigestMismatchAction {
@Override
public final JsonNode getJsonNode() {
String toolName = getToolName();
ToolVersionDownloadDescriptor descriptor = ToolHelper.getToolDownloadDescriptor(toolName).getVersionOrDefault(version, getCpuArchitecture());
ToolVersionDownloadDescriptor descriptor = ToolHelper.getToolDownloadDescriptor(toolName).getVersionOrDefault(version);
return downloadAndInstall(toolName, descriptor);
}

Expand All @@ -86,8 +87,9 @@ private final JsonNode downloadAndInstall(String toolName, ToolVersionDownloadDe
Path binPath = getBinPath(downloadDescriptor);
ToolVersionInstallDescriptor installDescriptor = new ToolVersionInstallDescriptor(downloadDescriptor, installPath, binPath);
emptyExistingInstallPath(installDescriptor.getInstallPath());
File downloadedFile = download(downloadDescriptor);
checkDigest(downloadDescriptor, downloadedFile);
ToolVersionArtifactDescriptor artifactDescriptor = getArtifactDescriptor(downloadDescriptor, type);
File downloadedFile = download(artifactDescriptor);
SignatureHelper.verifyFileSignature(artifactDescriptor.getRsa_sha256(), downloadedFile, onDigestMismatch == DigestMismatchAction.fail);
install(installDescriptor, downloadedFile);
ToolVersionCombinedDescriptor combinedDescriptor = ToolHelper.saveToolVersionInstallDescriptor(toolName, installDescriptor);
return new ObjectMapper().<ObjectNode>valueToTree(combinedDescriptor);
Expand All @@ -96,13 +98,28 @@ private final JsonNode downloadAndInstall(String toolName, ToolVersionDownloadDe
}
}

private final File download(ToolVersionDownloadDescriptor descriptor) throws IOException {
private final File download(ToolVersionArtifactDescriptor artifactDescriptor) throws IOException {
File tempDownloadFile = File.createTempFile("fcli-tool-download", null);
tempDownloadFile.deleteOnExit();
download(descriptor.getDownloadUrl(), tempDownloadFile);
download(artifactDescriptor.getDownloadUrl(), tempDownloadFile);
return tempDownloadFile;
}

private final ToolVersionArtifactDescriptor getArtifactDescriptor(ToolVersionDownloadDescriptor downloadDescriptor, String type) {
if(type==null || type.isBlank()) {
String OSString = OsAndArchHelper.getOSString();
String archString = OsAndArchHelper.getArchString();
type = OSString + "/" + archString;
}
if(downloadDescriptor.getArtifacts().containsKey(type)) {
return downloadDescriptor.getArtifacts().get(type);
} else if(downloadDescriptor.getArtifacts().containsKey("default")) {
return downloadDescriptor.getArtifacts().get("default");
} else {
throw new RuntimeException("No default or matching artifact found for type " + type);
}
}

private final Void download(String downloadUrl, File destFile) {
UnirestInstance unirest = GenericUnirestFactory.getUnirestInstance("tool",
u->ProxyHelper.configureProxy(u, "tool", downloadUrl));
Expand All @@ -113,10 +130,10 @@ private final Void download(String downloadUrl, File destFile) {
protected void install(ToolVersionInstallDescriptor descriptor, File downloadedFile) throws IOException {
Path installPath = descriptor.getInstallPath();
Files.createDirectories(installPath);
InstallType installType = getInstallType();
InstallType installType = getInstallType(descriptor.getOriginalDownloadDescriptor());
switch (installType) {
// TODO Clean this up
case COPY: Files.copy(downloadedFile.toPath(), installPath.resolve(StringUtils.substringAfterLast(descriptor.getOriginalDownloadDescriptor().getDownloadUrl(), "/")), StandardCopyOption.REPLACE_EXISTING); break;
case COPY: Files.copy(downloadedFile.toPath(), installPath.resolve(StringUtils.substringAfterLast(getArtifactDescriptor(descriptor.getOriginalDownloadDescriptor(), type).getDownloadUrl(), "/")), StandardCopyOption.REPLACE_EXISTING); break;
case EXTRACT_ZIP: FileUtils.extractZip(downloadedFile, installPath); break;
case EXTRACT_TGZ: FileUtils.extractTarGZ(downloadedFile, installPath); break;
default: throw new RuntimeException("Unknown install type: "+installType.name());
Expand All @@ -139,7 +156,16 @@ protected Path getBinPath(ToolVersionDownloadDescriptor descriptor) {
}

protected abstract String getToolName();
protected abstract InstallType getInstallType();
protected InstallType getInstallType(ToolVersionDownloadDescriptor descriptor) {
ToolVersionArtifactDescriptor artifact = getArtifactDescriptor(descriptor, type);
if(artifact.getName().endsWith("gz")) {
return InstallType.EXTRACT_TGZ;
} else if(artifact.getName().endsWith("zip")) {
return InstallType.EXTRACT_ZIP;
} else {
return InstallType.COPY;
}
};
protected abstract void postInstall(ToolVersionInstallDescriptor installDescriptor) throws IOException;
protected String getCpuArchitecture() {
return "";
Expand All @@ -152,20 +178,6 @@ private final void emptyExistingInstallPath(Path installPath) throws IOException
}
}

private final void checkDigest(ToolVersionDownloadDescriptor descriptor, File downloadedFile) {
String actualDigest = FileUtils.getFileDigest(downloadedFile, descriptor.getDigestAlgorithm());
String expectedDigest = descriptor.getExpectedDigest();
if ( !actualDigest.equals(expectedDigest) ) {
String msg = "Digest mismatch"
+"\n Expected: "+expectedDigest
+"\n Actual: "+actualDigest;
switch(onDigestMismatch) {
case fail: throw new IllegalStateException(msg);
case warn: LOG.warn(msg);
}
}
}

private static final void updateBinPermissions(Path binPath) throws IOException {
try (Stream<Path> walk = Files.walk(binPath)) {
walk.forEach(AbstractToolInstallCommand::updateFilePermissions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public abstract class AbstractToolUninstallCommand extends AbstractOutputCommand
@Override
public final JsonNode getJsonNode() {
String toolName = getToolName();
ToolVersionCombinedDescriptor descriptor = ToolHelper.loadToolVersionCombinedDescriptor(toolName, version, getCpuArchitecture());
ToolVersionCombinedDescriptor descriptor = ToolHelper.loadToolVersionCombinedDescriptor(toolName, version);
if ( descriptor==null ) {
throw new IllegalArgumentException("Tool installation not found");
}
Expand Down Expand Up @@ -67,7 +67,4 @@ public boolean isSingular() {
}

protected abstract String getToolName();
protected String getCpuArchitecture() {
return "";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
package com.fortify.cli.tool._common.helper;

import java.util.Locale;

public class OsAndArchHelper {
public static final String getOSString() {
String OS = normalizeOs(System.getProperty("os.name", "generic"));
if (OS.equals("linux") || OS.equals("windows") || OS.equals("darwin")) {
return OS;
} else {
throw new RuntimeException("Unexpected OS detected: '" + OS + "'");
}
}

public static final String getArchString() {
String arch = normalizeArch(System.getProperty("os.arch", "generic"));

if(arch.equals("x86") || arch.equals("x64") || arch.equals("arm64")) {
return arch;
} else {
throw new RuntimeException("Unexpected cpu architecture detected: '" + arch + "'");
}
}

private static String normalizeOs(String value) {
value = normalize(value);
if (value.startsWith("aix")) {
return "linux";
}
if (value.startsWith("hpux")) {
return "linux";
}
if (value.startsWith("os400")) {
// Avoid the names such as os4000
if (value.length() <= 5 || !Character.isDigit(value.charAt(5))) {
return "os400";
}
}
if (value.startsWith("linux")) {
return "linux";
}
if (value.startsWith("mac") || value.startsWith("osx")) {
return "darwin";
}
if (value.startsWith("freebsd")) {
return "linux";
}
if (value.startsWith("openbsd")) {
return "linux";
}
if (value.startsWith("netbsd")) {
return "linux";
}
if (value.startsWith("solaris") || value.startsWith("sunos")) {
return "linux";
}
if (value.startsWith("windows")) {
return "windows";
}
if (value.startsWith("zos")) {
return "linux";
}
if(value.contains("darwin")) {
return "darwin";
}

return "unknown";
}

private static String normalizeArch(String value) {
value = normalize(value);
if (value.matches("^(x8664|amd64|ia32e|em64t|x64)$")) {
return "x64";
}
if (value.matches("^(x8632|x86|i[3-6]86|ia32|x32)$")) {
return "x86";
}
if (value.matches("^(ia64w?|itanium64)$")) {
return "itanium_64";
}
if ("ia64n".equals(value)) {
return "itanium_32";
}
if (value.matches("^(sparc|sparc32)$")) {
return "sparc_32";
}
if (value.matches("^(sparcv9|sparc64)$")) {
return "sparc_64";
}
if (value.matches("^(arm|arm32)$")) {
return "arm_32";
}
if ("aarch64".equals(value)) {
return "arm64";
}
if (value.matches("^(mips|mips32)$")) {
return "mips_32";
}
if (value.matches("^(mipsel|mips32el)$")) {
return "mipsel_32";
}
if ("mips64".equals(value)) {
return "mips_64";
}
if ("mips64el".equals(value)) {
return "mipsel_64";
}
if (value.matches("^(ppc|ppc32)$")) {
return "ppc_32";
}
if (value.matches("^(ppcle|ppc32le)$")) {
return "ppcle_32";
}
if ("ppc64".equals(value)) {
return "ppc_64";
}
if ("ppc64le".equals(value)) {
return "ppcle_64";
}
if ("s390".equals(value)) {
return "s390_32";
}
if ("s390x".equals(value)) {
return "s390_64";
}
if (value.matches("^(riscv|riscv32)$")) {
return "riscv";
}
if ("riscv64".equals(value)) {
return "riscv64";
}
if ("e2k".equals(value)) {
return "e2k";
}
if ("loongarch64".equals(value)) {
return "loongarch_64";
}
return "unknown";
}

private static String normalize(String value) {
if (value == null) {
return "";
}
return value.toLowerCase(Locale.US).replaceAll("[^a-z0-9]+", "");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.fortify.cli.tool._common.helper;

import java.io.File;
import java.nio.file.Files;
import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.Signature;
import java.security.spec.X509EncodedKeySpec;

import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class SignatureHelper {
private static final Logger LOG = LoggerFactory.getLogger(SignatureHelper.class);
private static String pubKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArij9U9yJVNc53oEMFWYp"
+ "NrXUG1UoRZseDh/p34q1uywD70RGKKWZvXIcUAZZwbZtCu4i0UzsrKRJeUwqanbc"
+ "woJvYanp6lc3DccXUN1w1Y0WOHOaBxiiK3B1TtEIH1cK/X+ZzazPG5nX7TSGh8Tp"
+ "/uxQzUFli2mDVLqaP62/fB9uJ2joX9Gtw8sZfuPGNMRoc8IdhjagbFkhFT7WCZnk"
+ "FH/4Co007lmXLAe12lQQqR/pOTeHJv1sfda1xaHtj4/Tcrq04Kx0ZmGAd5D9lA92"
+ "8pdBbzoe/mI5/Sk+nIY3AHkLXB9YAaKJf//Wb1yiP1/hchtVkfXyIaGM+cVyn7AN"
+ "VQIDAQAB";

public static final void verifyFileSignature(String sigString, File destFile, boolean throwOnFailure) {
X509EncodedKeySpec spec = new X509EncodedKeySpec(Base64.decodeBase64(pubKey));
try {
KeyFactory kf = KeyFactory.getInstance("RSA");
PublicKey pub = kf.generatePublic(spec);

Signature fileSig = Signature.getInstance("SHA256withRSA");
fileSig.initVerify(pub);
fileSig.update(Files.readAllBytes(destFile.toPath()));
if(!fileSig.verify(Base64.decodeBase64(sigString))) {
String msg = "Signature mismatch"
+"\n Expected: "+sigString
+"\n Actual: "+fileSig.hashCode();
if(throwOnFailure) {
throw new IllegalStateException(msg);
} else {
LOG.warn(msg);
}
}
} catch (IllegalStateException e) {
throw e;
} catch (Exception e) {
if(throwOnFailure) {
throw new RuntimeException(e);
} else {
LOG.warn("Signature verification failed", e);
}
}
}
}
Loading

0 comments on commit 896167c

Please sign in to comment.