Skip to content

Commit 4d84996

Browse files
committed
Add Secret Manager samples
1 parent 21a517b commit 4d84996

18 files changed

+1188
-0
lines changed

secretmanager/README.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Google DLP PHP Sample Application
2+
3+
[![Open in Cloud Shell][shell_img]][shell_link]
4+
5+
[shell_img]: http://gstatic.com/cloudssh/images/open-btn.svg
6+
[shell_link]: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googlecloudplatform/php-docs-samples&page=editor&working_dir=dlp
7+
8+
## Description
9+
10+
This simple command-line application demonstrates how to invoke
11+
[Google Secret Manager][secretmanager] from PHP.
12+
13+
## Build and Run
14+
15+
1. **Enable APIs** - [Enable the Secret Manager
16+
API](https://console.cloud.google.com/flows/enableapi?apiid=secretmanager.googleapis.com)
17+
and create a new project or select an existing project.
18+
19+
1. **Download The Credentials** - Click "Go to credentials" after enabling the
20+
APIs. Click "New Credentials" and select "Service Account Key". Create a new
21+
service account, use the JSON key type, and select "Create". Once
22+
downloaded, set the environment variable `GOOGLE_APPLICATION_CREDENTIALS` to
23+
the path of the JSON key that was downloaded.
24+
25+
1. **Clone the repo** and cd into this directory
26+
27+
```text
28+
$ git clone https://github.com/GoogleCloudPlatform/php-docs-samples
29+
$ cd php-docs-samples/secretmanager
30+
```
31+
32+
1. **Install dependencies** via [Composer][install-composer]. If composer is
33+
installed locally:
34+
35+
36+
```text
37+
$ php composer.phar install
38+
```
39+
40+
If composer is installed globally:
41+
42+
```text
43+
$ composer install
44+
```
45+
46+
1. Execute the snippets in the [src/](src/) directory by running:
47+
48+
```text
49+
$ php src/SNIPPET_NAME.php
50+
```
51+
52+
The usage will print for each if no arguments are provided.
53+
54+
See the [Secret Manager Documentation](https://cloud.google.com/secret-manager/docs) for more information.
55+
56+
## Contributing changes
57+
58+
* See [CONTRIBUTING.md](../CONTRIBUTING.md)
59+
60+
## Licensing
61+
62+
* See [LICENSE](../LICENSE)
63+
64+
[install-composer]: http://getcomposer.org/doc/00-intro.md
65+
[secretmanager]: https://cloud.google.com/secret-manager

secretmanager/composer.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"require": {
3+
"google/cloud-secret-manager": "^0.1.0"
4+
}
5+
}

secretmanager/phpunit.xml.dist

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
xml version="1.0" encoding="UTF-8"?>
2+
17+
<phpunit bootstrap="../testing/bootstrap.php">
18+
<testsuites>
19+
<testsuite name="PHP Secret Manager test">
20+
<directory>testdirectory>
21+
testsuite>
22+
testsuites>
23+
<logging>
24+
<log type="coverage-clover" target="build/logs/clover.xml"/>
25+
logging>
26+
<filter>
27+
<whitelist>
28+
<directory suffix=".php">./srcdirectory>
29+
<exclude>
30+
<directory>./vendordirectory>
31+
exclude>
32+
whitelist>
33+
filter>
34+
<php>
35+
<env name="PHPUNIT_TESTS" value="1"/>
36+
php>
37+
phpunit>

secretmanager/quickstart.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
declare(strict_types=1);
19+
20+
require_once __DIR__ . '/vendor/autoload.php';
21+
22+
if (count($argv) != 3) {
23+
return printf("Usage: php %s PROJECT_ID SECRET_ID\n", basename(__FILE__));
24+
}
25+
list($_, $projectId, $secretId) = $argv;
26+
27+
// [START secretmanager_quickstart]
28+
// Import the Secret Manager client library.
29+
use Google\Cloud\SecretManager\V1Beta1\Replication;
30+
use Google\Cloud\SecretManager\V1Beta1\Replication\Automatic;
31+
use Google\Cloud\SecretManager\V1Beta1\Secret;
32+
use Google\Cloud\SecretManager\V1Beta1\SecretManagerServiceClient;
33+
use Google\Cloud\SecretManager\V1Beta1\SecretPayload;
34+
35+
/** Uncomment and populate these variables in your code */
36+
// $projectId = 'YOUR_GOOGLE_CLOUD_PROJECT' (e.g. 'my-project');
37+
// $secretId = 'YOUR_SECRET_ID' (e.g. 'my-secret');
38+
39+
// Create the Secret Manager client.
40+
$client = new SecretManagerServiceClient();
41+
42+
// Build the parent name from the project.
43+
$parent = $client->projectName($projectId);
44+
45+
// Create the parent secret.
46+
$secret = $client->createSecret($parent, $secretId, [
47+
'secret' => new Secret([
48+
'replication' => new Replication([
49+
'automatic' => new Automatic(),
50+
]),
51+
]),
52+
]);
53+
54+
// Add the secret version.
55+
$version = $client->addSecretVersion($secret->getName(), new SecretPayload([
56+
'data' => 'hello world',
57+
]));
58+
59+
// Access the secret version.
60+
$response = $client->accessSecretVersion($version->getName());
61+
62+
// Print the secret payload.
63+
//
64+
// WARNING: Do not print the secret in a production environment - this
65+
// snippet is showing how to access the secret material.
66+
$payload = $response->getPayload()->getData();
67+
printf('Plaintext: %s' . PHP_EOL, $payload);
68+
// [END secretmanager_quickstart]
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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 VERSION_ID\n", basename(__FILE__));
30+
}
31+
list($_, $projectId, $secretId, $versionId) = $argv;
32+
33+
// [START secretmanager_access_secret_version]
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+
// versionId = 'YOUR_VERSION_ID' (e.g. 'latest' or '5');
41+
42+
// Create the Secret Manager client.
43+
$client = new SecretManagerServiceClient();
44+
45+
// Build the resource name of the secret version.
46+
$name = $client->secretVersionName($projectId, $secretId, $versionId);
47+
48+
// Access the secret version.
49+
$response = $client->accessSecretVersion($name);
50+
51+
// Print the secret payload.
52+
//
53+
// WARNING: Do not print the secret in a production environment - this
54+
// snippet is showing how to access the secret material.
55+
$payload = $response->getPayload()->getData();
56+
printf('Plaintext: %s', $payload);
57+
// [END secretmanager_access_secret_version]
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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) != 3) {
29+
return printf("Usage: php %s PROJECT_ID SECRET_ID\n", basename(__FILE__));
30+
}
31+
list($_, $projectId, $secretId) = $argv;
32+
33+
// [START secretmanager_add_secret_version]
34+
// Import the Secret Manager client library.
35+
use Google\Cloud\SecretManager\V1Beta1\SecretManagerServiceClient;
36+
use Google\Cloud\SecretManager\V1Beta1\SecretPayload;
37+
38+
/** Uncomment and populate these variables in your code */
39+
// $projectId = 'YOUR_GOOGLE_CLOUD_PROJECT' (e.g. 'my-project');
40+
// $secretId = 'YOUR_SECRET_ID' (e.g. 'my-secret');
41+
42+
// Create the Secret Manager client.
43+
$client = new SecretManagerServiceClient();
44+
45+
// Build the resource name of the parent secret.
46+
$parent = $client->secretName($projectId, $secretId);
47+
48+
// Access the secret version.
49+
$response = $client->addSecretVersion($parent, new SecretPayload([
50+
'data' => 'my super secret data',
51+
]));
52+
53+
// Print the new secret version name.
54+
printf('Added secret version: %s', $response->getName());
55+
// [END secretmanager_add_secret_version]

secretmanager/src/create_secret.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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) != 3) {
29+
return printf("Usage: php %s PROJECT_ID SECRET_ID\n", basename(__FILE__));
30+
}
31+
list($_, $projectId, $secretId) = $argv;
32+
33+
// [START secretmanager_create_secret]
34+
// Import the Secret Manager client library.
35+
use Google\Cloud\SecretManager\V1Beta1\Replication;
36+
use Google\Cloud\SecretManager\V1Beta1\Replication\Automatic;
37+
use Google\Cloud\SecretManager\V1Beta1\Secret;
38+
use Google\Cloud\SecretManager\V1Beta1\SecretManagerServiceClient;
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+
44+
// Create the Secret Manager client.
45+
$client = new SecretManagerServiceClient();
46+
47+
// Build the resource name of the parent project.
48+
$parent = $client->projectName($projectId);
49+
50+
// Create the secret.
51+
$secret = $client->createSecret($parent, $secretId, [
52+
'secret' => new Secret([
53+
'replication' => new Replication([
54+
'automatic' => new Automatic(),
55+
]),
56+
]),
57+
]);
58+
59+
// Print the new secret name.
60+
printf('Created secret: %s', $secret->getName());
61+
// [END secretmanager_create_secret]

0 commit comments

Comments
 (0)