Skip to content

Commit 4caa3a8

Browse files
authored
Adds missing KMS samples (GoogleCloudPlatform#814)
1 parent 4727876 commit 4caa3a8

File tree

3 files changed

+149
-16
lines changed

3 files changed

+149
-16
lines changed

kms/src/get_cryptokey_policy.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
/**
3+
* Copyright 2019 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/kms/README.md
22+
*/
23+
24+
25+
// Include Google Cloud dependendencies using Composer
26+
require_once __DIR__ . '/../vendor/autoload.php';
27+
28+
if (count($argv) != 5) {
29+
return printf("Usage: php %s PROJECT_ID LOCATION_ID KEYRING_ID CRYPTOKEY_ID\n", basename(__FILE__));
30+
}
31+
list($_, $projectId, $locationId, $keyRingId, $cryptoKeyId) = $argv;
32+
33+
# [START kms_get_cryptokey_policy]
34+
use Google\Cloud\Kms\V1\KeyManagementServiceClient;
35+
36+
/** Uncomment and populate these variables in your code */
37+
// $projectId = 'The Google project ID';
38+
// $locationId = 'The location ID of the crypto key. Can be "global", "us-west1", etc.';
39+
// $keyRingId = 'The KMS key ring ID';
40+
// $cryptoKeyId = 'The KMS key ID';
41+
42+
$kms = new KeyManagementServiceClient();
43+
44+
// The resource name of the Crypto Key.
45+
$cryptoKeyName = $kms->cryptoKeyName($projectId, $locationId, $keyRingId, $cryptoKeyId);
46+
47+
// Get the Crypto Key Policy and print it.
48+
$cryptoKeyPolicy = $kms->getIamPolicy($cryptoKeyName);
49+
50+
foreach ($cryptoKeyPolicy->getBindings() as $binding) {
51+
printf("Role: %s\nMembers:\n", $binding->getRole());
52+
53+
foreach ($binding->getMembers() as $member) {
54+
printf(" %s\n", $member);
55+
}
56+
print("\n");
57+
}
58+
# [END kms_get_cryptokey_policy]

kms/src/get_keyring_policy.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
/**
3+
* Copyright 2019 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/kms/README.md
22+
*/
23+
24+
25+
// Include Google Cloud dependendencies using Composer
26+
require_once __DIR__ . '/../vendor/autoload.php';
27+
28+
if (count($argv) != 4) {
29+
return printf("Usage: php %s PROJECT_ID LOCATION_ID KEYRING_ID\n", basename(__FILE__));
30+
}
31+
list($_, $projectId, $locationId, $keyRingId) = $argv;
32+
33+
# [START kms_get_keyring_policy]
34+
use Google\Cloud\Kms\V1\KeyManagementServiceClient;
35+
36+
/** Uncomment and populate these variables in your code */
37+
// $projectId = 'The Google project ID';
38+
// $locationId = 'The location ID of the crypto key. Can be "global", "us-west1", etc.';
39+
// $keyRingId = 'The KMS key ring ID';
40+
41+
$kms = new KeyManagementServiceClient();
42+
43+
// The resource name of the Key Ring.
44+
$keyRingName = $kms->keyRingName($projectId, $locationId, $keyRingId);
45+
46+
// Get the Key Ring Policy and print it.
47+
$keyRingPolicy = $kms->getIamPolicy($keyRingName);
48+
49+
foreach ($keyRingPolicy->getBindings() as $binding) {
50+
printf("Role: %s\nMembers:\n", $binding->getRole());
51+
52+
foreach ($binding->getMembers() as $member) {
53+
printf(" %s\n", $member);
54+
}
55+
print("\n");
56+
}
57+
# [END kms_get_keyring_policy]

kms/test/kmsTest.php

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class kmsTest extends \PHPUnit_Framework_TestCase
2424
use TestTrait;
2525

2626
private static $locationId = 'global';
27+
private static $userEmail = '[email protected]';
2728
private static $encryptedFile;
2829
private static $tempRing;
2930
private static $tempKey;
@@ -81,55 +82,61 @@ public function testDecrypt()
8182

8283
public function testAddUserToKeyRing()
8384
{
84-
$userEmail = '[email protected]';
85-
8685
$output = $this->runSnippet('add_member_to_keyring_policy', [
8786
self::$ring,
88-
'user:' . $userEmail,
87+
'user:' . self::$userEmail,
8988
'roles/cloudkms.cryptoKeyEncrypterDecrypter'
9089
]);
9190

9291
$this->assertContains(sprintf(
9392
'Member user:%s added to policy for keyRing %s' . PHP_EOL,
94-
$userEmail,
93+
self::$userEmail,
9594
self::$ring
9695
), $output);
9796
}
9897

9998
/**
10099
* @depends testAddUserToKeyRing
101100
*/
102-
public function testRemoveUserFromKeyRing()
101+
public function testGetKeyRingPolicy()
103102
{
104-
$userEmail = '[email protected]';
103+
$output = $this->runSnippet('get_keyring_policy', [
104+
self::$ring,
105+
]);
106+
107+
$this->assertContains('user:' . self::$userEmail, $output);
108+
}
105109

110+
/**
111+
* @depends testAddUserToKeyRing
112+
*/
113+
public function testRemoveUserFromKeyRing()
114+
{
106115
$output = $this->runSnippet('remove_member_from_keyring_policy', [
107116
self::$ring,
108-
'user:' . $userEmail,
117+
'user:' . self::$userEmail,
109118
'roles/cloudkms.cryptoKeyEncrypterDecrypter'
110119
]);
111120

112121
$this->assertContains(sprintf(
113122
'Member user:%s removed from policy for keyRing %s' . PHP_EOL,
114-
$userEmail,
123+
self::$userEmail,
115124
self::$ring
116125
), $output);
117126
}
118127

119128
public function testAddUserToCryptoKey()
120129
{
121-
$userEmail = '[email protected]';
122-
123130
$output = $this->runSnippet('add_member_to_cryptokey_policy', [
124131
self::$ring,
125132
self::$key,
126-
'user:' . $userEmail,
133+
'user:' . self::$userEmail,
127134
'roles/cloudkms.cryptoKeyEncrypterDecrypter'
128135
]);
129136

130137
$this->assertContains(sprintf(
131138
'Member user:%s added to policy for cryptoKey %s in keyRing %s' . PHP_EOL,
132-
$userEmail,
139+
self::$userEmail,
133140
self::$key,
134141
self::$ring
135142
), $output);
@@ -138,20 +145,31 @@ public function testAddUserToCryptoKey()
138145
/**
139146
* @depends testAddUserToCryptoKey
140147
*/
141-
public function testRemoveUserFromCryptoKey()
148+
public function testGetCryptoKeyPolicy()
142149
{
143-
$userEmail = '[email protected]';
150+
$output = $this->runSnippet('get_cryptokey_policy', [
151+
self::$ring,
152+
self::$key,
153+
]);
154+
155+
$this->assertContains('user:' . self::$userEmail, $output);
156+
}
144157

158+
/**
159+
* @depends testAddUserToCryptoKey
160+
*/
161+
public function testRemoveUserFromCryptoKey()
162+
{
145163
$output = $this->runSnippet('remove_member_from_cryptokey_policy', [
146164
self::$ring,
147165
self::$key,
148-
'user:' . $userEmail,
166+
'user:' . self::$userEmail,
149167
'roles/cloudkms.cryptoKeyEncrypterDecrypter'
150168
]);
151169

152170
$this->assertContains(sprintf(
153171
'Member user:%s removed from policy for cryptoKey %s in keyRing %s' . PHP_EOL,
154-
$userEmail,
172+
self::$userEmail,
155173
self::$key,
156174
self::$ring
157175
), $output);

0 commit comments

Comments
 (0)