From 706660f73e5a8c23d27f19832c9389b306ab64c0 Mon Sep 17 00:00:00 2001 From: Douglas Barone Date: Fri, 18 Dec 2020 14:33:34 -0400 Subject: [PATCH] Added logger --- server/src/classes/ResetToken.js | 11 +++++++++++ server/src/classes/User.js | 31 ++++++++++++++++++++++++++----- server/src/cronTasks.js | 10 +++++----- server/src/index.js | 2 +- server/src/lib/logger.js | 4 +++- server/src/lib/paloalto.js | 8 +++++++- server/src/lib/wifiDevices.js | 8 +++++++- server/src/resolvers/Mutation.js | 8 +++++++- 8 files changed, 67 insertions(+), 15 deletions(-) diff --git a/server/src/classes/ResetToken.js b/server/src/classes/ResetToken.js index 97ed9b9..c9e19cb 100755 --- a/server/src/classes/ResetToken.js +++ b/server/src/classes/ResetToken.js @@ -6,6 +6,7 @@ import bcrypt from 'bcrypt' import { replacePassword } from '../lib/activeDirectory/passwordUtils' import prisma from '../prisma' +import { logInfo, logSuccess } from '../lib/logger' class ResetToken { /** @@ -37,6 +38,11 @@ class ResetToken { } }) + logInfo({ + tags: ['token', 'user'], + message: `User ${creatorUsername} created a reset-token for user ${username}` + }) + return { ...hashedToken, token @@ -98,6 +104,11 @@ class ResetToken { } }) + logSuccess({ + tags: ['token', 'user'], + message: `User ${user.sAMAccountName} used a reset token` + }) + return true } } diff --git a/server/src/classes/User.js b/server/src/classes/User.js index 1e27194..2dcf87c 100755 --- a/server/src/classes/User.js +++ b/server/src/classes/User.js @@ -202,7 +202,7 @@ class User { User.upsertUser(this.username) client.unbind(() => { logSuccess({ - message: `Client unbinded. Password updated for user ${this.username}`, + message: `User ${this.username} updated his password`, data: result, tags: ['ldap'] }) @@ -284,11 +284,32 @@ class User { */ static async login(username, password) { try { - await ad.authenticate('ifms\\' + username, password) + logInfo({ + tags: ['user', 'login'], + message: `User ${username} is trying to login` + }) + + await ad.authenticate(`ifms\\${username}`, password) + + logSuccess({ + tags: ['user', 'login'], + message: `User ${username} logged successfully` + }) } catch (err) { - throw (await ad.checkBinding()) - ? new Error('Usuário ou senha inválidos.') - : new Error('Problemas técnicos ao autenticar. Tente mais tarde.') + if (await ad.checkBinding()) { + logWarning({ + tags: ['user', 'login', 'password'], + message: `User ${username} tried to login but failed`, + data: err + }) + throw new Error('Usuário ou senha inválidos.') + } else { + logError({ + tags: ['user', 'login', 'password'], + message: `Can't bind with AD.` + }) + throw new Error('Problemas técnicos ao autenticar. Tente mais tarde.') + } } try { diff --git a/server/src/cronTasks.js b/server/src/cronTasks.js index 1a842ea..60e3663 100644 --- a/server/src/cronTasks.js +++ b/server/src/cronTasks.js @@ -21,7 +21,7 @@ cron.schedule('*/1 * * * *', async () => { const devices = await updateDevicesInfo() - logSuccess({ + logInfo({ tags: ['cron', 'wifiDevices'], message: `updateDevicesInfo updated ${devices} devices` }) @@ -36,7 +36,7 @@ cron.schedule('*/1 * * * *', async () => { mappings = await updateUserIdMappings() - logSuccess({ + logInfo({ tags: ['cron', 'user-id'], message: `updateUserIdMappings updated ${mappings} user-id mappings` }) @@ -51,11 +51,11 @@ cron.schedule( message: `User.importAllUsers started` }) - await User.importAllUsers() + const users = await User.importAllUsers() - logSuccess({ + logInfo({ tags: ['cron', 'user'], - message: `User.importAllUsers finished` + message: `User.importAllUsers imported ${users} users` }) }, {} diff --git a/server/src/index.js b/server/src/index.js index 99a45e8..583916b 100755 --- a/server/src/index.js +++ b/server/src/index.js @@ -22,6 +22,6 @@ server.listen().then(options => { }) logInfo({ tags: ['server'], - message: `Endpoint: ${options.url}graphql - WebSocket: ${options.subscriptionsUrl}` + message: `Endpoint: ${options.url}graphql | WebSocket: ${options.subscriptionsUrl}` }) }) diff --git a/server/src/lib/logger.js b/server/src/lib/logger.js index ea17acb..8626fc4 100644 --- a/server/src/lib/logger.js +++ b/server/src/lib/logger.js @@ -1,6 +1,8 @@ import prisma from '../prisma' import { format, subDays } from 'date-fns' +const DAYS_TO_KEEP = process.env.NODE_ENV === 'production' ? 30 : 1 + async function log({ level, tags = [], message = '', data }) { const logEntry = await prisma.log.create({ data: { @@ -48,7 +50,7 @@ function logError({ tags, message, data }) { async function deleteOldLogs() { return prisma.log.deleteMany({ - where: { timestamp: { lt: subDays(new Date(), 30) } } + where: { timestamp: { lt: subDays(new Date(), DAYS_TO_KEEP) } } }) } diff --git a/server/src/lib/paloalto.js b/server/src/lib/paloalto.js index 7fc206f..68c9b66 100644 --- a/server/src/lib/paloalto.js +++ b/server/src/lib/paloalto.js @@ -5,7 +5,7 @@ import prisma from '../prisma' import https from 'https' import { subMinutes } from 'date-fns' import qs from 'qs' -import { logError } from './logger' +import { logError, logSuccess } from './logger' const DEBOUNCE_TIME_IN_MS = 5000 const TIMEOUT_IN_MINUTES = '3' @@ -71,6 +71,12 @@ async function updateUserIdMappings() { httpsAgent }) + logSuccess({ + tags: ['user-id', 'paloalto'], + message: `Updated ${wifiDevices.length} user-id mappings`, + data: wifiDevices + }) + return wifiDevices.length } catch (e) { logError({ diff --git a/server/src/lib/wifiDevices.js b/server/src/lib/wifiDevices.js index 6f2cf3c..374d534 100644 --- a/server/src/lib/wifiDevices.js +++ b/server/src/lib/wifiDevices.js @@ -5,7 +5,7 @@ import { getOnlineWifiDevices as getOnlineCiscoDevices } from './ciscoController import prisma from '../prisma' import { pubsub, USER_PRESENCE_UPDATED } from '../pubsub' -import { logError } from './logger' +import { logError, logSuccess } from './logger' const DEBOUNCE_TIME_MS = 10000 const RECENT_THRESHOLD_IN_MINUTES = 3 @@ -109,6 +109,12 @@ async function updateDevicesInfo() { userPresenceUpdated: onlineDevices.length }) + logSuccess({ + tags: ['wifiDevices'], + message: `Updated ${onlineDevices.length} devices`, + data: onlineDevices + }) + return onlineDevices.length } catch (e) { logError({ diff --git a/server/src/resolvers/Mutation.js b/server/src/resolvers/Mutation.js index 583e876..8da649d 100755 --- a/server/src/resolvers/Mutation.js +++ b/server/src/resolvers/Mutation.js @@ -4,6 +4,7 @@ import { ResetToken } from '../classes/ResetToken' import { updateDevicesInfo } from '../lib/wifiDevices' import { updateUserIdMappings } from '../lib/paloalto' +import { logInfo, logSuccess } from '../lib/logger' const Mutation = { async login(_, { data }) { @@ -14,7 +15,12 @@ const Mutation = { return auth.updatePassword(data.oldPassword, data.newPassword) }, - async replacePassword(_, { data }) { + async replacePassword(_, { data }, { auth }) { + logInfo({ + tags: ['replacePassword', 'user'], + message: `User ${auth.sAMAccountName} is replacing the password for user ${data.username}` + }) + return replacePassword(data.username, data.newPassword) },