Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions app/Facades/SFTP.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace App\Facades;

use App\Support\Testing\SFTPFake;
use Illuminate\Support\Facades\Facade;

/**
* @method static bool connect(string $host, int $port, string $username, string $password)
* @method static void assertConnected(string $host)
*/
class SFTP extends Facade
{
protected static function getFacadeAccessor(): string
{
return 'sftp';
}

public static function fake(): SFTPFake
{
static::swap($fake = new SFTPFake);

return $fake;
}
}
20 changes: 20 additions & 0 deletions app/Helpers/SFTP.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace App\Helpers;

use phpseclib3\Net\SFTP as PhpseclibSFTP;
use Throwable;

class SFTP
{
public function connect(string $host, int $port, string $username, string $password): bool
{
try {
$sftp = new PhpseclibSFTP($host, $port);

return $sftp->login($username, $password);
} catch (Throwable) {
return false;
}
}
}
3 changes: 2 additions & 1 deletion app/Models/BackupFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use App\StorageProviders\FTP;
use App\StorageProviders\Local;
use App\StorageProviders\S3;
use App\StorageProviders\SFTP;
use Carbon\Carbon;
use Database\Factories\BackupFileFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
Expand Down Expand Up @@ -107,7 +108,7 @@ public function path(): string

return match ($storage->provider) {
Dropbox::id() => '/'.$backupName.'/'.$this->name.$extension,
S3::id(), FTP::id(), Local::id() => implode('/', [
S3::id(), FTP::id(), SFTP::id(), Local::id() => implode('/', [
rtrim((string) $storage->credentials['path'], '/'),
$backupName,
$this->name.$extension,
Expand Down
2 changes: 2 additions & 0 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Helpers\FTP;
use App\Helpers\Notifier;
use App\Helpers\SFTP;
use App\Helpers\SSH;
use App\Models\PersonalAccessToken;
use Illuminate\Http\Resources\Json\ResourceCollection;
Expand All @@ -26,6 +27,7 @@ public function boot(): void
$this->app->bind('ssh', fn (): SSH => new SSH);
$this->app->bind('notifier', fn (): Notifier => new Notifier);
$this->app->bind('ftp', fn (): FTP => new FTP);
$this->app->bind('sftp', fn (): SFTP => new SFTP);

Sanctum::usePersonalAccessTokenModel(PersonalAccessToken::class);

Expand Down
30 changes: 30 additions & 0 deletions app/Providers/StorageProviderServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use App\StorageProviders\FTP;
use App\StorageProviders\Local;
use App\StorageProviders\S3;
use App\StorageProviders\SFTP;
use Illuminate\Support\ServiceProvider;

class StorageProviderServiceProvider extends ServiceProvider
Expand All @@ -21,6 +22,7 @@ public function boot(): void
$this->aws();
$this->dropbox();
$this->ftp();
$this->sftp();
}

private function local(): void
Expand Down Expand Up @@ -118,4 +120,32 @@ private function ftp(): void
)
->register();
}

private function sftp(): void
{
RegisterStorageProvider::make(SFTP::id())
->label('SFTP')
->handler(SFTP::class)
->form(
DynamicForm::make([
DynamicField::make('host')
->text()
->label('Host'),
DynamicField::make('port')
->text()
->label('Port')
->default(22),
DynamicField::make('path')
->text()
->label('Path'),
DynamicField::make('username')
->text()
->label('Username'),
DynamicField::make('password')
->password()
->label('Password'),
])
)
->register();
}
}
65 changes: 65 additions & 0 deletions app/SSH/Storage/SFTP.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace App\SSH\Storage;

use App\Exceptions\SSHError;

class SFTP extends AbstractStorage
{
/**
* @throws SSHError
*/
public function upload(string $src, string $dest): array
{
$this->server->ssh()->exec(
view('ssh.storage.sftp.upload', [
'src' => $src,
'dest' => $dest,
'host' => $this->storageProvider->credentials['host'],
'port' => $this->storageProvider->credentials['port'],
'username' => $this->storageProvider->credentials['username'],
'password' => $this->storageProvider->credentials['password'],
]),
'upload-to-sftp'
);

return [
'size' => null,
];
}

/**
* @throws SSHError
*/
public function download(string $src, string $dest): void
{
$this->server->ssh()->exec(
view('ssh.storage.sftp.download', [
'src' => $src,
'dest' => $dest,
'host' => $this->storageProvider->credentials['host'],
'port' => $this->storageProvider->credentials['port'],
'username' => $this->storageProvider->credentials['username'],
'password' => $this->storageProvider->credentials['password'],
]),
'download-from-sftp'
);
}

/**
* @throws SSHError
*/
public function delete(string $src): void
{
$this->server->ssh()->exec(
view('ssh.storage.sftp.delete-file', [
'src' => $src,
'host' => $this->storageProvider->credentials['host'],
'port' => $this->storageProvider->credentials['port'],
'username' => $this->storageProvider->credentials['username'],
'password' => $this->storageProvider->credentials['password'],
]),
'delete-from-sftp'
);
}
}
62 changes: 62 additions & 0 deletions app/StorageProviders/SFTP.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace App\StorageProviders;

use App\Facades\SFTP as SFTPFacade;
use App\Models\Server;
use App\SSH\Storage\Storage;

class SFTP extends AbstractStorageProvider
{
public static function id(): string
{
return 'sftp';
}

/**
* @return array<string, mixed>
*/
public function validationRules(): array
{
return [
'host' => 'required',
'port' => [
'required',
'integer',
'min:1',
'max:65535',
],
'path' => 'required',
'username' => 'required',
'password' => 'required',
];
}

public function credentialData(array $input): array
{
return [
'host' => $input['host'],
'port' => $input['port'],
'path' => $input['path'],
'username' => $input['username'],
'password' => $input['password'],
];
}

public function connect(): bool
{
$credentials = $this->storageProvider->credentials;

return SFTPFacade::connect(
$credentials['host'],
(int) $credentials['port'],
$credentials['username'],
$credentials['password']
);
}

public function ssh(Server $server): Storage
{
return new \App\SSH\Storage\SFTP($server, $this->storageProvider);
}
}
37 changes: 37 additions & 0 deletions app/Support/Testing/SFTPFake.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace App\Support\Testing;

use PHPUnit\Framework\Assert;

class SFTPFake
{
/**
* @var array<array{host: string, port: int, username: string}>
*/
protected array $connections = [];

public function connect(string $host, int $port, string $username, string $password): bool
{
$this->connections[] = ['host' => $host, 'port' => $port, 'username' => $username];

return true;
}

public function assertConnected(string $host): void
{
if ($this->connections === []) {
Assert::fail('No SFTP connections are made');
}
$connected = false;
foreach ($this->connections as $connection) {
if ($connection['host'] === $host) {
$connected = true;
break;
}
}
if (! $connected) {
Assert::fail('The expected host is not connected');
}
}
}
1 change: 1 addition & 0 deletions resources/views/ssh/storage/sftp/delete-file.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
curl -k -u "{{ $username }}:{{ $password }}" sftp://{{ $host }}:{{ $port }}/ -Q "rm {{ $src }}"
1 change: 1 addition & 0 deletions resources/views/ssh/storage/sftp/download.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
curl -k -u "{{ $username }}:{{ $password }}" sftp://{{ $host }}:{{ $port }}/{{ $src }} -o "{{ $dest }}"
1 change: 1 addition & 0 deletions resources/views/ssh/storage/sftp/upload.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
curl -k --ftp-create-dirs -T "{{ $src }}" -u "{{ $username }}:{{ $password }}" sftp://{{ $host }}:{{ $port }}/{{ $dest }}
28 changes: 28 additions & 0 deletions tests/Feature/API/StorageProvidersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Tests\Feature\API;

use App\Facades\FTP;
use App\Facades\SFTP;
use App\Models\Backup;
use App\Models\Database;
use App\Models\StorageProvider as StorageProviderModel;
Expand Down Expand Up @@ -35,6 +36,10 @@ public function test_create(array $input): void
FTP::fake();
}

if ($input['provider'] === \App\StorageProviders\SFTP::id()) {
SFTP::fake();
}

$this->json('POST', route('api.projects.storage-providers.create', [
'project' => $this->user->current_project_id,
]), $input)
Expand Down Expand Up @@ -309,6 +314,29 @@ public static function createData(): array
'global' => 1,
],
],
[
[
'provider' => \App\StorageProviders\SFTP::id(),
'name' => 'sftp-test',
'host' => '1.2.3.4',
'port' => '22',
'path' => '/home/vito',
'username' => 'username',
'password' => 'password',
],
],
[
[
'provider' => \App\StorageProviders\SFTP::id(),
'name' => 'sftp-test',
'host' => '1.2.3.4',
'port' => '22',
'path' => '/home/vito',
'username' => 'username',
'password' => 'password',
'global' => 1,
],
],
];
}
}
Loading