Skip to content

feat(parametermanager): Added samples for kms_key field in parameter manager #4071

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 5 commits into
base: main
Choose a base branch
from

Conversation

durgesh-ninave-crest
Copy link

Description

Created samples for the KMS key field in the global and regional parameter manager.

Samples List:

  1. create_regional_param_with_kms_key
  2. remove_regional_param_kms_key
  3. update_regional_param_kms_key
  4. create_param_with_kms_key
  5. remove_param_kms_key
  6. update_param_kms_key

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 (let us know which ones)
  • These samples need a new/updated env vars in testing projects set to pass (let us know which ones)
  • 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
  • This sample adds a new sample directory, and I created GitHub Actions workflow for this sample
  • This sample adds a new Product API, and I updated the Blunderbuss issue/PR auto-assigner with the codeowners for this sample
  • Please merge this PR for me once it is approved

@product-auto-label product-auto-label bot added the samples Issues that are directly related to samples. label Apr 9, 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 @durgesh-ninave-crest, 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 introduces samples for managing KMS keys with the Parameter Manager, both for global and regional parameters. It includes samples for creating, updating, and removing KMS keys associated with parameters. The changes involve adding new JavaScript files for each of these operations, along with updates to the package.json file and test files to include the new samples.

Highlights

  • New samples: Added six new samples demonstrating how to create, update, and remove KMS keys for both global and regional parameters using the Parameter Manager SDK.
  • Regional parameter support: The samples include specific examples for regional parameters, showcasing the use of regional endpoints.
  • KMS key integration: Demonstrates how to integrate KMS keys for encryption of parameters, providing enhanced security.

Changelog

Click here to see the changelog
  • parametermanager/createParamWithKmsKey.js
    • Added a new sample to create a global parameter with a KMS key.
    • Includes code to instantiate a Parameter Manager client and create a parameter with the specified KMS key.
  • parametermanager/package.json
    • Updated dependencies to include @google-cloud/kms and other development dependencies.
    • Modified the test script to include testing for the new KMS key samples.
  • parametermanager/regional_samples/createRegionalParamWithKmsKey.js
    • Added a new sample to create a regional parameter with a KMS key.
    • Includes code to set the regional endpoint and create a regional parameter with the specified KMS key.
  • parametermanager/regional_samples/removeRegionalParamKmsKey.js
    • Added a new sample to remove a KMS key from a regional parameter.
    • Includes code to update the parameter and remove the KMS key association.
  • parametermanager/regional_samples/updateRegionalParamKmsKey.js
    • Added a new sample to update a regional parameter with a KMS key.
    • Includes code to update the parameter with the specified KMS key.
  • parametermanager/removeParamKmsKey.js
    • Added a new sample to remove a KMS key from a global parameter.
    • Includes code to update the parameter and remove the KMS key association.
  • parametermanager/test/.eslintrc.yaml
    • Added eslint configuration for mocha environment
  • parametermanager/test/parametermanager.test.js
    • Added tests for the new KMS key samples, covering create, update, and remove operations for both global and regional parameters.
    • Includes setup and teardown logic to create and destroy test parameters and KMS keys.
  • parametermanager/updateParamKmsKey.js
    • Added a new sample to update a global parameter with a KMS key.
    • Includes code to update the parameter with the specified KMS key.
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.


In realms of code, where secrets reside,
A KMS key, our trusted guide.
Encrypting data, safe and sound,
Parameter Manager, security profound.

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 samples for using KMS keys with the Parameter Manager service, covering both global and regional parameters. The samples include creating, updating, and removing KMS keys. Overall, the code is well-structured and easy to follow. However, there are a few areas that could be improved for clarity and consistency.

Summary of Findings

  • Missing KMS Key Version Destruction: The tests create KMS keys and crypto keys, but do not destroy the crypto key versions after use. This could lead to resource accumulation and unexpected costs.
  • Inconsistent Parameter ID Naming: In the tests, some parameter IDs are appended with '-1' during creation, while others are not. This inconsistency can be confusing and should be addressed for clarity.
  • Redundant KMS Key Parameter in Remove Samples: The remove samples include a KMS key parameter in the CLI arguments, but this parameter is not actually used in the code. This can be misleading to users.

Merge Readiness

The pull request is almost ready for merging. However, the identified issues should be addressed first. Specifically, the KMS key version destruction in tests is important to prevent resource accumulation. The inconsistent parameter ID naming and redundant KMS key parameter in remove samples should also be fixed for clarity and consistency. I am unable to approve this pull request, and recommend that another reviewer also approves this code before merging.

Comment on lines +192 to +230
try {
await kmsClient.destroyCryptoKeyVersion({
name: `${kmsKey}/cryptoKeyVersions/1`,
});
} catch (error) {
if (error.code === 5) {
// If the method is not found, skip it.
}
}

try {
await kmsClient.destroyCryptoKeyVersion({
name: `${kmsKey1}/cryptoKeyVersions/1`,
});
} catch (error) {
if (error.code === 5) {
// If the method is not found, skip it.
}
}

try {
await kmsClient.destroyCryptoKeyVersion({
name: `${regionalKmsKey}/cryptoKeyVersions/1`,
});
} catch (error) {
if (error.code === 5) {
// If the method is not found, skip it.
}
}

try {
await kmsClient.destroyCryptoKeyVersion({
name: `${regionalKmsKey1}/cryptoKeyVersions/1`,
});
} catch (error) {
if (error.code === 5) {
// If the method is not found, skip it.
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

high

Consider destroying the crypto key versions after the tests to avoid accumulating resources and incurring costs. You can use kmsClient.destroyCryptoKeyVersion to destroy the versions.

* @param {string} projectId - The Google Cloud project ID where the parameter is to be updated.
* @param {string} locationId - The ID of the region where parameter is to be updated.
* @param {string} parameterId - The ID of the parameter to update. This ID must be unique within the project.
*/
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 function description mentions that the parameter ID must be unique within the project, but it should also specify that it must be unique within the region.

async function main(
projectId = 'my-project',
locationId = 'us-central1',
parameterId = 'my-parameter'
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 kmsKey parameter is not used in this function. Consider removing it from the function signature to avoid confusion.

Suggested change
parameterId = 'my-parameter'
locationId = 'us-central1',
parameterId = 'my-parameter'
) {

*
* @param {string} projectId - The Google Cloud project ID where the parameter is to be updated.
* @param {string} parameterId - The ID of the parameter to update. This ID must be unique within the project.
*/
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 function description mentions that the parameter ID must be unique within the project.


it('should create a parameter with kms_key', async () => {
const output = execSync(
`node createParamWithKmsKey.js ${projectId} ${parameterId}-1 ${kmsKey}`
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

Inconsistent naming: Why is '-1' appended here but not in other create parameter tests? Consider removing the '-1' to keep the naming consistent with other tests.

    const output = execSync(
      `node createParamWithKmsKey.js ${projectId} ${parameterId} ${kmsKey}`
    );


it('should create a regional parameter with kms_key', async () => {
const output = execSync(
`node regional_samples/createRegionalParamWithKmsKey.js ${projectId} ${locationId} ${regionalParameterId}-1 ${regionalKmsKey}`
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

Inconsistent naming: Why is '-1' appended here but not in other create parameter tests? Consider removing the '-1' to keep the naming consistent with other tests.

    const output = execSync(
      `node regional_samples/createRegionalParamWithKmsKey.js ${projectId} ${locationId} ${regionalParameterId} ${regionalKmsKey}`
    );

@durgesh-ninave-crest durgesh-ninave-crest marked this pull request as ready for review May 2, 2025 10:28
@durgesh-ninave-crest durgesh-ninave-crest requested review from a team as code owners May 2, 2025 10:28
Copy link

snippet-bot bot commented May 2, 2025

Here is the summary of changes.

You are about to add 6 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

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

Successfully merging this pull request may close these issues.

1 participant