Fix .env Files for Isolated Users (#496)

This commit is contained in:
Richard Anderson 2025-02-22 08:23:03 +00:00 committed by GitHub
parent 2356e44f5b
commit 1223ea1499
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 39 additions and 8 deletions

View File

@ -2,19 +2,20 @@
namespace App\Actions\Site; namespace App\Actions\Site;
use App\Exceptions\SSHUploadFailed; use App\Exceptions\SSHError;
use App\Models\Site; use App\Models\Site;
class UpdateEnv class UpdateEnv
{ {
/** /**
* @throws SSHUploadFailed * @throws SSHError
*/ */
public function update(Site $site, array $input): void public function update(Site $site, array $input): void
{ {
$site->server->os()->editFile( $site->server->os()->editFileAs(
$site->path.'/.env', $site->path.'/.env',
$input['env'] $site->user,
trim($input['env']),
); );
} }
} }

View File

@ -112,7 +112,9 @@ public function exec(string $command, string $log = '', ?int $siteId = null, ?bo
try { try {
if ($this->asUser) { if ($this->asUser) {
$command = 'sudo su - '.$this->asUser.' -c '.'"'.addslashes($command).'"'; $command = addslashes($command);
$command = str_replace('\\\'', '\'', $command);
$command = 'sudo su - '.$this->asUser.' -c '.'"'.trim($command).'"';
} }
$this->connection->setTimeout(0); $this->connection->setTimeout(0);

View File

@ -198,16 +198,34 @@ public function editFile(string $path, ?string $content = null): void
} }
} }
/**
* @throws SSHError
*/
public function editFileAs(string $path, string $user, ?string $content = null): void
{
$sudo = $user === 'root';
$actualUser = $sudo ? $this->server->getSshUser() : $user;
$this->server->ssh($actualUser)->exec(
view('ssh.os.edit-file', [
'path' => $path,
'content' => $content,
'sudo' => $sudo,
]),
'edit-file'
);
}
/** /**
* @throws SSHError * @throws SSHError
*/ */
public function readFile(string $path): string public function readFile(string $path): string
{ {
return $this->server->ssh()->exec( return trim($this->server->ssh()->exec(
view('ssh.os.read-file', [ view('ssh.os.read-file', [
'path' => $path, 'path' => $path,
]) ])
); ));
} }
/** /**

View File

@ -0,0 +1,9 @@
@if($sudo) sudo @endif tee {!! $path !!} << 'VITO_SSH_EOF' > /dev/null
{!! $content !!}
VITO_SSH_EOF
if [ $? -eq 0 ]; then
echo "Successfully wrote to {{ $path }}"
else
echo 'VITO_SSH_ERROR' && exit 1
fi

View File

@ -202,7 +202,8 @@ public function test_update_env_file(): void
->assertSuccessful() ->assertSuccessful()
->assertNotified('.env updated!'); ->assertNotified('.env updated!');
SSH::assertFileUploaded('/home/vito/'.$this->site->domain.'/.env', 'APP_ENV="production"'); SSH::assertExecutedContains('tee /home/vito/vito.test/.env << \'VITO_SSH_EOF\'');
SSH::assertExecutedContains('APP_ENV="production"');
} }
public function test_git_hook_deployment(): void public function test_git_hook_deployment(): void