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

com.metaeffekt.mirror.query.VulnerabilityIndexQuery Maven / Gradle / Ivy

There is a newer version: 0.134.0
Show 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.mirror.query;

import com.metaeffekt.mirror.contents.store.VulnerabilityTypeIdentifier;
import com.metaeffekt.mirror.contents.vulnerability.Vulnerability;
import com.metaeffekt.mirror.contents.vulnerability.VulnerableSoftwareVersionRangeCpe;
import com.metaeffekt.mirror.index.Index;
import com.metaeffekt.mirror.index.IndexSearch;
import lombok.extern.slf4j.Slf4j;
import org.apache.lucene.document.Document;
import us.springett.parsers.cpe.Cpe;

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

@Slf4j
public abstract class VulnerabilityIndexQuery extends IndexQuery {

    protected VulnerabilityIndexQuery(File baseMirrorDirectory, Class requiredIndex) {
        super(baseMirrorDirectory, requiredIndex);
    }

    protected VulnerabilityIndexQuery(Index index) {
        super(index);
    }

    public abstract List findAll();

    public abstract Optional findVulnerabilityByName(String name);

    public abstract List findVulnerabilitiesByFlatAffectedConfiguration(Cpe cpe);

    public abstract Map findVulnerabilitiesByFlatAffectedConfigurationRetainSource(Cpe cpe);

    public abstract VulnerabilityTypeIdentifier getVulnerabilityType();

    public List findCreatedOrUpdatedInRange(long start, long end) {
        final List updatedOrCreated = new ArrayList<>();
        final Set knownVulnerabilityNames = new HashSet<>();

        final List createDateVulnerabilities = super.getIndex().findDocuments(new IndexSearch().fieldNumericBetween("createDate", start, end)).stream().map(this::fromDocument).collect(Collectors.toList());
        final List updateDateVulnerabilities = super.getIndex().findDocuments(new IndexSearch().fieldNumericBetween("updateDate", start, end)).stream().map(this::fromDocument).collect(Collectors.toList());

        for (Vulnerability vulnerability : createDateVulnerabilities) {
            if (knownVulnerabilityNames.add(vulnerability.getId())) {
                updatedOrCreated.add(vulnerability);
            }
        }

        for (Vulnerability vulnerability : updateDateVulnerabilities) {
            if (knownVulnerabilityNames.add(vulnerability.getId())) {
                updatedOrCreated.add(vulnerability);
            }
        }

        return updatedOrCreated;
    }

    public Vulnerability fromDocument(Document document) {
        final Vulnerability vulnerability = Vulnerability.fromDocument(document);
        vulnerability.setSourceIdentifier(this.getVulnerabilityType());
        return vulnerability;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy