Skip to content

Commit

Permalink
fix: Debug - mask secrets
Browse files Browse the repository at this point in the history
  • Loading branch information
vio committed Dec 10, 2023
1 parent bdf8476 commit 51b25f5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
6 changes: 3 additions & 3 deletions src/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import packageInfo from '../package.json';
import * as LOCALES from '../locales/en';
import send from './send';
import {
debug, getCommitMessage, getEnvVars,
debug, getCommitMessage, getEnvVars, maskObjectProperties,
} from './utils';

const WEBPACK_STATS = 'webpack.stats';
Expand Down Expand Up @@ -54,7 +54,7 @@ export const agent = (artifactsData, config, args = {}, logger = console) => {
endpoint: envVars.endpoint,
};

debug('normalized env vars ', normalizedEnvVars);
debug('normalized env vars ', maskObjectProperties(normalizedEnvVars, ['key']));

const { includeCommitMessage } = config;

Expand All @@ -68,7 +68,7 @@ export const agent = (artifactsData, config, args = {}, logger = console) => {
...includeCommitMessage && !args.commitMessage && { commitMessage: getCommitMessage() },
};

debug('Job parameters', params);
debug('Job parameters', maskObjectProperties(params, ['key']));

// Validate parameters
if (!params.key) {
Expand Down
4 changes: 2 additions & 2 deletions src/send.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { get } from 'lodash';
import fetch from 'isomorphic-fetch';

import LOCALES from '../locales/en';
import { debug } from './utils';
import { debug, maskObjectProperties } from './utils';

export default async (data, params, config, logger) => {
const {
Expand Down Expand Up @@ -41,7 +41,7 @@ export default async (data, params, config, logger) => {

const { payloadFilepath } = config;

debug('Payload', payload);
debug('Payload', maskObjectProperties(payload, ['key']));
debug('Payload size', Buffer.byteLength(JSON.stringify(payload)));

if (payloadFilepath) {
Expand Down
35 changes: 32 additions & 3 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,39 @@
import childProcess from 'child_process';
import envCI from 'env-ci';
import getDebug from 'debug';
import get from 'lodash/get';
import set from 'lodash/set';
import merge from 'lodash/merge';

const DEFAULT_ENDPOINT = 'https://api.relative-ci.com/save';
const DEBUG_NAME = 'relative-ci:agent';
const MASK = '******';

export const debug = getDebug('relative-ci:agent');
export const debug = getDebug(DEBUG_NAME);

/**
* @param {string|number} token
* @returns {string}
*/
export function maskToken(token) {
const text = token.toString();
return `${MASK}${text.substring(text.length - 6)}`;
}

/**
* @param {object} data
* @param {Array<string>} propertyPaths
*/
export function maskObjectProperties(data, propertyPaths) {
const normalizedData = merge({}, data);

propertyPaths.forEach((propertyPath) => {
const value = get(normalizedData, propertyPath, '');
set(normalizedData, propertyPath, maskToken(value));
});

return normalizedData;
}

export function getCommitMessage() {
let message = '';
Expand Down Expand Up @@ -96,7 +125,7 @@ export function getEnvVars() {
commit: process.env.RELATIVE_CI_COMMIT,
commitMessage: process.env.RELATIVE_CI_COMMIT_MESSAGE,
};
debug('custom environment variables', customEnvVars);
debug('custom environment variables', maskObjectProperties(customEnvVars, ['key']));

const resolvedEnvVars = {
key: customEnvVars.key,
Expand All @@ -111,7 +140,7 @@ export function getEnvVars() {
commit: customEnvVars.commit || envCIvars.commit,
commitMessage: customEnvVars.commitMessage,
};
debug('resolved environment variables', envCIvars);
debug('resolved environment variables', maskObjectProperties(envCIvars, ['key']));

return resolvedEnvVars;
}

0 comments on commit 51b25f5

Please sign in to comment.