From 9fc83cd171af8817c79643608546492c5d388de0 Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@keylocation.sg>
Date: Wed, 18 Jan 2017 14:14:07 +0100
Subject: [PATCH] Redact token in logs

commit e6ca456e63f9f42089275ceb14e42442d8cbe8eb
Author: Rhys Arkins <rhys@keylocation.sg>
Date:   Wed Jan 18 14:11:53 2017 +0100

    Redact string

commit 154367927f60a895708ef0f83192342bbf7a4093
Author: Rhys Arkins <rhys@keylocation.sg>
Date:   Wed Jan 18 14:02:12 2017 +0100

    Redact config
---
 lib/config/index.js | 22 ++++++++++++++++------
 package.json        |  1 +
 2 files changed, 17 insertions(+), 6 deletions(-)

diff --git a/lib/config/index.js b/lib/config/index.js
index 2b394074c7..e24189da4c 100644
--- a/lib/config/index.js
+++ b/lib/config/index.js
@@ -1,5 +1,6 @@
 const logger = require('winston');
 const program = require('commander');
+const stringify = require('json-stringify-pretty-compact');
 
 let config = null;
 
@@ -14,14 +15,14 @@ function parseConfigs() {
   const envConfig = require('./env');
   /* eslint-enable global-require */
 
-  logger.debug(`Default config = ${JSON.stringify(defaultConfig)}`);
-  logger.debug(`File config = ${JSON.stringify(fileConfig)}`);
-  logger.debug(`CLI config: ${JSON.stringify(cliConfig)}`);
-  logger.debug(`Env config: ${JSON.stringify(envConfig)}`);
+  logger.debug(`Default config = ${redact(defaultConfig)}`);
+  logger.debug(`File config = ${redact(fileConfig)}`);
+  logger.debug(`CLI config: ${redact(cliConfig)}`);
+  logger.debug(`Env config: ${redact(envConfig)}`);
 
   // Get global config
   config = Object.assign({}, defaultConfig, fileConfig, envConfig, cliConfig);
-  logger.debug(`raw config=${JSON.stringify(config)}`);
+  logger.debug(`raw config=${redact(config)}`);
 
   // Set log level
   logger.level = config.logLevel;
@@ -63,7 +64,7 @@ function parseConfigs() {
     });
   });
   // Print config
-  logger.verbose(`config=${JSON.stringify(config)}`);
+  logger.verbose(`config=${redact(config)}`);
 }
 
 function getCascadedConfig(repo, packageFile) {
@@ -81,6 +82,15 @@ function getGlobalConfig() {
   return config;
 }
 
+function redact(inputConfig) {
+  const tokenConfig = {};
+  if (inputConfig.token) {
+    tokenConfig.token = `${inputConfig.token.substr(0, 4)}${new Array(inputConfig.token.length - 3).join('*')}`;
+  }
+  const redactedConfig = Object.assign({}, inputConfig, tokenConfig);
+  return stringify(redactedConfig);
+}
+
 module.exports = {
   getCascadedConfig,
   getGlobalConfig,
diff --git a/package.json b/package.json
index f47cc8a169..2fe91c2a4e 100644
--- a/package.json
+++ b/package.json
@@ -38,6 +38,7 @@
     "gh-got": "5.0.0",
     "got": "6.7.1",
     "handlebars": "4.0.6",
+    "json-stringify-pretty-compact": "^1.0.2",
     "lodash": "4.17.4",
     "semver": "5.3.0",
     "semver-stable": "2.0.4",
-- 
GitLab