Skip to main content

Security Rule Models

Overview

The Security Rule models provide a structured way to manage security rules in Palo Alto Networks' Strata Cloud Manager. These models support defining security policies with source/destination zones, addresses, applications, and actions. Rules can be defined in folders, snippets, or devices and placed in either pre or post rulebases. The models handle validation of inputs and outputs when interacting with the SCM API.

Models

The module provides the following Pydantic models:

  • SecurityRuleBaseModel: Base model with fields common to all rule operations
  • SecurityRuleCreateModel: Model for creating new security rules
  • SecurityRuleUpdateModel: Model for updating existing security rules
  • SecurityRuleResponseModel: Response model for security rule operations
  • SecurityRuleMoveModel: Model for moving rules within a rulebase
  • SecurityRuleProfileSetting: Model for security profile settings

All models use extra="forbid" configuration, which rejects any fields not explicitly defined in the model.

Model Attributes

SecurityRuleBaseModel

AttributeTypeRequiredDefaultDescription
namestrYesNoneName of the rule. Pattern: ^[a-zA-Z0-9_ \.-]+$
disabledboolNoFalseWhether the rule is disabled
descriptionstrNoNoneDescription of the rule
tagList[str]No[]List of tags
from_List[str]No["any"]Source security zones
sourceList[str]No["any"]Source addresses
negate_sourceboolNoFalseNegate source addresses
source_userList[str]No["any"]Source users/groups
source_hipList[str]No["any"]Source Host Integrity Profiles
to_List[str]No["any"]Destination security zones
destinationList[str]No["any"]Destination addresses
negate_destinationboolNoFalseNegate destination addresses
destination_hipList[str]No["any"]Destination Host Integrity Profiles
applicationList[str]No["any"]Applications
serviceList[str]No["any"]Services
categoryList[str]No["any"]URL categories
actionSecurityRuleActionNoallowRule action
profile_settingSecurityRuleProfileSettingNoNoneSecurity profile settings
log_settingstrNoNoneLog forwarding profile
schedulestrNoNoneSchedule profile
log_startboolNoNoneLog at session start
log_endboolNoNoneLog at session end
folderstrNo**NoneFolder location. Max 64 chars
snippetstrNo**NoneSnippet location. Max 64 chars
devicestrNo**NoneDevice location. Max 64 chars

** Exactly one container (folder/snippet/device) must be provided for create operations

SecurityRuleCreateModel

Inherits all fields from SecurityRuleBaseModel and adds:

AttributeTypeRequiredDefaultDescription
rulebaseSecurityRuleRulebaseNoNoneWhich rulebase to use (pre/post)

Enforces that exactly one of folder, snippet, or device is provided during creation.

SecurityRuleUpdateModel

Extends SecurityRuleBaseModel by adding:

AttributeTypeRequiredDefaultDescription
idUUIDYesNoneThe unique identifier of the rule
rulebaseSecurityRuleRulebaseNoNoneWhich rulebase to use (pre/post)

SecurityRuleResponseModel

Extends SecurityRuleBaseModel by adding:

AttributeTypeRequiredDefaultDescription
idUUIDYesNoneThe unique identifier of the rule
rulebaseSecurityRuleRulebaseNoNoneWhich rulebase the rule belongs to
policy_typestrNoNoneThe policy type (e.g., 'Security')

SecurityRuleMoveModel

AttributeTypeRequiredDefaultDescription
destinationSecurityRuleMoveDestinationYesNoneWhere to move (top/bottom/before/after)
rulebaseSecurityRuleRulebaseYesNoneWhich rulebase to use (pre/post)
destination_ruleUUIDNoNoneUUID of reference rule (for before/after moves)

Enum Types

SecurityRuleAction

Defines the available rule actions:

ValueDescription
allowAllow the traffic
denyDeny the traffic
dropDrop the traffic
reset-clientReset client connection
reset-serverReset server connection
reset-bothReset both connections

SecurityRuleRulebase

Defines the available rulebases:

ValueDescription
prePre-rulebase
postPost-rulebase

SecurityRuleMoveDestination

Defines the move destinations:

ValueDescription
topMove to top of rulebase
bottomMove to bottom of rulebase
beforeMove before a specific rule
afterMove after a specific rule

Component Models

SecurityRuleProfileSetting

AttributeTypeRequiredDefaultDescription
groupList[str]No["best-practice"]Security profile group

Exceptions

The Security Rule models can raise the following exceptions during validation:

  • ValueError: Raised in several scenarios:
    • When multiple container types (folder/snippet/device) are specified
    • When no container type is specified for create operations
    • When list field values are not unique
    • When list field values are not strings
    • When invalid action types are provided
    • When name pattern validation fails
    • When container field pattern validation fails
    • When field length limits are exceeded
    • When invalid move configurations are provided (e.g. missing destination_rule for before/after moves)
    • When destination_rule is provided for top/bottom moves

Model Validators

Container Type Validation

For create operations, exactly one container type must be specified:

from scm.models.security import SecurityRuleCreateModel

# Error: multiple containers specified
try:
rule = SecurityRuleCreateModel(
name="invalid-rule",
folder="Texas",
device="fw01", # Can't specify both folder and device
action="allow"
)
except ValueError as e:
print(e) # "Exactly one of 'folder', 'snippet', or 'device' must be provided."

# Error: no container specified
try:
rule = SecurityRuleCreateModel(
name="invalid-rule",
action="allow"
)
except ValueError as e:
print(e) # "Exactly one of 'folder', 'snippet', or 'device' must be provided."

List Field Validation

All list fields are validated to ensure they contain only unique string values:

from scm.models.security import SecurityRuleCreateModel

# Error: duplicate values
try:
rule = SecurityRuleCreateModel(
name="invalid-rule",
folder="Texas",
source=["10.0.0.0/8", "10.0.0.0/8"] # Duplicate values not allowed
)
except ValueError as e:
print(e) # "List items must be unique"

Move Configuration Validation

The SecurityRuleMoveModel validates that destination_rule is provided only for before/after moves:

from scm.models.security import SecurityRuleMoveModel

# Error: missing destination_rule for 'before' move
try:
move = SecurityRuleMoveModel(
destination="before",
rulebase="pre"
# destination_rule is required for before/after
)
except ValueError as e:
print(e) # "destination_rule is required when destination is 'before'"

# Error: destination_rule provided for 'top' move
try:
move = SecurityRuleMoveModel(
destination="top",
rulebase="pre",
destination_rule="987fcdeb-51d3-a456-426655440000" # Not allowed for top/bottom
)
except ValueError as e:
print(e) # "destination_rule should not be provided when destination is 'top'"

Usage Examples

Creating a Basic Security Rule

from scm.client import Scm

# Initialize client
client = Scm(
client_id="your_client_id",
client_secret="your_client_secret",
tsg_id="your_tsg_id"
)

# Using dictionary
rule_dict = {
"name": "allow-web",
"description": "Allow web traffic",
"folder": "Texas",
"from_": ["trust"],
"to_": ["untrust"],
"source": ["10.0.0.0/8"],
"destination": ["any"],
"application": ["web-browsing", "ssl"],
"service": ["application-default"],
"action": "allow",
"log_end": True
}

response = client.security_rule.create(rule_dict, rulebase="pre")
print(f"Created rule: {response.name}")

Creating a Rule with Security Profiles

from scm.client import Scm

# Initialize client
client = Scm(
client_id="your_client_id",
client_secret="your_client_secret",
tsg_id="your_tsg_id"
)

# Using dictionary
rule_dict = {
"name": "secure-web",
"folder": "Texas",
"from_": ["trust"],
"to_": ["untrust"],
"source": ["10.0.0.0/8"],
"destination": ["any"],
"application": ["web-browsing", "ssl"],
"action": "allow",
"profile_setting": {
"group": ["strict-security"]
},
"log_setting": "detailed-logging",
"log_start": True,
"log_end": True
}

response = client.security_rule.create(rule_dict, rulebase="pre")
print(f"Created rule with profile: {response.name}")

Updating a Security Rule

from scm.client import Scm

# Initialize client
client = Scm(
client_id="your_client_id",
client_secret="your_client_secret",
tsg_id="your_tsg_id"
)

# Fetch existing rule
existing = client.security_rule.fetch(name="allow-web", folder="Texas", rulebase="pre")

# Modify attributes using dot notation
existing.description = "Updated web access rule"
existing.application = ["web-browsing", "ssl", "http2"]

# Update profile setting
existing.profile_setting = {"group": ["strict-security"]}

# Enable logging
existing.log_start = True
existing.log_end = True

# Pass modified object to update()
updated = client.security_rule.update(existing, rulebase="pre")
print(f"Updated rule: {updated.name}")

Moving a Security Rule

from scm.client import Scm

# Initialize client
client = Scm(
client_id="your_client_id",
client_secret="your_client_secret",
tsg_id="your_tsg_id"
)

# Fetch the rule to move
rule = client.security_rule.fetch(name="allow-web", folder="Texas", rulebase="pre")

# Move to top of rulebase
move_config = {
"destination": "top",
"rulebase": "pre"
}
client.security_rule.move(rule.id, move_config)

# Move before another rule
move_before = {
"destination": "before",
"rulebase": "pre",
"destination_rule": "987fcdeb-51d3-a456-426655440000"
}
client.security_rule.move(rule.id, move_before)