Skip to content

Commit 8c3b652

Browse files
HemangChothanitswast
authored andcommitted
feat(bigquery): implement defaultEncryptionConfiguration on datasets (#9489)
* feat: add customer managed encryption key for dataset * change as recommended. * change paramter name as per document * cosmetic changes * feat(bigquery): refactor class imports as it moved to new file * feat(bigquery): location name updated in key as suggested
1 parent ecb2162 commit 8c3b652

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

bigquery/google/cloud/bigquery/dataset.py

+26
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from google.cloud.bigquery.model import ModelReference
2525
from google.cloud.bigquery.routine import RoutineReference
2626
from google.cloud.bigquery.table import TableReference
27+
from google.cloud.bigquery.encryption_configuration import EncryptionConfiguration
2728

2829

2930
def _get_table_reference(self, table_id):
@@ -361,6 +362,7 @@ class Dataset(object):
361362
"default_partition_expiration_ms": "defaultPartitionExpirationMs",
362363
"default_table_expiration_ms": "defaultTableExpirationMs",
363364
"friendly_name": "friendlyName",
365+
"default_encryption_configuration": "defaultEncryptionConfiguration",
364366
}
365367

366368
def __init__(self, dataset_ref):
@@ -573,6 +575,30 @@ def labels(self, value):
573575
raise ValueError("Pass a dict")
574576
self._properties["labels"] = value
575577

578+
@property
579+
def default_encryption_configuration(self):
580+
"""google.cloud.bigquery.encryption_configuration.EncryptionConfiguration: Custom
581+
encryption configuration for all tables in the dataset.
582+
583+
Custom encryption configuration (e.g., Cloud KMS keys) or :data:`None`
584+
if using default encryption.
585+
586+
See `protecting data with Cloud KMS keys
587+
<https://cloud.google.com/bigquery/docs/customer-managed-encryption>`_
588+
in the BigQuery documentation.
589+
"""
590+
prop = self._properties.get("defaultEncryptionConfiguration")
591+
if prop:
592+
prop = EncryptionConfiguration.from_api_repr(prop)
593+
return prop
594+
595+
@default_encryption_configuration.setter
596+
def default_encryption_configuration(self, value):
597+
api_repr = value
598+
if value:
599+
api_repr = value.to_api_repr()
600+
self._properties["defaultEncryptionConfiguration"] = api_repr
601+
576602
@classmethod
577603
def from_string(cls, full_dataset_id):
578604
"""Construct a dataset from fully-qualified dataset ID.

bigquery/tests/unit/test_dataset.py

+25
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ class TestDataset(unittest.TestCase):
275275
PROJECT = "project"
276276
DS_ID = "dataset-id"
277277
DS_REF = DatasetReference(PROJECT, DS_ID)
278+
KMS_KEY_NAME = "projects/1/locations/us/keyRings/1/cryptoKeys/1"
278279

279280
@staticmethod
280281
def _get_target_class():
@@ -314,6 +315,7 @@ def _make_resource(self):
314315
{"role": "WRITER", "specialGroup": "projectWriters"},
315316
{"role": "READER", "specialGroup": "projectReaders"},
316317
],
318+
"defaultEncryptionConfiguration": {"kmsKeyName": self.KMS_KEY_NAME},
317319
}
318320

319321
def _verify_access_entry(self, access_entries, resource):
@@ -369,6 +371,13 @@ def _verify_resource_properties(self, dataset, resource):
369371
self.assertEqual(dataset.description, resource.get("description"))
370372
self.assertEqual(dataset.friendly_name, resource.get("friendlyName"))
371373
self.assertEqual(dataset.location, resource.get("location"))
374+
if "defaultEncryptionConfiguration" in resource:
375+
self.assertEqual(
376+
dataset.default_encryption_configuration.kms_key_name,
377+
resource.get("defaultEncryptionConfiguration")["kmsKeyName"],
378+
)
379+
else:
380+
self.assertIsNone(dataset.default_encryption_configuration)
372381

373382
if "access" in resource:
374383
self._verify_access_entry(dataset.access_entries, resource)
@@ -558,6 +567,22 @@ def test_to_api_repr_w_custom_field(self):
558567
}
559568
self.assertEqual(resource, exp_resource)
560569

570+
def test_default_encryption_configuration_setter(self):
571+
from google.cloud.bigquery.encryption_configuration import (
572+
EncryptionConfiguration,
573+
)
574+
575+
dataset = self._make_one(self.DS_REF)
576+
encryption_configuration = EncryptionConfiguration(
577+
kms_key_name=self.KMS_KEY_NAME
578+
)
579+
dataset.default_encryption_configuration = encryption_configuration
580+
self.assertEqual(
581+
dataset.default_encryption_configuration.kms_key_name, self.KMS_KEY_NAME
582+
)
583+
dataset.default_encryption_configuration = None
584+
self.assertIsNone(dataset.default_encryption_configuration)
585+
561586
def test_from_string(self):
562587
cls = self._get_target_class()
563588
got = cls.from_string("string-project.string_dataset")

0 commit comments

Comments
 (0)