-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: main
Are you sure you want to change the base?
Conversation
@mzur |
I think using a database transaction for this would be quite elegant! Then you can use I would advise against using |
It just came to my mind: You can probably use the VolumeUrl validation rule directly here. No need to duplicate code (unless it prevents you from showing a helpful error message). |
@mzur |
I found that an exception is only thrown if the host url is incorrect. If the bucket name is missing or wrong, it just returns an empty list. |
When I place the |
Make code more elegant by using Storage::disk and DB::transaction.
@mzur I'll add tests later after my method doesn't need to be changed any more. So I'm not finished, yet. |
15e96e7
to
fb8a21f
Compare
fb8a21f
to
16ee9be
Compare
The tests are failing because they still use |
You can update the command in this PR. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some comments left. Please be a little more diligent next time.
use Biigle\Modules\UserDisks\Http\Requests\StoreUserDisk; | ||
use Biigle\Modules\UserDisks\Http\Requests\ExtendUserDisk; | ||
use Biigle\Modules\UserDisks\Http\Requests\UpdateUserDisk; | ||
use Biigle\Modules\UserDisks\Exceptions\DiskValidationException; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where is this thrown? I only find ValidationException
.
$options = $disk->options; | ||
$endpoint = $options['endpoint']; | ||
$bucket = $options['bucket']; | ||
|
||
// Check if endpoint contains bucket name | ||
if (!preg_match("/(\b\/" . $bucket . "\.|\b" . $bucket . "\b)/", $endpoint)) { | ||
$errors['endpoint'] = 'Endpoint url requires bucket name'; | ||
return $errors; | ||
throw ValidationException::withMessages(['endpoint' => 'Endpoint url must contain bucket name. Please check if name is present and spelled correctly.']); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please check if proper English is used 😜 Seriously, these kinds of things must be user-friendly.
throw ValidationException::withMessages(['endpoint' => 'Endpoint url must contain bucket name. Please check if name is present and spelled correctly.']); | |
throw ValidationException::withMessages(['endpoint' => 'The endpoint URL must contain the bucket name. Please check if the name is present and spelled correctly.']); |
$files = $disk->getAdapter()->listContents('/', false); | ||
// Need to access an element to check if endpoint url is valid | ||
$files->current(); | ||
$this->canAccessDisk($disk); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why put this in an extra method if it is only used once?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah it's for testing. Please add a docblock to the method (which should be there anyway) and explain this in the comment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The method name may be a little misleading because the method returns void
and not bool
. Maybe call it validateDiskAccess
?
@@ -171,42 +172,41 @@ public function destroy($id) | |||
protected function validateS3Config(UserDisk $disk) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a docblock to this method.
<div class="col-xs-12" @error('error') has-error @enderror> | ||
@error('error') | ||
<div class="panel panel-danger"> | ||
<div class="panel-body text-danger"> | ||
{{$message}} | ||
</div> | ||
</div> | ||
@enderror | ||
</div> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please still apply the same change than in update.blade.php
. And please take a little more care next time when you work through my review comments, you are our "senior" dev after all 😉
} catch (DiskValidationException $e) { | ||
return $this->fuzzyRedirect() | ||
->withErrors($e->getValidationError()) | ||
->withInput(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the DiskValidationException
doesn't seem to exist, the try/catch
can probably be removed. Please be a little more careful about something like this. Mistakes like these shouldn't exist any more in a review-ready PR.
$this->postJson("/api/v1/user-disks", [ | ||
'name' => 'my disk', | ||
'type' => 's3', | ||
]) | ||
->assertStatus(422); | ||
|
||
$this->mockS3->shouldReceive('canAccessDisk')->once()->andReturn([]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why return an array when the original method returns void
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I couldn't find any test where canAccessDisk()
actually throws an exception. Didn't you test these (most important) cases at all?
Closes #4.