Skip list_secrets call when case_sensitive=True#862
Open
ecerulm wants to merge 5 commits into
Open
Conversation
af49a11 to
e9b9997
Compare
When case_sensitive=True, secret names are used as-is without any name translation, so there's no need to enumerate all secrets via list_secrets (which requires secretmanager.secrets.list permission). __getitem__ now calls _get_secret_value directly when case_sensitive=True. get_field_value likewise uses env_name directly when case_sensitive=True, avoiding access to _secret_name_map (and the underlying list_secrets call) even when a SecretVersion is specified.
- Add _is_not_found_error() helper using google.api_core.exceptions.NotFound - Add _get_secret_value_or_raise() which raises KeyError on NotFound but returns None for other errors (e.g. PermissionDenied) - Use _get_secret_value_or_raise() in __getitem__ for case_sensitive=True, avoiding the need for list_secrets to check key existence - Update test fixtures to raise NotFound for missing secrets and PermissionDenied for access-denied scenarios
hramezani
reviewed
May 15, 2026
| resp.payload.data.decode.return_value = secret_values[name] | ||
| return resp | ||
| raise Exception(f'Secret not found or access denied: {name}') | ||
| raise NotFound(f'Secret not found: {name}') |
Author
There was a problem hiding this comment.
No, those are the actual exceptions that the gcp actually raises. The previous test was just raising the general Exception now it raises the same exception it will be raised by gcloud sdk
Member
|
Thanks @ecerulm for the PR. |
- Verify list_secrets is not called when case_sensitive=True - Verify NotFound raises KeyError - Verify PermissionDenied returns None
Author
I added three test cases for
|
Member
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Avoid
list_secrets()whencase_sensitive=True, lowering the required GCP IAM permissions fromroles/secretmanager.viewer+roles/secretmanager.secretAccessordown to justroles/secretmanager.secretAccessor.Closes #861
Changes
list_secretswhencase_sensitive=True: key lookup goes directly toaccess_secret_version, so no listing permission is needed.NotFoundfromPermissionDenied: adds_get_secret_value_or_raise()which raisesKeyErrorongoogle.api_core.exceptions.NotFound(secret does not exist) but returnsNonefor other errors such asPermissionDenied(secret exists but access was denied). This preserves the correct__getitem__contract without requiringlist_secretsto verify key existence.google.api_core.exceptions.NotFoundfor missing secrets andgoogle.api_core.exceptions.PermissionDeniedfor access-denied scenarios instead of usingExceptionfor both.Permissions required
case_sensitiveFalse(default)roles/secretmanager.viewer+roles/secretmanager.secretAccessorTrueroles/secretmanager.secretAccessoronlySelected Reviewer: @dmontagu