From 54a57d217f81ff847b9af77e2222beac3bcf9b6f Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Fri, 15 Sep 2023 12:30:25 +0200 Subject: [PATCH] fix: ssh-agent revert --- app/Actions/CoolifyTask/RunRemoteProcess.php | 2 +- app/Jobs/ApplicationDeploymentJob.php | 2 +- app/Traits/ExecuteRemoteCommand.php | 2 +- bootstrap/helpers/remoteProcess.php | 69 +++++++++---------- config/coolify.php | 1 - docker-compose.dev.yml | 1 - docker-compose.prod.yml | 1 - .../etc/s6-overlay/s6-rc.d/ssh-agent/type | 1 - .../etc/s6-overlay/s6-rc.d/ssh-agent/up | 5 -- .../s6-rc.d/user/contents.d/ssh-agent | 0 .../etc/s6-overlay/s6-rc.d/ssh-agent/type | 1 - .../etc/s6-overlay/s6-rc.d/ssh-agent/up | 5 -- .../s6-rc.d/user/contents.d/ssh-agent | 0 13 files changed, 37 insertions(+), 53 deletions(-) delete mode 100644 docker/dev-ssu/etc/s6-overlay/s6-rc.d/ssh-agent/type delete mode 100644 docker/dev-ssu/etc/s6-overlay/s6-rc.d/ssh-agent/up delete mode 100644 docker/dev-ssu/etc/s6-overlay/s6-rc.d/user/contents.d/ssh-agent delete mode 100644 docker/prod-ssu/etc/s6-overlay/s6-rc.d/ssh-agent/type delete mode 100644 docker/prod-ssu/etc/s6-overlay/s6-rc.d/ssh-agent/up delete mode 100644 docker/prod-ssu/etc/s6-overlay/s6-rc.d/user/contents.d/ssh-agent diff --git a/app/Actions/CoolifyTask/RunRemoteProcess.php b/app/Actions/CoolifyTask/RunRemoteProcess.php index e213f57f1..842bdd2ac 100644 --- a/app/Actions/CoolifyTask/RunRemoteProcess.php +++ b/app/Actions/CoolifyTask/RunRemoteProcess.php @@ -73,7 +73,7 @@ public function __invoke(): ProcessResult $this->time_start = hrtime(true); $status = ProcessStatus::IN_PROGRESS; - $processResult = processWithEnv()->forever()->run($this->getCommand(), $this->handleOutput(...)); + $processResult = Process::forever()->run($this->getCommand(), $this->handleOutput(...)); if ($this->activity->properties->get('status') === ProcessStatus::ERROR->value) { $status = ProcessStatus::ERROR; diff --git a/app/Jobs/ApplicationDeploymentJob.php b/app/Jobs/ApplicationDeploymentJob.php index ea682b03a..dd6648603 100644 --- a/app/Jobs/ApplicationDeploymentJob.php +++ b/app/Jobs/ApplicationDeploymentJob.php @@ -652,7 +652,7 @@ private function set_labels_for_applications() private function generate_healthcheck_commands() { - if ($this->application->dockerfile) { + if ($this->application->dockerfile || $this->application->build_pack === 'dockerfile') { // TODO: disabled HC because there are several ways to hc a simple docker image, hard to figure out a good way. Like some docker images (pocketbase) does not have curl. return 'exit 0'; } diff --git a/app/Traits/ExecuteRemoteCommand.php b/app/Traits/ExecuteRemoteCommand.php index e03c414b8..7416e1ce3 100644 --- a/app/Traits/ExecuteRemoteCommand.php +++ b/app/Traits/ExecuteRemoteCommand.php @@ -39,7 +39,7 @@ public function execute_remote_command(...$commands) $this->save = data_get($single_command, 'save'); $remote_command = generateSshCommand( $ip, $user, $port, $command); - $process = processWithEnv()->timeout(3600)->idleTimeout(3600)->start($remote_command, function (string $type, string $output) use ($command, $hidden) { + $process = Process::timeout(3600)->idleTimeout(3600)->start($remote_command, function (string $type, string $output) use ($command, $hidden) { $output = Str::of($output)->trim(); $new_log_entry = [ 'command' => $command, diff --git a/bootstrap/helpers/remoteProcess.php b/bootstrap/helpers/remoteProcess.php index 84f2dc9d3..93128e04a 100644 --- a/bootstrap/helpers/remoteProcess.php +++ b/bootstrap/helpers/remoteProcess.php @@ -49,20 +49,23 @@ function remote_process( ])(); } -function removePrivateKeyFromSshAgent(Server $server) -{ - if (data_get($server, 'privateKey.private_key') === null) { - throw new \Exception("Server {$server->name} does not have a private key"); - } - processWithEnv()->run("echo '{$server->privateKey->private_key}' | ssh-add -d -"); -} +// function removePrivateKeyFromSshAgent(Server $server) +// { +// if (data_get($server, 'privateKey.private_key') === null) { +// throw new \Exception("Server {$server->name} does not have a private key"); +// } +// // processWithEnv()->run("echo '{$server->privateKey->private_key}' | ssh-add -d -"); +// } function addPrivateKeyToSshAgent(Server $server) { if (data_get($server, 'privateKey.private_key') === null) { throw new \Exception("Server {$server->name} does not have a private key"); } - // ray('adding key', $server->privateKey->private_key); - processWithEnv()->run("echo '{$server->privateKey->private_key}' | ssh-add -q -"); + $sshKeyFileLocation = "id.root@{$server->uuid}"; + Storage::disk('ssh-keys')->makeDirectory('.'); + Storage::disk('ssh-mux')->makeDirectory('.'); + Storage::disk('ssh-keys')->put($sshKeyFileLocation, $server->privateKey->private_key); + return '/var/www/html/storage/app/ssh/keys/' . $sshKeyFileLocation; } function generateSshCommand(string $server_ip, string $user, string $port, string $command, bool $isMux = true) @@ -71,7 +74,7 @@ function generateSshCommand(string $server_ip, string $user, string $port, strin if (!$server) { throw new \Exception("Server with ip {$server_ip} not found"); } - addPrivateKeyToSshAgent($server); + $privateKeyLocation = addPrivateKeyToSshAgent($server); $timeout = config('constants.ssh.command_timeout'); $connectionTimeout = config('constants.ssh.connection_timeout'); $serverInterval = config('constants.ssh.server_interval'); @@ -83,7 +86,8 @@ function generateSshCommand(string $server_ip, string $user, string $port, strin $ssh_command .= '-o ControlMaster=auto -o ControlPersist=1m -o ControlPath=/var/www/html/storage/app/ssh/mux/%h_%p_%r '; } $command = "PATH=\$PATH:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/host/usr/local/sbin:/host/usr/local/bin:/host/usr/sbin:/host/usr/bin:/host/sbin:/host/bin && $command"; - $ssh_command .= '-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null ' + $ssh_command .= "-i {$privateKeyLocation} " + . '-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null ' . '-o PasswordAuthentication=no ' . "-o ConnectTimeout=$connectionTimeout " . "-o ServerAliveInterval=$serverInterval " @@ -97,28 +101,28 @@ function generateSshCommand(string $server_ip, string $user, string $port, strin // ray($ssh_command); return $ssh_command; } -function processWithEnv() -{ - return Process::env(['SSH_AUTH_SOCK' => config('coolify.ssh_auth_sock')]); -} -function instantCommand(string $command, $throwError = true) -{ - $process = processWithEnv()->run($command); - $output = trim($process->output()); - $exitCode = $process->exitCode(); - if ($exitCode !== 0) { - if (!$throwError) { - return null; - } - throw new \RuntimeException($process->errorOutput(), $exitCode); - } - return $output; -} +// function processWithEnv() +// { +// return Process::env(['SSH_AUTH_SOCK' => config('coolify.ssh_auth_sock')]); +// } +// function instantCommand(string $command, $throwError = true) +// { +// $process = Process::run($command); +// $output = trim($process->output()); +// $exitCode = $process->exitCode(); +// if ($exitCode !== 0) { +// if (!$throwError) { +// return null; +// } +// throw new \RuntimeException($process->errorOutput(), $exitCode); +// } +// return $output; +// } function instant_remote_process(array $command, Server $server, $throwError = true, $repeat = 1) { $command_string = implode("\n", $command); $ssh_command = generateSshCommand($server->ip, $server->user, $server->port, $command_string); - $process = processWithEnv()->run($ssh_command); + $process = Process::run($ssh_command); $output = trim($process->output()); $exitCode = $process->exitCode(); if ($exitCode !== 0) { @@ -169,14 +173,9 @@ function decode_remote_command_output(?ApplicationDeploymentQueue $application_d function refresh_server_connection(PrivateKey $private_key) { foreach ($private_key->servers as $server) { - // Delete the old ssh mux file to force a new one to be created Storage::disk('ssh-mux')->delete($server->muxFilename()); - // check if user is authenticated - // if (currentTeam()->id) { - // currentTeam()->privateKeys = PrivateKey::where('team_id', currentTeam()->id)->get(); - // } } - removePrivateKeyFromSshAgent($server); + // removePrivateKeyFromSshAgent($server); } function validateServer(Server $server) diff --git a/config/coolify.php b/config/coolify.php index 68960015b..cd16d6b6f 100644 --- a/config/coolify.php +++ b/config/coolify.php @@ -8,5 +8,4 @@ 'dev_webhook' => env('SERVEO_URL'), 'base_config_path' => env('BASE_CONFIG_PATH', '/data/coolify'), 'helper_image' => env('HELPER_IMAGE', 'ghcr.io/coollabsio/coolify-helper:latest'), - 'ssh_auth_sock' => env('SSH_AUTH_SOCK', '/tmp/coolify-ssh-agent.sock'), ]; diff --git a/docker-compose.dev.yml b/docker-compose.dev.yml index 9af78058f..cefdec07f 100644 --- a/docker-compose.dev.yml +++ b/docker-compose.dev.yml @@ -21,7 +21,6 @@ services: SSL_MODE: "off" AUTORUN_LARAVEL_STORAGE_LINK: "false" AUTORUN_LARAVEL_MIGRATION: "false" - SSH_AUTH_SOCK: "/tmp/coolify-ssh-agent.sock" volumes: - .:/var/www/html/:cached postgres: diff --git a/docker-compose.prod.yml b/docker-compose.prod.yml index 473513115..6268f9963 100644 --- a/docker-compose.prod.yml +++ b/docker-compose.prod.yml @@ -64,7 +64,6 @@ services: - LEMON_SQUEEZY_BASIC_PLAN_IDS - LEMON_SQUEEZY_PRO_PLAN_IDS - LEMON_SQUEEZY_ULTIMATE_PLAN_IDS - - SSH_AUTH_SOCK="/tmp/coolify-ssh-agent.sock" ports: - "${APP_PORT:-8000}:80" expose: diff --git a/docker/dev-ssu/etc/s6-overlay/s6-rc.d/ssh-agent/type b/docker/dev-ssu/etc/s6-overlay/s6-rc.d/ssh-agent/type deleted file mode 100644 index bdd22a185..000000000 --- a/docker/dev-ssu/etc/s6-overlay/s6-rc.d/ssh-agent/type +++ /dev/null @@ -1 +0,0 @@ -oneshot diff --git a/docker/dev-ssu/etc/s6-overlay/s6-rc.d/ssh-agent/up b/docker/dev-ssu/etc/s6-overlay/s6-rc.d/ssh-agent/up deleted file mode 100644 index 0490fb31f..000000000 --- a/docker/dev-ssu/etc/s6-overlay/s6-rc.d/ssh-agent/up +++ /dev/null @@ -1,5 +0,0 @@ -#!/usr/bin/execlineb -P -foreground { - s6-sleep 5 - su - webuser -c "ssh-agent -a /tmp/coolify-ssh-agent.sock" -} diff --git a/docker/dev-ssu/etc/s6-overlay/s6-rc.d/user/contents.d/ssh-agent b/docker/dev-ssu/etc/s6-overlay/s6-rc.d/user/contents.d/ssh-agent deleted file mode 100644 index e69de29bb..000000000 diff --git a/docker/prod-ssu/etc/s6-overlay/s6-rc.d/ssh-agent/type b/docker/prod-ssu/etc/s6-overlay/s6-rc.d/ssh-agent/type deleted file mode 100644 index bdd22a185..000000000 --- a/docker/prod-ssu/etc/s6-overlay/s6-rc.d/ssh-agent/type +++ /dev/null @@ -1 +0,0 @@ -oneshot diff --git a/docker/prod-ssu/etc/s6-overlay/s6-rc.d/ssh-agent/up b/docker/prod-ssu/etc/s6-overlay/s6-rc.d/ssh-agent/up deleted file mode 100644 index 0490fb31f..000000000 --- a/docker/prod-ssu/etc/s6-overlay/s6-rc.d/ssh-agent/up +++ /dev/null @@ -1,5 +0,0 @@ -#!/usr/bin/execlineb -P -foreground { - s6-sleep 5 - su - webuser -c "ssh-agent -a /tmp/coolify-ssh-agent.sock" -} diff --git a/docker/prod-ssu/etc/s6-overlay/s6-rc.d/user/contents.d/ssh-agent b/docker/prod-ssu/etc/s6-overlay/s6-rc.d/user/contents.d/ssh-agent deleted file mode 100644 index e69de29bb..000000000