This repository has been archived by the owner on Nov 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Url filtering v1 model #638
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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
50 changes: 50 additions & 0 deletions
50
catalystwan/endpoints/configuration/policy/definition/url_filtering.py
This file contains 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,50 @@ | ||
# Copyright 2024 Cisco Systems, Inc. and its affiliates | ||
|
||
# mypy: disable-error-code="empty-body" | ||
|
||
from uuid import UUID | ||
|
||
from catalystwan.endpoints import APIEndpoints, delete, get, post, put | ||
from catalystwan.endpoints.configuration.policy.abstractions import PolicyDefinitionEndpoints | ||
from catalystwan.models.policy.definition.url_filtering import ( | ||
UrlFilteringPolicy, | ||
UrlFilteringPolicyEditPayload, | ||
UrlFilteringPolicyGetResponse, | ||
) | ||
from catalystwan.models.policy.policy_definition import ( | ||
PolicyDefinitionEditResponse, | ||
PolicyDefinitionId, | ||
PolicyDefinitionInfo, | ||
PolicyDefinitionPreview, | ||
) | ||
from catalystwan.typed_list import DataSequence | ||
|
||
|
||
class ConfigurationPolicyUrlFilteringDefinition(APIEndpoints, PolicyDefinitionEndpoints): | ||
@post("/template/policy/definition/urlfiltering") | ||
def create_policy_definition(self, payload: UrlFilteringPolicy) -> PolicyDefinitionId: | ||
... | ||
|
||
@delete("/template/policy/definition/urlfiltering/{id}") | ||
def delete_policy_definition(self, id: UUID) -> None: | ||
... | ||
|
||
@put("/template/policy/definition/urlfiltering/{id}") | ||
def edit_policy_definition(self, id: UUID, payload: UrlFilteringPolicyEditPayload) -> PolicyDefinitionEditResponse: | ||
... | ||
|
||
@get("/template/policy/definition/urlfiltering", "data") | ||
def get_definitions(self) -> DataSequence[PolicyDefinitionInfo]: | ||
... | ||
|
||
@get("/template/policy/definition/urlfiltering/{id}") | ||
def get_policy_definition(self, id: UUID) -> UrlFilteringPolicyGetResponse: | ||
... | ||
|
||
@post("/template/policy/definition/urlfiltering/preview") | ||
def preview_policy_definition(self, payload: UrlFilteringPolicy) -> PolicyDefinitionPreview: | ||
... | ||
|
||
@get("/template/policy/definition/urlfiltering/preview/{id}") | ||
def preview_policy_definition_by_id(self, id: UUID) -> PolicyDefinitionPreview: | ||
... |
This file contains 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 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 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,152 @@ | ||
# Copyright 2024 Cisco Systems, Inc. and its affiliates | ||
|
||
from typing import List, Literal, Optional, Set | ||
|
||
from pydantic import BaseModel, ConfigDict, Field, field_validator | ||
|
||
from catalystwan.models.common import PolicyModeType, VpnId | ||
from catalystwan.models.policy.policy_definition import ( | ||
PolicyDefinitionBase, | ||
PolicyDefinitionGetResponse, | ||
PolicyDefinitionId, | ||
Reference, | ||
) | ||
|
||
BLOCK_PAGE_CONTENT_HEADER = "Access to the requested page has been denied." | ||
|
||
BlockPageAction = Literal["text", "redirectUrl"] | ||
UrlFilteringAlerts = Literal["blacklist", "whitelist", "categories-reputation"] | ||
WebReputation = Literal["low-risk", "moderate-risk", "high-risk", "suspicious", "trustworthy"] | ||
WebCategoriesAction = Literal["allow", "block"] | ||
WebCategories = Literal[ | ||
"abused-drugs", | ||
"abortion", | ||
"adult-and-pornography", | ||
"alcohol-and-tobacco", | ||
"auctions", | ||
"bot-nets", | ||
"business-and-economy", | ||
"cdns", | ||
"cheating", | ||
"computer-and-internet-info", | ||
"computer-and-internet-security", | ||
"games", | ||
"gambling", | ||
"financial-services", | ||
"fashion-and-beauty", | ||
"entertainment-and-arts", | ||
"educational-institutions", | ||
"dynamic-content", | ||
"dead-sites", | ||
"confirmed-spam-sources", | ||
"cult-and-occult", | ||
"dating", | ||
"government", | ||
"gross", | ||
"hacking", | ||
"hate-and-racism", | ||
"health-and-medicine", | ||
"home", | ||
"hunting-and-fishing", | ||
"illegal", | ||
"image-and-video-search", | ||
"individual-stock-advice-and-tools", | ||
"internet-communications", | ||
"internet-portals", | ||
"job-search", | ||
"keyloggers-and-monitoring", | ||
"kids", | ||
"legal", | ||
"local-information", | ||
"malware-sites", | ||
"marijuana", | ||
"p2p", | ||
"parked-sites", | ||
"pay-to-surf", | ||
"personal-sites-and-blogs", | ||
"philosophy-and-political-advocacy", | ||
"phishing-and-other-frauds", | ||
"private-ip-addresses", | ||
"proxy-avoid-and-anonymizers", | ||
"questionable", | ||
"real-estate", | ||
"recreation-and-hobbies", | ||
"reference-and-research", | ||
"religion", | ||
"search-engines", | ||
"sex-education", | ||
"shareware-and-freeware", | ||
"shopping", | ||
"social-network", | ||
"society", | ||
"sports", | ||
"spam-urls", | ||
"spyware-and-adware", | ||
"streaming-media", | ||
"swimsuits-and-intimate-apparel", | ||
"training-and-tools", | ||
"translation", | ||
"travel", | ||
"uncategorized", | ||
"unconfirmed-spam-sources", | ||
"violence", | ||
"weapons", | ||
"web-advertisements", | ||
"web-based-email", | ||
"web-hosting", | ||
"open-http-proxies", | ||
"online-personal-storage", | ||
"online-greeting-cards", | ||
"nudity", | ||
"news-and-media", | ||
"music", | ||
"motor-vehicles", | ||
"military", | ||
] | ||
|
||
|
||
class UrlFilteringDefinition(BaseModel): | ||
model_config = ConfigDict(populate_by_name=True) | ||
web_categories_action: WebCategoriesAction = Field( | ||
validation_alias="webCategoriesAction", serialization_alias="webCategoriesAction" | ||
) | ||
web_categories: List[WebCategories] = Field(validation_alias="webCategories", serialization_alias="webCategories") | ||
web_reputation: WebReputation = Field(validation_alias="webReputation", serialization_alias="webReputation") | ||
url_white_list: Optional[Reference] = Field( | ||
default=None, validation_alias="urlWhiteList", serialization_alias="urlWhiteList" | ||
) | ||
url_black_list: Optional[Reference] = Field( | ||
default=None, validation_alias="urlBlackList", serialization_alias="urlBlackList" | ||
) | ||
block_page_action: BlockPageAction = Field( | ||
validation_alias="blockPageAction", serialization_alias="blockPageAction" | ||
) | ||
block_page_contents: str = Field( | ||
default=BLOCK_PAGE_CONTENT_HEADER, validation_alias="blockPageContents", serialization_alias="blockPageContents" | ||
) | ||
logging: List[str] = Field(default=[]) | ||
enable_alerts: bool = Field(validation_alias="enableAlerts", serialization_alias="enableAlerts") | ||
alerts: Set[UrlFilteringAlerts] = Field(default=[], validation_alias="alerts", serialization_alias="alerts") | ||
target_vpns: List[VpnId] = Field(default=[], validation_alias="targetVpns", serialization_alias="targetVpns") | ||
|
||
@field_validator("url_black_list", "url_white_list", mode="before") | ||
@classmethod | ||
def convert_empty_dict_to_none(cls, value): | ||
if not value: | ||
return None | ||
|
||
return value | ||
|
||
|
||
class UrlFilteringPolicy(PolicyDefinitionBase): | ||
type: Literal["urlFiltering"] = "urlFiltering" | ||
mode: PolicyModeType = "security" | ||
definition: UrlFilteringDefinition | ||
|
||
|
||
class UrlFilteringPolicyEditPayload(UrlFilteringPolicy, PolicyDefinitionId): | ||
pass | ||
|
||
|
||
class UrlFilteringPolicyGetResponse(UrlFilteringPolicy, PolicyDefinitionGetResponse): | ||
pass |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure, if it's the best solution.
in some cases the url_black_list / white_list is not None, but empty dict, which cannot be deserialised do the Reference object.
The validator checks if value is an empty dict and the returns None,.
Everything works as expected. Tested a few cases
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me 👍