Skip to content

Commit

Permalink
merge main, use afresh apiBuilder per request
Browse files Browse the repository at this point in the history
  • Loading branch information
EugenMayer committed Feb 18, 2025
1 parent bd50ec4 commit 519e51f
Showing 1 changed file with 40 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,43 @@ public Integer timedCall() throws Exception {
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
loggerContext.getLogger("io.github.jeremylong").setLevel(Level.DEBUG);
}

final NvdCveClientBuilder builder = createApiClientBuilder();

if (configGroup != null && configGroup.cacheSettings != null) {
CacheProperties properties = new CacheProperties(configGroup.cacheSettings.directory);
if (configGroup.cacheSettings.prefix != null) {
properties.set("prefix", configGroup.cacheSettings.prefix);
}
try {
int status = processRequest(properties);
properties.save();
return status;
} catch (CacheException ex) {
LOG.error(ex.getMessage(), ex);
}
return 1;
}

if (configGroup != null && configGroup.modifiedRange != null
&& configGroup.modifiedRange.lastModStartDate != null) {
ZonedDateTime end = configGroup.modifiedRange.lastModEndDate;
if (end == null) {
end = configGroup.modifiedRange.lastModStartDate.minusDays(-120);
}
builder.withLastModifiedFilter(configGroup.modifiedRange.lastModStartDate, end);
}

return processRequest(builder);
}

private NvdCveClientBuilder createApiClientBuilder() {
String apiKey = getApiKey();
if (apiKey == null || apiKey.isEmpty()) {
LOG.info("NVD_API_KEY not found. Supply an API key for more generous rate limits");
apiKey = null;// in case it is empty
}

NvdCveClientBuilder builder = NvdCveClientBuilder.aNvdCveApi().withApiKey(apiKey);
if (getDelay() > 0) {
builder.withDelay(getDelay());
Expand Down Expand Up @@ -260,55 +292,30 @@ public Integer timedCall() throws Exception {
builder.withThreadCount(getThreads());
}

if (configGroup != null && configGroup.cacheSettings != null) {
CacheProperties properties = new CacheProperties(configGroup.cacheSettings.directory);
if (configGroup.cacheSettings.prefix != null) {
properties.set("prefix", configGroup.cacheSettings.prefix);
}
try {
int status = processRequest(builder, properties);
properties.save();
return status;
} catch (CacheException ex) {
LOG.error(ex.getMessage(), ex);
}
return 1;
}
if (configGroup != null && configGroup.modifiedRange != null
&& configGroup.modifiedRange.lastModStartDate != null) {
ZonedDateTime end = configGroup.modifiedRange.lastModEndDate;
if (end == null) {
end = configGroup.modifiedRange.lastModStartDate.minusDays(-120);
}
builder.withLastModifiedFilter(configGroup.modifiedRange.lastModStartDate, end);
}
return processRequest(builder);
return builder;
}

/**
* For all years, fetch the NVD vulnerabilities and save them each into one cache file.
*/
private Integer processRequest(NvdCveClientBuilder apiBuilder, CacheProperties properties) {
private Integer processRequest(CacheProperties properties) {
// First update the modified cache
int exitCode = updateRecentChangedModifiedCache(apiBuilder, properties);
int exitCode = updateRecentChangedModifiedCache(properties);
if (exitCode != 0) {
return exitCode;
}
exitCode = updateYearCveCache(apiBuilder, properties);
exitCode = updateYearCveCache(properties);
return exitCode;
}

private int updateYearCveCache(NvdCveClientBuilder apiBuilder, CacheProperties properties) {
private int updateYearCveCache(CacheProperties properties) {
int exitCode = EXIT_CODE_SUCCESS;
apiBuilder.removeLastModifiedFilter();
apiBuilder.removePublishDateFilter();

for (int currentYear = START_YEAR; currentYear <= Year.now().getValue(); currentYear++) {
// Be forgiving, if we fail to fetch a year, we just continue with the next one
try {
// reset our filters for each year
apiBuilder.removeLastModifiedFilter();
apiBuilder.removePublishDateFilter();
final NvdCveClientBuilder apiBuilder = createApiClientBuilder();

Path cacheFilePath = buildCacheTargetFileForYear(properties, currentYear);

Expand Down Expand Up @@ -355,9 +362,8 @@ private int updateYearCveCache(NvdCveClientBuilder apiBuilder, CacheProperties p
return exitCode;
}

private int updateRecentChangedModifiedCache(NvdCveClientBuilder apiBuilder, CacheProperties properties) {
apiBuilder.removeLastModifiedFilter();
apiBuilder.removePublishDateFilter();
private int updateRecentChangedModifiedCache(CacheProperties properties) {
final NvdCveClientBuilder apiBuilder = createApiClientBuilder();

try {
LOG.info("INFO Updating most-recent-changed-cache");
Expand Down

0 comments on commit 519e51f

Please sign in to comment.