From 4e45c7e651c503383e6bb659397d27b3a3c76809 Mon Sep 17 00:00:00 2001 From: Roc Wu Date: Tue, 8 Feb 2022 09:28:54 +0800 Subject: [PATCH] fix: add more info logs for account/app management --- app.js | 48 +++++++++++++++----------------- core/services/account-manager.js | 1 - routes/accessKeys.js | 14 ++++++++-- routes/account.js | 2 +- routes/apps.js | 32 +++++++++++++++++++-- routes/auth.js | 29 +++++++++++++------ 6 files changed, 85 insertions(+), 41 deletions(-) diff --git a/app.js b/app.js index c4a6b36f..9b9c6855 100644 --- a/app.js +++ b/app.js @@ -1,23 +1,23 @@ -var express = require('express'); -var path = require('path'); -var cookieParser = require('cookie-parser'); -var bodyParser = require('body-parser'); -var helmet = require('helmet'); -var config = require('./core/config'); -var _ = require('lodash'); -var fs = require('fs'); -var { logger } = require('kv-logger'); +const express = require('express'); +const path = require('path'); +const cookieParser = require('cookie-parser'); +const bodyParser = require('body-parser'); +const helmet = require('helmet'); +const _ = require('lodash'); +const fs = require('fs'); +const { logger } = require('kv-logger'); -var routes = require('./routes/index'); -var indexV1 = require('./routes/indexV1'); -var auth = require('./routes/auth'); -var accessKeys = require('./routes/accessKeys'); -var account = require('./routes/account'); -var users = require('./routes/users'); -var apps = require('./routes/apps'); -var AppError = require('./core/app-error'); +const config = require('./core/config'); +const routes = require('./routes/index'); +const indexV1 = require('./routes/indexV1'); +const auth = require('./routes/auth'); +const accessKeys = require('./routes/accessKeys'); +const account = require('./routes/account'); +const users = require('./routes/users'); +const apps = require('./routes/apps'); +const { AppError, NotFound } = require('./core/app-error'); -var app = express(); +const app = express(); app.use( helmet({ @@ -33,9 +33,7 @@ app.use(bodyParser.urlencoded({ extended: false })); app.use(cookieParser()); app.use(express.static(path.join(__dirname, 'public'))); -//use nginx in production -//if (app.get('env') === 'development') { -logger.debug('set Access-Control Header'); +logger.debug('use set Access-Control Header'); app.all('*', function (req, res, next) { res.header('Access-Control-Allow-Origin', '*'); res.header( @@ -43,10 +41,8 @@ app.all('*', function (req, res, next) { 'Origin, X-Requested-With, Content-Type, Accept, Authorization, X-CodePush-Plugin-Version, X-CodePush-Plugin-Name, X-CodePush-SDK-Version', ); res.header('Access-Control-Allow-Methods', 'PUT,POST,GET,PATCH,DELETE,OPTIONS'); - logger.debug('use set Access-Control Header'); next(); }); -//} logger.debug('config common.storageType value: ' + _.get(config, 'common.storageType')); @@ -87,7 +83,7 @@ app.use('/apps', apps); // will print stacktrace if (app.get('env') === 'development') { app.use(function (req, res, next) { - var err = new AppError.NotFound(`${req.method} ${req.url}`); + var err = new NotFound(`${req.method} ${req.url}`); res.status(err.status || 404); res.render('error', { message: err.message, @@ -105,14 +101,14 @@ if (app.get('env') === 'development') { }); } else { app.use(function (req, res, next) { - var e = new AppError.NotFound(); + var e = new NotFound(); res.status(404).send(e.message); logger.debug(e); }); // production error handler // no stacktraces leaked to user app.use(function (err, req, res, next) { - if (err instanceof AppError.AppError) { + if (err instanceof AppError) { res.send(err.message); logger.debug(err); } else { diff --git a/core/services/account-manager.js b/core/services/account-manager.js index d9ab2a5a..30bc725c 100644 --- a/core/services/account-manager.js +++ b/core/services/account-manager.js @@ -22,7 +22,6 @@ proto.collaboratorCan = function (uid, appName) { logger.debug(`collaboratorCan App ${appName} not exists.`); throw new AppError.AppError(`App ${appName} not exists.`); } - logger.debug('collaboratorCan yes'); return data; }); }; diff --git a/routes/accessKeys.js b/routes/accessKeys.js index 8a4ab723..a1b36140 100644 --- a/routes/accessKeys.js +++ b/routes/accessKeys.js @@ -29,7 +29,10 @@ router.post('/', middleware.checkToken, (req, res, next) => { var friendlyName = _.trim(req.body.friendlyName); var ttl = parseInt(req.body.ttl); var description = _.trim(req.body.description); - logger.debug(req.body); + logger.info('try to generate access key', { + uid, + ...req.body, + }); var newAccessKey = security.randToken(28).concat(identical); return accountManager .isExsitAccessKeyName(uid, friendlyName) @@ -58,7 +61,14 @@ router.post('/', middleware.checkToken, (req, res, next) => { description: newToken.description, friendlyName: newToken.name, }; - logger.debug(info); + logger.info('access key created', { + uid, + friendlyName: newToken.name, + }); + logger.debug('access key', { + uid, + ...info, + }); res.send({ accessKey: info }); }) .catch((e) => { diff --git a/routes/account.js b/routes/account.js index 143cd928..6df21c38 100644 --- a/routes/account.js +++ b/routes/account.js @@ -9,7 +9,7 @@ router.get('/', middleware.checkToken, (req, res) => { linkedProviders: [], name: req.users.username, }; - logger.debug(userInfo); + logger.info('check account info', userInfo); res.send({ account: userInfo }); }); diff --git a/routes/apps.js b/routes/apps.js index 1c3338c5..878183fc 100644 --- a/routes/apps.js +++ b/routes/apps.js @@ -665,6 +665,11 @@ router.delete('/:appName/collaborators/:email', middleware.checkToken, (req, res router.delete('/:appName', middleware.checkToken, (req, res, next) => { var appName = _.trim(req.params.appName); var uid = req.users.id; + logger.info('try remove app', { + uid, + appName, + }); + var appManager = new AppManager(); accountManager .ownerCan(uid, appName) @@ -672,6 +677,11 @@ router.delete('/:appName', middleware.checkToken, (req, res, next) => { return appManager.deleteApp(col.appid); }) .then((data) => { + logger.info('remove app success', { + uid, + appName, + }); + res.send(data); }) .catch((e) => { @@ -687,6 +697,11 @@ router.patch('/:appName', middleware.checkToken, (req, res, next) => { var newAppName = _.trim(req.body.name); var appName = _.trim(req.params.appName); var uid = req.users.id; + logger.info('try rename app', { + uid, + appName, + newAppName, + }); if (_.isEmpty(newAppName)) { return res.status(406).send('Please input name!'); } else { @@ -702,6 +717,12 @@ router.patch('/:appName', middleware.checkToken, (req, res, next) => { }); }) .then(() => { + logger.info('rename app success', { + uid, + appName, + newAppName, + }); + res.send(''); }) .catch((e) => { @@ -745,7 +766,11 @@ router.post('/:appName/transfer/:email', middleware.checkToken, (req, res, next) }); router.post('/', middleware.checkToken, (req, res, next) => { - logger.debug('addApp params:', req.body); + var uid = req.users.id; + logger.info('try add app', { + uid, + ...req.body, + }); var constName = require('../core/const'); var appName = req.body.name; if (_.isEmpty(appName)) { @@ -772,7 +797,6 @@ router.post('/', middleware.checkToken, (req, res, next) => { return res.status(406).send('Please input platform [React-Native|Cordova]!'); } var manuallyProvisionDeployments = req.body.manuallyProvisionDeployments; - var uid = req.users.id; var appManager = new AppManager(); appManager @@ -789,6 +813,10 @@ router.post('/', middleware.checkToken, (req, res, next) => { }); }) .then((data) => { + logger.info('add app success', { + uid, + name: appName, + }); res.send({ app: data }); }) .catch((e) => { diff --git a/routes/auth.js b/routes/auth.js index 78ccea8c..0177ebfa 100644 --- a/routes/auth.js +++ b/routes/auth.js @@ -1,9 +1,12 @@ -var express = require('express'); -var router = express.Router(); -var _ = require('lodash'); -var config = require('../core/config'); -var validator = require('validator'); -var { logger } = require('kv-logger'); +const express = require('express'); +const _ = require('lodash'); +const validator = require('validator'); +const { logger } = require('kv-logger'); +const jwt = require('jsonwebtoken'); + +const config = require('../core/config'); + +const router = express.Router(); router.get('/password', (req, res) => { res.render('auth/password', { title: 'CodePushServer' }); @@ -54,18 +57,26 @@ router.post('/login', (req, res, next) => { var account = _.trim(req.body.account); var password = _.trim(req.body.password); var tokenSecret = _.get(config, 'jwt.tokenSecret'); - logger.debug(`login:${account}`); + logger.info('try login', { + account, + }); accountManager .login(account, password) .then((users) => { - var jwt = require('jsonwebtoken'); + logger.info('login success', { + account, + uid: users.id, + }); return jwt.sign( { uid: users.id, hash: security.md5(users.ack_code), expiredIn: 7200 }, tokenSecret, ); }) .then((token) => { - logger.debug(token); + logger.debug('login jwt token', { + account, + token, + }); res.send({ status: 'OK', results: { tokens: token } }); }) .catch((e) => {