Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add s3 disk validation step #22

Open
wants to merge 25 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ jobs:
docker pull ghcr.io/biigle/worker:latest

- name: Start test database
run: docker-compose up -d --no-build database_testing && sleep 5
run: docker compose up -d --no-build database_testing && sleep 5
working-directory: ../core

- name: Run tests
run: docker-compose run --rm -u 1001 worker php -d memory_limit=1G vendor/bin/phpunit --random-order --filter 'Biigle\\Tests\\Modules\\'${MODULE_NAME}
run: docker compose run --rm -u 1001 worker php -d memory_limit=1G vendor/bin/phpunit --random-order --filter 'Biigle\\Tests\\Modules\\'${MODULE_NAME}
working-directory: ../core
123 changes: 98 additions & 25 deletions src/Http/Controllers/Api/UserDiskController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@

namespace Biigle\Modules\UserDisks\Http\Controllers\Api;

use Exception;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\DB;
use Biigle\Modules\UserDisks\UserDisk;
use Illuminate\Support\Facades\Storage;
use Biigle\Http\Controllers\Api\Controller;
use Illuminate\Validation\ValidationException;
use Biigle\Modules\UserDisks\Http\Requests\StoreUserDisk;
use Biigle\Modules\UserDisks\Http\Requests\UpdateUserDisk;
use Biigle\Modules\UserDisks\Http\Requests\ExtendUserDisk;
use Biigle\Modules\UserDisks\UserDisk;
use Biigle\Modules\UserDisks\Http\Requests\UpdateUserDisk;

class UserDiskController extends Controller
{
Expand Down Expand Up @@ -35,21 +40,33 @@ class UserDiskController extends Controller
*/
public function store(StoreUserDisk $request)
{
$disk = UserDisk::create([
'name' => $request->input('name'),
'type' => $request->input('type'),
'user_id' => $request->user()->id,
'expires_at' => now()->addMonths(config('user_disks.expires_months')),
'options' => $request->getDiskOptions(),
]);

if ($this->isAutomatedRequest()) {
return $disk;
}
try {
$disk = DB::transaction(function () use ($request) {
$disk = UserDisk::create([
'name' => $request->input('name'),
'type' => $request->input('type'),
'user_id' => $request->user()->id,
'expires_at' => now()->addMonths(config('user_disks.expires_months')),
'options' => $request->getDiskOptions(),
]);

$this->validateS3Config($disk);

return $disk;
});

return $this->fuzzyRedirect('storage-disks')
->with('message', 'Storage disk created')
->with('messageType', 'success');
if ($this->isAutomatedRequest()) {
return $disk;
}

return $this->fuzzyRedirect('storage-disks')
->with('message', 'Storage disk created')
->with('messageType', 'success');
} catch (ValidationException $e) {
return $this->fuzzyRedirect()
->withErrors($e->errors())
->withInput();
}
Comment on lines +65 to +69
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for the try/catch here. The ValidationException handles both browser and script requests, so there is no need to call fuzzyRedirect(). This is only needed in the success state (as it was before).

}

/**
Expand Down Expand Up @@ -77,18 +94,27 @@ public function store(StoreUserDisk $request)
*/
public function update(UpdateUserDisk $request)
{
$request->disk->name = $request->input('name', $request->disk->name);
$request->disk->options = array_merge(
$request->disk->options,
$request->getDiskOptions()
);
try {
DB::transaction(function () use ($request) {
$request->disk->name = $request->input('name', $request->disk->name);
$request->disk->options = array_merge(
$request->disk->options,
$request->getDiskOptions()
);

$request->disk->save();
$request->disk->save();
$this->validateS3Config($request->disk);
});

if (!$this->isAutomatedRequest()) {
if (!$this->isAutomatedRequest()) {
return $this->fuzzyRedirect()
->with('message', 'Storage disk updated')
->with('messageType', 'success');
}
} catch (ValidationException $e) {
return $this->fuzzyRedirect()
->with('message', 'Storage disk updated')
->with('messageType', 'success');
->withErrors($e->errors())
->withInput();
}
Comment on lines +114 to 118
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also no need for try/catch here.

}

Expand Down Expand Up @@ -142,4 +168,51 @@ public function destroy($id)
->with('messageType', 'success');
}
}
protected function validateS3Config(UserDisk $disk)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please still add a docblock here.

{
if ($disk->type != 's3') {
return;
}

$options = $disk->options;
$endpoint = $options['endpoint'];
$bucket = $options['bucket'];

// Check if endpoint contains bucket name
if (!preg_match("/(\b\/" . $bucket . "\.|\b" . $bucket . "\b)/", $endpoint)) {
throw ValidationException::withMessages(['endpoint' => 'The endpoint URL must contain the bucket name. Please check if the name is present and spelled correctly.']);
}

try {
$this->validateDiskAccess($disk);
} catch (Exception $e) {
$msg = $e->getMessage();

if (Str::contains($msg, 'timeout', true)) {
throw ValidationException::withMessages(['error' => 'The endpoint URL could not be accessed. Does it exist?']);
} elseif (Str::contains($msg, ['cURL error', 'Error parsing XML'], true)) {
throw ValidationException::withMessages(['endpoint' => 'This does not seem to be a valid S3 endpoint.']);
} elseif (Str::contains($msg, ["AccessDenied", "NoSuchBucket", "NoSuchKey"], true)) {
throw ValidationException::withMessages(['error' => 'The bucket could not be accessed. Please check for typos or missing access permissions.']);
} else {
throw ValidationException::withMessages(['error' => 'An error occurred. Please check if your input is correct.']);
}
}
return;
}

/**
* Checks if the endpoint url is valid and the disk can be accessed
*
* @param mixed $disk Disk configured by the user that should be tested
* @return void
* @throws Exception If the disk cannot be accessed
*/
protected function validateDiskAccess($disk)
{
$disk = Storage::disk("disk-{$disk->id}");
$files = $disk->getAdapter()->listContents('', false);
// Need to access an element to check if endpoint url is valid
$files->current();
}
}
10 changes: 10 additions & 0 deletions src/resources/views/store.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@
</fieldset>
@endif
<div class="row">
@error('error')
<div class="col-xs-12">
<div class="panel panel-danger">
<div class="panel-body text-danger">
{{$message}}
</div>
</div>
</div>
@enderror
</div>
<div class="col-xs-12">
@csrf
<div class="form-group clearfix">
Expand Down
9 changes: 9 additions & 0 deletions src/resources/views/update.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@
@include("user-disks::update.{$disk->type}")
</div>
<div class="row">
@error('error')
<div class="col-xs-12">
<div class="panel panel-danger">
<div class="panel-body text-danger">
{{$message}}
</div>
</div>
</div>
@enderror
<div class="col-xs-12">
@csrf
@method('PUT')
Expand Down
Loading