2424from google .cloud import exceptions
2525from google .cloud import storage
2626from google .cloud .storage ._helpers import _base64_md5hash
27+ from google .cloud import kms
2728
2829from test_utils .retry import RetryErrors
2930from test_utils .system import unique_resource_id
@@ -912,7 +913,7 @@ def topic_path(self):
912913 return 'projects/{}/topics/{}' .format (
913914 Config .CLIENT .project , self .TOPIC_NAME )
914915
915- def _intialize_topic (self ):
916+ def _initialize_topic (self ):
916917 try :
917918 from google .cloud .pubsub_v1 import PublisherClient
918919 except ImportError :
@@ -923,14 +924,13 @@ def _intialize_topic(self):
923924 binding = policy .bindings .add ()
924925 binding .role = 'roles/pubsub.publisher'
925926 binding .members .append (
926- 'serviceAccount:{}'
927- '@gs-project-accounts.iam.gserviceaccount.com' .format (
928- Config .CLIENT .project ))
927+ 'serviceAccount:{}' .format (
928+ Config .CLIENT .get_service_account_email ()))
929929 self .publisher_client .set_iam_policy (self .topic_path , policy )
930930
931931 def setUp (self ):
932932 self .case_buckets_to_delete = []
933- self ._intialize_topic ()
933+ self ._initialize_topic ()
934934
935935 def tearDown (self ):
936936 retry_429 (self .publisher_client .delete_topic )(self .topic_path )
@@ -1054,6 +1054,62 @@ def _kms_key_name(self, key_name=None):
10541054 key_name ,
10551055 )
10561056
1057+ @classmethod
1058+ def setUpClass (cls ):
1059+ super (TestKMSIntegration , cls ).setUpClass ()
1060+
1061+ def setUp (self ):
1062+ super (TestKMSIntegration , self ).setUp ()
1063+ client = kms .KeyManagementServiceClient ()
1064+
1065+ # If the keyring doesn't exist create it.
1066+ name = client .key_ring_path (
1067+ Config .CLIENT .project ,
1068+ self .bucket .location .lower (),
1069+ self .KEYRING_NAME )
1070+
1071+ try :
1072+ client .get_key_ring (name )
1073+ except exceptions .NotFound :
1074+ parent = client .location_path (
1075+ Config .CLIENT .project , self .bucket .location .lower ())
1076+ client .create_key_ring (parent , self .KEYRING_NAME , {})
1077+
1078+ # Ensure this service account is marked as an owner to the test keyring
1079+ keyring_location_path = client .key_ring_path (
1080+ Config .CLIENT .project ,
1081+ self .bucket .location .lower (),
1082+ self .KEYRING_NAME )
1083+ service_account = Config .CLIENT .get_service_account_email ()
1084+ policy = {
1085+ "bindings" : [
1086+ {
1087+ "role" : "roles/owner" ,
1088+ "members" : [
1089+ "serviceAccount:" + service_account ,
1090+ ]
1091+ }
1092+ ]
1093+ }
1094+
1095+ client .set_iam_policy (keyring_location_path , policy )
1096+
1097+ # Populate the keyring with the keys we use in the tests
1098+ for keyname in ['gcs-test2' , 'gcs-test-alternate' ,
1099+ 'explicit-kms-key-name' , 'default-kms-key-name' ,
1100+ 'override-default-kms-key-name' ,
1101+ 'alt-default-kms-key-name' ]:
1102+ key_path = client .crypto_key_path (Config .CLIENT .project ,
1103+ self .bucket .location .lower (),
1104+ self .KEYRING_NAME ,
1105+ keyname )
1106+ try :
1107+ client .get_crypto_key (key_path )
1108+ except exceptions .NotFound :
1109+ purpose = kms .enums .CryptoKey .CryptoKeyPurpose .ENCRYPT_DECRYPT
1110+ key = {'purpose' : purpose }
1111+ client .create_crypto_key (keyring_location_path , keyname , key )
1112+
10571113 def test_blob_w_explicit_kms_key_name (self ):
10581114 BLOB_NAME = 'explicit-kms-key-name'
10591115 file_data = self .FILES ['simple' ]
@@ -1139,7 +1195,7 @@ def test_rewrite_rotate_csek_to_cmek(self):
11391195 kms_key_name = self ._kms_key_name ()
11401196
11411197 # We can't verify it, but ideally we would check that the following
1142- # URL was resolvable with our credentals
1198+ # URL was resolvable with our credentials
11431199 # KEY_URL = 'https://cloudkms.googleapis.com/v1/{}'.format(
11441200 # kms_key_name)
11451201
0 commit comments