שימוש ב-gRPC עם Cloud KMS

אם אתם משתמשים בספריות gRPC שנוצרו באופן ידני כדי לבצע קריאות ל-Cloud Key Management Service, אתם צריכים לציין ערך x-google-request-params במטא-נתונים או בכותרת של הקריאה. שימוש נכון ב-x-google-request-params ינתב את הקריאה לאזור המתאים למשאבי Cloud KMS.

מגדירים את הערך x-google-request-params לשדה בבקשה של השיטה, כמו שמוצג בטבלה הבאה.

‏Method שדה בקשה
AsymmetricDecrypt AsymmetricDecryptRequest.name
AsymmetricSign AsymmetricSignRequest.name
CreateCryptoKey CreateCryptoKeyRequest.parent
CreateCryptoKeyVersion CreateCryptoKeyVersionRequest.parent
CreateImportJob CreateImportJobRequest.parent
CreateKeyRing CreateKeyRingRequest.parent
פענוח DecryptRequest.name
DestroyCryptoKeyVersion DestroyCryptoKeyVersionRequest.name
הצפנה EncryptRequest.name
GenerateRandomBytes GenerateRandomBytesRequest.name
GetCryptoKey GetCryptoKeyRequest.name
GetCryptoKeyVersion GetCryptoKeyVersionRequest.name
GetImportJob GetImportJobRequest.name
GetKeyRing GetKeyRingRequest.name
GetPublicKey GetPublicKeyRequest.name
ImportCryptoKeyVersion ImportCryptoKeyVersionRequest.name
ListCryptoKeyVersions ListCryptoKeyVersionsRequest.parent
ListCryptoKeys ListCryptoKeysRequest.parent
ListImportJobs ListImportJobsRequest.parent
ListKeyRings ListKeyRingsRequest.parent
MacSign MacSignRequest.name
MacVerify MacVerifyRequest.name
RawDecrypt RawDecryptRequest.name
RawEncrypt RawEncryptRequest.name
RestoreCryptoKeyVersion RestoreCryptoKeyVersionRequest.name
UpdateCryptoKey UpdateCryptoKeyRequest.crypto_key.name
UpdateCryptoKeyPrimaryVersion UpdateCryptoKeyPrimaryVersionRequest.name
UpdateCryptoKeyVersion UpdateCryptoKeyVersionRequest.crypto_key_version.name

הגדרת שדה הבקשה

בדוגמאות הבאות אפשר לראות איפה מציינים את שם המשאב בשיטות שונות. מחליפים את הטקסט שמעוצב כ-place-holder בערכים בפועל שמשמשים במזהי משאבים של Cloud KMS.

דוגמה לפענוח

אם אתם מבצעים קריאה אל Decrypt, אתם צריכים למלא את השדות הבאים בבקשה:

name: 'projects/project-id/locations/location/keyRings/key-ring/cryptoKeys/key-name/'
ciphertext: 'iQALWM/r6alAxQm0VQe3...'

הערך שמוקצה לשדה name הוא שם המשאב של CryptoKey. כדי שהשיחה תנותב בצורה תקינה, צריך לכלול את שם המשאב הזה במטא-נתונים של השיחה, בפורמט הבא:

x-goog-request-params: 'name=projects/project-id/locations/location/keyRings/key-ring/cryptoKeys/key-name/'

דוגמה ל-CreateKeyRing

אם אתם מבצעים קריאה אל CreateKeyRing, אתם צריכים למלא את השדות הבאים בבקשה:

parent: 'projects/project-id/locations/location/'
key_ring_id: 'myKeyRing'
...

המטא-נתונים של השיחה צריכים לכלול גם את שם המשאב parent:

x-goog-request-params: 'parent=projects/project-id/locations/location/'

דוגמה ל-UpdateCryptoKey

אם אתם מבצעים קריאה אל UpdateCryptoKey, אתם צריכים לאכלס את השדות הבאים בבקשה:

name: 'projects/project-id/locations/location/keyRings/key-ring/cryptoKeys/key-name/'
field_mask: ...

המטא-נתונים צריכים לכלול גם את שם המשאב name. שימו לב לפורמט שבו משתמשים ב-crypto_key.name= ולא ב-name=:

x-goog-request-params: 'crypto_key.name=projects/project-id/locations/location/keyRings/key-ring/cryptoKeys/key-name/'

הוספת מטא-נתונים באמצעות C++‎

אם אתם משתמשים ב-C++, צריך להתקשר אל ClientContext::AddMetadata לפני שמתקשרים אל RPC כדי להוסיף את המידע המתאים למטא-נתונים של השיחה.

לדוגמה, אם מוסיפים מטא-נתונים לקריאה ל-Decrypt:

context.AddMetadata("x-goog-request-params",
                    "name=projects/project-id/locations/location/keyRings/key-ring-name/cryptoKeys/key-name/");

אחר כך תוכלו להעביר את ההקשר להפעלת method כרגיל, יחד עם מאגרי אחסון לפרוטוקולים של הבקשה והתגובה.