Skip to content

An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.

fix(install): keep operator SSH keys that mention coolify

Andras Bacsai54c930d8dd29
diff --git a/other/nightly/install.sh b/other/nightly/install.sh
index ebd309a021..f6975ef619 100755
--- a/other/nightly/install.sh
+++ b/other/nightly/install.sh
@@ -1006,7 +1006,8 @@ if [ "$IS_COOLIFY_VOLUME_EXISTS" -eq 0 ]; then
test -f /data/coolify/ssh/keys/[email protected] && rm -f /data/coolify/ssh/keys/[email protected]
ssh-keygen -t ed25519 -a 100 -f /data/coolify/ssh/keys/[email protected] -q -N "" -C coolify
chown 9999 /data/coolify/ssh/keys/[email protected]
- sed -i "/coolify/d" ~/.ssh/authorized_keys
+ # Remove only the key from a previous install (generated with the comment "coolify"), not other keys that contain "coolify"
+ sed -i -E "/[[:space:]]coolify[[:space:]]*$/d" ~/.ssh/authorized_keys
cat /data/coolify/ssh/keys/[email protected] >>~/.ssh/authorized_keys
rm -f /data/coolify/ssh/keys/[email protected]
fi
diff --git a/scripts/install.sh b/scripts/install.sh
index 0c8c2ae6a8..93fef122e2 100755
--- a/scripts/install.sh
+++ b/scripts/install.sh
@@ -1006,7 +1006,8 @@ if [ "$IS_COOLIFY_VOLUME_EXISTS" -eq 0 ]; then
test -f /data/coolify/ssh/keys/[email protected] && rm -f /data/coolify/ssh/keys/[email protected]
ssh-keygen -t ed25519 -a 100 -f /data/coolify/ssh/keys/[email protected] -q -N "" -C coolify
chown 9999 /data/coolify/ssh/keys/[email protected]
- sed -i "/coolify/d" ~/.ssh/authorized_keys
+ # Remove only the key from a previous install (generated with the comment "coolify"), not other keys that contain "coolify"
+ sed -i -E "/[[:space:]]coolify[[:space:]]*$/d" ~/.ssh/authorized_keys
cat /data/coolify/ssh/keys/[email protected] >>~/.ssh/authorized_keys
rm -f /data/coolify/ssh/keys/[email protected]
fi
diff --git a/scripts/restore-backup-vm.sh b/scripts/restore-backup-vm.sh
index 856e8fca1c..c60d6d6b8b 100755
--- a/scripts/restore-backup-vm.sh
+++ b/scripts/restore-backup-vm.sh
@@ -242,7 +242,7 @@ delete_vm "$VM_NAME"
mkdir -p "$IMAGE_DIR"
if [ ! -f "$SSH_KEY" ]; then
- # The comment must not contain "coolify": install.sh removes such lines from authorized_keys.
+ # Do not use the comment "coolify": install.sh removes authorized_keys lines with exactly that comment.
ssh-keygen -q -t ed25519 -N '' -C restore-test-vm -f "$SSH_KEY"
fi
diff --git a/tests/Unit/InstallScriptAuthorizedKeysTest.php b/tests/Unit/InstallScriptAuthorizedKeysTest.php
new file mode 100644
index 0000000000..b2fd5136e4
--- /dev/null
+++ b/tests/Unit/InstallScriptAuthorizedKeysTest.php
@@ -0,0 +1,36 @@
+<?php
+
+use Symfony\Component\Process\Process;
+
+it('removes only the previous Coolify key from authorized_keys', function (string $path) {
+ $script = file_get_contents(dirname(__DIR__, 2).'/'.$path);
+ preg_match('/^\s*(sed -i .*~\/\.ssh\/authorized_keys)$/m', $script, $matches);
+ expect($matches)->toHaveKey(1);
+
+ $home = sys_get_temp_dir().'/coolify-install-keys-'.bin2hex(random_bytes(4));
+ mkdir($home.'/.ssh', 0700, true);
+ file_put_contents($home.'/.ssh/authorized_keys', implode("\n", [
+ 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOperatorKey coolify-disposable',
+ 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOperatorKey operator-lab',
+ 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIHostKey root@coolify',
+ 'no-pty ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOptions [email protected]',
+ 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOldCoolifyKey coolify',
+ '',
+ ]));
+
+ $process = new Process(['bash', '-c', $matches[1]], null, ['HOME' => $home]);
+ $process->mustRun();
+
+ expect(file_get_contents($home.'/.ssh/authorized_keys'))->toBe(implode("\n", [
+ 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOperatorKey coolify-disposable',
+ 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOperatorKey operator-lab',
+ 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIHostKey root@coolify',
+ 'no-pty ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOptions [email protected]',
+ '',
+ ]));
+
+ exec('rm -rf '.escapeshellarg($home));
+})->with([
+ 'stable install script' => ['scripts/install.sh'],
+ 'nightly install script' => ['other/nightly/install.sh'],
+]);