Skip to content

chore(secretmanager): add global samples for delayed destroy #4073

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
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

YashSaraf11
Copy link

Description

Fixes - N/A

Note: Before submitting a pull request, please open an issue for discussion if you are not associated with Google.

Checklist

  • I have followed guidelines from CONTRIBUTING.MD and Samples Style Guide
  • Tests pass: npm test (see Testing)
  • Lint pass: npm run lint (see Style)
  • These samples need a new API enabled in testing projects to pass - N/A
  • These samples need a new/updated env vars in testing projects set to pass - N/A
  • This pull request is from a branch created directly off of GoogleCloudPlatform/nodejs-docs-samples. Not a fork.
  • This sample adds a new sample directory, and I updated the CODEOWNERS file with the codeowners for this sample - N/A
  • This sample adds a new sample directory, and I created GitHub Actions workflow for this sample - N/A
  • This sample adds a new Product API, and I updated the Blunderbuss issue/PR auto-assigner with the codeowners for this sample - N/A
  • Please merge this PR for me once it is approved

@YashSaraf11 YashSaraf11 requested review from a team as code owners April 14, 2025 11:06
Copy link

snippet-bot bot commented Apr 14, 2025

Here is the summary of changes.

You are about to add 3 region tags.

This comment is generated by snippet-bot.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment

@product-auto-label product-auto-label bot added api: secretmanager Issues related to the Secret Manager API. samples Issues that are directly related to samples. labels Apr 14, 2025
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @YashSaraf11, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

Summary of Changes

This pull request adds new samples for the Secret Manager service related to delayed destroy functionality. It includes samples for creating a secret with delayed destroy enabled, disabling delayed destroy, and updating a secret's delayed destroy settings. The PR also includes corresponding tests for these new samples.

Highlights

  • New samples: Adds createSecretWithDelayedDestroy.js, disableSecretDelayedDestroy.js, and updateSecretWithDelayedDestroy.js to demonstrate delayed destroy functionality.
  • Test coverage: Adds new tests to secretmanager.test.js to verify the functionality of the new samples.

Changelog

  • secret-manager/createSecretWithDelayedDestroy.js
    • Adds a new sample to create a secret with delayed destroy enabled using version_destroy_ttl.
  • secret-manager/disableSecretDelayedDestroy.js
    • Adds a new sample to disable the delayed destroy setting on a secret by updating the version_destroy_ttl.
  • secret-manager/test/secretmanager.test.js
    • Adds tests for creating, disabling, and updating secrets with delayed destroy functionality.
  • secret-manager/updateSecretWithDelayedDestroy.js
    • Adds a new sample to update the delayed destroy setting on a secret.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.


A secret's life,
A time to live, a time to fade,
Destroy, but not yet.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

The pull request introduces new samples for creating, disabling, and updating secrets with delayed destroy functionality in the Secret Manager. The changes include new JavaScript files for the samples and corresponding tests. Overall, the code seems well-structured and the tests cover the main functionalities. However, there are a few areas that could be improved for clarity and robustness.

Summary of Findings

  • Missing Error Handling: The samples lack explicit error handling for the API calls. While the .catch(console.error) at the end of the main function will catch some errors, it doesn't provide specific feedback or handling within the core logic of the functions. This could make debugging more difficult.
  • Inconsistent Naming in Tests: In the tests, the secret names are sometimes hardcoded and sometimes dynamically generated. This inconsistency can lead to confusion and potential issues with test cleanup.
  • Lack of Input Validation: The samples do not validate the input parameters, such as timeToLive. This could lead to unexpected behavior if invalid values are provided.

Merge Readiness

The pull request introduces important new functionality with adequate tests. However, addressing the error handling and input validation concerns would significantly improve the robustness and maintainability of the code. I recommend addressing these issues before merging. I am unable to directly approve this pull request, and recommend that others review and approve this code before merging.

seconds: timeToLive,
},
},
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Consider adding error handling here to catch any potential exceptions during secret creation. This will provide more informative error messages and prevent the program from crashing unexpectedly.

    }).catch(err => {
      console.error(`Failed to create secret: ${err}`);
      throw err; // Re-throw the error to prevent further execution
    });

updateMask: {
paths: ['version_destroy_ttl'],
},
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Consider adding error handling here to catch any potential exceptions during secret updating. This will provide more informative error messages and prevent the program from crashing unexpectedly.

    }).catch(err => {
      console.error(`Failed to disable delayed destroy: ${err}`);
      throw err; // Re-throw the error to prevent further execution
    });

updateMask: {
paths: ['version_destroy_ttl'],
},
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Consider adding error handling here to catch any potential exceptions during secret updating. This will provide more informative error messages and prevent the program from crashing unexpectedly.

    }).catch(err => {
      console.error(`Failed to update secret: ${err}`);
      throw err; // Re-throw the error to prevent further execution
    });

Comment on lines +570 to +571
`node disableSecretDelayedDestroy.js ${secret.name}-delayedDestroy`
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The secret name ${secret.name}-delayedDestroy is hardcoded here. It would be better to generate a unique secret ID for this test to avoid potential conflicts with other tests or existing secrets. Also, consider adding a try-catch block around the deleteSecret call to handle cases where the secret might not exist.

    const delayedDestroySecretId = `${secretId}-delayedDestroy-${v4()}`;
    await client.createSecret({
      parent: `projects/${projectId}`,
      secretId: delayedDestroySecretId,
      secret: {
        replication: {
          automatic: {},
        },
        version_destroy_ttl: {
          seconds: 24 * 60 * 60,
        },
      },
    });

    const output = execSync(
      `node disableSecretDelayedDestroy.js projects/${projectId}/secrets/${delayedDestroySecretId}`
    );
    assert.match(output, new RegExp('Disabled delayed destroy'));

    try {
      await client.deleteSecret({
        name: `projects/${projectId}/secrets/${delayedDestroySecretId}`,
      });
    } catch (err) {
      console.warn(`Failed to delete secret ${delayedDestroySecretId}: ${err}`);
    }

Comment on lines +594 to +595
const output = execSync(
`node updateSecretWithDelayedDestroy.js ${secret.name}-delayedDestroy ${updatedTimeToLive}`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The secret name ${secret.name}-delayedDestroy is hardcoded here. It would be better to generate a unique secret ID for this test to avoid potential conflicts with other tests or existing secrets. Also, consider adding a try-catch block around the deleteSecret call to handle cases where the secret might not exist.

    const delayedDestroySecretId = `${secretId}-delayedDestroy-${v4()}`;
    const updatedTimeToLive = 24 * 60 * 60 * 2;
    await client.createSecret({
      parent: `projects/${projectId}`,
      secretId: delayedDestroySecretId,
      secret: {
        replication: {
          automatic: {},
        },
        version_destroy_ttl: {
          seconds: 24 * 60 * 60,
        },
      },
    });

    const output = execSync(
      `node updateSecretWithDelayedDestroy.js projects/${projectId}/secrets/${delayedDestroySecretId} ${updatedTimeToLive}`
    );
    assert.match(output, new RegExp('Updated secret'));
    try {
      await client.deleteSecret({
        name: `projects/${projectId}/secrets/${delayedDestroySecretId}`,
      });
    } catch (err) {
      console.warn(`Failed to delete secret ${delayedDestroySecretId}: ${err}`);
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: secretmanager Issues related to the Secret Manager API. samples Issues that are directly related to samples.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants