-
Notifications
You must be signed in to change notification settings - Fork 1k
Add Secret Manager samples #1010
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
Merged
sethvargo
merged 11 commits into
GoogleCloudPlatform:master
from
sethvargo:sethvargo/secretmanager_samples
Jan 17, 2020
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
4d84996
Add Secret Manager samples
sethvargo a586116
Remove dlp references
sethvargo a2171c1
4 spaces in composer json
sethvargo a980d1b
Fix copyright headers
sethvargo 1f6c798
Fix import path, comments
sethvargo dad4ed6
Fix destroy comment
sethvargo b3e6571
Fix disable comment
sethvargo b6ae184
Fix enable comment
sethvargo d8237e8
Use parseName instead
sethvargo 31d9c10
Fix indentation
sethvargo 46406ea
Import gax error
sethvargo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| # Google Secret Manager PHP Sample Application | ||
|
|
||
| [![Open in Cloud Shell][shell_img]][shell_link] | ||
|
|
||
| [shell_img]: http://gstatic.com/cloudssh/images/open-btn.svg | ||
| [shell_link]: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googlecloudplatform/php-docs-samples&page=editor&working_dir=secretmanager | ||
|
|
||
| ## Description | ||
|
|
||
| This simple command-line application demonstrates how to invoke | ||
| [Google Secret Manager][secretmanager] from PHP. | ||
|
|
||
| ## Build and Run | ||
|
|
||
| 1. **Enable APIs** - [Enable the Secret Manager | ||
| API](https://console.cloud.google.com/flows/enableapi?apiid=secretmanager.googleapis.com) | ||
| and create a new project or select an existing project. | ||
|
|
||
| 1. **Download The Credentials** - Click "Go to credentials" after enabling the | ||
| APIs. Click "New Credentials" and select "Service Account Key". Create a new | ||
| service account, use the JSON key type, and select "Create". Once | ||
| downloaded, set the environment variable `GOOGLE_APPLICATION_CREDENTIALS` to | ||
| the path of the JSON key that was downloaded. | ||
|
|
||
| 1. **Clone the repo** and cd into this directory | ||
|
|
||
| ```text | ||
| $ git clone https://github.com/GoogleCloudPlatform/php-docs-samples | ||
| $ cd php-docs-samples/secretmanager | ||
| ``` | ||
| 1. **Install dependencies** via [Composer][install-composer]. If composer is | ||
| installed locally: | ||
| ```text | ||
| $ php composer.phar install | ||
| ``` | ||
| If composer is installed globally: | ||
| ```text | ||
| $ composer install | ||
| ``` | ||
| 1. Execute the snippets in the [src/](src/) directory by running: | ||
| ```text | ||
| $ php src/SNIPPET_NAME.php | ||
| ``` | ||
| The usage will print for each if no arguments are provided. | ||
| See the [Secret Manager Documentation](https://cloud.google.com/secret-manager/docs) for more information. | ||
| ## Contributing changes | ||
| * See [CONTRIBUTING.md](../CONTRIBUTING.md) | ||
| ## Licensing | ||
| * See [LICENSE](../LICENSE) | ||
| [install-composer]: http://getcomposer.org/doc/00-intro.md | ||
| [secretmanager]: https://cloud.google.com/secret-manager |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| { | ||
| "require": { | ||
| "google/cloud-secret-manager": "^0.1.0" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <!-- | ||
| Copyright 2020 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. | ||
| --> | ||
| <phpunit bootstrap="../testing/bootstrap.php"> | ||
| <testsuites> | ||
| <testsuite name="PHP Secret Manager test"> | ||
| <directory>test</directory> | ||
| </testsuite> | ||
| </testsuites> | ||
| <logging> | ||
| <log type="coverage-clover" target="build/logs/clover.xml"/> | ||
| </logging> | ||
| <filter> | ||
| <whitelist> | ||
| <directory suffix=".php">./src</directory> | ||
| <exclude> | ||
| <directory>./vendor</directory> | ||
| </exclude> | ||
| </whitelist> | ||
| </filter> | ||
| <php> | ||
| <env name="PHPUNIT_TESTS" value="1"/> | ||
| </php> | ||
| </phpunit> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| <?php | ||
| /** | ||
| * Copyright 2020 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. | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| require_once __DIR__ . '/vendor/autoload.php'; | ||
|
|
||
| if (count($argv) != 3) { | ||
| return printf("Usage: php %s PROJECT_ID SECRET_ID\n", basename(__FILE__)); | ||
| } | ||
| list($_, $projectId, $secretId) = $argv; | ||
|
|
||
| // [START secretmanager_quickstart] | ||
| // Import the Secret Manager client library. | ||
| use Google\Cloud\SecretManager\V1beta1\Replication; | ||
| use Google\Cloud\SecretManager\V1beta1\Replication\Automatic; | ||
| use Google\Cloud\SecretManager\V1beta1\Secret; | ||
| use Google\Cloud\SecretManager\V1beta1\SecretManagerServiceClient; | ||
| use Google\Cloud\SecretManager\V1beta1\SecretPayload; | ||
|
|
||
| /** Uncomment and populate these variables in your code */ | ||
| // $projectId = 'YOUR_GOOGLE_CLOUD_PROJECT' (e.g. 'my-project'); | ||
| // $secretId = 'YOUR_SECRET_ID' (e.g. 'my-secret'); | ||
|
|
||
| // Create the Secret Manager client. | ||
| $client = new SecretManagerServiceClient(); | ||
|
|
||
| // Build the parent name from the project. | ||
| $parent = $client->projectName($projectId); | ||
|
|
||
| // Create the parent secret. | ||
| $secret = $client->createSecret($parent, $secretId, [ | ||
| 'secret' => new Secret([ | ||
| 'replication' => new Replication([ | ||
| 'automatic' => new Automatic(), | ||
| ]), | ||
| ]), | ||
| ]); | ||
|
|
||
| // Add the secret version. | ||
| $version = $client->addSecretVersion($secret->getName(), new SecretPayload([ | ||
| 'data' => 'hello world', | ||
| ])); | ||
|
|
||
| // Access the secret version. | ||
| $response = $client->accessSecretVersion($version->getName()); | ||
|
|
||
| // Print the secret payload. | ||
| // | ||
| // WARNING: Do not print the secret in a production environment - this | ||
| // snippet is showing how to access the secret material. | ||
| $payload = $response->getPayload()->getData(); | ||
| printf('Plaintext: %s' . PHP_EOL, $payload); | ||
| // [END secretmanager_quickstart] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| <?php | ||
| /* | ||
| * Copyright 2020 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/secretmanager/README.md | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| require_once __DIR__ . '/../vendor/autoload.php'; | ||
|
|
||
| if (count($argv) != 4) { | ||
| return printf("Usage: php %s PROJECT_ID SECRET_ID VERSION_ID\n", basename(__FILE__)); | ||
| } | ||
| list($_, $projectId, $secretId, $versionId) = $argv; | ||
|
|
||
| // [START secretmanager_access_secret_version] | ||
dwsupplee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // Import the Secret Manager client library. | ||
| use Google\Cloud\SecretManager\V1beta1\SecretManagerServiceClient; | ||
|
|
||
| /** Uncomment and populate these variables in your code */ | ||
| // $projectId = 'YOUR_GOOGLE_CLOUD_PROJECT' (e.g. 'my-project'); | ||
| // $secretId = 'YOUR_SECRET_ID' (e.g. 'my-secret'); | ||
| // $versionId = 'YOUR_VERSION_ID' (e.g. 'latest' or '5'); | ||
|
|
||
| // Create the Secret Manager client. | ||
| $client = new SecretManagerServiceClient(); | ||
|
|
||
| // Build the resource name of the secret version. | ||
| $name = $client->secretVersionName($projectId, $secretId, $versionId); | ||
|
|
||
| // Access the secret version. | ||
| $response = $client->accessSecretVersion($name); | ||
|
|
||
| // Print the secret payload. | ||
| // | ||
| // WARNING: Do not print the secret in a production environment - this | ||
| // snippet is showing how to access the secret material. | ||
| $payload = $response->getPayload()->getData(); | ||
| printf('Plaintext: %s', $payload); | ||
| // [END secretmanager_access_secret_version] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| <?php | ||
| /* | ||
| * Copyright 2020 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/secretmanager/README.md | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| require_once __DIR__ . '/../vendor/autoload.php'; | ||
|
|
||
| if (count($argv) != 3) { | ||
| return printf("Usage: php %s PROJECT_ID SECRET_ID\n", basename(__FILE__)); | ||
| } | ||
| list($_, $projectId, $secretId) = $argv; | ||
|
|
||
| // [START secretmanager_add_secret_version] | ||
| // Import the Secret Manager client library. | ||
| use Google\Cloud\SecretManager\V1beta1\SecretManagerServiceClient; | ||
| use Google\Cloud\SecretManager\V1beta1\SecretPayload; | ||
|
|
||
| /** Uncomment and populate these variables in your code */ | ||
| // $projectId = 'YOUR_GOOGLE_CLOUD_PROJECT' (e.g. 'my-project'); | ||
| // $secretId = 'YOUR_SECRET_ID' (e.g. 'my-secret'); | ||
|
|
||
| // Create the Secret Manager client. | ||
| $client = new SecretManagerServiceClient(); | ||
|
|
||
| // Build the resource name of the parent secret. | ||
| $parent = $client->secretName($projectId, $secretId); | ||
|
|
||
| // Access the secret version. | ||
| $response = $client->addSecretVersion($parent, new SecretPayload([ | ||
| 'data' => 'my super secret data', | ||
| ])); | ||
|
|
||
| // Print the new secret version name. | ||
| printf('Added secret version: %s', $response->getName()); | ||
| // [END secretmanager_add_secret_version] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| <?php | ||
| /* | ||
| * Copyright 2020 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/secretmanager/README.md | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| require_once __DIR__ . '/../vendor/autoload.php'; | ||
|
|
||
| if (count($argv) != 3) { | ||
| return printf("Usage: php %s PROJECT_ID SECRET_ID\n", basename(__FILE__)); | ||
| } | ||
| list($_, $projectId, $secretId) = $argv; | ||
|
|
||
| // [START secretmanager_create_secret] | ||
| // Import the Secret Manager client library. | ||
| use Google\Cloud\SecretManager\V1beta1\Replication; | ||
| use Google\Cloud\SecretManager\V1beta1\Replication\Automatic; | ||
| use Google\Cloud\SecretManager\V1beta1\Secret; | ||
| use Google\Cloud\SecretManager\V1beta1\SecretManagerServiceClient; | ||
|
|
||
| /** Uncomment and populate these variables in your code */ | ||
| // $projectId = 'YOUR_GOOGLE_CLOUD_PROJECT' (e.g. 'my-project'); | ||
| // $secretId = 'YOUR_SECRET_ID' (e.g. 'my-secret'); | ||
|
|
||
| // Create the Secret Manager client. | ||
| $client = new SecretManagerServiceClient(); | ||
|
|
||
| // Build the resource name of the parent project. | ||
| $parent = $client->projectName($projectId); | ||
|
|
||
| // Create the secret. | ||
| $secret = $client->createSecret($parent, $secretId, [ | ||
| 'secret' => new Secret([ | ||
| 'replication' => new Replication([ | ||
| 'automatic' => new Automatic(), | ||
| ]), | ||
| ]), | ||
| ]); | ||
|
|
||
| // Print the new secret name. | ||
| printf('Created secret: %s', $secret->getName()); | ||
| // [END secretmanager_create_secret] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.