Skip to content

Commit 0c1bc61

Browse files
authored
feat: add public access prevention samples (GoogleCloudPlatform#1229)
1 parent e3a32af commit 0c1bc61

6 files changed

+276
-3
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
2+
/**
3+
* Copyright 2021 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/storage/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Storage;
25+
26+
# [START storage_get_public_access_prevention]
27+
use Google\Cloud\Storage\StorageClient;
28+
29+
/**
30+
* Get the Public Access Prevention setting for a bucket
31+
*
32+
* @param string $bucketName the name of your Cloud Storage bucket.
33+
*/
34+
function get_public_access_prevention($bucketName)
35+
{
36+
$storage = new StorageClient();
37+
$bucket = $storage->bucket($bucketName);
38+
39+
$iamConfiguration = $bucket->info()['iamConfiguration'];
40+
41+
printf(
42+
'The bucket public access prevention is %s for %s.' . PHP_EOL,
43+
$iamConfiguration['publicAccessPrevention'],
44+
$bucketName
45+
);
46+
}
47+
# [END storage_get_public_access_prevention]
48+
49+
// The following 2 lines are only needed to run the samples
50+
require_once __DIR__ . '/../../testing/sample_helpers.php';
51+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

storage/src/lock_retention_policy.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
* Locks a bucket's retention policy.
3131
*
3232
* @param string $bucketName the name of your Cloud Storage bucket.
33+
* Example: `$bucketName = 'my-bucket';`
3334
*/
3435
function lock_retention_policy($bucketName)
3536
{
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
2+
/**
3+
* Copyright 2021 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/storage/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Storage;
25+
26+
# [START storage_set_public_access_prevention_enforced]
27+
use Google\Cloud\Storage\StorageClient;
28+
29+
/**
30+
* Set the bucket Public Access Prevention to enforced.
31+
*
32+
* @param string $bucketName the name of your Cloud Storage bucket.
33+
* Example: `$bucketName = 'my-bucket';`
34+
*/
35+
function set_public_access_prevention_enforced($bucketName)
36+
{
37+
$storage = new StorageClient();
38+
$bucket = $storage->bucket($bucketName);
39+
40+
$bucket->update([
41+
'iamConfiguration' => [
42+
'publicAccessPrevention' => 'enforced'
43+
]
44+
]);
45+
46+
printf(
47+
'Public Access Prevention has been set to enforced for %s.' . PHP_EOL,
48+
$bucketName
49+
);
50+
}
51+
# [END storage_set_public_access_prevention_enforced]
52+
53+
// The following 2 lines are only needed to run the samples
54+
require_once __DIR__ . '/../../testing/sample_helpers.php';
55+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
2+
/**
3+
* Copyright 2021 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/storage/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Storage;
25+
26+
# [START storage_set_public_access_prevention_unspecified]
27+
use Google\Cloud\Storage\StorageClient;
28+
29+
/**
30+
* Set the bucket Public Access Prevention to unspecified.
31+
*
32+
* @param string $bucketName the name of your Cloud Storage bucket.
33+
* Example: `$bucketName = 'my-bucket';`
34+
*
35+
*/
36+
function set_public_access_prevention_unspecified($bucketName)
37+
{
38+
$storage = new StorageClient();
39+
$bucket = $storage->bucket($bucketName);
40+
41+
$bucket->update([
42+
'iamConfiguration' => [
43+
'publicAccessPrevention' => 'unspecified'
44+
]
45+
]);
46+
47+
printf(
48+
'Public Access Prevention has been set to unspecified for %s.' . PHP_EOL,
49+
$bucketName
50+
);
51+
}
52+
# [END storage_set_public_access_prevention_unspecified]
53+
54+
// The following 2 lines are only needed to run the samples
55+
require_once __DIR__ . '/../../testing/sample_helpers.php';
56+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

storage/test/UniformBucketLevelAccessCommandTest.php renamed to storage/test/IamConfigurationTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@
2323
use PHPUnit\Framework\TestCase;
2424

2525
/**
26-
* Unit Tests for UniformBucketLevelAccessCommand.
26+
* Unit Tests for IamConfiguration.
27+
* @group storage-iamconfiguration
2728
*/
28-
class UniformBucketLevelAccessCommandTest extends TestCase
29+
class IamConfigurationTest extends TestCase
2930
{
3031
use TestTrait;
3132
use ExecuteCommandTrait;
@@ -41,7 +42,7 @@ public function setUp(): void
4142
$this->storage = new StorageClient();
4243

4344
// Append random because tests for multiple PHP versions were running at the same time.
44-
$bucketName = 'php-ubla-' . time() . '-' . rand(1000, 9999);
45+
$bucketName = 'php-iamconfiguration-' . time() . '-' . rand(1000, 9999);
4546
$this->bucket = $this->storage->createBucket($bucketName);
4647
}
4748

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
2+
/**
3+
* Copyright 2021 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace Google\Cloud\Samples\Storage\Tests;
19+
20+
use Google\Cloud\Storage\StorageClient;
21+
use Google\Cloud\TestUtils\TestTrait;
22+
use PHPUnit\Framework\TestCase;
23+
24+
/**
25+
* Unit tests for public access prevention
26+
* @group pap
27+
*/
28+
class PublicAccessPreventionTest extends TestCase
29+
{
30+
use TestTrait;
31+
32+
private static $storage;
33+
private static $bucket;
34+
35+
public static function setUpBeforeClass(): void
36+
{
37+
self::$storage = new StorageClient();
38+
self::$bucket = self::$storage->createBucket(
39+
uniqid('samples-public-access-prevention-')
40+
);
41+
}
42+
43+
public static function tearDownAfterClass(): void
44+
{
45+
self::$bucket->delete();
46+
}
47+
48+
public function testSetPublicAccessPreventionToEnforced()
49+
{
50+
$output = self::runFunctionSnippet('set_public_access_prevention_enforced', [
51+
self::$bucket->name(),
52+
]);
53+
54+
$this->assertStringContainsString(
55+
sprintf(
56+
"Public Access Prevention has been set to enforced for %s.",
57+
self::$bucket->name()
58+
),
59+
$output
60+
);
61+
62+
self::$bucket->reload();
63+
$bucketInformation = self::$bucket->info();
64+
$pap = $bucketInformation['iamConfiguration']['publicAccessPrevention'];
65+
$this->assertEquals('enforced', $pap);
66+
}
67+
68+
/** @depends testSetPublicAccessPreventionToEnforced */
69+
public function testSetPublicAccessPreventionToUnspecified()
70+
{
71+
$output = self::runFunctionSnippet('set_public_access_prevention_unspecified', [
72+
self::$bucket->name(),
73+
]);
74+
75+
$this->assertStringContainsString(
76+
sprintf(
77+
"Public Access Prevention has been set to unspecified for %s.",
78+
self::$bucket->name()
79+
),
80+
$output
81+
);
82+
83+
self::$bucket->reload();
84+
$bucketInformation = self::$bucket->info();
85+
$pap = $bucketInformation['iamConfiguration']['publicAccessPrevention'];
86+
$this->assertEquals('unspecified', $pap);
87+
}
88+
89+
/** @depends testSetPublicAccessPreventionToUnspecified */
90+
public function testGetPublicAccessPrevention()
91+
{
92+
$output = self::runFunctionSnippet('get_public_access_prevention', [
93+
self::$bucket->name(),
94+
]);
95+
96+
$this->assertStringContainsString(
97+
sprintf(
98+
"The bucket public access prevention is unspecified for %s.",
99+
self::$bucket->name()
100+
),
101+
$output
102+
);
103+
104+
self::$bucket->reload();
105+
$bucketInformation = self::$bucket->info();
106+
$pap = $bucketInformation['iamConfiguration']['publicAccessPrevention'];
107+
$this->assertEquals('unspecified', $pap);
108+
}
109+
}

0 commit comments

Comments
 (0)