Skip to content
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

[CIAC-6775] [PAN-OS] Add audit-comment argument #32790

Merged
merged 26 commits into from Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
8b04910
new get-audit-comment command
yaakovpraisler Feb 4, 2024
224b870
audit-comment arg && ip-wildcard
yaakovpraisler Feb 4, 2024
78387e9
tests
yaakovpraisler Feb 5, 2024
e2f943b
tests
yaakovpraisler Feb 8, 2024
282c21c
docs
yaakovpraisler Feb 8, 2024
f5100e4
Merge branch 'master' into panos-audit-comment
yaakovpraisler Feb 8, 2024
4aad148
RN
yaakovpraisler Feb 8, 2024
9eb7d97
fix tests and update docker
yaakovpraisler Feb 8, 2024
c97317a
Merge branch 'master' into panos-audit-comment
yaakovpraisler Feb 8, 2024
66c896d
fix tests
yaakovpraisler Feb 11, 2024
369b0d6
Merge branch 'master' into panos-audit-comment
yaakovpraisler Feb 11, 2024
80e193d
Merge branch 'panos-audit-comment' of github.com:demisto/content into…
yaakovpraisler Feb 11, 2024
da4c801
Merge branch 'master' into panos-audit-comment
yaakovpraisler Feb 26, 2024
bb999b8
bump version
yaakovpraisler Feb 26, 2024
2691575
Merge branch 'master' into panos-audit-comment
yaakovpraisler Feb 28, 2024
bf6ea42
cr
yaakovpraisler Feb 29, 2024
59a156d
Merge branch 'master' into panos-audit-comment
yaakovpraisler Feb 29, 2024
f5c03e0
Merge branch 'master' into panos-audit-comment
yaakovpraisler Mar 3, 2024
49f5f94
Merge branch 'panos-audit-comment' of github.com:demisto/content into…
yaakovpraisler Mar 3, 2024
e7c3d61
cr
yaakovpraisler Mar 3, 2024
87d6b52
Update Packs/PAN-OS/Integrations/Panorama/Panorama.yml
yaakovpraisler Mar 3, 2024
4352890
Update Packs/PAN-OS/ReleaseNotes/2_1_23.md
yaakovpraisler Mar 3, 2024
3065eab
Merge branch 'master' into panos-audit-comment
yaakovpraisler Mar 3, 2024
c6f2cf8
fix
yaakovpraisler Mar 4, 2024
4bf4707
Merge branch 'panos-audit-comment' of github.com:demisto/content into…
yaakovpraisler Mar 4, 2024
bb62da1
Merge branch 'master' into panos-audit-comment
yaakovpraisler Mar 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
165 changes: 127 additions & 38 deletions Packs/PAN-OS/Integrations/Panorama/Panorama.py
Expand Up @@ -214,6 +214,12 @@
'file-forward',
'is-saas')

RULE_TYPES_MAP = {
"Security Rule": "security",
"NAT Rule": "nat",
"PBF Rule": "pbf"
}


class PAN_OS_Not_Found(Exception):
""" PAN-OS Error. """
Expand Down Expand Up @@ -1542,6 +1548,8 @@ def prettify_addresses_arr(addresses_arr: list) -> List:
pretty_address['IP_Netmask'] = address['ip-netmask']
if 'ip-range' in address:
pretty_address['IP_Range'] = address['ip-range']
if 'ip-wildcard' in address:
pretty_address['IP_Wildcard'] = address['ip-wildcard']
if 'fqdn' in address:
pretty_address['FQDN'] = address['fqdn']
if 'tag' in address and address['tag'] is not None and 'member' in address['tag']:
Expand Down Expand Up @@ -1588,7 +1596,8 @@ def panorama_list_addresses_command(args: dict):
'Contents': addresses_arr,
'ReadableContentsFormat': formats['markdown'],
'HumanReadable': tableToMarkdown('Addresses:', addresses_output,
['Name', 'IP_Netmask', 'IP_Range', 'FQDN', 'Tags'], removeNull=True),
['Name', 'IP_Netmask', 'IP_Range', 'IP_Wildcard', 'FQDN', 'Tags'],
removeNull=True),
'EntryContext': {
"Panorama.Addresses(val.Name == obj.Name)": addresses_output
}
Expand Down Expand Up @@ -3865,6 +3874,10 @@ def panorama_create_rule_command(args: dict):
log_forwarding=log_forwarding, tags=tags, category=categories,
from_=source_zone, to=destination_zone, profile_setting=profile_setting,
where=where, dst=dst)

if args.get('audit_comment'):
params['audit-comment'] = args.get('audit_comment')

result = http_request(
URL,
'POST',
Expand Down Expand Up @@ -4005,19 +4018,30 @@ def panorama_edit_rule_items(rulename: str, element_to_change: str, element_valu


def build_audit_comment_params(
name: str, audit_comment: str, pre_post: str, policy_type='security'
name: str, pre_post: str, audit_comment: str = '', policy_type='security', xml_type='set'
) -> dict:
"""
Builds up the params needed to update the audit comment of a policy rule.
"""
_xpath = f"{XPATH_RULEBASE}{pre_post}/{policy_type}/rules/entry[@name='{name}']"
return {
'type': 'op',
'cmd': f"<set><audit-comment><xpath>{_xpath}</xpath><comment>{audit_comment}</comment></audit-comment></set>",
'cmd': build_audit_comment_cmd(_xpath, audit_comment, xml_type),
'key': API_KEY
}


def build_audit_comment_cmd(xpath, audit_comment, xml_type='set') -> str:
"""
Builds up the needed `cmd` param to get or update the audit comment of a policy rule.
"""
if xml_type == 'set':
return f"<set><audit-comment><xpath>{xpath}</xpath><comment>{audit_comment}</comment></audit-comment></set>"
elif xml_type == 'show':
return f"<show><config><list><audit-comments><xpath>{xpath}</xpath></audit-comments></list></config></show>"
return ""


@logger
def panorama_edit_rule_command(args: dict):
"""
Expand Down Expand Up @@ -4046,7 +4070,7 @@ def panorama_edit_rule_command(args: dict):
new_audit_comment = args.get('element_value') or ''
# to update audit-comment of a security rule, it is required to build a 'cmd' parameter
params = build_audit_comment_params(
rulename, new_audit_comment, pre_post='rulebase' if VSYS else pre_post
rulename, pre_post='rulebase' if VSYS else pre_post, audit_comment=new_audit_comment
)
else:
params = {
Expand Down Expand Up @@ -12327,6 +12351,9 @@ def _set_up_original_packet_objects_body_request():
'key': API_KEY
}

if args.get('audit_comment'):
params['audit-comment'] = args.get('audit_comment')

return http_request(URL, 'POST', params=params)


Expand Down Expand Up @@ -12370,22 +12397,28 @@ def pan_os_edit_nat_rule(
):
xpath = build_nat_xpath(name=rule_name, pre_post='rulebase' if VSYS else pre_post, element=element_to_change)

params = {
'xpath': xpath,
'element': dict_to_xml(build_body_request_to_edit_pan_os_object(
behavior=behavior,
object_name=object_name,
element_value=element_value,
is_listable=is_listable,
xpath=xpath,
should_contain_entries=True,
is_commit_required=False
if element_to_change == 'audit-comment':
# to update audit-comment of a nat rule, it is required to build a 'cmd' parameter
params = build_audit_comment_params(
rule_name, pre_post='rulebase' if VSYS else pre_post, audit_comment=element_value, policy_type='nat'
)
),
'action': 'edit',
'type': 'config',
'key': API_KEY
}
else:
params = {
'xpath': xpath,
'element': dict_to_xml(build_body_request_to_edit_pan_os_object(
behavior=behavior,
object_name=object_name,
element_value=element_value,
is_listable=is_listable,
xpath=xpath,
should_contain_entries=True,
is_commit_required=False
)
),
'action': 'edit',
'type': 'config',
'key': API_KEY
}

return http_request(URL, 'POST', params=params)

Expand Down Expand Up @@ -12451,7 +12484,8 @@ def pan_os_edit_nat_rule_command(args):
'dynamic-destination-translation/distribution', 'distribution', False
),
'destination_translation_port': ('destination-translation/translated-port', 'translated-port', False),
'destination_translation_ip': ('destination-translation/translated-address', 'translated-address', False)
'destination_translation_ip': ('destination-translation/translated-address', 'translated-address', False),
'audit-comment': ('audit-comment', '', False)
}

element_to_change, object_name, is_listable = elements_to_change_mapping_pan_os_paths.get(
Expand Down Expand Up @@ -13038,6 +13072,9 @@ def _setup_general_rule_body_request():
'key': API_KEY
}

if args.get('audit_comment'):
params['audit-comment'] = args.get('audit_comment')

return http_request(URL, 'POST', params=params)


Expand All @@ -13058,23 +13095,29 @@ def pan_os_edit_pbf_rule(
name=rule_name, pre_post='rulebase' if VSYS else pre_post, element_to_change=element_to_change
)

params = {
'xpath': xpath,
'element': dict_to_xml(build_body_request_to_edit_pan_os_object(
behavior=behavior,
object_name=object_name,
element_value=element_value,
is_listable=is_listable,
xpath=xpath,
is_entry=True if object_name == 'nexthop-address-list' else False,
is_empty_tag=True if object_name == 'action' else False
),
contains_xml_chars=True
),
'action': 'edit',
'type': 'config',
'key': API_KEY
}
if element_to_change == 'audit-comment':
# to update audit-comment of a pbf rule, it is required to build a 'cmd' parameter
params = build_audit_comment_params(
rule_name, pre_post='rulebase' if VSYS else pre_post, audit_comment=element_value, policy_type='pbf'
)
else:
params = {
'xpath': xpath,
'element': dict_to_xml(build_body_request_to_edit_pan_os_object(
behavior=behavior,
object_name=object_name,
element_value=element_value,
is_listable=is_listable,
xpath=xpath,
is_entry=True if object_name == 'nexthop-address-list' else False,
is_empty_tag=True if object_name == 'action' else False
),
contains_xml_chars=True
),
'action': 'edit',
'type': 'config',
'key': API_KEY
}

return http_request(URL, 'POST', params=params)

Expand Down Expand Up @@ -13110,7 +13153,8 @@ def pan_os_edit_pbf_rule_command(args):
'description': ('description', 'description', False),
'negate_source': ('negate-source', 'negate-source', False),
'negate_destination': ('negate-destination', 'negate-destination', False),
'disabled': ('disabled', 'disabled', False)
'disabled': ('disabled', 'disabled', False),
'audit-comment': ('audit-comment', '', False)
}

if DEVICE_GROUP and not pre_post: # panorama instances must have the pre_post argument!
Expand Down Expand Up @@ -13660,6 +13704,49 @@ def pan_os_delete_tag_command(args: dict) -> CommandResults:
)


def pan_os_get_audit_comment_command(args: dict) -> CommandResults:
"""
executes the command pan-os-get-audit-comment to get the audit comment for a given policy rule.

Args:
args (dict): The command arguments.

Returns:
CommandResults: The command results with raw response, outputs and readable outputs.
"""
if DEVICE_GROUP and not PRE_POST:
raise DemistoException(f'The pre_post argument must be provided for panorama instance')

rule_name = args.get("rule_name") or ""
rule_type = args.get("rule_type") or ""
params = build_audit_comment_params(
name=rule_name,
pre_post='rulebase' if VSYS else f'{PRE_POST.lower()}-rulebase',
policy_type=RULE_TYPES_MAP[rule_type],
xml_type='show',
)

raw_response = http_request(URL, 'GET', params=params)
comment = (raw_response["response"]["result"] or {}).get("entry", {}).get("comment", "") or ""
outputs = {
"rule_name": rule_name,
"rule_type": rule_type,
"comment": comment
}

return CommandResults(
raw_response=raw_response,
outputs=outputs,
readable_output=tableToMarkdown(
f'Audit Comment for Rule: {rule_name}',
outputs,
headerTransform=string_to_table_header,
),
outputs_prefix='Panorama.AuditComment',
outputs_key_field=['rule_name', 'rule_type']
)


""" Fetch Incidents """


Expand Down Expand Up @@ -14805,6 +14892,8 @@ def main(): # pragma: no cover
return_results(list_device_groups_names())
elif command == 'pan-os-export-tech-support-file':
return_results(export_tsf_command(args))
elif command == 'pan-os-get-audit-comment':
return_results(pan_os_get_audit_comment_command(args))
else:
raise NotImplementedError(f'Command {command} is not implemented.')
except Exception as err:
Expand Down
40 changes: 40 additions & 0 deletions Packs/PAN-OS/Integrations/Panorama/Panorama.yml
Expand Up @@ -5148,6 +5148,8 @@ script:
- bottom
- description: The destination rule relative to the rule that you are moving. This field is only relevant if you specify "before" or "after" in the "where" argument.
name: dst
- description: An audit comment for the rule.
name: audit_comment
description: Creates a policy rule.
execution: true
name: pan-os-create-rule
Expand Down Expand Up @@ -8674,6 +8676,8 @@ script:
predefined:
- forward
- reverse
- description: An audit comment for the rule.
name: audit_comment
description: Creates a new NAT rule in a Panorama/firewall instance.
name: pan-os-create-nat-rule
- arguments:
Expand Down Expand Up @@ -8735,6 +8739,7 @@ script:
- destination_translation_dynamic_ip
- destination_translation_dynamic_distribution_method
- disabled
- audit-comment
yaakovpraisler marked this conversation as resolved.
Show resolved Hide resolved
- description: The value of the element to change. Can be a list for certain elements.
isArray: true
name: element_value
Expand Down Expand Up @@ -9113,6 +9118,8 @@ script:
- description: The nexthop addresses list for the symmetric return.
isArray: true
name: nexthop_address_list
- description: An audit comment for the rule.
name: audit_comment
description: Creates a new policy-based-forwarding (PBF) rule in a Panorama/firewall instance.
name: pan-os-create-pbf-rule
- arguments:
Expand Down Expand Up @@ -9148,6 +9155,7 @@ script:
- action_forward_discard
- action_forward_no_pbf
- disabled
- audit-comment
- description: The value of the element to change. Can be a list for some of the elements. When element_to_change == 'action_forward_egress_interface', the action of the rule will be changed to 'forward' automatically.
isArray: true
name: element_value
Expand Down Expand Up @@ -9364,6 +9372,38 @@ script:
description: The job ID to use when polling.
description: Exports a tech support file (TSF).
polling: true
- arguments:
- description: The rule name to apply.
name: rule_name
required: true
- auto: PREDEFINED
description: The rule type. Possible values are - Security Rule, NAT Rule, PBF Rule.
yaakovpraisler marked this conversation as resolved.
Show resolved Hide resolved
name: rule_type
required: true
predefined:
- Security Rule
- NAT Rule
- PBF Rule
- auto: PREDEFINED
description: The pre-rule or post-rule (Panorama instances only).
name: pre_post
predefined:
- Pre
- Post
- description: The device group that the rule is part of.
name: device-group
description: Gets the audit comment of a rule.
name: pan-os-get-audit-comment
outputs:
- contextPath: Panorama.AuditComment.comment
description: The audit comment ot the rule.
type: String
- contextPath: Panorama.AuditComment.rule_name
description: The rule name.
type: String
- contextPath: Panorama.AuditComment.rule_type
description: The rule type.
type: String
dockerimage: demisto/pan-os-python:1.0.0.87401
isfetch: true
runonce: false
Expand Down