Skip to content

Commit

Permalink
fix(variables): hide sensitive data from terminal output (closes #301)
Browse files Browse the repository at this point in the history
  • Loading branch information
Izak88 committed Dec 21, 2017
1 parent 412eb3e commit 6cb71d9
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 62 deletions.
21 changes: 11 additions & 10 deletions src/api/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ import { Observable, Observer } from 'rxjs';
import { s3Deploy } from './deploy/aws-s3';
import { codeDeploy } from './deploy/aws-code-deploy';
import { elasticDeploy } from './deploy/aws-elastic';
import * as envVars from './env-variables';

export function deploy(preferences: any, container: string, variables: string[]): Observable<any> {
export function deploy(
preferences: any, container: string, variables: envVars.EnvVariables
): Observable<any> {
return new Observable((observer: Observer<any>) => {
if (preferences) {
const provider = preferences.provider;
Expand All @@ -18,7 +21,9 @@ export function deploy(preferences: any, container: string, variables: string[])
});
}

function deployProvider(provider, preferences, container, variables): Observable<any> {
function deployProvider(
provider: string, preferences: any, container: string, variables: envVars.EnvVariables
): Observable<any> {
switch (provider) {
case 's3':
return s3Deploy(preferences, container, variables);
Expand All @@ -37,14 +42,10 @@ function deployProvider(provider, preferences, container, variables): Observable
}
}

export function findFromEnvVariables(variables, property) {
let value = variables.find(v => v.startsWith(property));

if (value) {
const tmp = value.split('=');
if (tmp.length > 1) {
return tmp[1];
}
export function findFromEnvVariables(variables: envVars.EnvVariables, property: string) {
let value = variables[property];
if (typeof value !== 'undefined') {
return value.value;
}

return null;
Expand Down
15 changes: 8 additions & 7 deletions src/api/deploy/aws-code-deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import { findFromEnvVariables } from '../deploy';
import * as style from 'ansi-styles';
import { error } from 'util';
import chalk from 'chalk';
import * as envVars from '../env-variables';

export function codeDeploy(
preferences: any, container: string, variables: string[]
preferences: any, container: string, variables: envVars.EnvVariables
): Observable<any> {
return new Observable((observer: Observer<any>) => {

Expand Down Expand Up @@ -89,7 +90,7 @@ export function codeDeploy(
let command = {
type: CommandType.deploy, command: `aws configure set aws_access_key_id ${accessKeyId}`
};
dockerExec(container, command)
dockerExec(container, command, variables)
.toPromise()
.then(result => {
if (!(result && result.data === 0)) {
Expand All @@ -103,7 +104,7 @@ export function codeDeploy(
command: `aws configure set aws_secret_access_key ${secretAccessKey}`
};

return dockerExec(container, command).toPromise();
return dockerExec(container, command, variables).toPromise();
})
.then(result => {
if (!(result && result.data === 0)) {
Expand All @@ -116,7 +117,7 @@ export function codeDeploy(
type: CommandType.deploy, command: `aws configure set region ${region}`
};

return dockerExec(container, command).toPromise();
return dockerExec(container, command, variables).toPromise();
})
.then(result => {
if (!(result && result.data === 0)) {
Expand All @@ -137,7 +138,7 @@ export function codeDeploy(
+ ` --deployment-group-name ${deployGroup} --service-role-arn ${arn}`
};

return dockerExec(container, command)
return dockerExec(container, command, variables)
.toPromise()
.then(result => {
if (!(result && result.data === 0)) {
Expand Down Expand Up @@ -182,7 +183,7 @@ export function codeDeploy(
return Promise.reject(1);
}

return dockerExec(container, command)
return dockerExec(container, command, variables)
.toPromise()
.then(result => {
if (!(result && result.data === 0)) {
Expand Down Expand Up @@ -211,7 +212,7 @@ export function codeDeploy(
});
}

function depGroupExists(container, application, group): Promise<any> {
function depGroupExists(container: string, application: string, group: string): Promise<any> {
return new Promise((resolve, reject) => {
const command = `aws deploy get-deployment-group --application-name ${application}`
+ ` --deployment-group ${group}`;
Expand Down
17 changes: 9 additions & 8 deletions src/api/deploy/aws-elastic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import { CommandType } from '../config';
import { findFromEnvVariables } from '../deploy';
import * as style from 'ansi-styles';
import chalk from 'chalk';
import * as envVars from '../env-variables';

export function elasticDeploy(
preferences: any, container: string, variables: string[]
preferences: any, container: string, variables: envVars.EnvVariables
): Observable<any> {
return new Observable((observer: Observer<any>) => {
// 1. check preferences
Expand Down Expand Up @@ -106,7 +107,7 @@ export function elasticDeploy(
let command = {
type: CommandType.deploy, command: `aws configure set aws_access_key_id ${accessKeyId}`
};
dockerExec(container, command)
dockerExec(container, command, variables)
.toPromise()
.then(result => {
if (!(result && result.data === 0)) {
Expand All @@ -120,7 +121,7 @@ export function elasticDeploy(
command: `aws configure set aws_secret_access_key ${secretAccessKey}`
};

return dockerExec(container, command).toPromise();
return dockerExec(container, command, variables).toPromise();
})
.then(result => {
if (!(result && result.data === 0)) {
Expand All @@ -133,7 +134,7 @@ export function elasticDeploy(
type: CommandType.deploy, command: `aws configure set region ${region}`
};

return dockerExec(container, command).toPromise();
return dockerExec(container, command, variables).toPromise();
})
.then(result => {
if (!(result && result.data === 0)) {
Expand All @@ -160,7 +161,7 @@ export function elasticDeploy(
};
}

return dockerExec(container, command).toPromise();
return dockerExec(container, command, variables).toPromise();
})
.then(() => {
// 3. check if environment exists
Expand All @@ -176,7 +177,7 @@ export function elasticDeploy(
+ ` --template-name "${environmentTemplate}"`
};

return dockerExec(container, command)
return dockerExec(container, command, variables)
.toPromise()
.then(result => {
if (!(result && result.data === 0)) {
Expand All @@ -194,7 +195,7 @@ export function elasticDeploy(
+ ` --solution-stack-name "${solutionStackName}"`
};

return dockerExec(container, command)
return dockerExec(container, command, variables)
.toPromise()
.then(result => {
if (!(result && result.data === 0)) {
Expand Down Expand Up @@ -233,7 +234,7 @@ export function elasticDeploy(
});
}

function environmentExists(container, environment): Promise<any> {
function environmentExists(container: string, environment: string): Promise<any> {
return new Promise((resolve, reject) => {
const getEnvCommand = `aws elasticbeanstalk describe-environments --environment-names`
+ ` "${environment}"`;
Expand Down
19 changes: 10 additions & 9 deletions src/api/deploy/aws-s3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import { CommandType } from '../config';
import { findFromEnvVariables } from '../deploy';
import * as style from 'ansi-styles';
import chalk from 'chalk';
import * as envVars from '../env-variables';

export function s3Deploy(
preferences: any, container: string, variables: string[]
preferences: any, container: string, variables: envVars.EnvVariables
): Observable<any> {
return new Observable((observer: Observer<any>) => {

Expand Down Expand Up @@ -75,7 +76,7 @@ export function s3Deploy(
}

return Observable
.concat(...commands.map(command => dockerExec(container, command)))
.concat(...commands.map(command => dockerExec(container, command, variables)))
.toPromise();
})
.then(result => {
Expand All @@ -90,7 +91,7 @@ export function s3Deploy(
type: CommandType.deploy, command: `aws configure set aws_access_key_id ${accessKeyId}`
};

return dockerExec(container, command).toPromise();
return dockerExec(container, command, variables).toPromise();
})
.then(result => {
if (!(result && result.data === 0)) {
Expand All @@ -104,7 +105,7 @@ export function s3Deploy(
command: `aws configure set aws_secret_access_key ${secretAccessKey}`
};

return dockerExec(container, command).toPromise();
return dockerExec(container, command, variables).toPromise();
})
.then(result => {
if (!(result && result.data === 0)) {
Expand All @@ -117,7 +118,7 @@ export function s3Deploy(
type: CommandType.deploy, command: `aws configure set region ${region}`
};

return dockerExec(container, command).toPromise();
return dockerExec(container, command, variables).toPromise();
})
.then(result => {
if (!(result && result.data === 0)) {
Expand All @@ -140,7 +141,7 @@ export function s3Deploy(
}

return Observable
.concat(...application.map(command => dockerExec(container, command)))
.concat(...application.map(command => dockerExec(container, command, variables)))
.toPromise();
})
.then(result => {
Expand All @@ -158,7 +159,7 @@ export function s3Deploy(
+ ` --s3-location s3://${preferences.bucket}/${zipName}.zip`
};

return dockerExec(container, deploy).toPromise();
return dockerExec(container, deploy, variables).toPromise();
})
.then(result => {
if (!(result && result.data === 0)) {
Expand All @@ -183,7 +184,7 @@ export function s3Deploy(
});
}

function appSpecExists(container): Promise<any> {
function appSpecExists(container: string): Promise<any> {
return new Promise((resolve, reject) => {
let appSpec = false;
dockerExec(container, { type: CommandType.deploy, command: 'ls'})
Expand All @@ -199,7 +200,7 @@ function appSpecExists(container): Promise<any> {
});
}

function applicationExists(container, application): Promise<any> {
function applicationExists(container: string, application: string): Promise<any> {
return new Promise((resolve, reject) => {
const getApplicationCommand = 'aws deploy list-applications';
let appExists = false;
Expand Down
31 changes: 28 additions & 3 deletions src/api/docker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const docker = new dockerode();
export function createContainer(
name: string,
image: string,
envs: string[]
envs: envVars.EnvVariables
): Observable<ProcessOutput> {
return new Observable(observer => {
docker.createContainer({
Expand All @@ -23,7 +23,7 @@ export function createContainer(
Tty: true,
OpenStdin: true,
StdinOnce: false,
Env: envs || [],
Env: envVars.serialize(envs) || [],
Binds: ['/var/run/docker.sock:/var/run/docker.sock'],
Privileged: true,
ExposedPorts: {
Expand Down Expand Up @@ -60,7 +60,9 @@ export function startContainer(id: string): Promise<dockerode.Container> {
return docker.getContainer(id).start();
}

export function dockerExec(id: string, cmd: any, env: envVars.EnvVariables = {}): Observable<any> {
export function dockerExec(
id: string, cmd: any, env: envVars.EnvVariables = {}
): Observable<any> {
return new Observable(observer => {
let exitCode = 255;
let command;
Expand Down Expand Up @@ -125,6 +127,13 @@ export function dockerExec(id: string, cmd: any, env: envVars.EnvVariables = {})
if (str.includes('//') && str.includes('@')) {
str = str.replace(/\/\/(.*)@/, '//');
}

const variable =
Object.keys(env).find(k => env[k].secure && str.indexOf(env[k].value) >= 0);
if (typeof variable !== 'undefined') {
str = str.replace(env[variable].value, '******');
}

observer.next({ type: 'data', data: str });
}

Expand All @@ -138,6 +147,22 @@ export function dockerExec(id: string, cmd: any, env: envVars.EnvVariables = {})
});
}

export function dockerPwd(id: string, env: envVars.EnvVariables): Observable<ProcessOutput> {
return new Observable(observer => {
dockerExec(id, { type: CommandType.before_install, command: 'pwd'}, env)
.subscribe(event => {
if (event && event.data && event.type === 'data') {
envVars.set(env, 'ABSTRUSE_BUILD_DIR', event.data.replace('\r\n', ''));
}
},
err => observer.error(err),
() => {
observer.next({ type: 'env', data: env });
observer.complete();
});
});
}

export function listContainers(): Promise<dockerode.ContainerInfo[]> {
return docker.listContainers();
}
Expand Down
Loading

0 comments on commit 6cb71d9

Please sign in to comment.