@@ -130,7 +130,7 @@ def test_df_apis(bq_cmek, session_with_bq_cmek, scalars_table_id):
130130 # Read a BQ table and assert encryption
131131 df = session_with_bq_cmek .read_gbq (scalars_table_id )
132132
133- # Perform a few dataframe operations and assert assertion
133+ # Perform a few dataframe operations and assert encryption
134134 df1 = df .dropna ()
135135 _assert_bq_table_is_encrypted (df1 , bq_cmek , session_with_bq_cmek )
136136
@@ -179,15 +179,32 @@ def test_to_gbq(bq_cmek, session_with_bq_cmek, scalars_table_id):
179179 df = session_with_bq_cmek .read_gbq (scalars_table_id )
180180 _assert_bq_table_is_encrypted (df , bq_cmek , session_with_bq_cmek )
181181
182- # Modify the dataframe and assert assertion
182+ # Modify the dataframe and assert encryption
183183 df = df .dropna ().head ()
184184 _assert_bq_table_is_encrypted (df , bq_cmek , session_with_bq_cmek )
185185
186- # Write the result to BQ and assert assertion
186+ # Write the result to BQ and assert encryption
187187 output_table_id = df .to_gbq ()
188188 output_table = session_with_bq_cmek .bqclient .get_table (output_table_id )
189189 assert output_table .encryption_configuration .kms_key_name == bq_cmek
190190
191+ # Write the result to BQ custom table and assert encryption
192+ session_with_bq_cmek .bqclient .get_table (output_table_id )
193+ output_table_ref = bigframes .session ._io .bigquery .random_table (
194+ session_with_bq_cmek ._anonymous_dataset
195+ )
196+ output_table_id = str (output_table_ref )
197+ df .to_gbq (output_table_id )
198+ output_table = session_with_bq_cmek .bqclient .get_table (output_table_id )
199+ assert output_table .encryption_configuration .kms_key_name == bq_cmek
200+
201+ # Lastly, assert that the encryption is not because of any default set at
202+ # the dataset level
203+ output_table_dataset = session_with_bq_cmek .bqclient .get_dataset (
204+ output_table .dataset_id
205+ )
206+ assert output_table_dataset .default_encryption_configuration is None
207+
191208
192209@pytest .mark .skip (
193210 reason = "Internal issue 327544164, cmek does not propagate to the dataframe."
@@ -254,3 +271,21 @@ def test_bqml(bq_cmek, session_with_bq_cmek, penguins_table_id):
254271 # Assert that model exists in BQ with intended encryption
255272 model_bq = session_with_bq_cmek .bqclient .get_model (new_model ._bqml_model .model_name )
256273 assert model_bq .encryption_configuration .kms_key_name == bq_cmek
274+
275+ # Assert that model registration keeps the encryption
276+ # Note that model registration only creates an entry (metadata) to be
277+ # included in the Vertex AI Model Registry. See for more details
278+ # https://cloud.google.com/bigquery/docs/update_vertex#add-existing.
279+ # When use deploys the model to an endpoint from the Model Registry then
280+ # they can specify an encryption key to further protect the artifacts at
281+ # rest on the Vertex AI side. See for more details:
282+ # https://cloud.google.com/vertex-ai/docs/general/deployment#deploy_a_model_to_an_endpoint,
283+ # https://cloud.google.com/vertex-ai/docs/general/cmek#create_resources_with_the_kms_key.
284+ # bigframes.ml does not provide any API for the model deployment.
285+ model_registered = new_model .register ()
286+ assert (
287+ model_registered ._bqml_model .model .encryption_configuration .kms_key_name
288+ == bq_cmek
289+ )
290+ model_bq = session_with_bq_cmek .bqclient .get_model (new_model ._bqml_model .model_name )
291+ assert model_bq .encryption_configuration .kms_key_name == bq_cmek
0 commit comments