Skip to content

Commit 7f1b382

Browse files
authored
Merge pull request #2874 from GoogleCloudPlatform/nodejs-secret-manager-migration
migrate code from googleapis/nodejs-secret-manager
2 parents 9910bb9 + 3574078 commit 7f1b382

22 files changed

+1155
-0
lines changed

.github/workflows/secret-manager.yaml

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
name: secret-manager
2+
on:
3+
push:
4+
branches:
5+
- main
6+
paths:
7+
- 'secret-manager/**'
8+
pull_request:
9+
paths:
10+
- 'secret-manager/**'
11+
pull_request_target:
12+
types: [labeled]
13+
paths:
14+
- 'secret-manager/**'
15+
schedule:
16+
- cron: '0 0 * * 0'
17+
jobs:
18+
test:
19+
if: ${{ github.event.action != 'labeled' || github.event.label.name == 'actions:force-run' }}
20+
runs-on: ubuntu-latest
21+
timeout-minutes: 60
22+
permissions:
23+
contents: 'write'
24+
pull-requests: 'write'
25+
id-token: 'write'
26+
steps:
27+
- uses: actions/checkout@v3.1.0
28+
with:
29+
ref: ${{github.event.pull_request.head.sha}}
30+
- uses: 'google-github-actions/auth@v1.0.0'
31+
with:
32+
workload_identity_provider: 'projects/1046198160504/locations/global/workloadIdentityPools/github-actions-pool/providers/github-actions-provider'
33+
service_account: 'kokoro-system-test@long-door-651.iam.gserviceaccount.com'
34+
create_credentials_file: 'true'
35+
access_token_lifetime: 600s
36+
- uses: actions/setup-node@v3.5.1
37+
with:
38+
node-version: 16
39+
- run: npm install
40+
working-directory: secret-manager
41+
- run: npm test
42+
working-directory: secret-manager
43+
env:
44+
MOCHA_REPORTER_SUITENAME: secret_manager
45+
MOCHA_REPORTER_OUTPUT: secret_manager_sponge_log.xml
46+
MOCHA_REPORTER: xunit
47+
- if: ${{ github.event.action == 'labeled' && github.event.label.name == 'actions:force-run' }}
48+
uses: actions/github-script@v6
49+
with:
50+
github-token: ${{ secrets.GITHUB_TOKEN }}
51+
script: |
52+
try {
53+
await github.rest.issues.removeLabel({
54+
name: 'actions:force-run',
55+
owner: 'GoogleCloudPlatform',
56+
repo: 'nodejs-docs-samples',
57+
issue_number: context.payload.pull_request.number
58+
});
59+
} catch (e) {
60+
if (!e.message.includes('Label does not exist')) {
61+
throw e;
62+
}
63+
}
64+
- if: ${{ github.event_name == 'schedule'}}
65+
run: |
66+
curl https://github.com/googleapis/repo-automation-bots/releases/download/flakybot-1.1.0/flakybot -o flakybot -s -L
67+
chmod +x ./flakybot
68+
./flakybot --repo GoogleCloudPlatform/nodejs-docs-samples --commit_hash ${{github.sha}} --build_url https://github.com/${{github.repository}}/actions/runs/${{github.run_id}}

.github/workflows/workflows.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
"datastore/functions",
6060
"service-directory/snippets",
6161
"scheduler",
62+
"secret-manager",
6263
"speech",
6364
"talent",
6465
"texttospeech",

secret-manager/accessSecretVersion.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2019 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
async function main(name = 'projects/my-project/secrets/my-secret/versions/1') {
18+
// [START secretmanager_access_secret_version]
19+
/**
20+
* TODO(developer): Uncomment these variables before running the sample.
21+
*/
22+
// const name = 'projects/my-project/secrets/my-secret/versions/5';
23+
// const name = 'projects/my-project/secrets/my-secret/versions/latest';
24+
25+
// Imports the Secret Manager library
26+
const {SecretManagerServiceClient} = require('@google-cloud/secret-manager');
27+
28+
// Instantiates a client
29+
const client = new SecretManagerServiceClient();
30+
31+
async function accessSecretVersion() {
32+
const [version] = await client.accessSecretVersion({
33+
name: name,
34+
});
35+
36+
// Extract the payload as a string.
37+
const payload = version.payload.data.toString();
38+
39+
// WARNING: Do not print the secret in a production environment - this
40+
// snippet is showing how to access the secret material.
41+
console.info(`Payload: ${payload}`);
42+
}
43+
44+
accessSecretVersion();
45+
// [END secretmanager_access_secret_version]
46+
}
47+
48+
const args = process.argv.slice(2);
49+
main(...args).catch(console.error);

secret-manager/addSecretVersion.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2019 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
async function main(parent = 'projects/my-project/secrets/my-secret') {
18+
// [START secretmanager_add_secret_version]
19+
/**
20+
* TODO(developer): Uncomment these variables before running the sample.
21+
*/
22+
// const parent = 'projects/my-project/secrets/my-secret';
23+
24+
// Imports the Secret Manager library
25+
const {SecretManagerServiceClient} = require('@google-cloud/secret-manager');
26+
27+
// Instantiates a client
28+
const client = new SecretManagerServiceClient();
29+
30+
// Payload is the plaintext data to store in the secret
31+
const payload = Buffer.from('my super secret data', 'utf8');
32+
33+
async function addSecretVersion() {
34+
const [version] = await client.addSecretVersion({
35+
parent: parent,
36+
payload: {
37+
data: payload,
38+
},
39+
});
40+
41+
console.log(`Added secret version ${version.name}`);
42+
}
43+
44+
addSecretVersion();
45+
// [END secretmanager_add_secret_version]
46+
}
47+
48+
const args = process.argv.slice(2);
49+
main(...args).catch(console.error);

secret-manager/createSecret.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright 2019 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
async function main(parent = 'projects/my-project', secretId = 'my-secret') {
18+
// [START secretmanager_create_secret]
19+
/**
20+
* TODO(developer): Uncomment these variables before running the sample.
21+
*/
22+
// const parent = 'projects/my-project';
23+
// const secretId = 'my-secret';
24+
25+
// Imports the Secret Manager library
26+
const {SecretManagerServiceClient} = require('@google-cloud/secret-manager');
27+
28+
// Instantiates a client
29+
const client = new SecretManagerServiceClient();
30+
31+
async function createSecret() {
32+
const [secret] = await client.createSecret({
33+
parent: parent,
34+
secretId: secretId,
35+
secret: {
36+
replication: {
37+
automatic: {},
38+
},
39+
},
40+
});
41+
42+
console.log(`Created secret ${secret.name}`);
43+
}
44+
45+
createSecret();
46+
// [END secretmanager_create_secret]
47+
}
48+
49+
const args = process.argv.slice(2);
50+
main(...args).catch(console.error);

secret-manager/createUmmrSecret.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
async function main(
18+
parent = 'projects/my-project',
19+
secretId = 'my-secret',
20+
...locations
21+
) {
22+
/**
23+
* TODO(developer): Uncomment these variables before running the sample.
24+
*/
25+
// const parent = 'projects/my-project';
26+
// const secretId = 'my-secret';
27+
// const locations = ['us-east1', 'us-east4', 'us-west1'];
28+
29+
// Imports the Secret Manager library
30+
const {SecretManagerServiceClient} = require('@google-cloud/secret-manager');
31+
32+
// Instantiates a client
33+
const client = new SecretManagerServiceClient();
34+
35+
async function createSecret() {
36+
const [secret] = await client.createSecret({
37+
parent: parent,
38+
secretId: secretId,
39+
secret: {
40+
replication: {
41+
userManaged: {
42+
replicas: locations.map(x => {
43+
return {location: x};
44+
}),
45+
},
46+
},
47+
},
48+
});
49+
50+
console.log(`Created secret ${secret.name}`);
51+
}
52+
53+
createSecret();
54+
}
55+
56+
const args = process.argv.slice(2);
57+
main(...args).catch(console.error);

secret-manager/deleteSecret.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright 2019 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
async function main(name = 'projects/my-project/secrets/my-secret') {
18+
// [START secretmanager_delete_secret]
19+
/**
20+
* TODO(developer): Uncomment these variables before running the sample.
21+
*/
22+
// const name = 'projects/my-project/secrets/my-secret';
23+
24+
// Imports the Secret Manager library
25+
const {SecretManagerServiceClient} = require('@google-cloud/secret-manager');
26+
27+
// Instantiates a client
28+
const client = new SecretManagerServiceClient();
29+
30+
async function deleteSecret() {
31+
await client.deleteSecret({
32+
name: name,
33+
});
34+
35+
console.log(`Deleted secret ${name}`);
36+
}
37+
38+
deleteSecret();
39+
// [END secretmanager_delete_secret]
40+
}
41+
42+
const args = process.argv.slice(2);
43+
main(...args).catch(console.error);
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright 2019 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
async function main(name = 'projects/my-project/secrets/my-secret/versions/1') {
18+
// [START secretmanager_destroy_secret_version]
19+
/**
20+
* TODO(developer): Uncomment these variables before running the sample.
21+
*/
22+
// const name = 'projects/my-project/secrets/my-secret/versions/5';
23+
24+
// Imports the Secret Manager library
25+
const {SecretManagerServiceClient} = require('@google-cloud/secret-manager');
26+
27+
// Instantiates a client
28+
const client = new SecretManagerServiceClient();
29+
30+
async function destroySecretVersion() {
31+
const [version] = await client.destroySecretVersion({
32+
name: name,
33+
});
34+
35+
console.info(`Destroyed ${version.name}`);
36+
}
37+
38+
destroySecretVersion();
39+
// [END secretmanager_destroy_secret_version]
40+
}
41+
42+
const args = process.argv.slice(2);
43+
main(...args).catch(console.error);

0 commit comments

Comments
 (0)