Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple new report requirements #139

Merged
merged 16 commits into from
Aug 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package org.metaeffekt.core.inventory.processor.configuration;

import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -234,6 +236,32 @@ protected void loadDoubleProperty(Map<String, Object> properties, String key, Co
}
}

protected void loadJsonObjectProperty(Map<String, Object> properties, String key, Consumer<JSONObject> consumer) {
if (properties.containsKey(key)) {
final Object value = properties.get(key);
if (value instanceof Map<?, ?>) {
final Map<String, Object> valueMap = (Map<String, Object>) value;
final JSONObject jsonObject = new JSONObject(valueMap);
consumer.accept(jsonObject);
} else {
throw createPropertyException(key, value, "json-object");
}
}
}

protected void loadJsonArrayProperty(Map<String, Object> properties, String key, Consumer<JSONArray> consumer) {
if (properties.containsKey(key)) {
final Object value = properties.get(key);
if (value instanceof List<?>) {
final List<Object> valueList = (List<Object>) value;
final JSONArray jsonArray = new JSONArray(valueList);
consumer.accept(jsonArray);
} else {
throw createPropertyException(key, value, "json-array");
}
}
}

protected <T> void loadProperty(Map<String, Object> properties, String key, Function<Object, T> converter, Consumer<T> consumer) {
if (properties != null && properties.containsKey(key)) {
final Object value = properties.get(key);
Expand All @@ -256,6 +284,22 @@ protected <T> void loadListProperty(Map<String, Object> properties, String key,
}
}

protected <K, V> void loadMapProperty(Map<String, Object> properties, String key, Function<Object, K> keyConverter, Function<Object, V> valueConverter, Consumer<Map<K, V>> consumer) {
if (properties.containsKey(key)) {
final Object value = properties.get(key);
if (value instanceof Map<?, ?>) {
final Map<K, V> convertedMap = new LinkedHashMap<>();
final Map<?, ?> valueMap = (Map<?, ?>) value;
valueMap.forEach((k, v) -> {
convertedMap.put(keyConverter.apply(k), valueConverter.apply(v));
});
consumer.accept(convertedMap);
} else {
throw createPropertyException(key, value, "map");
}
}
}

protected <T> void loadSetProperty(Map<String, Object> properties, String key, Function<Object, T> converter, Consumer<Set<T>> consumer) {
if (properties.containsKey(key)) {
final Object value = properties.get(key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public enum Attribute implements AbstractModelBase.Attribute {
URL("Url"),
SUMMARY("Summary"),
SOURCE("Source"),
SOURCE_IMPLEMENTATION("Source-Implementation"),
TYPE("Type"),

DESCRIPTION("Description"),
Expand All @@ -66,7 +67,11 @@ public enum Attribute implements AbstractModelBase.Attribute {
ACKNOWLEDGEMENTS("Acknowledgements"),
KEYWORDS("Keywords"),
REFERENCES("References"),
@Deprecated
REFERENCED_IDS("Referenced Ids"),
REFERENCED_VULNERABILITIES("Referenced Vulnerabilities"),
REFERENCED_SECURITY_ADVISORIES("Referenced Advisories"),
REFERENCED_OTHER("Other Referenced Ids"),
DATA_SOURCE("Data Source"),
MATCHING_SOURCE("Matching Source"),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ public boolean isOrHasParent(ArtifactType artifactType) {
public static final ArtifactType PYTHON_MODULE = new ArtifactType("python-module", "python module");

// FIXME: introduce further specific web module types
/**
* @deprecated use {@link #WEB_MODULE} instead
*/
@Deprecated
public static final ArtifactType NODEJS_MODULE = new ArtifactType("nodejs-module", "nodejs module");
public static final ArtifactType WEB_MODULE = new ArtifactType("web-module", "web module", NODEJS_MODULE);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1995,7 +1995,7 @@ public String getInventorySizePrintString() {
inventoryPrintString.add(String.format("ld: %d", licenseData.size()));

final StringJoiner vulnerabilityMetaDataPrintString = new StringJoiner(", ", "vmd: [", "]");
vulnerabilityMetaDataPrintString.setEmptyValue("vmd: 0 ");
vulnerabilityMetaDataPrintString.setEmptyValue("vmd: []");
for (String context : vulnerabilityMetaData.keySet()) {
vulnerabilityMetaDataPrintString.add(String.format("%s: %d", context, vulnerabilityMetaData.get(context).size()));
}
Expand All @@ -2009,6 +2009,45 @@ public String getInventorySizePrintString() {
return inventoryPrintString.toString();
}

public static List<String> mapAttributesToHorizontalTable(List<Map<String, String>> maps) {
if (maps == null || maps.isEmpty()) {
return new ArrayList<>();
}

// Collecting all unique attribute names and determining max width for each column
final Map<String, Integer> attributeWidths = new LinkedHashMap<>();
for (Map<String, String> map : maps) {
for (Map.Entry<String, String> entry : map.entrySet()) {
final int maxAttributeLength = Math.max(entry.getKey().length(),
entry.getValue() != null ? entry.getValue().replace("\n", "<br>").length() : 0);
attributeWidths.put(entry.getKey(), Math.max(attributeWidths.getOrDefault(entry.getKey(), 0), maxAttributeLength));
}
}

final Map<String, Integer> rearrangedAttributeWidths = logModelRearrangeAttributes(attributeWidths);
// Header and separator
final String header = rearrangedAttributeWidths.entrySet().stream()
.map(entry -> StringUtils.rightPad(entry.getKey(), entry.getValue()))
.collect(Collectors.joining(" | ", "| ", " |"));
final String separator = rearrangedAttributeWidths.values().stream()
.map(integer -> StringUtils.repeat("-", integer + 2))
.collect(Collectors.joining("|", "|", "|"));

final List<String> table = new ArrayList<>();
table.add(header);
table.add(separator);

// Logging each map's attributes
for (Map<String, String> map : maps) {
String row = rearrangedAttributeWidths.keySet().stream()
.map(key -> StringUtils.rightPad(map.get(key) != null ? map.get(key).replace("\n", "<br>") : "", rearrangedAttributeWidths.get(key)))
.collect(Collectors.joining(" | ", "| ", " |"));
table.add(row);
}

return table;
}

/**
* Component data. Please note that the component per-se does not have a version, but a license.
* Artifacts (with various versions) can be added to the component. When evaluating the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ public enum Attribute implements AbstractModelBase.Attribute {
STATUS("Status"),
RATIONALE("Rationale"),
RISK("Risk"),
SOURCE("Source"),
SOURCE_IMPLEMENTATION("Source-Implementation"),
REFERENCED_VULNERABILITIES("Referenced Vulnerabilities"),
REFERENCED_SECURITY_ADVISORIES("Referenced Advisories"),
REFERENCED_OTHER("Other Referenced Ids"),
REFERENCES("References");

private String key;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public static ComponentPatternContributorRunnerBuilder builder() {
* @param baseDir as in {@link ComponentPatternContributor}
* @param relativeAnchorFilePath as in {@link ComponentPatternContributor}
* @param checksum as in {@link ComponentPatternContributor}
*
* @param virtualRootPath as in {@link ComponentPatternContributor}
* @return returns a list of generated component patterns
*/
public List<ComponentPatternData> run(File baseDir, String virtualRootPath, String relativeAnchorFilePath, String checksum) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,7 @@ public List<ComponentPatternData> contributeStatusFileBased(File baseDir,
* @param baseDir same as {@link ComponentPatternContributor#contribute(File, String, String, String)}
* @param relativeAnchorFilePath same as {@link ComponentPatternContributor#contribute(File, String, String, String)}
* @param checksum same as {@link ComponentPatternContributor#contribute(File, String, String, String)}
* @param virtualRootPath same as {@link ComponentPatternContributor#contribute(File, String, String, String)}
* @return same as {@link ComponentPatternContributor#contribute(File, String, String, String)}
*/
public List<ComponentPatternData> contributeStatusDirectoryBased(File baseDir,
Expand Down
Loading
Loading