All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.metaeffekt.artifact.analysis.vulnerability.enrichment.vulnerabilitystatus.validation.VulnerabilityStatusValidationEntry Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2021-2024 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.metaeffekt.artifact.analysis.vulnerability.enrichment.vulnerabilitystatus.validation;

import com.metaeffekt.artifact.analysis.utils.WildcardUtilities;
import com.metaeffekt.artifact.analysis.version.Version;
import com.metaeffekt.artifact.analysis.version.curation.VersionContext;
import org.metaeffekt.core.inventory.processor.model.AbstractModelBase;
import org.metaeffekt.core.inventory.processor.model.Artifact;

import java.util.*;
import java.util.regex.Pattern;

public class VulnerabilityStatusValidationEntry {

    private final String field;
    private final CompareFunction function;
    private final String value;

    public VulnerabilityStatusValidationEntry(String field, CompareFunction function, String value) {
        this.field = field;
        this.function = function;
        this.value = value;
    }

    public boolean validate(AbstractModelBase model) {
        return validate(model.get(field), model);
    }

    /**
     * Validates a field value based on a specified comparison function.
* The model is used as {@link VersionContext} when comparing versions. Optional. * * @param fieldValue The field value to be validated. * @param model The model used for comparison if necessary. * @return True if the field value is valid according to the specified comparison function, false otherwise. * @throws IllegalStateException If the specified comparison function is unknown. */ public boolean validate(String fieldValue, AbstractModelBase model) { if (fieldValue == null) { return false; } switch (function) { case EQUALS: return value.equals(fieldValue); case MATCHES: return matches(fieldValue, value); case VERSION_SMALLER: return compareVersion(fieldValue, value, model) < 0; case VERSION_LARGER: return compareVersion(fieldValue, value, model) > 0; case VERSION_LARGER_OR_EQUAL: return compareVersion(fieldValue, value, model) >= 0; case VERSION_SMALLER_OR_EQUAL: return compareVersion(fieldValue, value, model) <= 0; case NUMERIC_SMALLER: return compareNumeric(fieldValue, value) < 0; case NUMERIC_LARGER: return compareNumeric(fieldValue, value) > 0; case NUMERIC_LARGER_OR_EQUAL: return compareNumeric(fieldValue, value) >= 0; case NUMERIC_SMALLER_OR_EQUAL: return compareNumeric(fieldValue, value) <= 0; case CSV_CONTAINS: return containsAllCsvValues(fieldValue, value); case CSV_NOT_CONTAINS: return !containsAllCsvValues(fieldValue, value); default: throw new IllegalStateException("Unknown compare function: " + function); } } private boolean matches(String fieldValue, String compareValue) { final Pattern pattern = WildcardUtilities.convertWildcardStringToPattern(compareValue); return pattern.matcher(fieldValue).matches(); } private int compareNumeric(String fieldValue, String compareValue) { if (fieldValue == null || compareValue == null) { return 0; } try { return Double.compare(Double.parseDouble(fieldValue), Double.parseDouble(compareValue)); } catch (NumberFormatException e) { return 0; } } private int compareVersion(String fieldValue, String compareValue, AbstractModelBase model) { final Artifact artifact = model instanceof Artifact ? (Artifact) model : null; final Version fieldVersion = Version.of(fieldValue, VersionContext.fromArtifact(artifact)); final Version compareVersion = Version.of(compareValue, VersionContext.fromArtifact(artifact)); return fieldVersion.after(compareVersion) ? 1 : fieldVersion.before(compareVersion) ? -1 : 0; } private boolean containsAllCsvValues(String fieldValue, String compareValue) { final String[] fieldValues = fieldValue.split(", "); final String[] compareValues = compareValue.split(", "); for (String comp : compareValues) { boolean found = false; for (String field : fieldValues) { if (comp.equals(field)) { found = true; break; } } if (!found) { return false; } } return true; } public static List fromYamlList(List yamlData) { List result = new ArrayList<>(); for (Object entry : yamlData) { result.addAll(fromYamlMap((Map) entry)); } return result; } public static List fromYamlMap(Map yamlData) { if (yamlData.containsKey("field") && yamlData.containsKey("function") && yamlData.containsKey("value")) { final String field = (String) yamlData.get("field"); final String function = (String) yamlData.get("function"); final String value = String.valueOf(yamlData.get("value")); return Arrays.asList(new VulnerabilityStatusValidationEntry(field, CompareFunction.fromLabel(function), value)); } else { final List result = new ArrayList<>(); for (Map.Entry entry : yamlData.entrySet()) { final String field = entry.getKey(); final String function = "matches"; final String value = String.valueOf(entry.getValue()); result.add(new VulnerabilityStatusValidationEntry(field, CompareFunction.fromLabel(function), value)); } return result; } } public enum CompareFunction { EQUALS("equals"), MATCHES("matches"), VERSION_SMALLER("version <"), VERSION_LARGER("version >"), VERSION_SMALLER_OR_EQUAL("version <="), VERSION_LARGER_OR_EQUAL("version >="), NUMERIC_SMALLER("numeric <"), NUMERIC_LARGER("numeric >"), NUMERIC_SMALLER_OR_EQUAL("numeric <="), NUMERIC_LARGER_OR_EQUAL("numeric >="), CSV_CONTAINS("csv contains"), CSV_NOT_CONTAINS("csv not contains"); public final String label; CompareFunction(String label) { this.label = label; } public static CompareFunction fromLabel(String label) { for (CompareFunction compareFunction : values()) { if (compareFunction.label.equals(label)) { return compareFunction; } } throw new IllegalArgumentException("Unknown compare function: " + label); } } @Override public String toString() { return "{" + field + " : " + function.label + " : " + value + "}"; } public Map toYamlMap() { final Map result = new LinkedHashMap<>(); result.put("field", field); result.put("function", function.label); result.put("value", value); return result; } }