-
Notifications
You must be signed in to change notification settings - Fork 2k
feat(secretmanager): Adding etag samples #4219
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
base: main
Are you sure you want to change the base?
Changes from all commits
4afb60e
0a6a9aa
b9b8b80
4512af6
be91f1f
fa6ec18
de18831
8fe4418
400dd49
22b217b
1b18d62
eb7d8a8
a11e7ac
1d534b5
ad9fa20
895a0f5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| // Copyright 2026 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 | ||
| // | ||
| // https://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. | ||
|
|
||
| 'use strict'; | ||
|
|
||
| async function main(parent, secretId, kmsKeyName) { | ||
| // [START secretmanager_create_secret_with_cmek] | ||
| /** | ||
| * TODO(developer): Uncomment these variables before running the sample. | ||
| */ | ||
| // const projectId = 'projects/my-project'; | ||
| // const secretId = 'my-secret-with-cmek'; | ||
| // const kmsKeyName = 'projects/my-project/locations/global/keyRings/my-keyring/cryptoKeys/my-key'; | ||
|
|
||
| // Import the Secret Manager library | ||
| const {SecretManagerServiceClient} = require('@google-cloud/secret-manager'); | ||
|
|
||
| // Create the Secret Manager client | ||
| const client = new SecretManagerServiceClient(); | ||
|
|
||
| async function createSecretWithCmek() { | ||
| // Create the secret with automatic replication and CMEK | ||
| const [secret] = await client.createSecret({ | ||
| parent: parent, | ||
| secretId: secretId, | ||
| secret: { | ||
| replication: { | ||
| automatic: { | ||
| customerManagedEncryption: { | ||
| kmsKeyName: kmsKeyName, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| console.log(`Created secret ${secret.name} with CMEK key ${kmsKeyName}`); | ||
| } | ||
|
|
||
| createSecretWithCmek(); | ||
| // [END secretmanager_create_secret_with_cmek] | ||
| } | ||
|
|
||
| const args = process.argv.slice(2); | ||
| main(...args).catch(console.error); | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,61 @@ | ||||||
| // Copyright 2026 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 | ||||||
| // | ||||||
| // https://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. | ||||||
|
|
||||||
| 'use strict'; | ||||||
|
|
||||||
| async function main(parent, secretId) { | ||||||
| // [START secretmanager_create_secret_with_expiration] | ||||||
| /** | ||||||
| * TODO(developer): Uncomment these variables before running the sample. | ||||||
| */ | ||||||
| // const parent = 'projects/my-project'; | ||||||
| // const secretId = 'my-secret'; | ||||||
|
|
||||||
| // Import the Secret Manager library | ||||||
| const {SecretManagerServiceClient} = require('@google-cloud/secret-manager'); | ||||||
|
|
||||||
| // Create the Secret Manager client | ||||||
| const client = new SecretManagerServiceClient(); | ||||||
|
|
||||||
| async function createSecretWithExpiration() { | ||||||
| // Calculate expiration time (1 hour from now) | ||||||
| const expireTime = new Date(); | ||||||
| expireTime.setHours(expireTime.getHours() + 1); | ||||||
|
|
||||||
| // Create the secret with automatic replication and expiration time | ||||||
| const [secret] = await client.createSecret({ | ||||||
| parent: parent, | ||||||
| secretId: secretId, | ||||||
| secret: { | ||||||
| replication: { | ||||||
| automatic: {}, | ||||||
| }, | ||||||
| expireTime: { | ||||||
| seconds: Math.floor(expireTime.getTime() / 1000), | ||||||
| nanos: (expireTime.getTime() % 1000) * 1000000, | ||||||
| }, | ||||||
| }, | ||||||
| }); | ||||||
|
|
||||||
| console.log( | ||||||
| `Created secret ${secret.name} with expiration time ${expireTime.toISOString()}` | ||||||
| ); | ||||||
| } | ||||||
|
|
||||||
| createSecretWithExpiration(); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Suggested change
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. await is added within function wherever required. |
||||||
| // [END secretmanager_create_secret_with_expiration] | ||||||
| } | ||||||
|
|
||||||
| const args = process.argv.slice(2); | ||||||
| main(...args).catch(console.error); | ||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,76 @@ | ||||||
| // Copyright 2026 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 | ||||||
| // | ||||||
| // https://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. | ||||||
|
|
||||||
| 'use strict'; | ||||||
|
|
||||||
| async function main(parent, secretId, topicName) { | ||||||
| // [START secretmanager_create_secret_with_rotation] | ||||||
| /** | ||||||
| * TODO(developer): Uncomment these variables before running the sample. | ||||||
| */ | ||||||
| // const parent = 'projects/my-project'; | ||||||
| // const secretId = 'my-rotating-secret'; | ||||||
| // const topicName = 'projects/my-project/topics/my-rotation-topic'; | ||||||
|
|
||||||
| // Import the Secret Manager library | ||||||
| const {SecretManagerServiceClient} = require('@google-cloud/secret-manager'); | ||||||
|
|
||||||
| // Create the Secret Manager client | ||||||
| const client = new SecretManagerServiceClient(); | ||||||
|
|
||||||
| async function createSecretWithRotation() { | ||||||
| // Set rotation period to 24 hours | ||||||
| const rotationPeriodHours = 24; | ||||||
|
|
||||||
| // Calculate next rotation time (24 hours from now) | ||||||
| const nextRotationTime = new Date(); | ||||||
| nextRotationTime.setHours(nextRotationTime.getHours() + 24); | ||||||
|
|
||||||
| // Create the secret with rotation configuration | ||||||
| const [secret] = await client.createSecret({ | ||||||
| parent: parent, | ||||||
| secretId: secretId, | ||||||
| secret: { | ||||||
| replication: { | ||||||
| automatic: {}, | ||||||
| }, | ||||||
| topics: [ | ||||||
| { | ||||||
| name: topicName, | ||||||
| }, | ||||||
| ], | ||||||
| rotation: { | ||||||
| nextRotationTime: { | ||||||
| seconds: Math.floor(nextRotationTime.getTime() / 1000), | ||||||
| nanos: (nextRotationTime.getTime() % 1000) * 1000000, | ||||||
| }, | ||||||
| rotationPeriod: { | ||||||
| seconds: rotationPeriodHours * 3600, | ||||||
| nanos: 0, | ||||||
| }, | ||||||
| }, | ||||||
| }, | ||||||
| }); | ||||||
|
|
||||||
| console.log( | ||||||
| `Created secret ${secret.name} with rotation period ${rotationPeriodHours} hours and topic ${topicName}` | ||||||
| ); | ||||||
| } | ||||||
|
|
||||||
| createSecretWithRotation(); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Suggested change
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. await is added within function wherever required. |
||||||
| // [END secretmanager_create_secret_with_rotation] | ||||||
| } | ||||||
|
|
||||||
| const args = process.argv.slice(2); | ||||||
| main(...args).catch(console.error); | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| // Copyright 2026 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 | ||
| // | ||
| // https://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. | ||
|
|
||
| 'use strict'; | ||
|
|
||
| async function main(parent, secretId, topicName) { | ||
| // [START secretmanager_create_secret_with_topic] | ||
| /** | ||
| * TODO(developer): Uncomment these variables before running the sample. | ||
| */ | ||
| // const parent = 'projects/my-project'; | ||
| // const secretId = 'my-secret-with-notifications'; | ||
| // const topicName = 'projects/my-project/topics/my-secret-topic'; | ||
|
|
||
| // Import the Secret Manager library | ||
| const {SecretManagerServiceClient} = require('@google-cloud/secret-manager'); | ||
|
|
||
| // Create the Secret Manager client | ||
| const client = new SecretManagerServiceClient(); | ||
|
|
||
| async function createSecretWithTopic() { | ||
| // Create the secret with topic configuration | ||
| const [secret] = await client.createSecret({ | ||
| parent: parent, | ||
| secretId: secretId, | ||
| secret: { | ||
| replication: { | ||
| automatic: {}, | ||
| }, | ||
| topics: [ | ||
| { | ||
| name: topicName, | ||
| }, | ||
| ], | ||
| }, | ||
| }); | ||
|
|
||
| console.log(`Created secret ${secret.name} with topic ${topicName}`); | ||
| } | ||
|
|
||
| createSecretWithTopic(); | ||
| // [END secretmanager_create_secret_with_topic] | ||
| } | ||
|
|
||
| const args = process.argv.slice(2); | ||
| main(...args).catch(console.error); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| // Copyright 2026 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 | ||
| // | ||
| // https://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. | ||
|
|
||
| 'use strict'; | ||
|
|
||
| async function main(parent, secretId, locations, ttl) { | ||
| // [START secretmanager_create_ummr_secret] | ||
| /** | ||
| * TODO(developer): Uncomment these variables before running the sample. | ||
| */ | ||
| // const parent = 'projects/my-project'; | ||
| // const secretId = 'my-new-secret'; | ||
| // const locations = ['us-east1', 'europe-west1']; | ||
| // const ttl = 7776000; // Optional: 90 days in seconds | ||
|
|
||
| // Import the Secret Manager library | ||
| const {SecretManagerServiceClient} = require('@google-cloud/secret-manager'); | ||
|
|
||
| // Create the Secret Manager client | ||
| const client = new SecretManagerServiceClient(); | ||
|
|
||
| async function createUmmrSecret() { | ||
| // Create the secret configuration | ||
| const secretConfig = { | ||
| replication: { | ||
| userManaged: { | ||
| replicas: locations.map(location => ({location})), | ||
| }, | ||
| }, | ||
| ttl: { | ||
| seconds: ttl, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
| }, | ||
| }; | ||
|
|
||
| // Create the secret | ||
| const [secret] = await client.createSecret({ | ||
| parent: parent, | ||
| secretId: secretId, | ||
| secret: secretConfig, | ||
| }); | ||
|
|
||
| console.log(`Created secret: ${secret.name}`); | ||
| } | ||
|
|
||
| createUmmrSecret(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. await is added within function wherever required. |
||
| // [END secretmanager_create_ummr_secret] | ||
| } | ||
|
|
||
| const args = process.argv.slice(2); | ||
| const locations = args[2] ? args[2].split(',') : []; | ||
| main(args[0], args[1], locations, args[3]).catch(console.error); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| // Copyright 2026 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 | ||
| // | ||
| // https://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. | ||
|
|
||
| 'use strict'; | ||
|
|
||
| async function main(name = 'projects/my-project/secrets/my-secret') { | ||
| // [START secretmanager_delete_secret_expiration] | ||
| /** | ||
| * TODO(developer): Uncomment these variables before running the sample. | ||
| */ | ||
| // const name = 'projects/my-project/secrets/my-secret'; | ||
|
|
||
| // Import the Secret Manager library | ||
| const {SecretManagerServiceClient} = require('@google-cloud/secret-manager'); | ||
|
|
||
| // Create the Secret Manager client | ||
| const client = new SecretManagerServiceClient(); | ||
|
|
||
| async function deleteSecretExpiration() { | ||
| // Update the secret with an empty expireTime field to remove the expiration | ||
| const [secret] = await client.updateSecret({ | ||
| secret: { | ||
| name: name, | ||
| // No expireTime field specified, which will clear it | ||
| }, | ||
| updateMask: { | ||
| paths: ['expire_time'], | ||
| }, | ||
| }); | ||
|
|
||
| console.log(`Removed expiration from secret ${secret.name}`); | ||
| } | ||
|
|
||
| await deleteSecretExpiration(); | ||
| // [END secretmanager_delete_secret_expiration] | ||
| } | ||
|
|
||
| const args = process.argv.slice(2); | ||
| main(...args).catch(console.error); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
createSecretWithCmekfunction isasync, but it's called withoutawaitin themainfunction. This meansmainmight complete before the secret creation is finished, potentially leading to unexpected behavior or unhandled rejections. Pleaseawaitthe call.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
await is added within function wherever required.