Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Storage] HMAC key samples #946

Merged
merged 30 commits into from Sep 11, 2019
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
31d4694
Add base HMAC key samples
frankyn Aug 20, 2019
300e0d1
Lint
frankyn Aug 20, 2019
559af70
Fix typo
frankyn Aug 21, 2019
f2b3759
Dont redefine projectId
frankyn Aug 21, 2019
4783bad
Use specific service account
frankyn Aug 27, 2019
03037ad
fix missing accessId
frankyn Aug 27, 2019
0c9e24c
Fix typo
frankyn Aug 27, 2019
eceb508
Access projectId as static variable
frankyn Aug 27, 2019
8a4dcc2
Add scope to method call
frankyn Aug 27, 2019
407be11
Remove options typo
frankyn Aug 27, 2019
9f61d2d
typo in serviceAccountEmail
frankyn Aug 27, 2019
6dde4e3
Update service account email usage in tests
frankyn Aug 27, 2019
34061e1
Update tests and library version
frankyn Aug 27, 2019
2ac043d
typo in serviceAccountEmail option
frankyn Aug 27, 2019
b7fccb2
Add missing accessId
frankyn Aug 27, 2019
335981e
Fix sample use of projectId
frankyn Aug 27, 2019
f72586d
Add projectId as a clear parameter
frankyn Aug 27, 2019
ecad177
Update format
frankyn Aug 27, 2019
f45424f
Fix tests
frankyn Aug 28, 2019
8b01905
fix lint issue
frankyn Aug 28, 2019
dc7ea3d
address comments
frankyn Sep 5, 2019
d41e2de
Update tests
frankyn Sep 9, 2019
11ed587
Update sample tests
frankyn Sep 9, 2019
3864cb2
Fix lint
frankyn Sep 9, 2019
c3d1185
address feedback
frankyn Sep 9, 2019
bea4dcb
Update storage/src/activate_hmac_key.php
frankyn Sep 10, 2019
a546f37
Update storage/README.md
frankyn Sep 10, 2019
7a44547
Apply suggestions from code review
frankyn Sep 10, 2019
6413141
Address comments
frankyn Sep 10, 2019
390cfa8
Fix lint
frankyn Sep 10, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions storage/README.md
Expand Up @@ -48,6 +48,9 @@ This simple command-line application demonstrates how to invoke
get-object-v2-signed-url Generate a v2 signed URL for downloading an object.
get-object-v4-signed-url Generate a v4 signed URL for downloading an object.
get-object-v4-upload-signed-url Generate a v4 signed URL for uploading an object.
hmac-sa-manage Manage HMAC Service Account keys.
hmac-sa-list List HMAC Service Account keys.
hmac-sa-create Create an HMAC Service Account key.
```
6. Run `php storage.php COMMAND --help` to print information about the usage of each command.

Expand Down
8 changes: 7 additions & 1 deletion storage/composer.json
@@ -1,6 +1,6 @@
{
"require": {
"google/cloud-storage": "^1.9.0",
"google/cloud-storage": "^1.14.0",
"paragonie/random_compat": "^2.0",
"symfony/console": " ^3.0"
},
Expand All @@ -13,21 +13,25 @@
"src/add_object_acl.php",
"src/copy_object.php",
"src/create_bucket.php",
"src/create_hmac_key.php",
"src/delete_bucket.php",
"src/delete_bucket_acl.php",
"src/delete_bucket_default_acl.php",
"src/delete_object.php",
"src/delete_object_acl.php",
"src/delete_hmac_key.php",
"src/disable_bucket_policy_only.php",
"src/disable_default_event_based_hold.php",
"src/disable_requester_pays.php",
"src/deactivate_hmac_key.php",
"src/download_encrypted_object.php",
"src/download_file_requester_pays.php",
"src/download_object.php",
"src/enable_bucket_policy_only.php",
"src/enable_default_event_based_hold.php",
"src/enable_default_kms_key.php",
"src/enable_requester_pays.php",
"src/activate_hmac_key.php",
"src/generate_encryption_key.php",
"src/bucket_metadata.php",
"src/get_bucket_acl.php",
Expand All @@ -44,9 +48,11 @@
"src/get_requester_pays_status.php",
"src/get_retention_policy.php",
"src/get_default_event_based_hold.php",
"src/get_hmac_key.php",
"src/list_buckets.php",
"src/list_objects.php",
"src/list_objects_with_prefix.php",
"src/list_hmac_keys.php",
"src/lock_retention_policy.php",
"src/make_public.php",
"src/move_object.php",
Expand Down
47 changes: 47 additions & 0 deletions storage/src/activate_hmac_key.php
@@ -0,0 +1,47 @@
<?php
/**
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* For instructions on how to run the full sample:
*
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/storage/README.md
*/

namespace Google\Cloud\Samples\Storage;

# [START storage_activate_hmac_key]
use Google\Cloud\Storage\StorageClient;

/**
* Activate HMAC key.
frankyn marked this conversation as resolved.
Show resolved Hide resolved
*
* @param string $accessId access ID for an inactive HMAC key.
frankyn marked this conversation as resolved.
Show resolved Hide resolved
* @param string $projectId Google Cloud Project ID.
*
*/
function activate_hmac_key($accessId, $projectId)
{
$storage = new StorageClient();
// By default hmacKey will use the projectId used by StorageClient().
$hmacKey = $storage->hmacKey($accessId, $projectId);

$hmacKey->update('ACTIVE');

print("The HMAC key is now active." . PHP_EOL);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a minor note, but WDYT about using single quotes? It is typically a best practice unless you need string interpolation.

printf("HMAC key Metadata: %s" . PHP_EOL, print_r($hmacKey->info(), true));
}
# [END storage_activate_hmac_key]
1 change: 0 additions & 1 deletion storage/src/create_bucket.php
Expand Up @@ -32,7 +32,6 @@
* @param string $bucketName name of the bucket to create.
* @param string $options options for the new bucket.
*
* @return Google\Cloud\Storage\Bucket the newly created bucket.
*/
function create_bucket($bucketName, $options = [])
{
Expand Down
46 changes: 46 additions & 0 deletions storage/src/create_hmac_key.php
@@ -0,0 +1,46 @@
<?php
/**
* Copyright 2019 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* For instructions on how to run the full sample:
*
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/storage/README.md
*/

namespace Google\Cloud\Samples\Storage;

# [START storage_create_hmac_key]
use Google\Cloud\Storage\StorageClient;

/**
* Create a new HMAC key.
*
* @param string $serviceAccountEmail service account email to associate with new HMAC key.
frankyn marked this conversation as resolved.
Show resolved Hide resolved
* @param string $projectId Google Cloud Project ID.
*
*/
function create_hmac_key($serviceAccountEmail, $projectId)
{
$storage = new StorageClient();
// By default createHmacKey will use the projectId used by StorageClient().
$hmacKeyCreated = $storage->createHmacKey($serviceAccountEmail, ['projectId' => $projectId]);

printf("The base64 encoded secret is: %s" . PHP_EOL, $hmacKeyCreated->secret());
print("Do not miss that secret, there is no API to recover it." . PHP_EOL);
printf("HMAC key Metadata: %s" . PHP_EOL, print_r($hmacKeyCreated->hmacKey()->info(), true));
}
# [END storage_create_hmac_key]
47 changes: 47 additions & 0 deletions storage/src/deactivate_hmac_key.php
@@ -0,0 +1,47 @@
<?php
/**
* Copyright 2019 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* For instructions on how to run the full sample:
*
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/storage/README.md
*/

namespace Google\Cloud\Samples\Storage;

# [START storage_deactivate_hmac_key]
use Google\Cloud\Storage\StorageClient;

/**
* Deactivate HMAC key.
frankyn marked this conversation as resolved.
Show resolved Hide resolved
*
* @param string $accessId access ID for an inactive HMAC key.
frankyn marked this conversation as resolved.
Show resolved Hide resolved
* @param string $projectId Google Cloud Project ID.
*
*/
function deactivate_hmac_key($accessId, $projectId)
{
$storage = new StorageClient();
// By default hmacKey will use the projectId used by StorageClient().
$hmacKey = $storage->hmacKey($accessId, $projectId);

$hmacKey->update('INACTIVE');

print("The HMAC key is now inactive." . PHP_EOL);
printf("HMAC key Metadata: %s" . PHP_EOL, print_r($hmacKey->info(), true));
}
# [END storage_deactivate_hmac_key]
46 changes: 46 additions & 0 deletions storage/src/delete_hmac_key.php
@@ -0,0 +1,46 @@
<?php
/**
* Copyright 2019 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* For instructions on how to run the full sample:
*
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/storage/README.md
*/

namespace Google\Cloud\Samples\Storage;

# [START storage_delete_hmac_key]
use Google\Cloud\Storage\StorageClient;

/**
* Delete HMAC key.
frankyn marked this conversation as resolved.
Show resolved Hide resolved
*
* @param string $accessId access ID for an inactive HMAC key.
frankyn marked this conversation as resolved.
Show resolved Hide resolved
* @param string $projectId Google Cloud Project ID.
*
*/
function delete_hmac_key($accessId, $projectId)
{
$storage = new StorageClient();
// By default hmacKey will use the projectId used by StorageClient().
$hmacKey = $storage->hmacKey($accessId, $projectId);

$hmacKey->delete();
print("The key is deleted, though it may still appear in results StorageClient.hmacKeys(\$options=['showDeletedKeys'=>true])");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A small suggestion here:

print(
    'The key is deleted, though it may still appear in the results of calls ' .
    'to StorageClient.hmacKeys([\'showDeletedKeys\' => true])' . PHP_EOL
);

print(" when is used." . PHP_EOL);
}
# [END storage_get_hmac_key]
43 changes: 43 additions & 0 deletions storage/src/get_hmac_key.php
@@ -0,0 +1,43 @@
<?php
/**
* Copyright 2019 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* For instructions on how to run the full sample:
*
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/storage/README.md
*/

namespace Google\Cloud\Samples\Storage;

# [START storage_get_hmac_key]
use Google\Cloud\Storage\StorageClient;

/**
* Get HMAC key.
frankyn marked this conversation as resolved.
Show resolved Hide resolved
*
* @param string $accessId access ID for HMAC key.
frankyn marked this conversation as resolved.
Show resolved Hide resolved
* @param string $projectId Google Cloud Project ID.
*
*/
function get_hmac_key($accessId, $projectId)
{
$storage = new StorageClient();
$hmacKey = $storage->hmacKey($accessId, $projectId);

printf("HMAC key Metadata: %s" . PHP_EOL, print_r($hmacKey->info(), true));
}
# [END storage_get_hmac_key]
45 changes: 45 additions & 0 deletions storage/src/list_hmac_keys.php
@@ -0,0 +1,45 @@
<?php
/**
* Copyright 2019 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* For instructions on how to run the full sample:
*
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/storage/README.md
*/

namespace Google\Cloud\Samples\Storage;

# [START storage_list_hmac_keys]
use Google\Cloud\Storage\StorageClient;

/**
* List HMAC keys.
*
* @param string $projectId Google Cloud Project ID.
*
*/
function list_hmac_keys($projectId)
{
$storage = new StorageClient();
// By default hmacKeys will use the projectId used by StorageClient() list HMAC Keys.
frankyn marked this conversation as resolved.
Show resolved Hide resolved
$hmacKeys = $storage->hmacKeys(['projectId' => $projectId]);

foreach ($hmacKeys as $hmacKey) {
printf("HMAC key Metadata: %s" . PHP_EOL, print_r($hmacKey->info(), true));
}
}
# [END storage_list_hmac_keys]