Skip to content

Commit 1dd4731

Browse files
HemangChothanitswast
authored andcommitted
feat(bigquery): expose customer managed encryption key for ML models (#9302)
* feat: expose customer managed encryption key for ML models * feat: add encryptionConfiguration in _PROPERTY_TO_API_FIELD * changes in condition * change in document and parameter * create a new file for class EncryptionConfiguration * feat(bigquery): refactor test class of encryption configuration and change location in key * feat(bigquery): add unit test in table class * feat(bigquery): add apache license in file and test to show previous location works
1 parent af96c3e commit 1dd4731

11 files changed

Lines changed: 291 additions & 159 deletions

File tree

bigquery/docs/reference.rst

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ Table
8888
table.TableReference
8989
table.Row
9090
table.RowIterator
91-
table.EncryptionConfiguration
9291
table.TimePartitioning
9392
table.TimePartitioningType
9493

@@ -173,6 +172,13 @@ Enums
173172

174173
enums.StandardSqlDataTypes
175174

175+
Encryption Configuration
176+
========================
177+
178+
.. autosummary::
179+
:toctree: generated
180+
181+
encryption_configuration.EncryptionConfiguration
176182

177183
Additional Types
178184
================

bigquery/google/cloud/bigquery/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,12 @@
7373
from google.cloud.bigquery.routine import RoutineArgument
7474
from google.cloud.bigquery.routine import RoutineReference
7575
from google.cloud.bigquery.schema import SchemaField
76-
from google.cloud.bigquery.table import EncryptionConfiguration
7776
from google.cloud.bigquery.table import Table
7877
from google.cloud.bigquery.table import TableReference
7978
from google.cloud.bigquery.table import Row
8079
from google.cloud.bigquery.table import TimePartitioningType
8180
from google.cloud.bigquery.table import TimePartitioning
81+
from google.cloud.bigquery.encryption_configuration import EncryptionConfiguration
8282

8383
__all__ = [
8484
"__version__",
@@ -94,7 +94,6 @@
9494
"DatasetReference",
9595
"AccessEntry",
9696
# Tables
97-
"EncryptionConfiguration",
9897
"Table",
9998
"TableReference",
10099
"Row",
@@ -136,6 +135,8 @@
136135
"StandardSqlDataTypes",
137136
"SourceFormat",
138137
"WriteDisposition",
138+
# EncryptionConfiguration
139+
"EncryptionConfiguration",
139140
]
140141

141142

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Copyright 2015 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Define class for the custom encryption configuration."""
16+
17+
import copy
18+
19+
20+
class EncryptionConfiguration(object):
21+
"""Custom encryption configuration (e.g., Cloud KMS keys).
22+
23+
Args:
24+
kms_key_name (str): resource ID of Cloud KMS key used for encryption
25+
"""
26+
27+
def __init__(self, kms_key_name=None):
28+
self._properties = {}
29+
if kms_key_name is not None:
30+
self._properties["kmsKeyName"] = kms_key_name
31+
32+
@property
33+
def kms_key_name(self):
34+
"""str: Resource ID of Cloud KMS key
35+
36+
Resource ID of Cloud KMS key or :data:`None` if using default
37+
encryption.
38+
"""
39+
return self._properties.get("kmsKeyName")
40+
41+
@kms_key_name.setter
42+
def kms_key_name(self, value):
43+
self._properties["kmsKeyName"] = value
44+
45+
@classmethod
46+
def from_api_repr(cls, resource):
47+
"""Construct an encryption configuration from its API representation
48+
49+
Args:
50+
resource (Dict[str, object]):
51+
An encryption configuration representation as returned from
52+
the API.
53+
54+
Returns:
55+
google.cloud.bigquery.table.EncryptionConfiguration:
56+
An encryption configuration parsed from ``resource``.
57+
"""
58+
config = cls()
59+
config._properties = copy.deepcopy(resource)
60+
return config
61+
62+
def to_api_repr(self):
63+
"""Construct the API resource representation of this encryption
64+
configuration.
65+
66+
Returns:
67+
Dict[str, object]:
68+
Encryption configuration as represented as an API resource
69+
"""
70+
return copy.deepcopy(self._properties)
71+
72+
def __eq__(self, other):
73+
if not isinstance(other, EncryptionConfiguration):
74+
return NotImplemented
75+
return self.kms_key_name == other.kms_key_name
76+
77+
def __ne__(self, other):
78+
return not self == other
79+
80+
def __hash__(self):
81+
return hash(self.kms_key_name)
82+
83+
def __repr__(self):
84+
return "EncryptionConfiguration({})".format(self.kms_key_name)

bigquery/google/cloud/bigquery/job.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@
3737
from google.cloud.bigquery.routine import RoutineReference
3838
from google.cloud.bigquery.schema import SchemaField
3939
from google.cloud.bigquery.table import _EmptyRowIterator
40-
from google.cloud.bigquery.table import EncryptionConfiguration
4140
from google.cloud.bigquery.table import _table_arg_to_table_ref
4241
from google.cloud.bigquery.table import TableReference
4342
from google.cloud.bigquery.table import Table
4443
from google.cloud.bigquery.table import TimePartitioning
4544
from google.cloud.bigquery import _helpers
45+
from google.cloud.bigquery.encryption_configuration import EncryptionConfiguration
4646

4747
_DONE_STATE = "DONE"
4848
_STOPPED_REASON = "stopped"
@@ -1040,7 +1040,7 @@ def create_disposition(self, value):
10401040

10411041
@property
10421042
def destination_encryption_configuration(self):
1043-
"""google.cloud.bigquery.table.EncryptionConfiguration: Custom
1043+
"""google.cloud.bigquery.encryption_configuration.EncryptionConfiguration: Custom
10441044
encryption configuration for the destination table.
10451045
10461046
Custom encryption configuration (e.g., Cloud KMS keys) or :data:`None`
@@ -1434,7 +1434,7 @@ def schema(self):
14341434

14351435
@property
14361436
def destination_encryption_configuration(self):
1437-
"""google.cloud.bigquery.table.EncryptionConfiguration: Custom
1437+
"""google.cloud.bigquery.encryption_configuration.EncryptionConfiguration: Custom
14381438
encryption configuration for the destination table.
14391439
14401440
Custom encryption configuration (e.g., Cloud KMS keys)
@@ -1638,7 +1638,7 @@ def write_disposition(self, value):
16381638

16391639
@property
16401640
def destination_encryption_configuration(self):
1641-
"""google.cloud.bigquery.table.EncryptionConfiguration: Custom
1641+
"""google.cloud.bigquery.encryption_configuration.EncryptionConfiguration: Custom
16421642
encryption configuration for the destination table.
16431643
16441644
Custom encryption configuration (e.g., Cloud KMS keys) or :data:`None`
@@ -1709,7 +1709,7 @@ def write_disposition(self):
17091709

17101710
@property
17111711
def destination_encryption_configuration(self):
1712-
"""google.cloud.bigquery.table.EncryptionConfiguration: Custom
1712+
"""google.cloud.bigquery.encryption_configuration.EncryptionConfiguration: Custom
17131713
encryption configuration for the destination table.
17141714
17151715
Custom encryption configuration (e.g., Cloud KMS keys) or :data:`None`
@@ -2041,7 +2041,7 @@ def __init__(self, **kwargs):
20412041

20422042
@property
20432043
def destination_encryption_configuration(self):
2044-
"""google.cloud.bigquery.table.EncryptionConfiguration: Custom
2044+
"""google.cloud.bigquery.encryption_configuration.EncryptionConfiguration: Custom
20452045
encryption configuration for the destination table.
20462046
20472047
Custom encryption configuration (e.g., Cloud KMS keys) or :data:`None`
@@ -2460,7 +2460,7 @@ def destination(self):
24602460

24612461
@property
24622462
def destination_encryption_configuration(self):
2463-
"""google.cloud.bigquery.table.EncryptionConfiguration: Custom
2463+
"""google.cloud.bigquery.encryption_configuration.EncryptionConfiguration: Custom
24642464
encryption configuration for the destination table.
24652465
24662466
Custom encryption configuration (e.g., Cloud KMS keys) or :data:`None`

bigquery/google/cloud/bigquery/model.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from google.api_core import datetime_helpers
2626
from google.cloud.bigquery import _helpers
2727
from google.cloud.bigquery_v2 import types
28+
from google.cloud.bigquery.encryption_configuration import EncryptionConfiguration
2829

2930

3031
class Model(object):
@@ -48,6 +49,7 @@ class Model(object):
4849
# have an exhaustive list of all mutable properties.
4950
"labels": "labels",
5051
"description": "description",
52+
"encryption_configuration": "encryptionConfiguration",
5153
}
5254

5355
def __init__(self, model_ref):
@@ -253,6 +255,30 @@ def labels(self, value):
253255
value = {}
254256
self._properties["labels"] = value
255257

258+
@property
259+
def encryption_configuration(self):
260+
"""google.cloud.bigquery.encryption_configuration.EncryptionConfiguration: Custom
261+
encryption configuration for the model.
262+
263+
Custom encryption configuration (e.g., Cloud KMS keys) or :data:`None`
264+
if using default encryption.
265+
266+
See `protecting data with Cloud KMS keys
267+
<https://cloud.google.com/bigquery/docs/customer-managed-encryption>`_
268+
in the BigQuery documentation.
269+
"""
270+
prop = self._properties.get("encryptionConfiguration")
271+
if prop:
272+
prop = EncryptionConfiguration.from_api_repr(prop)
273+
return prop
274+
275+
@encryption_configuration.setter
276+
def encryption_configuration(self, value):
277+
api_repr = value
278+
if value:
279+
api_repr = value.to_api_repr()
280+
self._properties["encryptionConfiguration"] = api_repr
281+
256282
@classmethod
257283
def from_api_repr(cls, resource):
258284
"""Factory: construct a model resource given its API representation

bigquery/google/cloud/bigquery/table.py

Lines changed: 2 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
from google.cloud.bigquery.schema import _build_schema_resource
5656
from google.cloud.bigquery.schema import _parse_schema_resource
5757
from google.cloud.bigquery.external_config import ExternalConfig
58+
from google.cloud.bigquery.encryption_configuration import EncryptionConfiguration
5859

5960

6061
_LOGGER = logging.getLogger(__name__)
@@ -113,73 +114,6 @@ def _view_use_legacy_sql_getter(table):
113114
return True
114115

115116

116-
class EncryptionConfiguration(object):
117-
"""Custom encryption configuration (e.g., Cloud KMS keys).
118-
119-
Args:
120-
kms_key_name (str): resource ID of Cloud KMS key used for encryption
121-
"""
122-
123-
def __init__(self, kms_key_name=None):
124-
self._properties = {}
125-
if kms_key_name is not None:
126-
self._properties["kmsKeyName"] = kms_key_name
127-
128-
@property
129-
def kms_key_name(self):
130-
"""str: Resource ID of Cloud KMS key
131-
132-
Resource ID of Cloud KMS key or :data:`None` if using default
133-
encryption.
134-
"""
135-
return self._properties.get("kmsKeyName")
136-
137-
@kms_key_name.setter
138-
def kms_key_name(self, value):
139-
self._properties["kmsKeyName"] = value
140-
141-
@classmethod
142-
def from_api_repr(cls, resource):
143-
"""Construct an encryption configuration from its API representation
144-
145-
Args:
146-
resource (Dict[str, object]):
147-
An encryption configuration representation as returned from
148-
the API.
149-
150-
Returns:
151-
google.cloud.bigquery.table.EncryptionConfiguration:
152-
An encryption configuration parsed from ``resource``.
153-
"""
154-
config = cls()
155-
config._properties = copy.deepcopy(resource)
156-
return config
157-
158-
def to_api_repr(self):
159-
"""Construct the API resource representation of this encryption
160-
configuration.
161-
162-
Returns:
163-
Dict[str, object]:
164-
Encryption configuration as represented as an API resource
165-
"""
166-
return copy.deepcopy(self._properties)
167-
168-
def __eq__(self, other):
169-
if not isinstance(other, EncryptionConfiguration):
170-
return NotImplemented
171-
return self.kms_key_name == other.kms_key_name
172-
173-
def __ne__(self, other):
174-
return not self == other
175-
176-
def __hash__(self):
177-
return hash(self.kms_key_name)
178-
179-
def __repr__(self):
180-
return "EncryptionConfiguration({})".format(self.kms_key_name)
181-
182-
183117
class TableReference(object):
184118
"""TableReferences are pointers to tables.
185119
@@ -479,7 +413,7 @@ def labels(self, value):
479413

480414
@property
481415
def encryption_configuration(self):
482-
"""google.cloud.bigquery.table.EncryptionConfiguration: Custom
416+
"""google.cloud.bigquery.encryption_configuration.EncryptionConfiguration: Custom
483417
encryption configuration for the table.
484418
485419
Custom encryption configuration (e.g., Cloud KMS keys) or :data:`None`

0 commit comments

Comments
 (0)