diff --git a/HISTORY.rst b/HISTORY.rst index 7925520..57a1209 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -2,6 +2,15 @@ History ======= +1.7.0 (2026-11-23) +------------------ + +* Added ``BigQueryConnectionConfig`` and ``DatabaseType.bigquery`` for BigQuery connections. + BigQuery is a preview connection type: the server refuses to create one until an administrator enables preview features. + ``acknowledge_time_travel_recovery`` is required, and the server refuses to save the connection unless it is ``True``. + +Requires server version 3.26.22. + 1.6.0 (2026-11-20) ------------------ diff --git a/datamasque/client/__init__.py b/datamasque/client/__init__.py index e3c674a..1db539b 100644 --- a/datamasque/client/__init__.py +++ b/datamasque/client/__init__.py @@ -41,6 +41,7 @@ ) from datamasque.client.models.connection import ( AzureConnectionConfig, + BigQueryConnectionConfig, CassandraConnectionConfig, ConnectionConfig, ConnectionId, @@ -233,6 +234,7 @@ "AsyncRulesetGenerationInProgressError", "AsyncRulesetGenerationTaskStatus", "AzureConnectionConfig", + "BigQueryConnectionConfig", "BooleanPreview", "BooleanStatistics", "BundleImportCounts", diff --git a/datamasque/client/models/connection.py b/datamasque/client/models/connection.py index cde6bd0..89ec8c2 100644 --- a/datamasque/client/models/connection.py +++ b/datamasque/client/models/connection.py @@ -54,6 +54,7 @@ class DatabaseType(Enum): cassandra = "cassandra" sap_ase = "sap_ase" salesforce = "salesforce" + bigquery = "bigquery" class SseSelection(Enum): @@ -374,7 +375,8 @@ class DatabaseConnectionConfig(ConnectionConfig): Use `DynamoConnectionConfig` for DynamoDB, `SnowflakeConnectionConfig` for Snowflake, `MongoConnectionConfig` for MongoDB, `CassandraConnectionConfig` for Apache Cassandra, - `SapAseConnectionConfig` for SAP ASE (Sybase) and `SalesforceConnectionConfig` for Salesforce. + `SapAseConnectionConfig` for SAP ASE (Sybase), `SalesforceConnectionConfig` for Salesforce + and `BigQueryConnectionConfig` for BigQuery. """ host: str @@ -415,6 +417,8 @@ def _reject_special_engines(self) -> "DatabaseConnectionConfig": raise ValueError("For SAP ASE (Sybase), use the SapAseConnectionConfig class instead") if self.database_type is DatabaseType.salesforce: raise ValueError("For Salesforce, use the SalesforceConnectionConfig class instead") + if self.database_type is DatabaseType.bigquery: + raise ValueError("For BigQuery, use the BigQueryConnectionConfig class instead") return self mask_type: Literal["database"] = "database" @@ -716,6 +720,48 @@ def _strip_encrypted_passphrase(cls, data: dict) -> dict: return data +class BigQueryConnectionConfig(ConnectionConfig): + """ + Connection configuration for a BigQuery dataset, reached with a service account key. + + `project_id` is the Google Cloud project, not the DataMasque `project` the connection belongs to. + `service_account_key` holds the contents of a service account's JSON key file, + or the ARN of an AWS Secrets Manager secret that holds it. + `maximum_bytes_billed` fails any BigQuery job that would bill more than that many bytes. + `acknowledge_time_travel_recovery` acknowledges that original data can be recovered through BigQuery time travel + until the dataset's time travel window and its 7-day fail-safe end; + the server refuses to save the connection unless it is `True`. + + Requires server version 3.26.22. + BigQuery is a preview connection type there: + the server refuses to create one until an administrator enables preview features + under Settings, Preview Features. + """ + + project_id: str + dataset: str + location: str + service_account_key: Optional[str] = Field(default=None, repr=False) + maximum_bytes_billed: Optional[int] = Field(default=None, ge=1) + is_read_only: bool = False + acknowledge_time_travel_recovery: bool + + mask_type: Literal["database"] = "database" + db_type: Literal["bigquery"] = "bigquery" + + @property + def database_type(self) -> DatabaseType: + return DatabaseType.bigquery + + @model_validator(mode="before") + @classmethod + def _strip_encrypted_service_account_key(cls, data: dict) -> dict: + if isinstance(data, dict): + # The API returns the encrypted form; drop it so `service_account_key` stays None. + data.pop("service_account_key_encrypted", None) + return data + + FILE_TYPE_MAP: dict[str, type[FileConnectionConfig]] = { "s3_connection": S3ConnectionConfig, "azure_blob_connection": AzureConnectionConfig, @@ -734,6 +780,7 @@ def _strip_encrypted_passphrase(cls, data: dict) -> dict: DatabaseType.cassandra.value: CassandraConnectionConfig, DatabaseType.sap_ase.value: SapAseConnectionConfig, DatabaseType.salesforce.value: SalesforceConnectionConfig, + DatabaseType.bigquery.value: BigQueryConnectionConfig, # others use the default `DatabaseConnectionConfig` } diff --git a/pyproject.toml b/pyproject.toml index 6f3eaed..7fced60 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "datamasque-python" -version = "1.6.0" +version = "1.7.0" description = "Official Python client for the DataMasque data-masking API." authors = [ { name = "DataMasque Ltd" }, diff --git a/tests/test_connections.py b/tests/test_connections.py index 9f22271..efcdc36 100644 --- a/tests/test_connections.py +++ b/tests/test_connections.py @@ -10,6 +10,7 @@ from datamasque.client.exceptions import DataMasqueApiError, DataMasqueException from datamasque.client.models.connection import ( AzureConnectionConfig, + BigQueryConnectionConfig, CassandraConnectionConfig, ConnectionConfig, ConnectionId, @@ -1730,6 +1731,7 @@ def test_redshift_connection_keeps_its_cluster_s3_role_separate_from_its_tagging (DatabaseType.cassandra, "For Apache Cassandra"), (DatabaseType.sap_ase, "For SAP ASE"), (DatabaseType.salesforce, "For Salesforce"), + (DatabaseType.bigquery, "For BigQuery"), ], ) def test_database_connection_config_rejects_engines_with_their_own_class( @@ -1931,3 +1933,89 @@ def test_connection_config_dispatch_picks_salesforce_subclass(): assert "salesforce_private_key_passphrase_encrypted" not in conn.model_dump() # The version the server stored is kept, so sending the connection back does not change it. assert conn.model_dump(exclude_none=True)["api_version"] == "62.0" + + +def _bigquery_connection(**overrides) -> BigQueryConnectionConfig: + fields = { + "project": PROJECT, + "name": "bigquery", + "project_id": "my-gcp-project", + "dataset": "people", + "location": "australia-southeast2", + "service_account_key": '{"type": "service_account"}', + "acknowledge_time_travel_recovery": True, + } + return BigQueryConnectionConfig(**{**fields, **overrides}) + + +def test_bigquery_connection_serializes_for_create(): + conn = _bigquery_connection(maximum_bytes_billed=100 * 1024 * 1024, is_read_only=True) + + d = conn.model_dump(exclude_none=True, by_alias=True, mode="json") + + assert d == { + "project": PROJECT, + "name": "bigquery", + "project_id": "my-gcp-project", + "dataset": "people", + "location": "australia-southeast2", + "service_account_key": '{"type": "service_account"}', + "maximum_bytes_billed": 100 * 1024 * 1024, + "is_read_only": True, + "acknowledge_time_travel_recovery": True, + "mask_type": "database", + "db_type": "bigquery", + } + assert conn.database_type is DatabaseType.bigquery + + +def test_bigquery_connection_omits_an_unset_byte_cap(): + """Left unset, the server applies no cap of its own.""" + d = _bigquery_connection().model_dump(exclude_none=True, by_alias=True, mode="json") + + assert "maximum_bytes_billed" not in d + assert d["is_read_only"] is False + + +def test_bigquery_connection_hides_the_key_from_repr(): + assert "service_account" not in repr(_bigquery_connection()) + + +@pytest.mark.parametrize("missing", ["project_id", "dataset", "location", "acknowledge_time_travel_recovery"]) +def test_bigquery_connection_requires_its_fields(missing: str) -> None: + fields = _bigquery_connection().model_dump(exclude_none=True) + del fields[missing] + + with pytest.raises(ValueError, match=missing): + BigQueryConnectionConfig(**fields) + + +@pytest.mark.parametrize("maximum_bytes_billed", [0, -1]) +def test_bigquery_connection_rejects_a_byte_cap_below_one(maximum_bytes_billed: int) -> None: + with pytest.raises(ValueError, match="maximum_bytes_billed"): + _bigquery_connection(maximum_bytes_billed=maximum_bytes_billed) + + +def test_connection_config_dispatch_picks_bigquery_subclass(): + payload = { + "id": "bigquery-id-1", + "name": "bigquery", + "project": PROJECT, + "mask_type": "database", + "db_type": "bigquery", + "project_id": "my-gcp-project", + "dataset": "people", + "location": "australia-southeast2", + "service_account_key_encrypted": "some_base64_here", + "maximum_bytes_billed": 1024, + "is_read_only": False, + "acknowledge_time_travel_recovery": True, + "version": "1.0", + } + + conn = validate_connection(payload) + + assert isinstance(conn, BigQueryConnectionConfig) + assert conn.service_account_key is None + assert conn.maximum_bytes_billed == 1024 + assert "service_account_key_encrypted" not in conn.model_dump() diff --git a/uv.lock b/uv.lock index 0fb45ec..c712118 100644 --- a/uv.lock +++ b/uv.lock @@ -419,7 +419,7 @@ toml = [ [[package]] name = "datamasque-python" -version = "1.6.0" +version = "1.7.0" source = { editable = "." } dependencies = [ { name = "pydantic" },