Skip to content

Commit

Permalink
fix: allow mkdir() via stream wrapper when UBL is enabled (#2541)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdpedrie authored and dwsupplee committed Jan 7, 2020
1 parent 006487a commit aef45e7
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 5 deletions.
15 changes: 13 additions & 2 deletions Storage/src/StreamWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -388,11 +388,22 @@ public function mkdir($path, $mode, $options)
// If the file name is empty, we were trying to create a bucket. In this case,
// don't create the placeholder file.
if ($this->file != '') {
$bucketInfo = $this->bucket->info();
$ublEnabled = isset($bucketInfo['iamConfiguration']['uniformBucketLevelAccess']) &&
$bucketInfo['iamConfiguration']['uniformBucketLevelAccess']['enabled'] === true;

// if bucket has uniform bucket level access enabled, don't set ACLs.
$acl = [];
if (!$ublEnabled) {
$acl = [
'predefinedAcl' => $predefinedAcl
];
}

// Fake a directory by creating an empty placeholder file whose name ends in '/'
$this->bucket->upload('', [
'name' => $this->file,
'predefinedAcl' => $predefinedAcl
]);
] + $acl);
}
} catch (ServiceException $e) {
return false;
Expand Down
20 changes: 20 additions & 0 deletions Storage/tests/System/StreamWrapper/DirectoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,26 @@ public function testMkDir()
$this->assertTrue(is_dir($dir . '/'));
}

public function testMkDirWithUbl()
{
$bucket = self::createBucket(
self::$client,
uniqid(self::TESTING_PREFIX),
[
'iamConfiguration' => [
'uniformBucketLevelAccess' => [
'enabled' => true
]
]
]
);

$dir = self::generateUrl('test_directory', $bucket);
$this->assertTrue(mkdir($dir));
$this->assertFileExists($dir . '/');
$this->assertTrue(is_dir($dir . '/'));
}

public function testIsDir()
{
$dir = self::generateUrl('test_directory');
Expand Down
7 changes: 4 additions & 3 deletions Storage/tests/System/StreamWrapper/StreamWrapperTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

namespace Google\Cloud\Storage\Tests\System\StreamWrapper;

use Google\Cloud\Storage\Storage\Tests\SystemTestCase;
use Google\Cloud\Storage\Bucket;
use Google\Cloud\Storage\Tests\System\StorageTestCase;

/**
Expand All @@ -38,9 +38,10 @@ public static function tearDownAfterClass()
parent::tearDownAfterClass();
}

protected static function generateUrl($file)
protected static function generateUrl($file, Bucket $bucket = null)
{
$bucketName = self::$bucket->name();
$bucket = $bucket ?: self::$bucket;
$bucketName = $bucket->name();
return "gs://$bucketName/$file";
}
}
26 changes: 26 additions & 0 deletions Storage/tests/Unit/StreamWrapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,15 +250,27 @@ public function testUnlinkOnNonExistentFile()
*/
public function testMkdir()
{
$this->mockBucketInfoUbl(false);
$this->bucket->upload('', ['name' => 'foo/bar/', 'predefinedAcl' => 'publicRead'])->shouldBeCalled();
$this->assertTrue(mkdir('gs://my_bucket/foo/bar'));
}

/**
* @group storageDirectory
*/
public function testMkdirWithUbl()
{
$this->mockBucketInfoUbl(true);
$this->bucket->upload('', ['name' => 'foo/bar/'])->shouldBeCalled();
$this->assertTrue(mkdir('gs://my_bucket/foo/bar'));
}

/**
* @group storageDirectory
*/
public function testMkdirProjectPrivate()
{
$this->mockBucketInfoUbl(false);
$this->bucket->upload('', ['name' => 'foo/bar/', 'predefinedAcl' => 'projectPrivate'])->shouldBeCalled();
$this->assertTrue(mkdir('gs://my_bucket/foo/bar', 0740));
}
Expand All @@ -268,6 +280,7 @@ public function testMkdirProjectPrivate()
*/
public function testMkdirPrivate()
{
$this->mockBucketInfoUbl(false);
$this->bucket->upload('', ['name' => 'foo/bar/', 'predefinedAcl' => 'private'])->shouldBeCalled();
$this->assertTrue(mkdir('gs://my_bucket/foo/bar', 0700));
}
Expand All @@ -277,6 +290,7 @@ public function testMkdirPrivate()
*/
public function testMkdirOnBadDirectory()
{
$this->mockBucketInfoUbl(false);
$this->bucket->upload('', ['name' => 'foo/bar/', 'predefinedAcl' => 'publicRead'])
->willThrow(NotFoundException::class);
$this->assertFalse(mkdir('gs://my_bucket/foo/bar'));
Expand All @@ -289,6 +303,7 @@ public function testMkDirCreatesBucket()
{
$this->bucket->exists()->willReturn(false);
$this->bucket->name()->willReturn('my_bucket');
$this->mockBucketInfoUbl(false);
$this->client->createBucket('my_bucket', [
'predefinedAcl' => 'publicRead',
'predefinedDefaultObjectAcl' => 'publicRead'
Expand Down Expand Up @@ -450,4 +465,15 @@ private function fileListGenerator($filesToReturn)
}
}
}

private function mockBucketInfoUbl($enabled = false)
{
$this->bucket->info()->willReturn([
'iamConfiguration' => [
'uniformBucketLevelAccess' => [
'enabled' => $enabled
]
]
]);
}
}

0 comments on commit aef45e7

Please sign in to comment.