Skip to content

Commit

Permalink
refactor: use interpolate and mask at better places for readability
Browse files Browse the repository at this point in the history
  • Loading branch information
David Buday committed Nov 9, 2024
1 parent 9132578 commit 71856c8
Showing 1 changed file with 36 additions and 37 deletions.
73 changes: 36 additions & 37 deletions app/Traits/ExecuteRemoteCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,45 +49,44 @@ public function execute_remote_command(...$commands)
}
$remote_command = SshMultiplexingHelper::generateSshCommand($this->server, $command);
$process = Process::timeout(3600)->idleTimeout(3600)->start($remote_command, function (string $type, string $output) use ($command, $secrets, $hidden, $customType, $append) {
$output = str($output)->trim();
if (count($secrets) > 0) {
$output = $this->maskSecrets($output, $secrets);
}
if (str($output)->startsWith('')) {
$output = "\n" . $output;
}
$output = str($output)->trim();
if (count($secrets) > 0) {
$output = $this->maskSecrets($output, $secrets);
$command = $this->maskSecrets($command, $secrets);
}
if (str($output)->startsWith('')) {
$output = "\n" . $output;
}
$new_log_entry = [
'command' => remove_iip($command),
'output' => remove_iip($output),
'type' => $customType ?? $type === 'err' ? 'stderr' : 'stdout',
'timestamp' => Carbon::now('UTC'),
'hidden' => $hidden,
'batch' => static::$batch_counter,
];
if (! $this->application_deployment_queue->logs) {
$new_log_entry['order'] = 1;
} else {
$previous_logs = json_decode($this->application_deployment_queue->logs, associative: true, flags: JSON_THROW_ON_ERROR);
$new_log_entry['order'] = count($previous_logs) + 1;
}
$previous_logs[] = $new_log_entry;
$this->application_deployment_queue->logs = json_encode($previous_logs, flags: JSON_THROW_ON_ERROR);
$this->application_deployment_queue->save();

// Prepare log entry with masked command and output
$new_log_entry = [
'command' => remove_iip($this->maskSecrets($this->interpolateCommand($command, $secrets), $secrets)),
'output' => remove_iip($output),
'type' => $customType ?? $type === 'err' ? 'stderr' : 'stdout',
'timestamp' => Carbon::now('UTC'),
'hidden' => $hidden,
'batch' => static::$batch_counter,
];
if (! $this->application_deployment_queue->logs) {
$new_log_entry['order'] = 1;
} else {
$previous_logs = json_decode($this->application_deployment_queue->logs, associative: true, flags: JSON_THROW_ON_ERROR);
$new_log_entry['order'] = count($previous_logs) + 1;
if ($this->save) {
if (data_get($this->saved_outputs, $this->save, null) === null) {
data_set($this->saved_outputs, $this->save, str());
}
$previous_logs[] = $new_log_entry;
$this->application_deployment_queue->logs = json_encode($previous_logs, flags: JSON_THROW_ON_ERROR);
$this->application_deployment_queue->save();

if ($this->save) {
if (data_get($this->saved_outputs, $this->save, null) === null) {
data_set($this->saved_outputs, $this->save, str());
}
if ($append) {
$this->saved_outputs[$this->save] .= str($output)->trim();
if ($append) {
$this->saved_outputs[$this->save] .= str($output)->trim();
$this->saved_outputs[$this->save] = str($this->saved_outputs[$this->save]);
} else {
$this->saved_outputs[$this->save] = str($output)->trim();
}
} else {
$this->saved_outputs[$this->save] = str($output)->trim();
}
});
}
});
$this->application_deployment_queue->update([
'current_process_id' => $process->id(),
]);
Expand Down Expand Up @@ -117,7 +116,7 @@ private function interpolateCommand(string $command, array $secrets): string
private function maskSecrets(string $text, array $secrets): string
{
// Sort secrets by length descending to prevent partial masking
usort($secrets, function($a, $b) {
usort($secrets, function ($a, $b) {
return strlen($b) - strlen($a);
});

Expand All @@ -127,4 +126,4 @@ private function maskSecrets(string $text, array $secrets): string
}
return $text;
}
}
}

0 comments on commit 71856c8

Please sign in to comment.