Skip to content
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
6 changes: 3 additions & 3 deletions src/main/java/io/github/guacsec/trustifyda/Provider.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ public Content(byte[] buffer, String type) {
/** The ecosystem of this provider, i.e. maven. */
public final Ecosystem.Type ecosystem;

public final Path manifest;
public final Path manifestPath;

protected final ObjectMapper objectMapper = new ObjectMapper();

protected Provider(Ecosystem.Type ecosystem, Path manifest) {
protected Provider(Ecosystem.Type ecosystem, Path manifestPath) {
this.ecosystem = ecosystem;
this.manifest = manifest;
this.manifestPath = manifestPath;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ private void addDependencies(
handleSingleCrate(sbom, root, metadata, nodeMap, packageMap, ignoredDeps, analysisType);
}

} catch (Exception e) {
} catch (IOException | InterruptedException e) {
log.severe("Unexpected error during " + analysisType + " analysis: " + e.getMessage());
}
}
Expand Down Expand Up @@ -385,7 +385,7 @@ private Path findOutermostCargoTomlDirectory(Path startDir) {
}

private CargoMetadata executeCargoMetadata() throws IOException, InterruptedException {
Path workingDir = manifest.getParent();
Path workingDir = manifestPath.getParent();

if (debugLoggingIsNeeded()) {
log.info("Executing cargo metadata for full dependency resolution with resolved versions");
Expand Down Expand Up @@ -657,20 +657,21 @@ public CargoProvider(Path manifest) {
@Override
public String readLicenseFromManifest() {
String manifestLicense = readLicenseFromToml(null);
return LicenseUtils.getLicense(manifestLicense, manifest);
return LicenseUtils.getLicense(manifestLicense, manifestPath);
}

private String readLicenseFromToml(TomlParseResult existingResult) {
try {
TomlParseResult tomlResult = existingResult != null ? existingResult : Toml.parse(manifest);
TomlParseResult tomlResult =
existingResult != null ? existingResult : Toml.parse(manifestPath);
if (tomlResult.hasErrors()) {
return null;
}
String license = tomlResult.getString("package.license");
return LicenseUtils.getLicense(license, manifest);
return LicenseUtils.getLicense(license, manifestPath);
} catch (IOException e) {
log.warning("Failed to read license from Cargo.toml: " + e.getMessage());
return LicenseUtils.getLicense(null, manifest);
return LicenseUtils.getLicense(null, manifestPath);
}
}

Expand All @@ -687,11 +688,11 @@ public Content provideStack() throws IOException {
}

private Sbom createSbom(AnalysisType analysisType) throws IOException {
if (!Files.exists(manifest) || !Files.isRegularFile(manifest)) {
throw new IOException("Cargo.toml not found: " + manifest);
if (!Files.exists(manifestPath) || !Files.isRegularFile(manifestPath)) {
throw new IOException("Cargo.toml not found: " + manifestPath);
}

TomlParseResult tomlResult = Toml.parse(manifest);
TomlParseResult tomlResult = Toml.parse(manifestPath);
if (tomlResult.hasErrors()) {
throw new IOException(
"Invalid Cargo.toml format: " + tomlResult.errors().get(0).getMessage());
Expand All @@ -706,7 +707,7 @@ private Sbom createSbom(AnalysisType analysisType) throws IOException {
Type.CARGO.getType(), null, projectInfo.name(), projectInfo.version(), null, null);
sbom.addRoot(root, readLicenseFromToml(tomlResult));

String cargoContent = Files.readString(manifest, StandardCharsets.UTF_8);
String cargoContent = Files.readString(manifestPath, StandardCharsets.UTF_8);
Set<String> ignoredDeps = getIgnoredDependencies(tomlResult, cargoContent);
addDependencies(sbom, root, ignoredDeps, tomlResult, analysisType);
return sbom;
Expand Down Expand Up @@ -744,7 +745,7 @@ private ProjectInfo parseCargoToml(TomlParseResult result) throws IOException {
boolean hasWorkspace = result.contains("workspace");
if (hasWorkspace) {
String workspaceVersion = result.getString(WORKSPACE_PACKAGE_VERSION);
String dirName = manifest.toAbsolutePath().getParent().getFileName().toString();
String dirName = manifestPath.toAbsolutePath().getParent().getFileName().toString();
if (debugLoggingIsNeeded()) {
log.info(
"Using workspace fallback: name="
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,27 +73,27 @@ public GoModulesProvider(Path manifest) {

@Override
public String readLicenseFromManifest() {
return LicenseUtils.readLicenseFile(manifest);
return LicenseUtils.readLicenseFile(manifestPath);
}

@Override
public Content provideStack() throws IOException {
// check for custom executable
Sbom sbom = getDependenciesSbom(manifest, true);
Sbom sbom = getDependenciesSbom(manifestPath, true);
return new Content(
sbom.getAsJsonString().getBytes(StandardCharsets.UTF_8), Api.CYCLONEDX_MEDIA_TYPE);
}

@Override
public Content provideComponent() throws IOException {
if (!Files.exists(manifest)) {
throw new IllegalArgumentException("Missing required go.mod file: " + manifest);
if (!Files.exists(manifestPath)) {
throw new IllegalArgumentException("Missing required go.mod file: " + manifestPath);
}
if (!Files.isRegularFile(manifest)) {
if (!Files.isRegularFile(manifestPath)) {
throw new IllegalArgumentException(
"The provided manifest is not a regular file: " + manifest);
"The provided manifest is not a regular file: " + manifestPath);
}
var sbom = getDependenciesSbom(manifest, false);
var sbom = getDependenciesSbom(manifestPath, false);
return new Content(
sbom.getAsJsonString().getBytes(StandardCharsets.UTF_8), Api.CYCLONEDX_MEDIA_TYPE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ public GradleProvider(Path manifest) {

@Override
public String readLicenseFromManifest() {
return LicenseUtils.readLicenseFile(manifest);
return LicenseUtils.readLicenseFile(manifestPath);
}

@Override
public Content provideStack() throws IOException {
Path tempFile = getDependencies(manifest);
Path tempFile = getDependencies(manifestPath);
try {
if (debugLoggingIsNeeded()) {
String stackAnalysisDependencyTree = Files.readString(tempFile);
Expand All @@ -84,10 +84,10 @@ public Content provideStack() throws IOException {
"Package Manager Gradle Stack Analysis Dependency Tree Output: %s %s",
System.lineSeparator(), stackAnalysisDependencyTree));
}
Map<String, String> propertiesMap = extractProperties(manifest);
Map<String, String> propertiesMap = extractProperties(manifestPath);

var sbom = buildSbomFromTextFormat(tempFile, propertiesMap, AnalysisType.STACK);
var ignored = getIgnoredDeps(manifest);
var ignored = getIgnoredDeps(manifestPath);

return new Content(
sbom.filterIgnoredDeps(ignored).getAsJsonString().getBytes(), Api.CYCLONEDX_MEDIA_TYPE);
Expand Down Expand Up @@ -605,12 +605,12 @@ private List<String> extractLines(Path inputFilePath, String startMarker) throws

@Override
public Content provideComponent() throws IOException {
Path tempFile = getDependencies(manifest);
Path tempFile = getDependencies(manifestPath);
try {
Map<String, String> propertiesMap = extractProperties(manifest);
Map<String, String> propertiesMap = extractProperties(manifestPath);

Sbom sbom = buildSbomFromTextFormat(tempFile, propertiesMap, AnalysisType.COMPONENT);
var ignored = getIgnoredDeps(manifest);
var ignored = getIgnoredDeps(manifestPath);

return new Content(
sbom.filterIgnoredDeps(ignored).getAsJsonString().getBytes(), Api.CYCLONEDX_MEDIA_TYPE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ public JavaMavenProvider(Path manifest) {

@Override
public String readLicenseFromManifest() {
String manifestLicense = readLicenseFromPom(manifest);
return LicenseUtils.getLicense(manifestLicense, manifest);
String manifestLicense = readLicenseFromPom(manifestPath);
return LicenseUtils.getLicense(manifestLicense, manifestPath);
}

/**
Expand Down Expand Up @@ -124,10 +124,11 @@ private String readLicenseFromPom(Path pomPath) {

@Override
public Content provideStack() throws IOException {
var mvnCleanCmd = buildMvnCommandArgs("clean", "-f", manifest.toString(), "--batch-mode", "-q");
var mvnCleanCmd =
buildMvnCommandArgs("clean", "-f", manifestPath.toString(), "--batch-mode", "-q");
var mvnEnvs = getMvnExecEnvs();
// execute the clean command
Operations.runProcess(manifest.getParent(), mvnCleanCmd.toArray(String[]::new), mvnEnvs);
Operations.runProcess(manifestPath.getParent(), mvnCleanCmd.toArray(String[]::new), mvnEnvs);
// create a temp file for storing the dependency tree in
var tmpFile = Files.createTempFile("TRUSTIFY_DA_dot_graph_", null);
// the tree command will build the project and create the dependency tree in the temp file
Expand All @@ -140,12 +141,12 @@ public Content provideStack() throws IOException {
"-DoutputType=text",
String.format("-DoutputFile=%s", tmpFile.toString()),
"-f",
manifest.toString(),
manifestPath.toString(),
"--batch-mode",
"-q"));
// if we have dependencies marked as ignored, exclude them from the tree command
var ignored =
getDependencies(manifest).stream()
getDependencies(manifestPath).stream()
.filter(d -> d.ignored)
.map(DependencyAggregator::toPurlWithoutVersion)
.map(PackageURL::getCoordinates)
Expand All @@ -157,7 +158,7 @@ public Content provideStack() throws IOException {

// execute the tree command
var mvnTreeCmd = buildMvnCommandArgs(mvnTreeCmdArgs.toArray(String[]::new));
Operations.runProcess(manifest.getParent(), mvnTreeCmd.toArray(String[]::new), mvnEnvs);
Operations.runProcess(manifestPath.getParent(), mvnTreeCmd.toArray(String[]::new), mvnEnvs);
if (debugLoggingIsNeeded()) {
String stackAnalysisDependencyTree = Files.readString(tmpFile);
log.info(
Expand Down Expand Up @@ -201,12 +202,12 @@ private Content generateSbomFromEffectivePom() throws IOException {
"help:effective-pom",
String.format("-Doutput=%s", tmpEffPom.toString()),
"-f",
manifest.toString(),
manifestPath.toString(),
"--batch-mode",
"-q");
// execute the effective pom command
Operations.runProcess(
manifest.getParent(), mvnEffPomCmd.toArray(String[]::new), getMvnExecEnvs());
manifestPath.getParent(), mvnEffPomCmd.toArray(String[]::new), getMvnExecEnvs());
if (debugLoggingIsNeeded()) {
String CaEffectivePoM = Files.readString(tmpEffPom);
log.info(
Expand All @@ -216,7 +217,7 @@ private Content generateSbomFromEffectivePom() throws IOException {
}
// if we have dependencies marked as ignored grab ignored dependencies from the original pom
// the effective-pom goal doesn't carry comments
List<DependencyAggregator> dependencies = getDependencies(manifest);
List<DependencyAggregator> dependencies = getDependencies(manifestPath);
var ignored =
dependencies.stream()
.filter(d -> d.ignored)
Expand Down Expand Up @@ -437,7 +438,7 @@ private String selectMvnRuntime(final Path manifestPath) {
if (mvnw != null) {
try {
// verify maven wrapper is accessible
Operations.runProcess(manifest.getParent(), mvnw, ARG_VERSION);
Operations.runProcess(manifestPath.getParent(), mvnw, ARG_VERSION);
if (debugLoggingIsNeeded()) {
log.info(String.format("using maven wrapper from : %s", mvnw));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
Expand Down Expand Up @@ -128,9 +127,7 @@ private void addDependenciesOf(Sbom sbom, PackageURL from, JsonNode node) {
if (dependencies == null) {
return;
}
Iterator<Entry<String, JsonNode>> fields = dependencies.fields();
while (fields.hasNext()) {
Entry<String, JsonNode> e = fields.next();
for (var e : dependencies.properties()) {
String name = e.getKey();
JsonNode versionNode = e.getValue().get("version");
if (versionNode == null) {
Expand Down Expand Up @@ -174,14 +171,13 @@ private void addDependenciesFromKey(Sbom sbom, JsonNode depTree, String key) {
if (deps == null) {
return;
}
deps.fields()
.forEachRemaining(
deps.properties()
.forEach(
e -> {
JsonNode versionNode = e.getValue().get("version");
if (versionNode == null || versionNode.isNull()) {
return; // skip entries without a resolved version
}
var target = toPurl(e.getKey(), versionNode.asText());
String version =
Comment thread
ruromero marked this conversation as resolved.
(versionNode != null && !versionNode.isNull()) ? versionNode.asText() : null;
var target = toPurl(e.getKey(), version);
sbom.addDependency(manifest.root, target, null);
addDependenciesOf(sbom, target, e.getValue());
});
Expand Down Expand Up @@ -216,16 +212,15 @@ private void addRootDependenciesFromKey(
if (node == null) {
return;
}
node.fields()
.forEachRemaining(
node.properties()
.forEach(
e -> {
String name = e.getKey();
JsonNode versionNode = e.getValue().get("version");
if (versionNode != null) {
String version = versionNode.asText();
PackageURL purl = toPurl(name, version);
direct.put(name, purl);
}
String version =
(versionNode != null && !versionNode.isNull()) ? versionNode.asText() : null;
PackageURL purl = toPurl(name, version);
direct.put(name, purl);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,16 @@ public void setPythonController(PythonControllerBase pythonController) {
@Override
public Content provideStack() throws IOException {
PythonControllerBase controller = getPythonController();
List<Map<String, Object>> dependencies = controller.getDependencies(manifest.toString(), true);
List<Map<String, Object>> dependencies =
controller.getDependencies(manifestPath.toString(), true);
printDependenciesTree(dependencies);
Sbom sbom = SbomFactory.newInstance(Sbom.BelongingCondition.PURL, "sensitive");
sbom.addRoot(
toPurl(getRootComponentName(), getRootComponentVersion()), readLicenseFromManifest());
for (Map<String, Object> component : dependencies) {
addAllDependencies(sbom.getRoot(), component, sbom);
}
String manifestContent = Files.readString(manifest);
String manifestContent = Files.readString(manifestPath);
handleIgnoredDependencies(manifestContent, sbom);
return new Content(
sbom.getAsJsonString().getBytes(StandardCharsets.UTF_8), Api.CYCLONEDX_MEDIA_TYPE);
Expand All @@ -74,7 +75,8 @@ public Content provideStack() throws IOException {
@Override
public Content provideComponent() throws IOException {
PythonControllerBase controller = getPythonController();
List<Map<String, Object>> dependencies = controller.getDependencies(manifest.toString(), false);
List<Map<String, Object>> dependencies =
controller.getDependencies(manifestPath.toString(), false);
printDependenciesTree(dependencies);
Sbom sbom = SbomFactory.newInstance();
sbom.addRoot(
Expand All @@ -85,7 +87,7 @@ public Content provideComponent() throws IOException {
sbom.getRoot(),
toPurl((String) component.get("name"), (String) component.get("version")),
null));
String manifestContent = Files.readString(manifest);
String manifestContent = Files.readString(manifestPath);
handleIgnoredDependencies(manifestContent, sbom);
return new Content(
sbom.getAsJsonString().getBytes(StandardCharsets.UTF_8), Api.CYCLONEDX_MEDIA_TYPE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected PythonProvider(Path manifest) {

@Override
public String readLicenseFromManifest() {
return LicenseUtils.readLicenseFile(manifest);
return LicenseUtils.readLicenseFile(manifestPath);
}

protected String getRootComponentName() {
Expand Down
Loading
Loading