Encrypt sensitive credential in session state with KMS - #6614
Open
guillaumeblaquiere wants to merge 3 commits into
Open
Encrypt sensitive credential in session state with KMS#6614guillaumeblaquiere wants to merge 3 commits into
guillaumeblaquiere wants to merge 3 commits into
Conversation
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.
Link to Issue and Description of Change
Problem:
During authentication flows, sensitive credentials (including OAuth 2.0 access/refresh tokens, basic authentication passwords, API keys, and Service Account private keys) are persisted in plaintext inside the session state database. This exposes sensitive credentials to unauthorized access if the session database is compromised.
Solution:
Implemented a comprehensive, highly secure, and optimized envelope and direct encryption mechanism utilizing Google Cloud KMS to protect all sensitive credential types before persisting them, with a robust re-authentication fallback in case of decryption failures:
KmsEncryptedString): Created a custom type using Pydantic v2BeforeValidatorandPlainSerializerto automatically encrypt sensitive fields during serialization (model_dump()) and decrypt them during validation/deserialization (model_validate()). Used this type for:HttpCredentials(password,token)OAuth2Auth(access_token,refresh_token,client_secret)ServiceAccountCredential(private_key)AuthCredential(api_key)_KMS_KEY_DEK_CACHEand_DEK_FERNET_CACHE). Deserialization requires calling Cloud KMS only once per session load, and subsequent encryptions/decryptions are processed locally in-memory (instantaneous).SessionStateCredentialService.load_credential,AuthHandler.get_auth_response, andGoogleCredentialsManager.get_valid_credentials. If decryption fails (e.g. KMS key destroyed or IAM permissions revoked), the system logs a warning, suppresses the exception, and returnsNoneto gracefully trigger the standard re-authentication flow (re-prompting the user for credentials) instead of crashing the runner.google-cloud-kmsunder required dependencies inpyproject.toml.Testing Plan
Unit Tests:
Summary of pytest results:
Added and updated tests in
tests/unittests/auth/test_kms_credentials.pymocking the KMS client (avoiding network dependency) to assert:KmsEncryptedStringfields (verifyingkms:prefixes and decryption back to plaintext).None(triggering re-authentication) rather than crashing the session load.All 210 authentication unit tests passed successfully: