|
| 1 | +# Copyright 2026 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 | +from typing import Generator |
| 16 | +import uuid |
| 17 | + |
| 18 | +from google.api_core import exceptions |
| 19 | +from google.cloud import storage |
| 20 | +import pytest |
| 21 | + |
| 22 | +import storage_create_bucket_ip_filtering |
| 23 | +import storage_delete_ip_filtering_rules |
| 24 | +import storage_disable_ip_filtering |
| 25 | +import storage_enable_ip_filtering |
| 26 | +import storage_get_ip_filtering |
| 27 | +import storage_list_buckets_ip_filtering |
| 28 | + |
| 29 | + |
| 30 | +@pytest.fixture |
| 31 | +def test_bucket() -> Generator[str, None, None]: |
| 32 | + storage_client = storage.Client() |
| 33 | + bucket_name = f"ipfilter-test-{uuid.uuid4().hex[:10]}" |
| 34 | + yield bucket_name |
| 35 | + try: |
| 36 | + bucket = storage_client.get_bucket(bucket_name) |
| 37 | + ip_filter = bucket.ip_filter |
| 38 | + if ip_filter and ip_filter.mode != "Disabled": |
| 39 | + ip_filter.mode = "Disabled" |
| 40 | + bucket.ip_filter = ip_filter |
| 41 | + bucket.patch() |
| 42 | + bucket.delete(force=True) |
| 43 | + except Exception: |
| 44 | + pass |
| 45 | + |
| 46 | + |
| 47 | +def test_ip_filter_lifecycle(test_bucket: str, capsys: pytest.CaptureFixture) -> None: |
| 48 | + public_range = "0.0.0.0/0" |
| 49 | + project_id = storage.Client().project |
| 50 | + vpc_network = f"projects/{project_id}/global/networks/default" |
| 51 | + |
| 52 | + # 1. Create with IP filtering |
| 53 | + try: |
| 54 | + created = storage_create_bucket_ip_filtering.create_bucket_ip_filtering( |
| 55 | + test_bucket, public_range |
| 56 | + ) |
| 57 | + assert created.ip_filter is not None |
| 58 | + assert created.ip_filter.mode == "Disabled" |
| 59 | + |
| 60 | + # 2. Enable IP filtering |
| 61 | + enabled = storage_enable_ip_filtering.enable_ip_filtering( |
| 62 | + test_bucket, |
| 63 | + public_range=public_range, |
| 64 | + vpc_network=vpc_network, |
| 65 | + vpc_range="10.0.0.0/24", |
| 66 | + ) |
| 67 | + assert enabled.ip_filter.mode == "Enabled" |
| 68 | + |
| 69 | + # 3. Get IP filtering |
| 70 | + fetched = storage_get_ip_filtering.get_ip_filtering(test_bucket) |
| 71 | + assert fetched.mode == "Enabled" |
| 72 | + |
| 73 | + # 4. Disable IP filtering |
| 74 | + disabled = storage_disable_ip_filtering.disable_ip_filtering(test_bucket) |
| 75 | + assert disabled.ip_filter.mode == "Disabled" |
| 76 | + |
| 77 | + # 5. Delete IP filtering rules |
| 78 | + modified = storage_delete_ip_filtering_rules.delete_ip_filtering_rules( |
| 79 | + test_bucket, |
| 80 | + public_range_to_delete=public_range, |
| 81 | + vpc_network_to_delete=vpc_network, |
| 82 | + ) |
| 83 | + assert ( |
| 84 | + modified.ip_filter.public_network_source is None |
| 85 | + or public_range |
| 86 | + not in modified.ip_filter.public_network_source.allowed_ip_cidr_ranges |
| 87 | + ) |
| 88 | + assert not any( |
| 89 | + v.network == vpc_network for v in modified.ip_filter.vpc_network_sources |
| 90 | + ) |
| 91 | + |
| 92 | + # 6. List buckets with IP filtering |
| 93 | + storage_list_buckets_ip_filtering.list_buckets_ip_filtering() |
| 94 | + out, _ = capsys.readouterr() |
| 95 | + assert test_bucket in out |
| 96 | + except exceptions.Forbidden as e: |
| 97 | + pytest.skip( |
| 98 | + "Skipping test due to insufficient permissions or IP filter" |
| 99 | + f" network restriction: {e}" |
| 100 | + ) |
0 commit comments