Skip to content
This repository was archived by the owner on Mar 31, 2026. It is now read-only.

Commit 9e0c411

Browse files
samples: add samples for bucket encryption enforcement config
Co-authored-by: nidhiii-27 <224584462+nidhiii-27@users.noreply.github.com>
1 parent da08329 commit 9e0c411

6 files changed

Lines changed: 34 additions & 8 deletions

fix_test.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import re
2+
with open("samples/snippets/encryption_test.py", "r") as f:
3+
lines = f.readlines()
4+
5+
new_lines = []
6+
for i, line in enumerate(lines):
7+
if line.startswith("def test_") or line.startswith("@pytest.fixture"):
8+
# Make sure there are two blank lines before it
9+
# by checking the end of new_lines
10+
if not (len(new_lines) >= 2 and new_lines[-1] == "\n" and new_lines[-2] == "\n"):
11+
while len(new_lines) > 0 and new_lines[-1] == "\n":
12+
new_lines.pop()
13+
if len(new_lines) > 0:
14+
new_lines.append("\n")
15+
new_lines.append("\n")
16+
if line.startswith("def test_blob"):
17+
# make sure no blank lines between @pytest.fixture and def test_blob
18+
while len(new_lines) > 0 and new_lines[-1] == "\n":
19+
new_lines.pop()
20+
new_lines.append(line)
21+
22+
with open("samples/snippets/encryption_test.py", "w") as f:
23+
f.writelines(new_lines)

samples/snippets/encryption_test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ def test_object_csek_to_cmek(test_blob):
131131

132132
assert cmek_blob.download_as_bytes(), test_blob_content
133133

134+
134135
def test_bucket_encryption_enforcement_config(capsys):
135136
bucket_name = f"test_encryption_enforcement_{uuid.uuid4().hex}"
136137

samples/snippets/storage_get_bucket_encryption_enforcement_config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
from google.cloud import storage
1616

17+
1718
# [START storage_get_bucket_encryption_enforcement_config]
1819
def get_bucket_encryption_enforcement_config(bucket_name):
1920
"""Gets the bucket encryption enforcement configuration."""
@@ -32,9 +33,8 @@ def get_bucket_encryption_enforcement_config(bucket_name):
3233
print(f"Customer-managed encryption enforcement config restriction mode: {cmek_config.restriction_mode if cmek_config else None}")
3334
print(f"Customer-supplied encryption enforcement config restriction mode: {csek_config.restriction_mode if csek_config else None}")
3435
print(f"Google-managed encryption enforcement config restriction mode: {gmek_config.restriction_mode if gmek_config else None}")
35-
36-
3736
# [END storage_get_bucket_encryption_enforcement_config]
3837

38+
3939
if __name__ == "__main__":
4040
get_bucket_encryption_enforcement_config(bucket_name="your-unique-bucket-name")

samples/snippets/storage_remove_all_bucket_encryption_enforcement_config.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
from google.cloud import storage
1616

17+
1718
# [START storage_remove_all_bucket_encryption_enforcement_config]
1819
def remove_all_bucket_encryption_enforcement_config(bucket_name):
1920
"""Removes all bucket encryption enforcement configuration."""
@@ -29,8 +30,8 @@ def remove_all_bucket_encryption_enforcement_config(bucket_name):
2930
bucket.patch()
3031

3132
print(f"Removed Encryption Enforcement Config from bucket {bucket.name}.")
32-
3333
# [END storage_remove_all_bucket_encryption_enforcement_config]
3434

35+
3536
if __name__ == "__main__":
3637
remove_all_bucket_encryption_enforcement_config(bucket_name="your-unique-bucket-name")

samples/snippets/storage_set_bucket_encryption_enforcement_config.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
# limitations under the License.
1414

1515
from google.cloud import storage
16+
from google.cloud.storage.bucket import EncryptionEnforcementConfig
17+
1618

1719
# [START storage_set_bucket_encryption_enforcement_config]
1820
def set_bucket_encryption_enforcement_config(bucket_name):
@@ -24,17 +26,15 @@ def set_bucket_encryption_enforcement_config(bucket_name):
2426
bucket = storage_client.bucket(bucket_name)
2527

2628
# Restriction mode can be "FULLY_RESTRICTED" or "NOT_RESTRICTED"
27-
from google.cloud.storage.bucket import EncryptionEnforcementConfig
28-
2929
bucket.customer_managed_encryption_enforcement_config = EncryptionEnforcementConfig(restriction_mode="NOT_RESTRICTED")
3030
bucket.customer_supplied_encryption_enforcement_config = EncryptionEnforcementConfig(restriction_mode="FULLY_RESTRICTED")
3131
bucket.google_managed_encryption_enforcement_config = EncryptionEnforcementConfig(restriction_mode="FULLY_RESTRICTED")
3232

3333
bucket.create()
3434

3535
print(f"Created bucket {bucket.name} with Encryption Enforcement Config.")
36-
3736
# [END storage_set_bucket_encryption_enforcement_config]
3837

38+
3939
if __name__ == "__main__":
4040
set_bucket_encryption_enforcement_config(bucket_name="your-unique-bucket-name")

samples/snippets/storage_update_encryption_enforcement_config.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
# limitations under the License.
1414

1515
from google.cloud import storage
16+
from google.cloud.storage.bucket import EncryptionEnforcementConfig
17+
1618

1719
# [START storage_update_encryption_enforcement_config]
1820
def update_encryption_enforcement_config(bucket_name):
@@ -24,7 +26,6 @@ def update_encryption_enforcement_config(bucket_name):
2426
bucket = storage_client.get_bucket(bucket_name)
2527

2628
# 1. Update a specific type (e.g., change GMEK to FULLY_RESTRICTED)
27-
from google.cloud.storage.bucket import EncryptionEnforcementConfig
2829
bucket.google_managed_encryption_enforcement_config = EncryptionEnforcementConfig(restriction_mode="FULLY_RESTRICTED")
2930
bucket.customer_managed_encryption_enforcement_config = EncryptionEnforcementConfig(restriction_mode="NOT_RESTRICTED")
3031

@@ -35,8 +36,8 @@ def update_encryption_enforcement_config(bucket_name):
3536

3637
print(f"Encryption enforcement policy updated for bucket {bucket.name}.")
3738
print("GMEK is now fully restricted, CMEK is now not restricted, and CSEK enforcement has been removed.")
38-
3939
# [END storage_update_encryption_enforcement_config]
4040

41+
4142
if __name__ == "__main__":
4243
update_encryption_enforcement_config(bucket_name="your-unique-bucket-name")

0 commit comments

Comments
 (0)