Skip to content

Commit

Permalink
Add loggingMode option, to allow less verbose logs
Browse files Browse the repository at this point in the history
  • Loading branch information
mikemintz committed Feb 1, 2016
1 parent 14ee2dd commit 3959e81
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 9 deletions.
15 changes: 11 additions & 4 deletions src/Connection.js
Expand Up @@ -10,9 +10,10 @@ import url from 'url';
// start() is called, it will forward traffic in both directions until either
// side disconnects. Each incoming WebSocket spawns a new outgoing TCP socket.
export class Connection {
constructor(queryValidator, webSocket) {
constructor(queryValidator, webSocket, loggingMode) {
this.queryValidator = queryValidator;
this.webSocket = webSocket;
this.loggingMode = loggingMode;
this.remoteAddress = webSocket._socket.remoteAddress;
this.remotePort = webSocket._socket.remotePort;
}
Expand All @@ -28,7 +29,9 @@ export class Connection {
this.dbSocket = net.createConnection(dbPort, dbHost);
this.setupDbSocket();
this.setupWebSocket();
this.log('Connect');
if (this.loggingMode === 'all') {
this.log('Connect');
}
}

setupDbSocket() {
Expand All @@ -46,7 +49,9 @@ export class Connection {
this.cleanup();
});
this.dbSocket.on('close', () => {
this.log('dbSocket closed');
if (this.loggingMode === 'all') {
this.log('dbSocket closed');
}
this.cleanup();
});
this.dbSocket.on('error', e => {
Expand All @@ -65,7 +70,9 @@ export class Connection {
}
});
this.webSocket.on('close', () => {
this.log('webSocket closed');
if (this.loggingMode === 'all') {
this.log('webSocket closed');
}
this.cleanup();
});
this.webSocket.on('error', e => {
Expand Down
18 changes: 15 additions & 3 deletions src/QueryValidator.js
Expand Up @@ -80,9 +80,10 @@ const queryMatches = (patternRQ, actualRQ, session) => {


export class QueryValidator {
constructor({queryWhitelist, unsafelyAllowAnyQuery}) {
constructor({queryWhitelist, unsafelyAllowAnyQuery, loggingMode}) {
this.queryWhitelist = queryWhitelist;
this.unsafelyAllowAnyQuery = unsafelyAllowAnyQuery;
this.loggingMode = loggingMode;
}

// Return a promise that resolves to true or false if the specified RQ
Expand Down Expand Up @@ -113,6 +114,8 @@ export class QueryValidator {
// whether they were allowed. Also log a pretty parsed version of queries
// missing from the whitelist so that developers can easily copy those into
// their whitelist source file as they write new queries in the frontend.
// Logging behavior depends on the value of loggingMode (see comment in
// index.js).
validateQuery(token, query, queryOptions, session, logFn) {
return Promise.try(reqlJsonToAst, [{query, queryOptions}]).then(ast => {
return Promise.try(parseQuery, [query, queryOptions]).then(rq => {
Expand All @@ -128,7 +131,14 @@ export class QueryValidator {
if (!inWhitelist) {
logMsgParts.push('\n\n', colors.cyan(rqToString(rq, 1, 2)), ',\n');
}
logFn(logMsgParts.join(''), token);
const shouldLog = (
this.loggingMode === 'all'
) || (
this.loggingMode === 'denied' && !inWhitelist
);
if (shouldLog) {
logFn(logMsgParts.join(''), token);
}
return Promise.resolve(allow);
});
});
Expand All @@ -150,7 +160,9 @@ export class QueryValidator {
return this.validateQuery(token, query, queryOptions, session, logFn);
} else if (type === CONTINUE || type === STOP || type === NOREPLY_WAIT) {
if (query === undefined && queryOptions === undefined) {
logFn(queryTypeString(type), token);
if (this.loggingMode === 'all') {
logFn(queryTypeString(type), token);
}
return true;
} else {
throw new Error(`Invalid ${queryTypeString(type)}`);
Expand Down
14 changes: 12 additions & 2 deletions src/index.js
Expand Up @@ -47,15 +47,25 @@ export function listen({
// validation.
sessionCreator = (urlQueryParams) => Promise.resolve({}),

// Specify how much to log to the console
// - 'all' logs everything
// - 'denied' logs queries that aren't in the query whitelist
// - 'none' logs nothing, other than exception stack traces
loggingMode = 'all',

}) {
const wsServer = new WebSocketServer({
server: httpServer,
path: httpPath,
perMessageDeflate: false, // necessary due to https://github.com/websockets/ws/issues/523
});
const queryValidator = new QueryValidator({queryWhitelist, unsafelyAllowAnyQuery});
const queryValidator = new QueryValidator({
queryWhitelist,
unsafelyAllowAnyQuery,
loggingMode,
});
wsServer.on('connection', webSocket => {
const connection = new Connection(queryValidator, webSocket);
const connection = new Connection(queryValidator, webSocket, loggingMode);
connection.start({sessionCreator, dbHost, dbPort});
});
}
1 change: 1 addition & 0 deletions test/index.spec.js
Expand Up @@ -32,6 +32,7 @@ function setupInstances({
queryWhitelist,
unsafelyAllowAnyQuery,
sessionCreator,
loggingMode: 'none',
};
WsServer.listen(wsServerOpts);
httpServer.on('listening', () => {
Expand Down

0 comments on commit 3959e81

Please sign in to comment.