Skip to content

Commit 3629c3e

Browse files
authored
Merge pull request GoogleCloudPlatform#946 from GoogleCloudPlatform/hmac-sa
[Storage] HMAC key samples
2 parents 9640c97 + 390cfa8 commit 3629c3e

11 files changed

+507
-2
lines changed

storage/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ This simple command-line application demonstrates how to invoke
4848
get-object-v2-signed-url Generate a v2 signed URL for downloading an object.
4949
get-object-v4-signed-url Generate a v4 signed URL for downloading an object.
5050
get-object-v4-upload-signed-url Generate a v4 signed URL for uploading an object.
51+
hmac-sa-manage Manage HMAC Service Account keys.
52+
hmac-sa-list List HMAC Service Account keys.
53+
hmac-sa-create Create an HMAC Service Account key.
5154
```
5255
6. Run `php storage.php COMMAND --help` to print information about the usage of each command.
5356

storage/composer.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"require": {
3-
"google/cloud-storage": "^1.9.0",
3+
"google/cloud-storage": "^1.14.0",
44
"paragonie/random_compat": "^2.0",
55
"symfony/console": " ^3.0"
66
},
@@ -13,21 +13,25 @@
1313
"src/add_object_acl.php",
1414
"src/copy_object.php",
1515
"src/create_bucket.php",
16+
"src/create_hmac_key.php",
1617
"src/delete_bucket.php",
1718
"src/delete_bucket_acl.php",
1819
"src/delete_bucket_default_acl.php",
1920
"src/delete_object.php",
2021
"src/delete_object_acl.php",
22+
"src/delete_hmac_key.php",
2123
"src/disable_bucket_policy_only.php",
2224
"src/disable_default_event_based_hold.php",
2325
"src/disable_requester_pays.php",
26+
"src/deactivate_hmac_key.php",
2427
"src/download_encrypted_object.php",
2528
"src/download_file_requester_pays.php",
2629
"src/download_object.php",
2730
"src/enable_bucket_policy_only.php",
2831
"src/enable_default_event_based_hold.php",
2932
"src/enable_default_kms_key.php",
3033
"src/enable_requester_pays.php",
34+
"src/activate_hmac_key.php",
3135
"src/generate_encryption_key.php",
3236
"src/bucket_metadata.php",
3337
"src/get_bucket_acl.php",
@@ -44,9 +48,11 @@
4448
"src/get_requester_pays_status.php",
4549
"src/get_retention_policy.php",
4650
"src/get_default_event_based_hold.php",
51+
"src/get_hmac_key.php",
4752
"src/list_buckets.php",
4853
"src/list_objects.php",
4954
"src/list_objects_with_prefix.php",
55+
"src/list_hmac_keys.php",
5056
"src/lock_retention_policy.php",
5157
"src/make_public.php",
5258
"src/move_object.php",

storage/src/activate_hmac_key.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
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/storage/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Storage;
25+
26+
# [START storage_activate_hmac_key]
27+
use Google\Cloud\Storage\StorageClient;
28+
29+
/**
30+
* Activate an HMAC key.
31+
*
32+
* @param string $accessId Access ID for an inactive HMAC key.
33+
* @param string $projectId Google Cloud Project ID.
34+
*
35+
*/
36+
function activate_hmac_key($accessId, $projectId)
37+
{
38+
$storage = new StorageClient();
39+
// By default hmacKey will use the projectId used by StorageClient().
40+
$hmacKey = $storage->hmacKey($accessId, $projectId);
41+
42+
$hmacKey->update('ACTIVE');
43+
44+
print('The HMAC key is now active.' . PHP_EOL);
45+
printf('HMAC key Metadata: %s' . PHP_EOL, print_r($hmacKey->info(), true));
46+
}
47+
# [END storage_activate_hmac_key]

storage/src/create_bucket.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
* @param string $bucketName name of the bucket to create.
3333
* @param string $options options for the new bucket.
3434
*
35-
* @return Google\Cloud\Storage\Bucket the newly created bucket.
3635
*/
3736
function create_bucket($bucketName, $options = [])
3837
{

storage/src/create_hmac_key.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
/**
3+
* Copyright 2019 Google Inc.
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_create_hmac_key]
27+
use Google\Cloud\Storage\StorageClient;
28+
29+
/**
30+
* Create a new HMAC key.
31+
*
32+
* @param string $serviceAccountEmail Service account email to associate with the new HMAC key.
33+
* @param string $projectId Google Cloud Project ID.
34+
*
35+
*/
36+
function create_hmac_key($serviceAccountEmail, $projectId)
37+
{
38+
$storage = new StorageClient();
39+
// By default createHmacKey will use the projectId used by StorageClient().
40+
$hmacKeyCreated = $storage->createHmacKey($serviceAccountEmail, ['projectId' => $projectId]);
41+
42+
printf('The base64 encoded secret is: %s' . PHP_EOL, $hmacKeyCreated->secret());
43+
print('Do not miss that secret, there is no API to recover it.' . PHP_EOL);
44+
printf('HMAC key Metadata: %s' . PHP_EOL, print_r($hmacKeyCreated->hmacKey()->info(), true));
45+
}
46+
# [END storage_create_hmac_key]

storage/src/deactivate_hmac_key.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2+
/**
3+
* Copyright 2019 Google Inc.
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_deactivate_hmac_key]
27+
use Google\Cloud\Storage\StorageClient;
28+
29+
/**
30+
* Deactivate an HMAC key.
31+
*
32+
* @param string $accessId Access ID for an inactive HMAC key.
33+
* @param string $projectId Google Cloud Project ID.
34+
*
35+
*/
36+
function deactivate_hmac_key($accessId, $projectId)
37+
{
38+
$storage = new StorageClient();
39+
// By default hmacKey will use the projectId used by StorageClient().
40+
$hmacKey = $storage->hmacKey($accessId, $projectId);
41+
42+
$hmacKey->update('INACTIVE');
43+
44+
print('The HMAC key is now inactive.' . PHP_EOL);
45+
printf('HMAC key Metadata: %s' . PHP_EOL, print_r($hmacKey->info(), true));
46+
}
47+
# [END storage_deactivate_hmac_key]

storage/src/delete_hmac_key.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
2+
/**
3+
* Copyright 2019 Google Inc.
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_delete_hmac_key]
27+
use Google\Cloud\Storage\StorageClient;
28+
29+
/**
30+
* Delete an HMAC key.
31+
*
32+
* @param string $accessId Access ID for an HMAC key.
33+
* @param string $projectId Google Cloud Project ID.
34+
*
35+
*/
36+
function delete_hmac_key($accessId, $projectId)
37+
{
38+
$storage = new StorageClient();
39+
// By default hmacKey will use the projectId used by StorageClient().
40+
$hmacKey = $storage->hmacKey($accessId, $projectId);
41+
42+
$hmacKey->delete();
43+
print(
44+
'The key is deleted, though it may still appear in the results of calls ' .
45+
'to StorageClient.hmacKeys([\'showDeletedKeys\' => true])' . PHP_EOL
46+
);
47+
}
48+
# [END storage_get_hmac_key]

storage/src/get_hmac_key.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
/**
3+
* Copyright 2019 Google Inc.
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_hmac_key]
27+
use Google\Cloud\Storage\StorageClient;
28+
29+
/**
30+
* Get an HMAC key.
31+
*
32+
* @param string $accessId Access ID for an HMAC key.
33+
* @param string $projectId Google Cloud Project ID.
34+
*
35+
*/
36+
function get_hmac_key($accessId, $projectId)
37+
{
38+
$storage = new StorageClient();
39+
$hmacKey = $storage->hmacKey($accessId, $projectId);
40+
41+
printf('HMAC key Metadata: %s' . PHP_EOL, print_r($hmacKey->info(), true));
42+
}
43+
# [END storage_get_hmac_key]

storage/src/list_hmac_keys.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2+
/**
3+
* Copyright 2019 Google Inc.
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_list_hmac_keys]
27+
use Google\Cloud\Storage\StorageClient;
28+
29+
/**
30+
* List HMAC keys.
31+
*
32+
* @param string $projectId Google Cloud Project ID.
33+
*
34+
*/
35+
function list_hmac_keys($projectId)
36+
{
37+
$storage = new StorageClient();
38+
// By default hmacKeys will use the projectId used by StorageClient() to list HMAC Keys.
39+
$hmacKeys = $storage->hmacKeys(['projectId' => $projectId]);
40+
41+
foreach ($hmacKeys as $hmacKey) {
42+
printf('HMAC key Metadata: %s' . PHP_EOL, print_r($hmacKey->info(), true));
43+
}
44+
}
45+
# [END storage_list_hmac_keys]

0 commit comments

Comments
 (0)