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

com.metaeffekt.artifact.enrichment.configurations.VulnerabilityStatusEnrichmentConfiguration 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.enrichment.configurations;

import com.metaeffekt.artifact.analysis.vulnerability.enrichment.vulnerabilitystatus.VulnerabilityStatus;
import com.metaeffekt.artifact.analysis.vulnerability.enrichment.vulnerabilitystatus.VulnerabilityStatusConverter;
import lombok.Getter;
import org.json.JSONObject;
import org.metaeffekt.core.inventory.processor.configuration.ProcessConfiguration;
import org.metaeffekt.core.inventory.processor.configuration.ProcessMisconfiguration;
import org.metaeffekt.core.inventory.processor.report.configuration.CentralSecurityPolicyConfiguration;

import java.io.File;
import java.util.*;
import java.util.stream.Collectors;

public class VulnerabilityStatusEnrichmentConfiguration extends ProcessConfiguration {

    @Getter
    private final List statusFiles = new ArrayList<>();
    @Getter
    private final List additionalStatus = new ArrayList<>();
    @Getter
    private String[] activeLabels = new String[]{};
    @Getter
    private boolean failOnValidationErrors = false;
    @Getter
    private boolean failOnAmbiguousMatchingInformation = false;

    @Getter
    private boolean debugMatchingCriteria = false;

    public VulnerabilityStatusEnrichmentConfiguration setStatusFiles(List statusFiles) {
        this.statusFiles.clear();
        this.statusFiles.addAll(statusFiles);
        return this;
    }

    public VulnerabilityStatusEnrichmentConfiguration addStatusFile(File statusFile) {
        this.statusFiles.add(statusFile);
        return this;
    }

    public VulnerabilityStatusEnrichmentConfiguration addStatus(VulnerabilityStatus status) {
        this.additionalStatus.add(status);
        return this;
    }

    public VulnerabilityStatusEnrichmentConfiguration addStatuses(Collection statuses) {
        this.additionalStatus.addAll(statuses);
        return this;
    }

    public VulnerabilityStatusEnrichmentConfiguration removeStatus(VulnerabilityStatus status) {
        this.additionalStatus.remove(status);
        return this;
    }

    public VulnerabilityStatusEnrichmentConfiguration setActiveLabels(String[] activeLabels) {
        this.activeLabels = activeLabels;
        return this;
    }

    public VulnerabilityStatusEnrichmentConfiguration setActiveLabels(Collection activeLabels) {
        this.activeLabels = activeLabels.toArray(new String[0]);
        return this;
    }

    public VulnerabilityStatusEnrichmentConfiguration setFailOnValidationErrors(boolean failOnValidationErrors) {
        this.failOnValidationErrors = failOnValidationErrors;
        return this;
    }

    public VulnerabilityStatusEnrichmentConfiguration setFailOnAmbiguousMatchingInformation(boolean failOnAmbiguousMatchingInformation) {
        this.failOnAmbiguousMatchingInformation = failOnAmbiguousMatchingInformation;
        return this;
    }

    public VulnerabilityStatusEnrichmentConfiguration setDebugMatchingCriteria(boolean debugMatchingCriteria) {
        this.debugMatchingCriteria = debugMatchingCriteria;
        return this;
    }

    public Set readVulnerabilityStatusEntries(CentralSecurityPolicyConfiguration.JsonSchemaValidationErrorsHandling jsonSchemaValidationErrorsHandling) {
        final Set anyScopeStatus = new LinkedHashSet<>();

        for (File statusFile : statusFiles) {
            anyScopeStatus.addAll(VulnerabilityStatusConverter.fromStatusFileOrDirectory(statusFile, jsonSchemaValidationErrorsHandling));
        }
        anyScopeStatus.addAll(additionalStatus);

        return anyScopeStatus;
    }

    @Override
    public LinkedHashMap getProperties() {
        final LinkedHashMap properties = new LinkedHashMap<>();

        properties.put("statusFiles", statusFiles);
        properties.put("additionalStatus", additionalStatus.stream()
                .filter(Objects::nonNull)
                .map(VulnerabilityStatus::toJson)
                .collect(Collectors.toList()));
        properties.put("activeLabels", Arrays.asList(activeLabels));
        properties.put("failOnValidationErrors", failOnValidationErrors);
        properties.put("failOnAmbiguousMatchingInformation", failOnAmbiguousMatchingInformation);
        properties.put("debugMatchingCriteria", debugMatchingCriteria);

        return properties;
    }

    @Override
    public void setProperties(LinkedHashMap properties) {
        super.loadListProperty(properties, "statusFiles", obj -> new File(String.valueOf(obj)), this::setStatusFiles);
        super.loadListProperty(properties, "additionalStatus", obj -> VulnerabilityStatusConverter.fromJson(new JSONObject(String.valueOf(obj))), list -> list.forEach(this::addStatus));
        super.loadListProperty(properties, "activeLabels", String::valueOf, this::setActiveLabels);
        super.loadBooleanProperty(properties, "failOnValidationErrors", this::setFailOnValidationErrors);
        super.loadBooleanProperty(properties, "failOnAmbiguousMatchingInformation", this::setFailOnAmbiguousMatchingInformation);
        super.loadBooleanProperty(properties, "debugMatchingCriteria", this::setDebugMatchingCriteria);
    }

    @Override
    protected void collectMisconfigurations(List misconfigurations) {
        if (additionalStatus.stream().anyMatch(Objects::isNull)) {
            misconfigurations.add(new ProcessMisconfiguration("additionalStatus", "contains null values"));
        }

        if (statusFiles.isEmpty() && additionalStatus.isEmpty()) {
            misconfigurations.add(new ProcessMisconfiguration("statusFiles", "Must not be empty, set enricher to inactive to avoid exception"));
        }
        if (statusFiles.stream().anyMatch(Objects::isNull)) {
            misconfigurations.add(new ProcessMisconfiguration("statusFiles", "Must not contain null values"));
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy