Skip to content

feat(parametermanager): Added samples for kms_key in regional parameter #5262

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

Conversation

durgesh-ninave-crest
Copy link
Contributor

Description

Added samples for kms_key field in regional parameter using Parameter manager SDK

Sample List (regional):

  1. create_regional_param_with_kms_key
  2. update_regional_param_kms_key
  3. remove_regional_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) - For testing regional samples it requires GOLANG_REGIONAL_SAMPLES_LOCATION variable to be set
  • These samples need a new/updated env vars in testing projects set to pass (let us know which ones)
  • 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 7, 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 @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 using KMS keys with regional parameters in the Parameter Manager SDK. It includes new functions for creating, updating, and removing KMS keys associated with regional parameters. Additionally, it incorporates corresponding tests to ensure the functionality works as expected. The changes also include updates to the go.mod and go.sum files to reflect the new dependencies and versions.

Highlights

  • New Samples: Added three new samples demonstrating how to create, update, and remove KMS keys for regional parameters.
  • KMS Key Integration: Introduced functionality to manage KMS keys for encrypting regional parameters, enhancing security.
  • Testing: Included comprehensive tests to validate the new KMS key functionalities for regional parameters.

Changelog

Click here to see the changelog
  • parametermanager/go.mod
    • Upgraded the go version to 1.23.0
    • Added cloud.google.com/go/kms as a new dependency.
    • Updated cloud.google.com/go/parametermanager to v0.2.0.
    • Updated google.golang.org/api to v0.228.0.
    • Updated google.golang.org/genproto to v0.0.0-20250404141209-ee84b53bf3d0.
    • Updated google.golang.org/grpc to v1.71.1.
  • parametermanager/go.sum
    • Updated checksums for all the modules in go.mod
  • parametermanager/regional_samples/create_regional_param_with_kms_key.go
    • Added a new function createRegionalParamWithKmsKey to create a regional parameter with a KMS key.
    • The function takes projectID, locationID, parameterID, and kmsKey as arguments.
    • It uses the Parameter Manager SDK to create the parameter with the specified KMS key.
  • parametermanager/regional_samples/regional_parametermanager_test.go
    • Added tests for creating, updating, and removing KMS keys for regional parameters.
    • Includes helper functions for creating test names, retrieving test locations, creating parameters with KMS keys, and cleaning up resources after testing.
    • The tests verify the functionality of the new samples by checking the output and ensuring the parameters are created, updated, and removed correctly.
  • parametermanager/regional_samples/remove_regional_param_kms_key.go
    • Added a new function removeRegionalParamKmsKey to remove the KMS key from a regional parameter.
    • The function takes projectID, locationID, and parameterID as arguments.
    • It uses the Parameter Manager SDK to update the parameter and remove the KMS key.
  • parametermanager/regional_samples/update_regional_param_kms_key.go
    • Added a new function updateRegionalParamKmsKey to update the KMS key for a regional parameter.
    • The function takes projectID, locationID, parameterID, and kmsKey as arguments.
    • 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.


In regions wide, parameters reside,
With KMS keys, their secrets hide.
Create, update, then erase,
A digital, secured space,
Where data's safety is the guide.

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 using KMS keys with regional parameters in the Parameter Manager SDK. The changes include adding new sample files and updating the go.mod and go.sum files. Overall, the code seems well-structured and includes necessary tests. However, there are a few areas that could be improved for clarity and efficiency.

Summary of Findings

  • Error message consistency: The error messages in the sample functions could be more consistent and informative. Consider adding context to the error messages to help users understand the root cause of the failure.
  • KMS Key Version Cleanup: The test cleanup function testCleanupKeyVersions attempts to delete key versions, but this operation is not directly supported. Instead, the crypto key itself should be disabled or destroyed. The current implementation might lead to confusion or unexpected behavior.
  • Key Ring and Key Creation: The test functions testCreateKeyRing and testCreateKeyHSM check if the key ring and key already exist before creating them. However, the check is performed by attempting to get the resource and handling a NotFound error. This approach is less efficient than using a dedicated "exists" check if available in the API.

Merge Readiness

The pull request is almost ready for merging. However, the issues related to error message consistency and KMS key version cleanup should be addressed before merging. I am unable to directly approve the pull request, and users should have others review and approve this code before merging. Addressing the high severity issues is recommended before merging.

Comment on lines +116 to +134
// testCleanupKeyVersions deletes the specified key version in the GCP project.
// It fails the test if the key version deletion fails.
func testCleanupKeyVersions(t *testing.T, name string) {
t.Helper()
ctx := context.Background()

client, err := kms.NewKeyManagementClient(ctx)
if err != nil {
t.Fatalf("failed to create client: %v", err)
}
defer client.Close()

if _, err := client.DestroyCryptoKeyVersion(ctx, &kmspb.DestroyCryptoKeyVersionRequest{
Name: name,
}); 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.

high

The DestroyCryptoKeyVersion method is used to delete key versions, but this operation is not directly supported. Instead, the crypto key itself should be disabled or destroyed. Consider updating this function to disable or destroy the crypto key instead of attempting to delete the key version, as this is the correct way to clean up the KMS resources.

@@ -1,40 +1,61 @@
module github.com/GoogleCloudPlatform/golang-samples/parametermanager

go 1.22.7
go 1.23.0

Choose a reason for hiding this comment

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

medium

Consider using go 1.22 instead of go 1.23 to align with the go version used in other golang-samples, unless there is a specific reason to use go 1.23.

}
parameter, err := client.CreateParameter(ctx, req)
if err != nil {
return fmt.Errorf("Failed to create parameter: %w", err)

Choose a reason for hiding this comment

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

medium

Consider adding more context to the error message to indicate which parameter failed to create. This will help in debugging.

For example, you could include the parameter ID in the error message.

Suggested change
return fmt.Errorf("Failed to create parameter: %w", err)
return fmt.Errorf("failed to create parameter %s: %w", parameterID, err)

Comment on lines +152 to +170
// Check if key ring already exists
req := &kmspb.GetKeyRingRequest{
Name: parent + "/keyRings/" + keyRingId,
}
_, err = client.GetKeyRing(ctx, req)
if err != nil {
if terr, ok := grpcstatus.FromError(err); !ok || terr.Code() != grpccodes.NotFound {
t.Fatalf("failed to get key ring: %v", err)
}
// Key ring not found, create it
req := &kmspb.CreateKeyRingRequest{
Parent: parent,
KeyRingId: keyRingId,
}
_, err = client.CreateKeyRing(ctx, req)
if err != nil {
t.Fatalf("failed to create key ring: %v", err)
}
}

Choose a reason for hiding this comment

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

medium

The test functions testCreateKeyRing and testCreateKeyHSM check if the key ring and key already exist before creating them. However, the check is performed by attempting to get the resource and handling a NotFound error. This approach is less efficient than using a dedicated "exists" check if available in the API. Consider using a more efficient method to check for the existence of the resources before attempting to create them.

}
parameter, err := client.UpdateParameter(ctx, req)
if err != nil {
return fmt.Errorf("Failed to update parameter: %w", err)

Choose a reason for hiding this comment

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

medium

Consider adding more context to the error message to indicate which parameter failed to update. This will help in debugging.

For example, you could include the parameter ID in the error message.

Suggested change
return fmt.Errorf("Failed to update parameter: %w", err)
return fmt.Errorf("failed to update parameter %s: %w", parameterID, err)

}
parameter, err := client.UpdateParameter(ctx, req)
if err != nil {
return fmt.Errorf("Failed to update parameter: %w", err)

Choose a reason for hiding this comment

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

medium

Consider adding more context to the error message to indicate which parameter failed to update. This will help in debugging.

For example, you could include the parameter ID in the error message.

Suggested change
return fmt.Errorf("Failed to update parameter: %w", err)
return fmt.Errorf("failed to update parameter %s: %w", parameterID, err)

@durgesh-ninave-crest durgesh-ninave-crest marked this pull request as ready for review April 9, 2025 06:07
@durgesh-ninave-crest durgesh-ninave-crest requested review from a team as code owners April 9, 2025 06:07
Copy link

snippet-bot bot commented Apr 9, 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

@arpangoswami arpangoswami added kokoro:force-run Add this label to force Kokoro to re-run the tests. kokoro:run Add this label to force Kokoro to re-run the tests. labels Apr 9, 2025
@kokoro-team kokoro-team removed kokoro:run Add this label to force Kokoro to re-run the tests. kokoro:force-run Add this label to force Kokoro to re-run the tests. labels Apr 9, 2025
@arpangoswami
Copy link
Contributor

Please resolve the merge conflicts

@durgesh-ninave-crest
Copy link
Contributor Author

Please resolve the merge conflicts

@arpangoswami Resolved the merge conflict

@OremGLG OremGLG assigned OremGLG and unassigned OremGLG Apr 9, 2025
@OremGLG OremGLG self-requested a review April 9, 2025 20:16
@OremGLG
Copy link
Contributor

OremGLG commented Apr 11, 2025

Please, apply the same changes that were requested in the past PRs:

  • If you are building the same string multiple times, it could be better to create a variable that stores this string and re-use it, by following this we can reduce the line lenght.
  • Avoid the jumpline "\n" in the got assignment, it is enough to validate if the string contains the essential content.

@OremGLG OremGLG self-assigned this Apr 11, 2025
@OremGLG OremGLG added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Apr 11, 2025
@kokoro-team kokoro-team removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Apr 11, 2025
@durgesh-ninave-crest
Copy link
Contributor Author

Please, apply the same changes that were requested in the past PRs:

  • If you are building the same string multiple times, it could be better to create a variable that stores this string and re-use it, by following this we can reduce the line lenght.
  • Avoid the jumpline "\n" in the got assignment, it is enough to validate if the string contains the essential content.

I've applied all the requested changes, including reusing the string variable and removing the newline from the got assignment.

@OremGLG
Copy link
Contributor

OremGLG commented Apr 14, 2025

Please, fix your PR conflicts in order to re-run the checks.

@durgesh-ninave-crest
Copy link
Contributor Author

Please, fix your PR conflicts in order to re-run the checks.

@OremGLG Resolved the merge conflict.

@OremGLG
Copy link
Contributor

OremGLG commented Apr 15, 2025

Still with merge conflicts 😬

@durgesh-ninave-crest
Copy link
Contributor Author

durgesh-ninave-crest commented Apr 16, 2025

Still with merge conflicts 😬

One of the PR got merged, which caused the conflict — I’ve resolved it.

@arpangoswami arpangoswami enabled auto-merge (squash) April 17, 2025 05:31
auto-merge was automatically disabled April 17, 2025 17:59

Head branch was pushed to by a user without write access

@telpirion telpirion enabled auto-merge (squash) April 17, 2025 19:48
@telpirion telpirion merged commit 1e606a4 into GoogleCloudPlatform:main Apr 17, 2025
9 checks passed
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.

5 participants