Skip to content

Commit 856dcee

Browse files
committed
Update reservations to allow new year reservations within 90 days
1 parent c646956 commit 856dcee

File tree

1 file changed

+78
-9
lines changed

1 file changed

+78
-9
lines changed

src/controller/cve-id.controller/cve-id.controller.js

Lines changed: 78 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -455,9 +455,22 @@ async function priorityReservation (year, amount, shortName, orgShortName, reque
455455

456456
// Cve Id Range for 'year' does not exists
457457
if (!result) {
458-
logger.info({ uuid: req.ctx.uuid, message: 'CVE IDs for year ' + year + ' cannot be reserved at this time.' })
459-
res.header(CONSTANTS.QUOTA_HEADER, availableIds)
460-
return res.status(403).json(error.cannotReserveForYear(year))
458+
// If there are less than or equal to 90 days until the end of the year, auto reserve it and move on.
459+
// Otherwise throw failure
460+
if (daysUntilYear(year) <= 90) {
461+
// Auto reserve the year
462+
const successfullyReservedYear = await reserveYear(year, req)
463+
if (!successfullyReservedYear) {
464+
logger.info({ uuid: req.ctx.uuid, message: 'CVE IDs for year ' + year + ' failed to be automatically reserved at this time.' })
465+
res.header(CONSTANTS.QUOTA_HEADER, availableIds)
466+
return res.status(403).json(error.cannotReserveForYear(year))
467+
}
468+
result = await cveIdRangeRepo.findOne({ cve_year: year })
469+
} else {
470+
logger.info({ uuid: req.ctx.uuid, message: 'CVE IDs for year ' + year + ' cannot be reserved at this time.' })
471+
res.header(CONSTANTS.QUOTA_HEADER, availableIds)
472+
return res.status(403).json(error.cannotReserveForYear(year))
473+
}
461474
}
462475

463476
const endRange = parseInt(result.ranges.priority.end)
@@ -531,9 +544,22 @@ async function sequentialReservation (year, amount, shortName, orgShortName, req
531544

532545
// Cve Id Range for 'year' does not exists
533546
if (!result) {
534-
logger.info({ uuid: req.ctx.uuid, message: 'CVE IDs for year ' + year + ' cannot be reserved at this time.' })
535-
res.header(CONSTANTS.QUOTA_HEADER, availableIds)
536-
return res.status(403).json(error.cannotReserveForYear(year))
547+
// If there are less than or equal to 90 days until the end of the year, auto reserve it and move on.
548+
// Otherwise throw failure
549+
if (daysUntilYear(year) <= 90) {
550+
// Auto reserve the year
551+
const successfullyReservedYear = await reserveYear(year, req)
552+
if (!successfullyReservedYear) {
553+
logger.info({ uuid: req.ctx.uuid, message: 'CVE IDs for year ' + year + ' failed to be automatically reserved at this time.' })
554+
res.header(CONSTANTS.QUOTA_HEADER, availableIds)
555+
return res.status(403).json(error.cannotReserveForYear(year))
556+
}
557+
result = await cveIdRangeRepo.findOne({ cve_year: year })
558+
} else {
559+
logger.info({ uuid: req.ctx.uuid, message: 'CVE IDs for year ' + year + ' cannot be reserved at this time.' })
560+
res.header(CONSTANTS.QUOTA_HEADER, availableIds)
561+
return res.status(403).json(error.cannotReserveForYear(year))
562+
}
537563
}
538564

539565
const topId = parseInt(result.ranges.general.top_id)
@@ -627,9 +653,22 @@ async function nonSequentialReservation (year, amount, shortName, orgShortName,
627653

628654
// Cve Id Range for 'year' does not exists
629655
if (!result) {
630-
logger.info({ uuid: req.ctx.uuid, message: 'CVE IDs for year ' + year + ' cannot be reserved at this time.' })
631-
res.header(CONSTANTS.QUOTA_HEADER, availableIds)
632-
return res.status(403).json(error.cannotReserveForYear(year))
656+
// If there are less than or equal to 90 days until the end of the year, auto reserve it and move on.
657+
// Otherwise throw failure
658+
if (daysUntilYear(year) <= 90) {
659+
// Auto reserve the year
660+
const successfullyReservedYear = await reserveYear(year, req)
661+
if (!successfullyReservedYear) {
662+
logger.info({ uuid: req.ctx.uuid, message: 'CVE IDs for year ' + year + ' failed to be automatically reserved at this time.' })
663+
res.header(CONSTANTS.QUOTA_HEADER, availableIds)
664+
return res.status(403).json(error.cannotReserveForYear(year))
665+
}
666+
result = await cveIdRangeRepo.findOne({ cve_year: year })
667+
} else {
668+
logger.info({ uuid: req.ctx.uuid, message: 'CVE IDs for year ' + year + ' cannot be reserved at this time.' })
669+
res.header(CONSTANTS.QUOTA_HEADER, availableIds)
670+
return res.status(403).json(error.cannotReserveForYear(year))
671+
}
633672
}
634673

635674
available = await cveIdRepo.find({ cve_year: year, state: 'AVAILABLE' }, { limit: availableLimit }) // get available ids
@@ -944,6 +983,36 @@ function setMinAggregateObj (query) {
944983
]
945984
}
946985

986+
function daysUntilYear (targetYear) {
987+
// Get today's date
988+
const today = new Date()
989+
990+
// Create a date object for January 1st of the target year
991+
const targetDate = new Date(targetYear, 0, 1) // Month is 0-indexed, so 0 is January
992+
993+
// Calculate the difference in milliseconds
994+
const differenceInMilliseconds = targetDate - today
995+
996+
// Convert milliseconds to days
997+
const millisecondsPerDay = 1000 * 60 * 60 * 24
998+
const differenceInDays = Math.ceil(differenceInMilliseconds / millisecondsPerDay)
999+
1000+
return differenceInDays
1001+
}
1002+
1003+
async function reserveYear (targetYear, req) {
1004+
try {
1005+
const CONSTANTS = getConstants()
1006+
const cveIdRangeRepo = req.ctx.repositories.getCveIdRangeRepository()
1007+
const defaultDoc = CONSTANTS.DEFAULT_CVE_ID_RANGE
1008+
defaultDoc.cve_year = targetYear
1009+
await cveIdRangeRepo.findOneAndUpdate({ cve_year: targetYear }, defaultDoc, { upsert: true })
1010+
return true
1011+
} catch (err) {
1012+
return false
1013+
}
1014+
}
1015+
9471016
module.exports = {
9481017
CVEID_GET_FILTER: getFilteredCveId,
9491018
CVEID_RESERVE: reserveCveId,

0 commit comments

Comments
 (0)