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

feat: load and sync nvd database using API #24

Merged
merged 1 commit into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
12 changes: 0 additions & 12 deletions deploy/git-job-image/Dockerfile

This file was deleted.

12 changes: 0 additions & 12 deletions deploy/git-job-image/scripts/clone.sh

This file was deleted.

19 changes: 0 additions & 19 deletions deploy/git-job-image/scripts/common.sh

This file was deleted.

57 changes: 0 additions & 57 deletions deploy/git-job-image/scripts/load_all.sh

This file was deleted.

17 changes: 0 additions & 17 deletions deploy/git-job-image/scripts/pull.sh

This file was deleted.

15 changes: 0 additions & 15 deletions deploy/git-job-image/scripts/sync.sh

This file was deleted.

74 changes: 74 additions & 0 deletions src/main/java/com/redhat/ecosystemappeng/onguard/model/Bulk.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright 2024 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* 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.redhat.ecosystemappeng.onguard.model;

import java.time.LocalDateTime;

import io.quarkus.runtime.annotations.RegisterForReflection;

@RegisterForReflection
public record Bulk(LocalDateTime started, LocalDateTime completed, Integer index, Integer pageSize) {

public static class Builder {
LocalDateTime started;
LocalDateTime completed;
Integer index;
Integer pageSize;

private Builder() {}

private Builder(Bulk other) {
this.started = other.started;
this.completed = other.completed;
this.index = other.index;
this.pageSize = other.pageSize;
}

public Builder started(LocalDateTime started) {
this.started = started;
return this;
}
public Builder completed(LocalDateTime completed) {
this.completed = completed;
return this;
}
public Builder index(Integer index) {
this.index = index;
return this;
}
public Builder pageSize(Integer pageSize) {
this.pageSize = pageSize;
return this;
}

public Bulk build() {
if(index == null) {
index = 0;
}
return new Bulk(started, completed, index, pageSize);
}
}

public static Bulk.Builder builder(Bulk other) {
return new Builder(other);
}

public static Bulk.Builder builder() {
return new Builder();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@
*/
package com.redhat.ecosystemappeng.onguard.model;

import static com.redhat.ecosystemappeng.onguard.model.osv.OsvVulnerability.CVE_PATTERN;

import java.util.Date;
import java.util.List;

import com.redhat.ecosystemappeng.onguard.model.nvd.Metrics;
import com.redhat.ecosystemappeng.onguard.model.osv.Affected;
import com.redhat.ecosystemappeng.onguard.service.cve.CveFileService;

public record Vulnerability(
List<String> aliases,
Expand Down Expand Up @@ -112,7 +113,7 @@ public List<String> getAliases() {

public Vulnerability build() {
if(cveId == null && aliases != null) {
var alias = aliases.stream().filter(a -> CveFileService.CVE_PATTERN.matcher(a).matches()).findAny();
var alias = aliases.stream().filter(a -> CVE_PATTERN.matcher(a).matches()).findAny();
if(alias.isPresent()) {
cveId = alias.get();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@
*/
package com.redhat.ecosystemappeng.onguard.model.osv;

import static com.redhat.ecosystemappeng.onguard.service.cve.CveFileService.CVE_PATTERN;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

public record OsvVulnerability(String id, String summary, String details, List<String> aliases, Date published,
Date modified, List<Affected> affected) {

public static final Pattern CVE_PATTERN = Pattern.compile("CVE-\\d{4}-\\d{4,7}", Pattern.CASE_INSENSITIVE);

public OsvVulnerability {
if (!CVE_PATTERN.matcher(id).matches() && aliases != null && !aliases.isEmpty()) {
List<String> newAliases = new ArrayList<>();
Expand Down
117 changes: 117 additions & 0 deletions src/main/java/com/redhat/ecosystemappeng/onguard/rest/LoadRoute.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* Copyright 2024 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* 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.redhat.ecosystemappeng.onguard.rest;

import java.text.ParseException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.redhat.ecosystemappeng.onguard.service.LoadService;

import io.quarkus.vertx.http.ManagementInterface;
import io.smallrye.mutiny.Uni;
import io.smallrye.mutiny.infrastructure.Infrastructure;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.event.Observes;
import jakarta.inject.Inject;

@ApplicationScoped
public class LoadRoute {

private static final Logger LOGGER = LoggerFactory.getLogger(LoadRoute.class);

@Inject
LoadService loadService;

@Inject
ObjectMapper mapper;

public void registerManagementRoutes(@Observes ManagementInterface mi) {
mi.router().post("/load").handler(ctx -> {
final LocalDateTime since;
try {
since = parseDateParam(ctx.request().getParam("since"));
} catch (ParseException e) {
ctx.response().setStatusCode(400).setStatusMessage(e.getMessage()).end();
return;
}
Boolean reload = Boolean.TRUE.toString().equalsIgnoreCase(ctx.request().getParam("reload"));
Uni.createFrom()
.voidItem()
.invoke(() -> {
if(reload) {
loadService.restart();
} else {
loadService.loadFromNvdApi(since);
}

})
.runSubscriptionOn(Infrastructure.getDefaultExecutor())
.subscribeAsCompletionStage()
.whenComplete((s, e) -> {
if (e != null) {
LOGGER.error("Load interrupted", e);
} else {
LOGGER.info("Completed");
}
});
ctx.response().setStatusCode(202).end();
});

mi.router().get("/load").blockingHandler(ctx -> {
try {
var bulk = loadService.get();
if (bulk == null) {
ctx.response().setStatusCode(404).end();
} else {
ctx.response().headers().add("Content-Type", "application/json");
ctx.response().setStatusCode(200).end(mapper.writeValueAsString(bulk));
}
} catch (JsonProcessingException e) {
ctx.response().setStatusCode(500).setStatusMessage(e.getMessage()).end();
}
});

mi.router().delete("/load").blockingHandler(ctx -> {
try {
var bulk = loadService.cancel();
if (bulk == null) {
ctx.response().setStatusCode(404).end();
} else {
ctx.response().headers().add("Content-Type", "application/json");
ctx.response().setStatusCode(200).end(mapper.writeValueAsString(bulk));
}
} catch (JsonProcessingException e) {
ctx.response().setStatusCode(500).setStatusMessage(e.getMessage()).end();
}
});
}

private LocalDateTime parseDateParam(String param) throws ParseException {
if(param != null) {
return LocalDateTime.parse(param, DateTimeFormatter.ISO_DATE);
}
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@ public class ObjectMapperCustomizerImpl implements ObjectMapperCustomizer {
@Override
public void customize(ObjectMapper mapper) {
mapper.setSerializationInclusion(Include.NON_NULL);

}
}
Loading