Skip to content

Commit 4766812

Browse files
authored
feat(deployment): add command_hidden flag to hide command text in logs (#9167)
2 parents 944a038 + 333cc95 commit 4766812

2 files changed

Lines changed: 16 additions & 11 deletions

File tree

app/Jobs/ApplicationDeploymentJob.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,7 @@ private function deploy_docker_compose_buildpack()
783783

784784
try {
785785
$this->execute_remote_command(
786-
[executeInDocker($this->deployment_uuid, "cd {$this->workdir} && {$start_command}"), 'hidden' => true],
786+
[executeInDocker($this->deployment_uuid, "cd {$this->workdir} && {$start_command}"), 'hidden' => false, 'type' => 'stdout', 'command_hidden' => true],
787787
);
788788
} catch (\RuntimeException $e) {
789789
if (str_contains($e->getMessage(), "matching `'") || str_contains($e->getMessage(), 'unexpected EOF')) {
@@ -801,7 +801,7 @@ private function deploy_docker_compose_buildpack()
801801
$command .= " --env-file {$server_workdir}/.env";
802802
$command .= " --project-directory {$server_workdir} -f {$server_workdir}{$this->docker_compose_location} up -d";
803803
$this->execute_remote_command(
804-
['command' => $command, 'hidden' => true],
804+
['command' => $command, 'hidden' => false, 'type' => 'stdout', 'command_hidden' => true],
805805
);
806806
}
807807
} else {
@@ -818,11 +818,11 @@ private function deploy_docker_compose_buildpack()
818818
$this->write_deployment_configurations();
819819
if ($this->preserveRepository) {
820820
$this->execute_remote_command(
821-
['command' => "cd {$server_workdir} && {$start_command}", 'hidden' => true],
821+
['command' => "cd {$server_workdir} && {$start_command}", 'hidden' => false, 'type' => 'stdout', 'command_hidden' => true],
822822
);
823823
} else {
824824
$this->execute_remote_command(
825-
[executeInDocker($this->deployment_uuid, "cd {$this->basedir} && {$start_command}"), 'hidden' => true],
825+
[executeInDocker($this->deployment_uuid, "cd {$this->basedir} && {$start_command}"), 'hidden' => false, 'type' => 'stdout', 'command_hidden' => true],
826826
);
827827
}
828828
} else {
@@ -834,14 +834,14 @@ private function deploy_docker_compose_buildpack()
834834
$this->write_deployment_configurations();
835835

836836
$this->execute_remote_command(
837-
['command' => $command, 'hidden' => true],
837+
['command' => $command, 'hidden' => false, 'type' => 'stdout', 'command_hidden' => true],
838838
);
839839
} else {
840840
// Always use .env file
841841
$command .= " --env-file {$this->workdir}/.env";
842842
$command .= " --project-name {$this->application->uuid} --project-directory {$this->workdir} -f {$this->workdir}{$this->docker_compose_location} up -d";
843843
$this->execute_remote_command(
844-
[executeInDocker($this->deployment_uuid, $command), 'hidden' => true],
844+
[executeInDocker($this->deployment_uuid, $command), 'hidden' => false, 'type' => 'stdout', 'command_hidden' => true],
845845
);
846846
$this->write_deployment_configurations();
847847
}

app/Traits/ExecuteRemoteCommand.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ public function execute_remote_command(...$commands)
7878
$customType = data_get($single_command, 'type');
7979
$ignore_errors = data_get($single_command, 'ignore_errors', false);
8080
$append = data_get($single_command, 'append', true);
81+
$command_hidden = data_get($single_command, 'command_hidden', false);
8182
$this->save = data_get($single_command, 'save');
8283
if ($this->server->isNonRoot()) {
8384
if (str($command)->startsWith('docker exec')) {
@@ -102,7 +103,7 @@ public function execute_remote_command(...$commands)
102103

103104
while ($attempt < $maxRetries && ! $commandExecuted) {
104105
try {
105-
$this->executeCommandWithProcess($command, $hidden, $customType, $append, $ignore_errors);
106+
$this->executeCommandWithProcess($command, $hidden, $customType, $append, $ignore_errors, $command_hidden);
106107
$commandExecuted = true;
107108
} catch (\RuntimeException|DeploymentException $e) {
108109
$lastError = $e;
@@ -152,10 +153,14 @@ public function execute_remote_command(...$commands)
152153
/**
153154
* Execute the actual command with process handling
154155
*/
155-
private function executeCommandWithProcess($command, $hidden, $customType, $append, $ignore_errors)
156+
private function executeCommandWithProcess($command, $hidden, $customType, $append, $ignore_errors, $command_hidden = false)
156157
{
158+
if ($command_hidden && isset($this->application_deployment_queue)) {
159+
$this->application_deployment_queue->addLogEntry('[CMD]: '.$this->redact_sensitive_info($command), hidden: true);
160+
}
161+
157162
$remote_command = SshMultiplexingHelper::generateSshCommand($this->server, $command);
158-
$process = Process::timeout(config('constants.ssh.command_timeout'))->idleTimeout(3600)->start($remote_command, function (string $type, string $output) use ($command, $hidden, $customType, $append) {
163+
$process = Process::timeout(config('constants.ssh.command_timeout'))->idleTimeout(3600)->start($remote_command, function (string $type, string $output) use ($command, $hidden, $customType, $append, $command_hidden) {
159164
$output = str($output)->trim();
160165
if ($output->startsWith('')) {
161166
$output = "\n".$output;
@@ -165,9 +170,9 @@ private function executeCommandWithProcess($command, $hidden, $customType, $appe
165170
$sanitized_output = sanitize_utf8_text($output);
166171

167172
$new_log_entry = [
168-
'command' => $this->redact_sensitive_info($command),
173+
'command' => $command_hidden ? null : $this->redact_sensitive_info($command),
169174
'output' => $this->redact_sensitive_info($sanitized_output),
170-
'type' => $customType ?? $type === 'err' ? 'stderr' : 'stdout',
175+
'type' => $customType ?? ($type === 'err' ? 'stderr' : 'stdout'),
171176
'timestamp' => Carbon::now('UTC'),
172177
'hidden' => $hidden,
173178
'batch' => static::$batch_counter,

0 commit comments

Comments
 (0)