Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion src/main/java/com/redhat/exhort/Api.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
/** The Api interface is used for contracting API implementations. * */
public interface Api {

public static final String CYCLONEDX_MEDIA_TYPE = "application/vnd.cyclonedx+json";
String CYCLONEDX_MEDIA_TYPE = "application/vnd.cyclonedx+json";

enum MediaType {
APPLICATION_JSON,
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/redhat/exhort/image/Image.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public Image(String fullName) {

/**
* Create an image name with a tag. If a tag is provided (i.e. is not null) then this tag is used.
* Otherwise the tag of the provided name is used (if any).
* Otherwise, the tag of the provided name is used (if any).
*
* @param fullName The fullname of the image in Docker format. I
* @param givenTag tag to use. Can be null in which case the tag specified in fullName is used.
Expand Down Expand Up @@ -190,7 +190,7 @@ public String toString() {
}

public boolean hasRegistry() {
return registry != null && registry.length() > 0;
return registry != null && !registry.isEmpty();
}

private String joinTail(String[] parts) {
Expand Down Expand Up @@ -329,7 +329,7 @@ private void doValidate() {
checks[i], value, checkPattern.pattern()));
}
}
if (errors.size() > 0) {
if (!errors.isEmpty()) {
StringBuilder buf = new StringBuilder();
buf.append(String.format("Given Docker name '%s' is invalid:\n", getFullName()));
for (String error : errors) {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/redhat/exhort/image/ImageRef.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class ImageRef {
public static final String OS_QUALIFIER = "os";
public static final String VARIANT_QUALIFIER = "variant";

private Image image;
private final Image image;
private Platform platform;

public ImageRef(String image, String platform) {
Expand All @@ -48,8 +48,8 @@ public ImageRef(String image, String platform) {
}

public ImageRef(PackageURL packageURL) {
String name = null;
String version = null;
String name;
String version;
String tag = null;
String repositoryRrl = null;
String arch = null;
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/redhat/exhort/impl/ExhortApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ private void commonHookAfterProviderCreatedSbomAndBeforeExhort() {
"Time took to create sbom file to be sent to exhort backend, in ms : %s, in seconds:"
+ " %s",
this.startTime.until(this.providerEndTime, ChronoUnit.MILLIS),
(float) (this.startTime.until(this.providerEndTime, ChronoUnit.MILLIS) / 1000F)));
this.startTime.until(this.providerEndTime, ChronoUnit.MILLIS) / 1000F));
}
}

Expand Down Expand Up @@ -673,13 +673,13 @@ private HttpRequest buildRequest(
// set rhda-token
// Environment variable/property name = RHDA_TOKEN
String rhdaToken = calculateHeaderValue(RHDA_TOKEN_HEADER);
if (rhdaToken != null && Optional.of(rhdaToken).isPresent()) {
if (rhdaToken != null) {
request.setHeader(RHDA_TOKEN_HEADER, rhdaToken);
}
// set rhda-source ( extension/plugin id/name)
// Environment variable/property name = RHDA_SOURCE
String rhdaSource = calculateHeaderValue(RHDA_SOURCE_HEADER);
if (rhdaSource != null && Optional.of(rhdaSource).isPresent()) {
if (rhdaSource != null) {
request.setHeader(RHDA_SOURCE_HEADER, rhdaSource);
}
request.setHeader(RHDA_OPERATION_TYPE_HEADER, analysisType);
Expand Down
8 changes: 3 additions & 5 deletions src/main/java/com/redhat/exhort/impl/RequestManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
public class RequestManager {

private static RequestManager requestManager;
private Map<String, String> requests;
private final Map<String, String> requests;

public static RequestManager getInstance() {
if (Objects.isNull(requestManager)) {
Expand All @@ -47,11 +47,9 @@ public synchronized void removeClientTraceIdFromRequest() {
Optional<String> keyOfParent =
requests.entrySet().stream()
.filter(pair -> pair.getValue().equals(removedClientTraceId))
.map(pair -> pair.getKey())
.map(Map.Entry::getKey)
.findFirst();
if (keyOfParent.isPresent()) {
requests.remove(keyOfParent.get());
}
keyOfParent.ifPresent(requests::remove);
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/main/java/com/redhat/exhort/providers/BaseJavaProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void parseDependencyTree(String src, int srcDepth, String[] lines, Sbom sbom, St
if (lines.length == 0) {
return;
}
if (lines.length == 1 && lines[0].trim().equals("")) {
if (lines.length == 1 && lines[0].trim().isEmpty()) {
return;
}
int index = 0;
Expand Down Expand Up @@ -90,7 +90,7 @@ PackageURL parseDep(String dep) {
dependency = dependency.replace(":runtime", ":compile").replace(":provided", ":compile");
int endIndex = Math.max(dependency.indexOf(":compile"), dependency.indexOf(":test"));
int scopeLength;
if (dependency.indexOf(":compile") > -1) {
if (dependency.contains(":compile")) {
scopeLength = ":compile".length();
} else {
scopeLength = ":test".length();
Expand Down Expand Up @@ -140,7 +140,7 @@ else if (parts.length == 6) {
}

int getDepth(String line) {
if (line == null || line.trim().equals("")) {
if (line == null || line.trim().isEmpty()) {
return -1;
}

Expand All @@ -165,7 +165,7 @@ static final class DependencyAggregator {
/**
* Get the string representation of the dependency to use as excludes
*
* @return an exclude string for the dependency:tree plugin, ie. group-id:artifact-id:*:version
* @return an exclude string for the dependency:tree plugin, i.e. group-id:artifact-id:*:version
*/
@Override
public String toString() {
Expand All @@ -188,7 +188,7 @@ PackageURL toPurl() {
groupId,
artifactId,
version,
this.scope == "*" ? null : new TreeMap<>(Map.of("scope", this.scope)),
this.scope.equals("*") ? null : new TreeMap<>(Map.of("scope", this.scope)),
null);
} catch (MalformedPackageURLException e) {
throw new IllegalArgumentException("Unable to parse PackageURL", e);
Expand Down
102 changes: 50 additions & 52 deletions src/main/java/com/redhat/exhort/providers/GoModulesProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

import static com.redhat.exhort.impl.ExhortApi.debugLoggingIsNeeded;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.github.packageurl.MalformedPackageURLException;
import com.github.packageurl.PackageURL;
import com.redhat.exhort.Api;
Expand All @@ -36,7 +34,13 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.TreeMap;
import java.util.logging.Logger;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -152,47 +156,45 @@ private void performManifestVersionsCheck(String[] goModGraphLines, Path manifes
List<String> comparisonLines =
Arrays.stream(goModGraphLines)
.filter((line) -> line.startsWith(root))
.map((line) -> getChildVertex(line))
.map(GoModulesProvider::getChildVertex)
.collect(Collectors.toList());
List<String> goModDependencies = collectAllDepsFromManifest(lines, goModLines);
comparisonLines.stream()
.forEach(
(dependency) -> {
String[] parts = dependency.split("@");
String version = parts[1];
String depName = parts[0];
goModDependencies.stream()
.forEach(
(dep) -> {
String[] artifactParts = dep.trim().split(" ");
String currentDepName = artifactParts[0];
String currentVersion = artifactParts[1];
if (currentDepName.trim().equals(depName.trim())) {
if (!currentVersion.trim().equals(version.trim())) {
throw new RuntimeException(
String.format(
"Can't continue with analysis - versions mismatch for"
+ " dependency name=%s, manifest version=%s, installed"
+ " Version=%s, if you want to allow version mismatch for"
+ " analysis between installed and requested packages,"
+ " set environment variable/setting -"
+ " %s=false",
depName,
currentVersion,
version,
Provider.PROP_MATCH_MANIFEST_VERSIONS));
}
}
});
});
comparisonLines.forEach(
(dependency) -> {
String[] parts = dependency.split("@");
String version = parts[1];
String depName = parts[0];
goModDependencies.forEach(
(dep) -> {
String[] artifactParts = dep.trim().split(" ");
String currentDepName = artifactParts[0];
String currentVersion = artifactParts[1];
if (currentDepName.trim().equals(depName.trim())) {
if (!currentVersion.trim().equals(version.trim())) {
throw new RuntimeException(
String.format(
"Can't continue with analysis - versions mismatch for"
+ " dependency name=%s, manifest version=%s, installed"
+ " Version=%s, if you want to allow version mismatch for"
+ " analysis between installed and requested packages,"
+ " set environment variable/setting -"
+ " %s=false",
depName,
currentVersion,
version,
Provider.PROP_MATCH_MANIFEST_VERSIONS));
}
}
});
});
} catch (IOException e) {
throw new RuntimeException(
"Failed to open go.mod file for manifest versions check validation!");
}
}

private List<String> collectAllDepsFromManifest(String[] lines, String goModLines) {
List<String> result = new ArrayList<>();
List<String> result;
Comment thread
soul2zimate marked this conversation as resolved.
Outdated
// collect all deps that starts with require keyword
result =
Arrays.stream(lines)
Expand Down Expand Up @@ -245,15 +247,15 @@ public void determineMainModuleVersion(Path directory) {
VersionControlSystem vcs = new GitVersionControlSystemImpl();
if (vcs.isDirectoryRepo(directory)) {
TagInfo latestTagInfo = vcs.getLatestTag(directory);
if (!latestTagInfo.getTagName().trim().equals("")) {
if (!latestTagInfo.getTagName().trim().isEmpty()) {
if (!latestTagInfo.isCurrentCommitPointedByTag()) {
String nextTagVersion = vcs.getNextTagVersion(latestTagInfo);
this.mainModuleVersion = vcs.getPseudoVersion(latestTagInfo, nextTagVersion);
} else {
this.mainModuleVersion = latestTagInfo.getTagName();
}
} else {
if (!latestTagInfo.getCurrentCommitDigest().trim().equals("")) {
if (!latestTagInfo.getCurrentCommitDigest().trim().isEmpty()) {
this.mainModuleVersion =
vcs.getPseudoVersion(latestTagInfo, getDefaultMainModuleVersion());
}
Expand All @@ -262,7 +264,7 @@ public void determineMainModuleVersion(Path directory) {
}

private Sbom buildSbomFromGraph(
String goModulesResult, List<PackageURL> ignoredDeps, Path manifestPath) throws IOException {
String goModulesResult, List<PackageURL> ignoredDeps, Path manifestPath) {
// Each entry contains a key of the module, and the list represents the module direct
// dependencies , so
// pairing of the key with each of the dependencies in a list is basically an edge in the graph.
Expand All @@ -272,7 +274,7 @@ private Sbom buildSbomFromGraph(
// value of list of that module' dependencies.
List<String> linesList = Arrays.asList(goModulesResult.split(System.lineSeparator()));

Integer startingIndex = 0;
int startingIndex = 0;
for (String line : linesList) {
if (!edges.containsKey(getParentVertex(line))) {
// Collect all direct dependencies of the current module into a list.
Expand All @@ -298,8 +300,7 @@ private Sbom buildSbomFromGraph(
PackageURL source = toPurl(key, "@", this.goEnvironmentVariableForPurl);
value.forEach(
dep -> {
PackageURL targetPurl =
toPurl((String) dep, "@", this.goEnvironmentVariableForPurl);
PackageURL targetPurl = toPurl(dep, "@", this.goEnvironmentVariableForPurl);
sbom.addDependency(source, targetPurl, null);
});
});
Expand Down Expand Up @@ -348,12 +349,11 @@ private Map<String, List<String>> getFinalPackagesVersionsForModule(

private List<String> getListOfPackagesWithFinalVersions(
Map<String, String> finalModulesVersions, Map.Entry<String, List<String>> entry) {
return (List<String>)
entry.getValue().stream()
.map(
(packageWithVersion) ->
getPackageWithFinalVersion(finalModulesVersions, (String) packageWithVersion))
.collect(Collectors.toList());
return entry.getValue().stream()
.map(
(packageWithVersion) ->
getPackageWithFinalVersion(finalModulesVersions, packageWithVersion))
.collect(Collectors.toList());
}

public static String getPackageWithFinalVersion(
Expand Down Expand Up @@ -387,11 +387,10 @@ private TreeMap<String, String> getQualifiers(boolean includeOsAndArch) {
getEnvironmentVariable(goEnvironmentVariables, GO_HOST_ARCHITECTURE_ENV_NAME);
String hostOS =
getEnvironmentVariable(goEnvironmentVariables, GO_HOST_OPERATION_SYSTEM_ENV_NAME);
return new TreeMap<String, String>(
Map.of("type", "module", "goos", hostOS, "goarch", hostArch));
return new TreeMap<>(Map.of("type", "module", "goos", hostOS, "goarch", hostArch));
}

return new TreeMap<String, String>(Map.of("type", "module"));
return new TreeMap<>(Map.of("type", "module"));
}

private static String getEnvironmentVariable(String goEnvironmentVariables, String envName) {
Expand All @@ -403,8 +402,7 @@ private static String getEnvironmentVariable(String goEnvironmentVariables, Stri
return envValue.replaceAll("\"", "");
}

private String buildGoModulesDependencies(Path manifestPath)
throws JsonMappingException, JsonProcessingException {
private String buildGoModulesDependencies(Path manifestPath) {
String[] goModulesDeps;
goModulesDeps = new String[] {goExecutable, "mod", "graph"};

Expand Down
5 changes: 2 additions & 3 deletions src/main/java/com/redhat/exhort/providers/GradleProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ private Path getDependencies(Path manifestPath) throws IOException {
return tempFile;
}

protected Path getProperties(Path manifestPath) throws IOException {
private Path getProperties(Path manifestPath) throws IOException {
Path propsTempFile = Files.createTempFile("propsfile", ".txt");
String propCmd = gradleExecutable + " properties";
String[] propCmdList = propCmd.split("\\s+");
Expand Down Expand Up @@ -504,8 +504,7 @@ private String getRoot(Path textFormatFile, Map<String, String> propertiesMap)
String group = propertiesMap.get("group");
String version = propertiesMap.get("version");
String rootName = extractRootProjectValue(textFormatFile);
String root = group + ':' + rootName + ':' + "jar" + ':' + version;
return root;
return group + ':' + rootName + ':' + "jar" + ':' + version;
}

private String extractRootProjectValue(Path inputFilePath) throws IOException {
Expand Down
10 changes: 2 additions & 8 deletions src/main/java/com/redhat/exhort/providers/JavaMavenProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,7 @@ private Content generateSbomFromEffectivePom() throws IOException {
deps.stream()
.filter(dep -> !testsDeps.contains(dep))
.map(DependencyAggregator::toPurl)
.filter(
dep ->
ignored.stream()
.filter(artifact -> artifact.isCoordinatesEquals(dep))
.collect(Collectors.toList())
.size()
== 0)
.filter(dep -> ignored.stream().noneMatch(artifact -> artifact.isCoordinatesEquals(dep)))
.forEach(d -> sbom.addDependency(sbom.getRoot(), d, null));

// build and return content for constructing request to the backend
Expand Down Expand Up @@ -206,7 +200,7 @@ private PackageURL getRoot(final Path manifestPath) throws IOException {
break;
}
}
if (isRoot && dependencyAggregator.isValid()) {
if (dependencyAggregator.isValid()) {
return dependencyAggregator.toPurl();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public JavaScriptNpmProvider(Path manifest) {
}

@Override
protected final String lockFileName() {
protected String lockFileName() {
return LOCK_FILE;
}

Expand Down
Loading
Loading