Skip to content

feat(parametermanager): Added samples for kms_key field #5240

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

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

vatsal-vora-crestdata
Copy link
Contributor

Description

Created samples for kms_key field using Parameter Manager SDK

Sample List:

  1. create_param_with_kms_key
  2. update_param_kms_key
  3. remove_param_kms_key

Added required Tests for the same.

Checklist

  • I have followed Contributing Guidelines from CONTRIBUTING.MD
  • Tests pass: go test -v ./.. (see Testing)
  • Code formatted: gofmt (see Formatting)
  • Vetting pass: go vet (see Formatting)
  • These samples need a new API enabled in testing projects to pass (let us know which ones) - This requires Parameter manager API to be enabled
  • These samples need a new/updated env vars in testing projects set to pass (let us know which ones) - For testing regional samples it requires GOLANG_REGIONAL_SAMPLES_LOCATION variable to be set
  • This sample adds a new sample directory, and I updated the CODEOWNERS file with the codeowners 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 2, 2025
Copy link

@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 @vatsal-vora-crestdata, 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 the kms_key field within the Parameter Manager SDK. It includes functionalities for creating, updating, and removing the kms_key associated with a parameter. The changes involve adding new Go files for each of these operations, updating the go.mod and go.sum files to include necessary dependencies, and adding corresponding tests to ensure the new functionalities work as expected.

Highlights

  • New Samples: Added three new samples: create_param_with_kms_key, update_param_kms_key, and remove_param_kms_key to demonstrate the usage of the kms_key field.
  • Dependency Updates: The go.mod and go.sum files have been updated to include the necessary dependencies for the Parameter Manager and KMS APIs.
  • Testing: Added new tests to verify the functionality of the new samples, including creating KMS keys and key rings for testing purposes.

Changelog

Click here to see the changelog
  • go.work
    • Added the parametermanager directory to the go.work file to include it in the workspace.
  • parametermanager/create_param_with_kms_key.go
    • Created a new function createParamWithKmsKey that creates a parameter with a KMS key.
    • The function takes projectID, parameterID, and kmsKey as input.
    • It uses the Parameter Manager SDK to create the parameter with the specified KMS key.
  • parametermanager/go.mod
    • Added dependencies for cloud.google.com/go/kms and cloud.google.com/go/parametermanager.
  • parametermanager/go.sum
    • Added checksums for the new dependencies.
  • parametermanager/parametermanager_test.go
    • Added new tests for creating, updating, and removing the kms_key field of a parameter.
    • Includes helper functions for creating KMS key rings and keys for testing.
    • Added cleanup functions to delete the created resources after testing.
  • parametermanager/remove_param_kms_key.go
    • Created a new function removeParamKmsKey that removes the KMS key from a parameter.
    • The function takes projectID and parameterID as input.
    • It uses the Parameter Manager SDK to update the parameter and remove the KMS key.
  • parametermanager/update_param_kms_key.go
    • Created a new function updateParamKmsKey that updates the KMS key of a parameter.
    • The function takes projectID, parameterID, and kmsKey as input.
    • It uses the Parameter Manager SDK 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.


A KMS key,
A shield for secrets kept,
Parameter's guard.

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

@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 the kms_key field in the Parameter Manager SDK. The changes include creating, updating, and removing parameters with KMS keys, along with corresponding tests. Overall, the code is well-structured and addresses the feature request. However, there are a few areas that could be improved for clarity and efficiency.

Summary of Findings

  • Error Handling in Tests: The test functions testCleanupParameter and testCleanupKeyVersions use t.Fatalf when a deletion fails, even if the resource is not found. This can cause tests to fail unnecessarily. It's better to log the error and continue if the resource is not found.
  • KMS Key Version Cleanup: The testCleanupKeyVersions function attempts to destroy a crypto key version, but the sample code doesn't create any key versions. This cleanup function might not be necessary or might need adjustment based on how key versions are managed in the samples.
  • Redundant Parameter Format Setting: In removeParamKmsKey, the Parameter struct in the UpdateParameterRequest sets the Format field to UNFORMATTED, which might be redundant since the goal is to remove the KMS key, not change the format.

Merge Readiness

The pull request is almost ready for merging. Addressing the error handling in the cleanup functions and reviewing the necessity of the testCleanupKeyVersions function would improve the reliability and clarity of the tests. The redundant parameter format setting in removeParamKmsKey should also be addressed. I am unable to approve the pull request, and users should have others review and approve this code before merging. Once these points are addressed, the pull request should be in good shape to be merged.

Comment on lines +78 to +81
}); err != nil {
if terr, ok := grpcstatus.FromError(err); !ok || terr.Code() != grpccodes.NotFound {
t.Fatalf("testCleanupParameter: failed to delete parameter: %v", err)
}

Choose a reason for hiding this comment

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

medium

Instead of t.Fatalf, consider using t.Logf or t.Errorf and returning. This allows the test to continue and potentially clean up other resources, even if one deletion fails because the resource was already gone. This makes the tests more robust.

if terr, ok := grpcstatus.FromError(err); ok && terr.Code() == grpccodes.NotFound {
			t.Logf("testCleanupParameter: parameter %s not found, skipping deletion", name)
			return
		}
		t.Fatalf("testCleanupParameter: failed to delete parameter: %v", err)

Comment on lines +96 to +99
}); err != nil {
if terr, ok := grpcstatus.FromError(err); !ok || terr.Code() != grpccodes.NotFound {
t.Fatalf("testCleanupKeyVersion: failed to delete key version: %v", err)
}

Choose a reason for hiding this comment

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

medium

Similar to testCleanupParameter, use t.Logf or t.Errorf instead of t.Fatalf to allow the test to continue even if the key version is not found. Also, double check if this cleanup is actually needed, since the sample code doesn't create any key versions.

if terr, ok := grpcstatus.FromError(err); ok && terr.Code() == grpccodes.NotFound {
			t.Logf("testCleanupKeyVersion: key version %s not found, skipping deletion", name)
			return
		}
		t.Fatalf("testCleanupKeyVersion: failed to delete key version: %v", err)

@durgesh-ninave-crest
Copy link
Contributor

Raised a duplicate of this PR #5261 because Vatsal's CLA has expired, which is blocking the merge. Once the new PR is merged, will close the original one.

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.

2 participants