-
Notifications
You must be signed in to change notification settings - Fork 167
feat: add support for bucket IP filter #1516
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
93b0815
feat: add support for bucket IP filter
Pulkit0110 274c4aa
minor fix
Pulkit0110 64eb3c1
fix unit tests
Pulkit0110 fa6b341
change create bucket with filter system test
Pulkit0110 9bccc2c
add more system tests
Pulkit0110 a2186f5
update system tests
Pulkit0110 78caaed
resolving comments
Pulkit0110 8fc779d
Merge branch 'main' into bucket-ip-filter
Pulkit0110 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| # Copyright 2014 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """IP Filter configuration for Google Cloud Storage Buckets.""" | ||
|
|
||
| from typing import Dict, Any, Optional, List | ||
|
|
||
| _MODE = "mode" | ||
| _PUBLIC_NETWORK_SOURCE = "publicNetworkSource" | ||
| _VPC_NETWORK_SOURCES = "vpcNetworkSources" | ||
| _ALLOWED_IP_CIDR_RANGES = "allowedIpCidrRanges" | ||
| _NETWORK = "network" | ||
| _ALLOW_ALL_SERVICE_AGENT_ACCESS = "allowAllServiceAgentAccess" | ||
|
|
||
| class PublicNetworkSource: | ||
| """Represents a public network source for a GCS Bucket IP Filter. | ||
|
|
||
| :type allowed_ip_cidr_ranges: list(str) or None | ||
| :param allowed_ip_cidr_ranges: A list of public IPv4 or IPv6 ranges in | ||
| CIDR notation that are allowed to access | ||
| the bucket. | ||
| """ | ||
|
|
||
| def __init__(self, allowed_ip_cidr_ranges: Optional[List[str]] = None): | ||
| self.allowed_ip_cidr_ranges = allowed_ip_cidr_ranges or [] | ||
|
|
||
| def _to_api_resource(self) -> Dict[str, Any]: | ||
| """Serializes this object to a dictionary for API requests.""" | ||
| return {_ALLOWED_IP_CIDR_RANGES: self.allowed_ip_cidr_ranges} | ||
|
|
||
|
|
||
| class VpcNetworkSource: | ||
| """Represents a VPC network source for a GCS Bucket IP Filter. | ||
|
|
||
| :type network: str | ||
| :param network: The resource name of the VPC network. | ||
|
|
||
| :type allowed_ip_cidr_ranges: list(str) or None | ||
| :param allowed_ip_cidr_ranges: A list of IPv4 or IPv6 ranges in CIDR | ||
| notation allowed to access the bucket | ||
| from this VPC. | ||
| """ | ||
|
|
||
| def __init__(self, network: str, allowed_ip_cidr_ranges: Optional[List[str]] = None): | ||
Pulkit0110 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| self.network = network | ||
| self.allowed_ip_cidr_ranges = allowed_ip_cidr_ranges or [] | ||
|
|
||
| def _to_api_resource(self) -> Dict[str, Any]: | ||
| """Serializes this object to a dictionary for API requests.""" | ||
| return { | ||
| _NETWORK: self.network, | ||
| _ALLOWED_IP_CIDR_RANGES: self.allowed_ip_cidr_ranges, | ||
| } | ||
|
|
||
|
|
||
| class IPFilter: | ||
| """Represents a GCS Bucket IP Filter configuration. | ||
|
|
||
| This class is a helper for constructing the IP Filter dictionary to be | ||
| assigned to a bucket's ``ip_filter`` property. | ||
| """ | ||
|
|
||
| def __init__(self): | ||
Pulkit0110 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| self.mode: Optional[str] = None | ||
| """str: The mode of the IP filter. Can be "Enabled" or "Disabled".""" | ||
|
|
||
| self.public_network_source: Optional[PublicNetworkSource] = None | ||
| """(Optional) :class:`PublicNetworkSource`: The configuration for public sources.""" | ||
|
|
||
| self.vpc_network_sources: List[VpcNetworkSource] = [] | ||
| """(Optional) list of :class:`VpcNetworkSource`: Configurations for VPC sources.""" | ||
|
|
||
| self.allow_all_service_agent_access: Optional[bool] = None | ||
Pulkit0110 marked this conversation as resolved.
Show resolved
Hide resolved
Pulkit0110 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """(Optional) bool: If True, allows GCS service agents to bypass the filter.""" | ||
|
|
||
| @classmethod | ||
| def _from_api_resource(cls, resource: Dict[str, Any]) -> "IPFilter": | ||
| """Factory: creates an IPFilter instance from a server response.""" | ||
| ip_filter = cls() | ||
| ip_filter.mode = resource.get(_MODE) | ||
| ip_filter.allow_all_service_agent_access = resource.get( | ||
| _ALLOW_ALL_SERVICE_AGENT_ACCESS | ||
Pulkit0110 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ) | ||
|
|
||
| pns_res = resource.get(_PUBLIC_NETWORK_SOURCE) | ||
Pulkit0110 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
Pulkit0110 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if pns_res: | ||
| ip_filter.public_network_source = PublicNetworkSource( | ||
| allowed_ip_cidr_ranges=pns_res.get(_ALLOWED_IP_CIDR_RANGES) | ||
| ) | ||
|
|
||
| vns_res_list = resource.get(_VPC_NETWORK_SOURCES, []) | ||
| ip_filter.vpc_network_sources = [ | ||
| VpcNetworkSource( | ||
| network=vns.get(_NETWORK), | ||
| allowed_ip_cidr_ranges=vns.get(_ALLOWED_IP_CIDR_RANGES), | ||
| ) | ||
| for vns in vns_res_list | ||
| ] | ||
| return ip_filter | ||
|
|
||
| def _to_api_resource(self) -> Dict[str, Any]: | ||
| """Serializes this object to a dictionary for API requests.""" | ||
| resource = {_MODE: self.mode} | ||
| if self.public_network_source: | ||
| resource[_PUBLIC_NETWORK_SOURCE] = self.public_network_source._to_api_resource() | ||
| if self.vpc_network_sources is not None: | ||
| resource[_VPC_NETWORK_SOURCES] = [ | ||
| vns._to_api_resource() for vns in self.vpc_network_sources | ||
| ] | ||
| resource[_ALLOW_ALL_SERVICE_AGENT_ACCESS] = ( | ||
| self.allow_all_service_agent_access | ||
| if self.allow_all_service_agent_access is not None | ||
| else False | ||
| ) | ||
| return resource | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.