Skip to content

Commit

Permalink
misc: filter data in logs
Browse files Browse the repository at this point in the history
  • Loading branch information
embbnux committed Jul 4, 2024
1 parent 7dde406 commit eb2f5d0
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 17 deletions.
3 changes: 2 additions & 1 deletion src/server/handlers/authorizationHandler.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { User } = require('../models/userModel');
const { Subscription } = require('../models/subscriptionModel');
const { GoogleClient } = require('../lib/GoogleClient');
const { errorLogger } = require('../lib/logger');

async function onAuthorize(accessToken, refreshToken, expires) {
const googleClient = new GoogleClient({ token: accessToken });
Expand Down Expand Up @@ -48,7 +49,7 @@ async function onUnauthorize(user) {
await Subscription.destroy({ where: { id: subscription.id } });
} catch (e) {
console.error('Failed to delete watch: ', subscription.id);
console.error(e && e.message);
errorLogger(e);
}
}));
await googleClient.revokeToken(user.refreshToken || user.accessToken);
Expand Down
3 changes: 2 additions & 1 deletion src/server/handlers/notificationHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const { sendAdaptiveCardMessage } = require('../lib/messageHelper');
const { checkAndRefreshAccessToken } = require('../lib/oauth');
const { formatGoogleFormResponseIntoCard } = require('../lib/formatGoogleFormResponse');
const { GoogleClient } = require('../lib/GoogleClient');
const { errorLogger } = require('../lib/logger');

async function onReceiveNotification(subscription, messageTime) {
const userId = subscription.userId;
Expand Down Expand Up @@ -52,7 +53,7 @@ async function onReceiveNotification(subscription, messageTime) {
}
} catch (e) {
console.error('Error sending message to RC Webhook:');
console.error(e && e.message);
errorLogger(e);
}
}));
} else if (subscription.rcWebhookUri) {
Expand Down
9 changes: 3 additions & 6 deletions src/server/handlers/subscriptionHandler.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { GoogleClient } = require('../lib/GoogleClient');
const { Subscription } = require('../models/subscriptionModel');
const { errorLogger } = require('../lib/logger');

async function onSubscribe(user, rcWebhookId, rcWebhookUri, formIds) {
let userSubscriptions = [...user.subscriptions];
Expand Down Expand Up @@ -52,12 +53,8 @@ async function onSubscribe(user, rcWebhookId, rcWebhookUri, formIds) {
rcWebhookId,
});
} catch (e) {
console.error(`Subscription error for user ${user.id} and form ${formId}`);
console.error(e && e.message);
if (e.response) {
console.error(e.response.status);
console.error(e.response.data);
}
console.error('Subscription error');
errorLogger(e && e.message);
}
}));
user.subscriptions = userSubscriptions;
Expand Down
24 changes: 23 additions & 1 deletion src/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,29 @@ const constants = require('./lib/constants');
const { checkAuth } = require('./middlewares/auth');

const app = express()
app.use(morgan('tiny'))
app.use(morgan(function (tokens, req, res) {
let url = tokens.url(req, res);
const rcWebhookUri = req.query.rcWebhookUri || req.query.webhook;
if (rcWebhookUri) {
const webhookId = rcWebhookUri.split('/').pop();
if (webhookId) {
url = url.replace(webhookId, '[MASK]');
}
}
if (url.indexOf('/get-form-data') === 0) {
const formIds = req.query.formIds;
if (formIds) {
url = url.replace(formIds, '[MASK]');
}
}
return [
tokens.method(req, res),
url,
tokens.status(req, res),
tokens.res(req, res, 'content-length'), '-',
tokens['response-time'](req, res), 'ms'
].join(' ');
}));
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())

Expand Down
3 changes: 2 additions & 1 deletion src/server/lib/analytics.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const Mixpanel = require('mixpanel');
const { errorLogger } = require('./logger');

class Analytics {
constructor({
Expand Down Expand Up @@ -33,7 +34,7 @@ class Analytics {
try {
await this._track(event, properties);
} catch (e) {
console.error(e && e.message);
errorLogger(e);
}
}
}
Expand Down
33 changes: 33 additions & 0 deletions src/server/lib/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

function logErrorResponse(response) {
const status = response.status;
const data = response.data;
const headers = response.headers || {};
const rcRequestId = headers['rcrequestid'];
console.error(
'Response error: ', status,
' RequestId: ', rcRequestId,
' Data: ',
data
);
}

function errorLogger(error) {
if (!error) {
return;
}
if (error.response) {
// https://axios-http.com/docs/handling_errors
// The request was made and the server responded with a status code
logErrorResponse(error.response);
return;
}
if (error.request) {
// The request was made but no response was received
console.error('Request error: ', error.code);
return;
}
console.error('Error: ', error.message);
}

exports.errorLogger = errorLogger;
7 changes: 4 additions & 3 deletions src/server/routes/authorization.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const { onAuthorize, onUnauthorize } = require('../handlers/authorizationHandler
const { checkAndRefreshAccessToken } = require('../lib/oauth');
const { getRCWebhookId } = require('../lib/getRCWebhookId');
const { GoogleClient } = require('../lib/GoogleClient');
const { errorLogger } = require('../lib/logger');

async function openAuthPage(req, res) {
const url = GoogleClient.authorizationUrl();
Expand Down Expand Up @@ -39,7 +40,7 @@ async function getUserInfo(req, res) {
res.send('Unauthorized.');
return;
}
console.error(e && e.message);
errorLogger(e);
res.status(500);
res.send('Internal error');
return;
Expand Down Expand Up @@ -95,7 +96,7 @@ async function generateToken(req, res) {
res.send('invalid scope');
return;
}
console.error(e && e.message);
errorLogger(e);
res.status(500);
res.send('internal error');
}
Expand Down Expand Up @@ -149,7 +150,7 @@ async function revokeToken(req, res) {
authorized: false,
});
} catch (e) {
console.error(e && e.message);
errorLogger(e);
res.status(500);
res.send('internal error');
}
Expand Down
3 changes: 2 additions & 1 deletion src/server/routes/notification.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { Subscription } = require('../models/subscriptionModel');
const { onReceiveNotification } = require('../handlers/notificationHandler');
const { errorLogger } = require('../lib/logger');

async function notification(req, res) {
// console.log(JSON.stringify(req.body, null, 2));
Expand All @@ -23,7 +24,7 @@ async function notification(req, res) {
result: 'ok',
});
} catch (e) {
console.error(e && e.message);
errorLogger(e);
res.status(200);
res.json({
result: 'error',
Expand Down
7 changes: 4 additions & 3 deletions src/server/routes/subscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const { onSubscribe, onDeleteSubscription } = require('../handlers/subscriptionH
const { GoogleClient } = require('../lib/GoogleClient');
const { checkAndRefreshAccessToken } = require('../lib/oauth');
const { getRCWebhookId } = require('../lib/getRCWebhookId');
const { errorLogger } = require('../lib/logger');

async function getFormData(req, res) {
const userId = req.currentUserId;
Expand Down Expand Up @@ -38,7 +39,7 @@ async function getFormData(req, res) {
forms,
});
} catch (e) {
console.error(e && e.message);
errorLogger(e && e.message);
if (e.response) {
if (e.response.status === 401) {
if (user) {
Expand Down Expand Up @@ -126,7 +127,7 @@ async function subscribe(req, res) {
res.send('Unauthorized');
return;
}
console.error(e && e.message);
errorLogger(e && e.message);
res.status(500);
res.send('Internal server error');
return;
Expand Down Expand Up @@ -190,7 +191,7 @@ async function deleteSubscription(req, res) {
res.send('Unauthorized');
return;
}
console.error(e && e.message);
errorLogger(e && e.message);
res.status(500);
res.send('Internal server error');
}
Expand Down
7 changes: 7 additions & 0 deletions tests/view.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ describe('Setup', () => {
expect(res.text).toContain(`${process.env.ASSETS_PATH}/app.js`);
});

it('should get 200 when visit setup page with webhook', async () => {
const res = await request(server).get('/setup?webhook=https://example.com/webhooks/xxxx');
expect(res.status).toEqual(200);
expect(res.text).toContain('window.clientConfig');
expect(res.text).toContain(`${process.env.ASSETS_PATH}/app.js`);
});

it('should get 200 when visit home page', async () => {
const res = await request(server).get('/home');
expect(res.status).toEqual(200);
Expand Down

0 comments on commit eb2f5d0

Please sign in to comment.