-
Notifications
You must be signed in to change notification settings - Fork 2k
feat(secretmanager): Adding expiretime samples #4217
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
Open
khilan-crest
wants to merge
14
commits into
GoogleCloudPlatform:main
Choose a base branch
from
khilan-crest:node_expiretime_samples
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
4afb60e
feat(secretmanager): Adding tags samples
khilan-crest 0a6a9aa
feat(secretmanager): Add secret version
khilan-crest b9b8b80
feat(secretmanager): Adding cmek samples
khilan-crest 4512af6
feat(secretmanager): Add secret version
khilan-crest be91f1f
Merge branch 'node_list_samples' of https://github.com/khilan-crest/n…
khilan-crest fa6ec18
Merge branch 'node_list_samples' of https://github.com/khilan-crest/n…
khilan-crest de18831
feat(secretmanager): Update formatting
khilan-crest 8fe4418
feat(secretmanager): Update formatting
khilan-crest 400dd49
feat(secretmanager): Adding expire time samples
khilan-crest eb7d8a8
feat(secretmanager): updating as per comment
khilan-crest a11e7ac
feat(secretmanager): updating as per comment
khilan-crest 1d534b5
Merge branch 'node_cmek_samples' of https://github.com/khilan-crest/n…
khilan-crest 8289251
feat(secretmanager): update test
khilan-crest c4376a6
Merge branch 'node_cmek_samples' of https://github.com/khilan-crest/n…
khilan-crest 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,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); | ||
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 @@ | ||
| // 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(); | ||
khilan-crest marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // [END secretmanager_create_secret_with_expiration] | ||
| } | ||
|
|
||
| const args = process.argv.slice(2); | ||
| main(...args).catch(console.error); | ||
62 changes: 62 additions & 0 deletions
62
secret-manager/createSecretWithUserManagedReplicationPolicy.js
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,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, | ||
khilan-crest marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }, | ||
| }; | ||
|
|
||
| // Create the secret | ||
| const [secret] = await client.createSecret({ | ||
| parent: parent, | ||
| secretId: secretId, | ||
| secret: secretConfig, | ||
| }); | ||
|
|
||
| console.log(`Created secret: ${secret.name}`); | ||
| } | ||
|
|
||
| createUmmrSecret(); | ||
khilan-crest marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // [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); | ||
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,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); |
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,72 @@ | ||
| // 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, tagValue) { | ||
| // [START secretmanager_detach_tag_binding] | ||
| /** | ||
| * TODO(developer): Uncomment these variables before running the sample. | ||
| */ | ||
| // const name = 'projects/my-project/secrets/my-secret'; | ||
| // const tagValue = 'tagValues/123456789012'; | ||
|
|
||
| // Import the Resource Manager and Secret Manager libraries | ||
| const {TagBindingsClient} = require('@google-cloud/resource-manager').v3; | ||
|
|
||
| // Create the Resource Manager client | ||
| const rmClient = new TagBindingsClient(); | ||
|
|
||
| // Build the resource name of the parent secret | ||
| const parent = `//secretmanager.googleapis.com/${name}`; | ||
|
|
||
| async function detachTag() { | ||
| // Find the binding name for the given tag value | ||
| let bindingName = null; | ||
| const iterable = rmClient.listTagBindingsAsync( | ||
| { | ||
| parent: parent, | ||
| pageSize: 50, | ||
| }, | ||
| {autoPaginate: false} | ||
| ); | ||
|
|
||
| for await (const binding of iterable) { | ||
| if (binding.tagValue === tagValue) { | ||
| bindingName = binding.name; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| if (bindingName === null) { | ||
| console.log(`Tag binding for value ${tagValue} not found on ${name}.`); | ||
| return; | ||
| } | ||
|
|
||
| // Delete the tag binding | ||
| const [operation] = await rmClient.deleteTagBinding({ | ||
| name: bindingName, | ||
| }); | ||
|
|
||
| // Wait for the operation to complete | ||
| await operation.promise(); | ||
| console.log(`Detached tag value ${tagValue} from ${name}`); | ||
| } | ||
|
|
||
| detachTag(); | ||
khilan-crest marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // [END secretmanager_detach_tag_binding] | ||
| } | ||
|
|
||
| const args = process.argv.slice(2); | ||
| main(...args).catch(console.error); | ||
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,47 @@ | ||
| // 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 = 'projects/my-project/secrets/my-secret') { | ||
| // [START secretmanager_list_secret_versions_with_filter] | ||
| /** | ||
| * TODO(developer): Uncomment these variables before running the sample. | ||
| */ | ||
| // const parent = 'projects/my-project/secrets/my-secret'; | ||
| const filterStr = 'state=DISABLED'; | ||
|
|
||
| // Imports the Secret Manager library | ||
| const {SecretManagerServiceClient} = require('@google-cloud/secret-manager'); | ||
|
|
||
| // Instantiates a client | ||
| const client = new SecretManagerServiceClient(); | ||
|
|
||
| async function listSecretVersionsWithFilter() { | ||
| const [versions] = await client.listSecretVersions({ | ||
| parent: parent, | ||
| filter: filterStr, | ||
| }); | ||
|
|
||
| versions.forEach(version => { | ||
| console.log(`Found version: ${version.name}`); | ||
| }); | ||
| } | ||
|
|
||
| listSecretVersionsWithFilter(); | ||
khilan-crest marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // [END secretmanager_list_secret_versions_with_filter] | ||
| } | ||
|
|
||
| const args = process.argv.slice(2); | ||
| main(...args).catch(console.error); | ||
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.