Skip to content

Commit fee5b26

Browse files
authored
Merge pull request GoogleCloudPlatform#1014 from GoogleCloudPlatform/sethvargo/iam_samples
Add Secret Manager IAM samples
2 parents 1b4dc4a + 285ee4b commit fee5b26

File tree

3 files changed

+165
-0
lines changed

3 files changed

+165
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
2+
/*
3+
* Copyright 2020 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/secretmanager/README.md
22+
*/
23+
24+
declare(strict_types=1);
25+
26+
require_once __DIR__ . '/../vendor/autoload.php';
27+
28+
if (count($argv) != 4) {
29+
return printf("Usage: php %s PROJECT_ID SECRET_ID MEMBER\n", basename(__FILE__));
30+
}
31+
list($_, $projectId, $secretId, $member) = $argv;
32+
33+
// [START secretmanager_iam_grant_access]
34+
// Import the Secret Manager client library.
35+
use Google\Cloud\SecretManager\V1beta1\SecretManagerServiceClient;
36+
37+
// Import the Secret Manager IAM library.
38+
use Google\Cloud\Iam\V1\Binding;
39+
40+
/** Uncomment and populate these variables in your code */
41+
// $projectId = 'YOUR_GOOGLE_CLOUD_PROJECT' (e.g. 'my-project');
42+
// $secretId = 'YOUR_SECRET_ID' (e.g. 'my-secret');
43+
// $member = 'YOUR_MEMBER' (e.g. 'user:[email protected]');
44+
45+
// Create the Secret Manager client.
46+
$client = new SecretManagerServiceClient();
47+
48+
// Build the resource name of the secret.
49+
$name = $client->secretName($projectId, $secretId);
50+
51+
// Get the current IAM policy.
52+
$policy = $client->getIamPolicy($name);
53+
54+
// Update the bindings to include the new member.
55+
$bindings = $policy->getBindings();
56+
$bindings[] = new Binding([
57+
'members' => [$member],
58+
'role' => 'roles/secretmanager.secretAccessor',
59+
]);
60+
$policy->setBindings($bindings);
61+
62+
// Save the updated policy to the server.
63+
$client->setIamPolicy($name, $policy);
64+
65+
// Print out a success message.
66+
printf('Updated IAM policy for %s', $secretId);
67+
// [END secretmanager_iam_grant_access]
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
2+
/*
3+
* Copyright 2020 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/secretmanager/README.md
22+
*/
23+
24+
declare(strict_types=1);
25+
26+
require_once __DIR__ . '/../vendor/autoload.php';
27+
28+
if (count($argv) != 4) {
29+
return printf("Usage: php %s PROJECT_ID SECRET_ID MEMBER\n", basename(__FILE__));
30+
}
31+
list($_, $projectId, $secretId, $member) = $argv;
32+
33+
// [START secretmanager_iam_revoke_access]
34+
// Import the Secret Manager client library.
35+
use Google\Cloud\SecretManager\V1beta1\SecretManagerServiceClient;
36+
37+
/** Uncomment and populate these variables in your code */
38+
// $projectId = 'YOUR_GOOGLE_CLOUD_PROJECT' (e.g. 'my-project');
39+
// $secretId = 'YOUR_SECRET_ID' (e.g. 'my-secret');
40+
// $member = 'YOUR_MEMBER' (e.g. 'user:[email protected]');
41+
42+
// Create the Secret Manager client.
43+
$client = new SecretManagerServiceClient();
44+
45+
// Build the resource name of the secret.
46+
$name = $client->secretName($projectId, $secretId);
47+
48+
// Get the current IAM policy.
49+
$policy = $client->getIamPolicy($name);
50+
51+
// Remove the member from the list of bindings.
52+
foreach ($policy->getBindings() as $binding) {
53+
if ($binding->getRole() == 'roles/secretmanager.secretAccessor') {
54+
$members = $binding->getMembers();
55+
foreach ($members as $i => $existingMember) {
56+
if ($member == $existingMember) {
57+
unset($members[$i]);
58+
$binding->setMembers($members);
59+
break;
60+
}
61+
}
62+
}
63+
}
64+
65+
// Save the updated policy to the server.
66+
$client->setIamPolicy($name, $policy);
67+
68+
// Print out a success message.
69+
printf('Updated IAM policy for %s', $secretId);
70+
// [END secretmanager_iam_revoke_access]

secretmanager/test/secretmanagerTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ class secretmanagerTest extends TestCase
4343
private static $testSecretVersionToDisable;
4444
private static $testSecretVersionToEnable;
4545

46+
private static $iamUser = 'user:[email protected]';
47+
4648
public static function setUpBeforeClass()
4749
{
4850
self::$client = new SecretManagerServiceClient();
@@ -224,6 +226,32 @@ public function testGetSecret()
224226
$this->assertContains('replication policy AUTOMATIC', $output);
225227
}
226228

229+
public function testIamGrantAccess()
230+
{
231+
$name = self::$client->parseName(self::$testSecret->getName());
232+
233+
$output = $this->runSnippet('iam_grant_access', [
234+
$name['project'],
235+
$name['secret'],
236+
self::$iamUser,
237+
]);
238+
239+
$this->assertContains('Updated IAM policy', $output);
240+
}
241+
242+
public function testIamRevokeAccess()
243+
{
244+
$name = self::$client->parseName(self::$testSecret->getName());
245+
246+
$output = $this->runSnippet('iam_revoke_access', [
247+
$name['project'],
248+
$name['secret'],
249+
self::$iamUser,
250+
]);
251+
252+
$this->assertContains('Updated IAM policy', $output);
253+
}
254+
227255
public function testListSecretVersions()
228256
{
229257
$name = self::$client->parseName(self::$testSecretWithVersions->getName());

0 commit comments

Comments
 (0)