Skip to content

Commit 330cc61

Browse files
fix: don't stringify JSON for logging
Previously, some logging calls would stringify a JSON structure before writing it to the log. This is incorrect, and makes for some awkward logs when reaching. Instead, if you include a message field in your struct, then it will get printed while the other fields are printed as structured information in the log. So this updates logging calls throughout the application to do the correct thing. Signed-off-by: Andrew Lilley Brinker <abrinker@mitre.org>
1 parent 5cc0a25 commit 330cc61

7 files changed

Lines changed: 30 additions & 25 deletions

File tree

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -578,9 +578,9 @@ async function sequentialReservation (year, amount, shortName, orgShortName, req
578578
isFull = true
579579

580580
if (isPriority) {
581-
logger.error(JSON.stringify({ message: 'The cve id priority and sequential blocks are full for year ' + year + '. No more priority or sequential ids can be reserved at this time.' }))
581+
logger.error({ message: 'The cve id priority and sequential blocks are full for year ' + year + '. No more priority or sequential ids can be reserved at this time.' })
582582
} else {
583-
logger.error(JSON.stringify({ message: 'The cve id sequential block is full for year ' + year + '. No more sequential ids can be reserved at this time.' }))
583+
logger.error({ message: 'The cve id sequential block is full for year ' + year + '. No more sequential ids can be reserved at this time.' })
584584
}
585585

586586
res.header(CONSTANTS.QUOTA_HEADER, availableIds)
@@ -688,7 +688,7 @@ async function nonSequentialReservation (year, amount, shortName, orgShortName,
688688
isFull = result.isFull
689689

690690
if (isFull) {
691-
logger.error(JSON.stringify({ message: 'The cve id non-sequential block is full for year ' + year + '. No more sequential ids can be reserved at this time.' }))
691+
logger.error({ message: 'The cve id non-sequential block is full for year ' + year + '. No more sequential ids can be reserved at this time.' })
692692
res.header(CONSTANTS.QUOTA_HEADER, availableIds)
693693
return res.status(403).json(error.yearRangeFull(year))
694694
}

src/controller/cve.controller/cve.controller.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ async function submitCna (req, res, next) {
552552
result = Cve.validateCveRecord(cveModel.cve)
553553

554554
if (!result.isValid) {
555-
logger.error(JSON.stringify({ uuid: req.ctx.uuid, message: 'CVE JSON schema validation FAILED.' }))
555+
logger.error({ uuid: req.ctx.uuid, message: 'CVE JSON schema validation FAILED.' })
556556
return res.status(400).json(error.invalidCnaContainerJsonSchema(result.errors))
557557
}
558558

@@ -658,7 +658,7 @@ async function updateCna (req, res, next) {
658658
result = Cve.validateCveRecord(cveModel.cve)
659659

660660
if (!result.isValid) {
661-
logger.error(JSON.stringify({ uuid: req.ctx.uuid, message: 'CVE JSON schema validation FAILED.' }))
661+
logger.error({ uuid: req.ctx.uuid, message: 'CVE JSON schema validation FAILED.' })
662662
return res.status(400).json(error.invalidCnaContainerJsonSchema(result.errors))
663663
}
664664

@@ -730,7 +730,7 @@ async function rejectCVE (req, res, next) {
730730

731731
result = Cve.validateCveRecord(newCveObj.cve)
732732
if (!result.isValid) {
733-
logger.error(JSON.stringify({ uuid: req.ctx.uuid, message: 'CVE JSON schema validation FAILED.' }))
733+
logger.error({ uuid: req.ctx.uuid, message: 'CVE JSON schema validation FAILED.' })
734734
return res.status(400).json(error.invalidCnaContainerJsonSchema(result.errors))
735735
}
736736

@@ -805,7 +805,7 @@ async function rejectExistingCve (req, res, next) {
805805

806806
result = Cve.validateCveRecord(updatedCve.cve)
807807
if (!result.isValid) {
808-
logger.error(JSON.stringify({ uuid: req.ctx.uuid, message: 'CVE JSON schema validation FAILED.' }))
808+
logger.error({ uuid: req.ctx.uuid, message: 'CVE JSON schema validation FAILED.' })
809809
return res.status(400).json(error.invalidCnaContainerJsonSchema(result.errors))
810810
}
811811
result = await cveRepo.updateByCveId(id, updatedCve)
@@ -916,7 +916,7 @@ async function insertAdp (req, res, next) {
916916
const cveModel = new Cve({ cve: convertDatesToISO(cveRecord, CONSTANTS.DATE_FIELDS) })
917917
result = Cve.validateCveRecord(cveModel.cve)
918918
if (!result.isValid) {
919-
logger.error(JSON.stringify({ uuid: req.ctx.uuid, message: 'CVE JSON schema validation FAILED.' }))
919+
logger.error({ uuid: req.ctx.uuid, message: 'CVE JSON schema validation FAILED.' })
920920
return res.status(400).json(error.badAdpJson(result.errors))
921921
}
922922

src/controller/cve.controller/cve.middleware.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ function validateCveCnaContainerJsonSchema (req, res, next) {
134134
const cnaContainer = req.body
135135
const result = validateCnaContainer(cnaContainer)
136136
if (!result) {
137-
logger.error(JSON.stringify({ uuid: req.ctx.uuid, message: 'CVE JSON schema validation FAILED.' }))
137+
logger.error({ uuid: req.ctx.uuid, message: 'CVE JSON schema validation FAILED.' })
138138
const temp = validateCnaContainer.errors
139139
const errorsArray = []
140140
temp.forEach((error) => {
@@ -144,7 +144,7 @@ function validateCveCnaContainerJsonSchema (req, res, next) {
144144
})
145145
return res.status(400).json(error.invalidCnaContainerJsonSchema(errorsArray))
146146
}
147-
logger.info(JSON.stringify({ uuid: req.ctx.uuid, message: 'SUCCESSFUL CVE JSON schema validation.' }))
147+
logger.info({ uuid: req.ctx.uuid, message: 'SUCCESSFUL CVE JSON schema validation.' })
148148
next()
149149
}
150150

src/controller/org.controller/org.controller.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ async function createOrg (req, res, next) {
239239
// If we are creating an org via the registry flag, we can do a full validation.
240240
const result = await repo.validateOrg(body, { session })
241241
if (!result.isValid) {
242-
logger.error(JSON.stringify({ uuid: req.ctx.uuid, message: 'CVE JSON schema validation FAILED.' }))
242+
logger.error({ uuid: req.ctx.uuid, message: 'CVE JSON schema validation FAILED.' })
243243
await session.abortTransaction()
244244
if (!Array.isArray(body?.authority) || body?.authority.some(item => typeof item !== 'string')) {
245245
return res.status(400).json({ error: 'BAD_INPUT', message: 'Parameters were invalid', details: [{ param: 'authority', msg: 'Parameter must be a one-dimensional array of strings' }] })
@@ -401,7 +401,7 @@ async function createUser (req, res, next) {
401401
return res.status(400).json({ message: 'Parameters were invalid', details: [{ param: 'role', msg: `Role must be one of the following: ${constants.USER_ROLES}` }] })
402402
}
403403
if (!result.isValid) {
404-
logger.error(JSON.stringify({ uuid: req.ctx.uuid, message: 'User JSON schema validation FAILED.' }))
404+
logger.error({ uuid: req.ctx.uuid, message: 'User JSON schema validation FAILED.' })
405405
await session.abortTransaction()
406406
return res.status(400).json({ message: 'Parameters were invalid', errors: result.errors })
407407
}

src/controller/registry-org.controller/registry-org.controller.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ async function createOrg (req, res, next) {
132132
session.startTransaction()
133133
const result = repo.validateOrg(body, { session })
134134
if (!result.isValid) {
135-
logger.error(JSON.stringify({ uuid: req.ctx.uuid, message: 'CVE JSON schema validation FAILED.' }))
135+
logger.error({ uuid: req.ctx.uuid, message: 'CVE JSON schema validation FAILED.' })
136136
await session.abortTransaction()
137137

138138
// TODO: Investigate this, right now we are accepting either a one one-dimensional array of strings or just a string.
@@ -267,7 +267,7 @@ async function updateOrg (req, res, next) {
267267

268268
const result = repo.validateOrg(body, { session })
269269
if (!result.isValid) {
270-
logger.error(JSON.stringify({ uuid: req.ctx.uuid, message: 'CVE JSON schema validation FAILED.' }))
270+
logger.error({ uuid: req.ctx.uuid, message: 'CVE JSON schema validation FAILED.' })
271271
await session.abortTransaction()
272272
return res.status(400).json({ message: 'Parameters were invalid', errors: result.errors })
273273
}
@@ -482,7 +482,7 @@ async function createUserByOrg (req, res, next) {
482482
return res.status(400).json({ message: 'Parameters were invalid', details: [{ param: 'role', msg: 'Parameter must be a string' }] })
483483
}
484484
if (!result.isValid) {
485-
logger.error(JSON.stringify({ uuid: req.ctx.uuid, message: 'User JSON schema validation FAILED.' }))
485+
logger.error({ uuid: req.ctx.uuid, message: 'User JSON schema validation FAILED.' })
486486
await session.abortTransaction()
487487
return res.status(400).json({ message: 'Parameters were invalid', errors: result.errors })
488488
}

src/controller/registry-user.controller/registry-user.controller.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ async function createUser (req, res, next) {
8282
return res.status(400).json({ message: 'Parameters were invalid', details: [{ param: 'role', msg: 'Parameter must be a string' }] })
8383
}
8484
if (!result.isValid) {
85-
logger.error(JSON.stringify({ uuid: req.ctx.uuid, message: 'User JSON schema validation FAILED.' }))
85+
logger.error({ uuid: req.ctx.uuid, message: 'User JSON schema validation FAILED.' })
8686
await session.abortTransaction()
8787
return res.status(400).json({ message: 'Parameters were invalid', errors: result.errors })
8888
}
@@ -136,7 +136,7 @@ async function updateUser (req, res, next) {
136136
return res.status(400).json({ message: 'Parameters were invalid', details: [{ param: 'role', msg: 'Parameter must be a string' }] })
137137
}
138138
if (!result.isValid) {
139-
logger.error(JSON.stringify({ uuid: req.ctx.uuid, message: 'User JSON schema validation FAILED.' }))
139+
logger.error({ uuid: req.ctx.uuid, message: 'User JSON schema validation FAILED.' })
140140
await session.abortTransaction()
141141
return res.status(400).json({ message: 'Parameters were invalid', errors: result.errors })
142142
}

src/middleware/middleware.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,12 @@ function createCtxAndReqUUID (req, res, next) {
3232
repositories: new RepositoryFactory()
3333
}
3434

35-
logger.info(JSON.stringify({ uuid: req.ctx.uuid, path: req.path }))
35+
logger.info({
36+
message: 'setting up req context',
37+
uuid: req.ctx.uuid,
38+
path: req.path
39+
})
40+
3641
next()
3742
} catch (err) {
3843
next(err)
@@ -129,18 +134,18 @@ async function validateUser (req, res, next) {
129134

130135
const result = await userRepo.findOneByUserNameAndOrgUUID(user, orgUUID)
131136
if (!result) {
132-
logger.warn(JSON.stringify({ uuid: req.ctx.uuid, message: 'User not found. User authentication FAILED for ' + user }))
137+
logger.warn({ uuid: req.ctx.uuid, message: 'User not found. User authentication FAILED for ' + user })
133138
return res.status(401).json(error.unauthorized())
134139
}
135140

136141
if (result.active === false || result.status === 'inactive') {
137-
logger.warn(JSON.stringify({ uuid: req.ctx.uuid, message: 'User deactivated. Authentication failed for ' + user }))
142+
logger.warn({ uuid: req.ctx.uuid, message: 'User deactivated. Authentication failed for ' + user })
138143
return res.status(401).json(error.unauthorized())
139144
}
140145

141146
const isPwd = await argon2.verify(result.secret, key)
142147
if (!isPwd) {
143-
logger.warn(JSON.stringify({ uuid: req.ctx.uuid, message: 'Incorrect apikey. User authentication FAILED for ' + user }))
148+
logger.warn({ uuid: req.ctx.uuid, message: 'Incorrect apikey. User authentication FAILED for ' + user })
144149
return res.status(401).json(error.unauthorized())
145150
}
146151

@@ -345,7 +350,7 @@ function validateCveJsonSchema (req, res, next) {
345350
}
346351

347352
if (cveState === undefined) {
348-
logger.error(JSON.stringify({ uuid: req.ctx.uuid, message: 'CVE JSON schema validation FAILED.' }))
353+
logger.error({ uuid: req.ctx.uuid, message: 'CVE JSON schema validation FAILED.' })
349354
return res.status(400).json(error.invalidJsonSchema(['instance.cveMetadata is not defined']))
350355
}
351356
cveState = cveState.state
@@ -356,15 +361,15 @@ function validateCveJsonSchema (req, res, next) {
356361
if (['PUBLISHED', 'RESERVED', 'REJECTED'].includes(cveState)) {
357362
result = validate(cve)
358363
} else {
359-
logger.error(JSON.stringify({ uuid: req.ctx.uuid, message: 'CVE JSON schema validation FAILED.' }))
364+
logger.error({ uuid: req.ctx.uuid, message: 'CVE JSON schema validation FAILED.' })
360365
return res.status(400).json(error.invalidJsonSchema(['instance.cveMetadata.state is not one of enum values']))
361366
}
362367

363368
if (result) {
364-
logger.info(JSON.stringify({ uuid: req.ctx.uuid, message: 'SUCCESSFUL CVE JSON schema validation.' }))
369+
logger.info({ uuid: req.ctx.uuid, message: 'SUCCESSFUL CVE JSON schema validation.' })
365370
next()
366371
} else {
367-
logger.error(JSON.stringify({ uuid: req.ctx.uuid, message: 'CVE JSON schema validation FAILED.' }))
372+
logger.error({ uuid: req.ctx.uuid, message: 'CVE JSON schema validation FAILED.' })
368373
const temp = validate.errors
369374
const errors = []
370375
temp.forEach((error) => {

0 commit comments

Comments
 (0)