Skip to content

Commit 6cb71d9

Browse files
committed
fix(variables): hide sensitive data from terminal output (closes #301)
1 parent 412eb3e commit 6cb71d9

File tree

8 files changed

+115
-62
lines changed

8 files changed

+115
-62
lines changed

src/api/deploy.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ import { Observable, Observer } from 'rxjs';
22
import { s3Deploy } from './deploy/aws-s3';
33
import { codeDeploy } from './deploy/aws-code-deploy';
44
import { elasticDeploy } from './deploy/aws-elastic';
5+
import * as envVars from './env-variables';
56

6-
export function deploy(preferences: any, container: string, variables: string[]): Observable<any> {
7+
export function deploy(
8+
preferences: any, container: string, variables: envVars.EnvVariables
9+
): Observable<any> {
710
return new Observable((observer: Observer<any>) => {
811
if (preferences) {
912
const provider = preferences.provider;
@@ -18,7 +21,9 @@ export function deploy(preferences: any, container: string, variables: string[])
1821
});
1922
}
2023

21-
function deployProvider(provider, preferences, container, variables): Observable<any> {
24+
function deployProvider(
25+
provider: string, preferences: any, container: string, variables: envVars.EnvVariables
26+
): Observable<any> {
2227
switch (provider) {
2328
case 's3':
2429
return s3Deploy(preferences, container, variables);
@@ -37,14 +42,10 @@ function deployProvider(provider, preferences, container, variables): Observable
3742
}
3843
}
3944

40-
export function findFromEnvVariables(variables, property) {
41-
let value = variables.find(v => v.startsWith(property));
42-
43-
if (value) {
44-
const tmp = value.split('=');
45-
if (tmp.length > 1) {
46-
return tmp[1];
47-
}
45+
export function findFromEnvVariables(variables: envVars.EnvVariables, property: string) {
46+
let value = variables[property];
47+
if (typeof value !== 'undefined') {
48+
return value.value;
4849
}
4950

5051
return null;

src/api/deploy/aws-code-deploy.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ import { findFromEnvVariables } from '../deploy';
55
import * as style from 'ansi-styles';
66
import { error } from 'util';
77
import chalk from 'chalk';
8+
import * as envVars from '../env-variables';
89

910
export function codeDeploy(
10-
preferences: any, container: string, variables: string[]
11+
preferences: any, container: string, variables: envVars.EnvVariables
1112
): Observable<any> {
1213
return new Observable((observer: Observer<any>) => {
1314

@@ -89,7 +90,7 @@ export function codeDeploy(
8990
let command = {
9091
type: CommandType.deploy, command: `aws configure set aws_access_key_id ${accessKeyId}`
9192
};
92-
dockerExec(container, command)
93+
dockerExec(container, command, variables)
9394
.toPromise()
9495
.then(result => {
9596
if (!(result && result.data === 0)) {
@@ -103,7 +104,7 @@ export function codeDeploy(
103104
command: `aws configure set aws_secret_access_key ${secretAccessKey}`
104105
};
105106

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

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

140-
return dockerExec(container, command)
141+
return dockerExec(container, command, variables)
141142
.toPromise()
142143
.then(result => {
143144
if (!(result && result.data === 0)) {
@@ -182,7 +183,7 @@ export function codeDeploy(
182183
return Promise.reject(1);
183184
}
184185

185-
return dockerExec(container, command)
186+
return dockerExec(container, command, variables)
186187
.toPromise()
187188
.then(result => {
188189
if (!(result && result.data === 0)) {
@@ -211,7 +212,7 @@ export function codeDeploy(
211212
});
212213
}
213214

214-
function depGroupExists(container, application, group): Promise<any> {
215+
function depGroupExists(container: string, application: string, group: string): Promise<any> {
215216
return new Promise((resolve, reject) => {
216217
const command = `aws deploy get-deployment-group --application-name ${application}`
217218
+ ` --deployment-group ${group}`;

src/api/deploy/aws-elastic.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import { CommandType } from '../config';
44
import { findFromEnvVariables } from '../deploy';
55
import * as style from 'ansi-styles';
66
import chalk from 'chalk';
7+
import * as envVars from '../env-variables';
78

89
export function elasticDeploy(
9-
preferences: any, container: string, variables: string[]
10+
preferences: any, container: string, variables: envVars.EnvVariables
1011
): Observable<any> {
1112
return new Observable((observer: Observer<any>) => {
1213
// 1. check preferences
@@ -106,7 +107,7 @@ export function elasticDeploy(
106107
let command = {
107108
type: CommandType.deploy, command: `aws configure set aws_access_key_id ${accessKeyId}`
108109
};
109-
dockerExec(container, command)
110+
dockerExec(container, command, variables)
110111
.toPromise()
111112
.then(result => {
112113
if (!(result && result.data === 0)) {
@@ -120,7 +121,7 @@ export function elasticDeploy(
120121
command: `aws configure set aws_secret_access_key ${secretAccessKey}`
121122
};
122123

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

136-
return dockerExec(container, command).toPromise();
137+
return dockerExec(container, command, variables).toPromise();
137138
})
138139
.then(result => {
139140
if (!(result && result.data === 0)) {
@@ -160,7 +161,7 @@ export function elasticDeploy(
160161
};
161162
}
162163

163-
return dockerExec(container, command).toPromise();
164+
return dockerExec(container, command, variables).toPromise();
164165
})
165166
.then(() => {
166167
// 3. check if environment exists
@@ -176,7 +177,7 @@ export function elasticDeploy(
176177
+ ` --template-name "${environmentTemplate}"`
177178
};
178179

179-
return dockerExec(container, command)
180+
return dockerExec(container, command, variables)
180181
.toPromise()
181182
.then(result => {
182183
if (!(result && result.data === 0)) {
@@ -194,7 +195,7 @@ export function elasticDeploy(
194195
+ ` --solution-stack-name "${solutionStackName}"`
195196
};
196197

197-
return dockerExec(container, command)
198+
return dockerExec(container, command, variables)
198199
.toPromise()
199200
.then(result => {
200201
if (!(result && result.data === 0)) {
@@ -233,7 +234,7 @@ export function elasticDeploy(
233234
});
234235
}
235236

236-
function environmentExists(container, environment): Promise<any> {
237+
function environmentExists(container: string, environment: string): Promise<any> {
237238
return new Promise((resolve, reject) => {
238239
const getEnvCommand = `aws elasticbeanstalk describe-environments --environment-names`
239240
+ ` "${environment}"`;

src/api/deploy/aws-s3.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import { CommandType } from '../config';
44
import { findFromEnvVariables } from '../deploy';
55
import * as style from 'ansi-styles';
66
import chalk from 'chalk';
7+
import * as envVars from '../env-variables';
78

89
export function s3Deploy(
9-
preferences: any, container: string, variables: string[]
10+
preferences: any, container: string, variables: envVars.EnvVariables
1011
): Observable<any> {
1112
return new Observable((observer: Observer<any>) => {
1213

@@ -75,7 +76,7 @@ export function s3Deploy(
7576
}
7677

7778
return Observable
78-
.concat(...commands.map(command => dockerExec(container, command)))
79+
.concat(...commands.map(command => dockerExec(container, command, variables)))
7980
.toPromise();
8081
})
8182
.then(result => {
@@ -90,7 +91,7 @@ export function s3Deploy(
9091
type: CommandType.deploy, command: `aws configure set aws_access_key_id ${accessKeyId}`
9192
};
9293

93-
return dockerExec(container, command).toPromise();
94+
return dockerExec(container, command, variables).toPromise();
9495
})
9596
.then(result => {
9697
if (!(result && result.data === 0)) {
@@ -104,7 +105,7 @@ export function s3Deploy(
104105
command: `aws configure set aws_secret_access_key ${secretAccessKey}`
105106
};
106107

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

120-
return dockerExec(container, command).toPromise();
121+
return dockerExec(container, command, variables).toPromise();
121122
})
122123
.then(result => {
123124
if (!(result && result.data === 0)) {
@@ -140,7 +141,7 @@ export function s3Deploy(
140141
}
141142

142143
return Observable
143-
.concat(...application.map(command => dockerExec(container, command)))
144+
.concat(...application.map(command => dockerExec(container, command, variables)))
144145
.toPromise();
145146
})
146147
.then(result => {
@@ -158,7 +159,7 @@ export function s3Deploy(
158159
+ ` --s3-location s3://${preferences.bucket}/${zipName}.zip`
159160
};
160161

161-
return dockerExec(container, deploy).toPromise();
162+
return dockerExec(container, deploy, variables).toPromise();
162163
})
163164
.then(result => {
164165
if (!(result && result.data === 0)) {
@@ -183,7 +184,7 @@ export function s3Deploy(
183184
});
184185
}
185186

186-
function appSpecExists(container): Promise<any> {
187+
function appSpecExists(container: string): Promise<any> {
187188
return new Promise((resolve, reject) => {
188189
let appSpec = false;
189190
dockerExec(container, { type: CommandType.deploy, command: 'ls'})
@@ -199,7 +200,7 @@ function appSpecExists(container): Promise<any> {
199200
});
200201
}
201202

202-
function applicationExists(container, application): Promise<any> {
203+
function applicationExists(container: string, application: string): Promise<any> {
203204
return new Promise((resolve, reject) => {
204205
const getApplicationCommand = 'aws deploy list-applications';
205206
let appExists = false;

src/api/docker.ts

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export const docker = new dockerode();
1414
export function createContainer(
1515
name: string,
1616
image: string,
17-
envs: string[]
17+
envs: envVars.EnvVariables
1818
): Observable<ProcessOutput> {
1919
return new Observable(observer => {
2020
docker.createContainer({
@@ -23,7 +23,7 @@ export function createContainer(
2323
Tty: true,
2424
OpenStdin: true,
2525
StdinOnce: false,
26-
Env: envs || [],
26+
Env: envVars.serialize(envs) || [],
2727
Binds: ['/var/run/docker.sock:/var/run/docker.sock'],
2828
Privileged: true,
2929
ExposedPorts: {
@@ -60,7 +60,9 @@ export function startContainer(id: string): Promise<dockerode.Container> {
6060
return docker.getContainer(id).start();
6161
}
6262

63-
export function dockerExec(id: string, cmd: any, env: envVars.EnvVariables = {}): Observable<any> {
63+
export function dockerExec(
64+
id: string, cmd: any, env: envVars.EnvVariables = {}
65+
): Observable<any> {
6466
return new Observable(observer => {
6567
let exitCode = 255;
6668
let command;
@@ -125,6 +127,13 @@ export function dockerExec(id: string, cmd: any, env: envVars.EnvVariables = {})
125127
if (str.includes('//') && str.includes('@')) {
126128
str = str.replace(/\/\/(.*)@/, '//');
127129
}
130+
131+
const variable =
132+
Object.keys(env).find(k => env[k].secure && str.indexOf(env[k].value) >= 0);
133+
if (typeof variable !== 'undefined') {
134+
str = str.replace(env[variable].value, '******');
135+
}
136+
128137
observer.next({ type: 'data', data: str });
129138
}
130139

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

150+
export function dockerPwd(id: string, env: envVars.EnvVariables): Observable<ProcessOutput> {
151+
return new Observable(observer => {
152+
dockerExec(id, { type: CommandType.before_install, command: 'pwd'}, env)
153+
.subscribe(event => {
154+
if (event && event.data && event.type === 'data') {
155+
envVars.set(env, 'ABSTRUSE_BUILD_DIR', event.data.replace('\r\n', ''));
156+
}
157+
},
158+
err => observer.error(err),
159+
() => {
160+
observer.next({ type: 'env', data: env });
161+
observer.complete();
162+
});
163+
});
164+
}
165+
141166
export function listContainers(): Promise<dockerode.ContainerInfo[]> {
142167
return docker.listContainers();
143168
}

0 commit comments

Comments
 (0)