Skip to content

Commit

Permalink
Improve script to connect to custom servers
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalbaljet committed Aug 3, 2023
1 parent e3d7341 commit c86d983
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 2 deletions.
4 changes: 2 additions & 2 deletions app/Http/Controllers/ServerProvisionScriptController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use App\Models\Server;
use App\Provider;
use App\Tasks\AuthorizePublicKey;
use App\Tasks\AuthorizeManagementRootKey;

/**
* @codeCoverageIgnore Handled by Dusk tests.
Expand All @@ -26,7 +26,7 @@ public function __invoke(Server $server)
return '';
}

$task = new AuthorizePublicKey($server, $server->public_key, true);
$task = new AuthorizeManagementRootKey($server);

return $task->getScript();
}
Expand Down
14 changes: 14 additions & 0 deletions app/Tasks/AuthorizeManagementRootKey.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace App\Tasks;

use App\Models\Server;

class AuthorizeManagementRootKey extends Task
{
protected int $timeout = 15;

public function __construct(public Server $server)
{
}
}
21 changes: 21 additions & 0 deletions resources/views/tasks/authorize-management-root-key.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<x-task-shell-defaults />

echo "Setup SSH keys for root"

if [ ! -d /root/.ssh ]
then
mkdir -p /root/.ssh
touch /root/.ssh/authorized_keys
fi

cat <<EOF >> /root/.ssh/authorized_keys

{{ $server->public_key }}
EOF

echo "Fix root permissions"

chown root:root /root
chown -R root:root /root/.ssh
chmod 700 /root/.ssh
chmod 600 /root/.ssh/authorized_keys
25 changes: 25 additions & 0 deletions tests/Unit/Tasks/AuthorizeManagementRootKeyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Tests\Unit\Tasks;

use App\Models\Server;
use App\Tasks\AuthorizeManagementRootKey;
use Database\Factories\ServerFactory;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class AuthorizeManagementRootKeyTest extends TestCase
{
use RefreshDatabase;

/** @test */
public function it_authorizes_the_public_key()
{
/** @var Server */
$server = ServerFactory::new()->create();

$this->assertMatchesBashSnapshot(
(new AuthorizeManagementRootKey($server))->getScript()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
set -eu
export DEBIAN_FRONTEND=noninteractive

echo "Setup SSH keys for root"

if [ ! -d /root/.ssh ]
then
mkdir -p /root/.ssh
touch /root/.ssh/authorized_keys
fi

cat <<EOF >> /root/.ssh/authorized_keys
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIAb5M7vlstlBOPx6NocXAewxzfxX8AujDifR0lrQf+On [email protected]
EOF

echo "Fix root permissions"

chown root:root /root
chown -R root:root /root/.ssh
chmod 700 /root/.ssh
chmod 600 /root/.ssh/authorized_keys

0 comments on commit c86d983

Please sign in to comment.