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

com.metaeffekt.artifact.enrichment.configurations.InventoryValidationEnrichmentConfiguration 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.enrichment.validation.InventoryValidator;
import com.metaeffekt.artifact.enrichment.validation.validators.AdditionalCpeIsNotEffectiveInventoryValidator;
import com.metaeffekt.artifact.enrichment.validation.validators.ArtifactAndCpeVersionsDifferGreatlyInventoryValidator;
import com.metaeffekt.artifact.enrichment.validation.validators.MultipleArtifactsAndVersionsOnVulnerabilityInventoryValidator;
import com.metaeffekt.artifact.enrichment.validation.validators.VulnerabilityInvalidNameValidator;
import lombok.Getter;
import lombok.Setter;
import org.metaeffekt.core.inventory.processor.configuration.ProcessConfiguration;
import org.metaeffekt.core.inventory.processor.configuration.ProcessMisconfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class InventoryValidationEnrichmentConfiguration extends ProcessConfiguration {

    private final static Logger LOG = LoggerFactory.getLogger(InventoryValidationEnrichmentConfiguration.class);

    @Getter
    private boolean failOnValidationErrors = false;
    @Getter
    @Setter
    private boolean addAsCorrelationWarnings = true;

    @Getter
    private AdditionalCpeIsNotEffectiveInventoryValidator additionalCpeIsNotEffectiveInventoryValidator;
    @Getter
    private MultipleArtifactsAndVersionsOnVulnerabilityInventoryValidator multipleArtifactsAndVersionsOnVulnerabilityInventoryValidator;
    @Getter
    private ArtifactAndCpeVersionsDifferGreatlyInventoryValidator artifactAndCpeVersionsDifferGreatlyInventoryValidator;
    @Getter
    private VulnerabilityInvalidNameValidator vulnerabilityInvalidNameValidator;

    private final List additionalValidators = new ArrayList<>();

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

    public InventoryValidationEnrichmentConfiguration setAdditionalCpeIsNotEffectiveInventoryValidator(AdditionalCpeIsNotEffectiveInventoryValidator additionalCpeIsNotEffectiveInventoryValidator) {
        this.additionalCpeIsNotEffectiveInventoryValidator = additionalCpeIsNotEffectiveInventoryValidator;
        return this;
    }

    public InventoryValidationEnrichmentConfiguration activateAdditionalCpeIsNotEffectiveInventoryValidator() {
        this.additionalCpeIsNotEffectiveInventoryValidator = new AdditionalCpeIsNotEffectiveInventoryValidator();
        return this;
    }

    public InventoryValidationEnrichmentConfiguration setMultipleArtifactsAndVersionsOnVulnerabilityInventoryValidator(MultipleArtifactsAndVersionsOnVulnerabilityInventoryValidator multipleArtifactsAndVersionsOnVulnerabilityInventoryValidator) {
        this.multipleArtifactsAndVersionsOnVulnerabilityInventoryValidator = multipleArtifactsAndVersionsOnVulnerabilityInventoryValidator;
        return this;
    }

    public InventoryValidationEnrichmentConfiguration activateMultipleArtifactsAndVersionsOnVulnerabilityInventoryValidator() {
        this.multipleArtifactsAndVersionsOnVulnerabilityInventoryValidator = new MultipleArtifactsAndVersionsOnVulnerabilityInventoryValidator();
        return this;
    }

    public InventoryValidationEnrichmentConfiguration setArtifactAndCpeVersionsDifferGreatlyInventoryValidator(ArtifactAndCpeVersionsDifferGreatlyInventoryValidator artifactAndCpeVersionsDifferGreatlyInventoryValidator) {
        this.artifactAndCpeVersionsDifferGreatlyInventoryValidator = artifactAndCpeVersionsDifferGreatlyInventoryValidator;
        return this;
    }

    public InventoryValidationEnrichmentConfiguration activateArtifactAndCpeVersionsDifferGreatlyInventoryValidator() {
        this.artifactAndCpeVersionsDifferGreatlyInventoryValidator = new ArtifactAndCpeVersionsDifferGreatlyInventoryValidator();
        return this;
    }

    public InventoryValidationEnrichmentConfiguration setVulnerabilityInvalidNameValidator(VulnerabilityInvalidNameValidator vulnerabilityInvalidNameValidator) {
        this.vulnerabilityInvalidNameValidator = vulnerabilityInvalidNameValidator;
        return this;
    }

    public InventoryValidationEnrichmentConfiguration activateVulnerabilityInvalidNameValidator() {
        this.vulnerabilityInvalidNameValidator = new VulnerabilityInvalidNameValidator();
        return this;
    }

    public InventoryValidationEnrichmentConfiguration addAdditionalValidator(InventoryValidator validator) {
        additionalValidators.add(validator);
        return this;
    }

    public InventoryValidationEnrichmentConfiguration setValidator(InventoryValidator validator) {
        if (validator == null) {
            return this;
        }
        if (validator instanceof VulnerabilityInvalidNameValidator) {
            this.vulnerabilityInvalidNameValidator = (VulnerabilityInvalidNameValidator) validator;
        } else if (validator instanceof ArtifactAndCpeVersionsDifferGreatlyInventoryValidator) {
            this.artifactAndCpeVersionsDifferGreatlyInventoryValidator = (ArtifactAndCpeVersionsDifferGreatlyInventoryValidator) validator;
        } else if (validator instanceof MultipleArtifactsAndVersionsOnVulnerabilityInventoryValidator) {
            this.multipleArtifactsAndVersionsOnVulnerabilityInventoryValidator = (MultipleArtifactsAndVersionsOnVulnerabilityInventoryValidator) validator;
        } else if (validator instanceof AdditionalCpeIsNotEffectiveInventoryValidator) {
            this.additionalCpeIsNotEffectiveInventoryValidator = (AdditionalCpeIsNotEffectiveInventoryValidator) validator;
        } else {
            throw new IllegalArgumentException("Unknown validator type: " + validator.getClass().getName());
        }
        return this;
    }

    public List buildValidators() {
        final List validators = new ArrayList<>();

        if (additionalCpeIsNotEffectiveInventoryValidator != null) {
            validators.add(additionalCpeIsNotEffectiveInventoryValidator);
        }
        if (multipleArtifactsAndVersionsOnVulnerabilityInventoryValidator != null) {
            validators.add(multipleArtifactsAndVersionsOnVulnerabilityInventoryValidator);
        }
        if (artifactAndCpeVersionsDifferGreatlyInventoryValidator != null) {
            validators.add(artifactAndCpeVersionsDifferGreatlyInventoryValidator);
        }
        if (vulnerabilityInvalidNameValidator != null) {
            validators.add(vulnerabilityInvalidNameValidator);
        }

        validators.addAll(additionalValidators);

        return validators;
    }

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

        for (InventoryValidator validator : buildValidators()) {
            configuration.put(getValidatorPropertyName(validator), validator.getProperties());
        }

        configuration.put("failOnValidationErrors", failOnValidationErrors);
        configuration.put("addAsCorrelationWarnings", addAsCorrelationWarnings);

        return configuration;
    }

    private String getValidatorPropertyName(InventoryValidator validator) {
        if (validator instanceof AdditionalCpeIsNotEffectiveInventoryValidator) {
            return "additionalCpeIsNotEffectiveInventoryValidator";
        } else if (validator instanceof MultipleArtifactsAndVersionsOnVulnerabilityInventoryValidator) {
            return "multipleArtifactsAndVersionsOnVulnerabilityInventoryValidator";
        } else if (validator instanceof ArtifactAndCpeVersionsDifferGreatlyInventoryValidator) {
            return "artifactAndCpeVersionsDifferGreatlyInventoryValidator";
        } else if (validator instanceof VulnerabilityInvalidNameValidator) {
            return "vulnerabilityInvalidNameValidator";
        } else {
            throw new IllegalArgumentException("Unknown validator: " + validator);
        }
    }

    private InventoryValidator constructValidator(String validatorName) {
        switch (validatorName) {
            case "additionalCpeIsNotEffectiveInventoryValidator":
                return new AdditionalCpeIsNotEffectiveInventoryValidator();
            case "multipleArtifactsAndVersionsOnVulnerabilityInventoryValidator":
                return new MultipleArtifactsAndVersionsOnVulnerabilityInventoryValidator();
            case "artifactAndCpeVersionsDifferGreatlyInventoryValidator":
                return new ArtifactAndCpeVersionsDifferGreatlyInventoryValidator();
            case "vulnerabilityInvalidNameValidator":
                return new VulnerabilityInvalidNameValidator();
            default:
                return null;
        }
    }

    @Override
    public void setProperties(LinkedHashMap properties) {
        for (Map.Entry entry : properties.entrySet()) {
            final String validatorName = entry.getKey();
            final Object validatorProperties = entry.getValue();

            final InventoryValidator validator = constructValidator(validatorName);
            if (validator != null) {
                validator.setProperties((LinkedHashMap) validatorProperties);
            }

            this.setValidator(validator);
        }

        final Object failOnValidationErrors = properties.get("failOnValidationErrors");
        if (failOnValidationErrors != null) {
            this.failOnValidationErrors = (boolean) failOnValidationErrors;
        }

        final Object addAsCorrelationWarnings = properties.get("addAsCorrelationWarnings");
        if (addAsCorrelationWarnings != null) {
            this.addAsCorrelationWarnings = (boolean) addAsCorrelationWarnings;
        }
    }

    @Override
    protected void collectMisconfigurations(List misconfigurations) {
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy