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

refactor: multi-level snapshot comparison and config parser refactor #128

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

alperenkose
Copy link
Collaborator

@alperenkose alperenkose commented Dec 5, 2023

Description

This is a draft PR for review. Unit tests and docstrings need to be updated with code clean-up.

This PR enhances snapshot comparison by allowing properties to be filtered on different levels of the snapshots.
missing/added comparison fields are also filtered according to the properties set.
It includes refactoring of ConfigParser class providing usable static methods for SnapshotComparison.

Motivation and Context

It fixes the issue #108 and complements the draft #109 PR.

In addition to the mentioned issue, considering the below snapshots (snapshot-1 and snapshot-2), it makes it possible to compare/exclude fields on different levels. See all possible examples below:

        {"license": {
            "properties": ["PAN-DB URL Filtering"]  # comparison for parent level
        }},
        {"license": {
            "properties": ["!Logging Service"]  # also support if property exists in different levels in different dicts
        }},
        {"license": {
            "properties": ["issued", "PAN-DB URL Filtering"]  # multi-level "AND" operation (combination with parent) for properties is not supported on purpose - "PAN-DB URL Filtering" diff will be made for all its attributes since its the parent
        }},

        # 1st and 2nd level keys
        {"license": {
            "properties": ["!_Log_Storage_TB"]
        }},
        {"license": {
            "properties": ["_Log_Storage_TB"]
        }},
        {"license": {
            "properties": ["_Log_Storage_TB","issued"]
        }},
        {"license": {
            "properties": ["issued", "!logtype"]
        }},
        {"license": {
            "properties": ["serial", "!logtype"]
        }},
        {"license": {
            "properties": ["custom"]
        }},
        {"license": {
            "properties": ["!custom"]
        }},
        {"license": {
            "properties": ["alogging"]
        }},
        {"license": {
            "properties": ["blogging"]
        }},
        {"license": {
            "properties": ["something"]  # since no such key is there it passes - works
        }},
        {"license": {
            "properties": ["serial", "non-existing"] # compare only requested
        }},
        {"license": {
            "properties": ["all"]
        }},
        {"license": {
            "properties": ["!issued", "all"] # compare all except
        }},
        {"license": {
            "properties": ["!logtype"]
        }},
        {"license": {
            "properties": ["!Logging Service", "issued"]
        }},

snapshot-1

{
  "license": {
    "Logging Service": {
      "authcode": null,
      "custom": {
          "_Log_Storage_TB": "7",
          "logtype": "disk",
          "alogging": "enabled"
      },
      "description": "Device Logging Service",
      "expired": "no",
      "expires": "August 04, 2024",
      "feature": "Logging Service",
      "issued": "June 27, 2022",
      "serial": "013201027229"
    },
    "Y feature": {
      "authcode": null,
      "description": "Device Logging Service",
      "expired": "no",
      "expires": "August 04, 2024",
      "feature": "Logging Service",
      "issued": "June 29, 2022",
      "serial": "013201027248"
    },
    "PAN-DB URL Filtering": {
      "Logging Service": "somehting",
      "authcode": null,
      "description": "Palo Alto Networks URL Filtering License",
      "expired": "no",
      "expires": "June 30, 2028",
      "feature": "PAN-DB URL Filtering",
      "issued": "April 27, 2023",
      "serial": "013201027229"
    },
    "C feature": {
      "authcode": null,
      "description": "Palo Alto Networks URL Filtering License",
      "expired": "no",
      "expires": "June 30, 2028",
      "feature": "PAN-DB URL Filtering",
      "issued": "April 27, 2023",
      "serial": "013201027229"
    }
  }
}

snapshot-2

{
  "license": {
    "Logging Service": {
      "authcode": null,
      "custom": {
          "_Log_Storage_TB": "9",
          "logtype": "tape",
          "blogging": "enabled"
      },
      "description": "Device Logging Service",
      "expired": "no",
      "expires": "August 04, 2024",
      "feature": "Logging Service",
      "issued": "June 29, 2022",
      "serial": "013201027229"
    },
    "X feature": {
      "authcode": null,
      "description": "Device Logging Service",
      "expired": "no",
      "expires": "August 04, 2024",
      "feature": "Logging Service",
      "issued": "June 29, 2022",
      "serial": "013201027248"
    },
    "PAN-DB URL Filtering": {
      "authcode": null,
      "description": "Palo Alto Networks URL Filtering License",
      "expired": "no",
      "expires": "June 30, 2028",
      "feature": "PAN-DB URL Filtering",
      "issued": "April 29, 2023",
      "serial": "013201027248"
    },
    "C feature": {
      "authcode": null,
      "description": "Palo Alto Networks URL Filtering License",
      "expired": "no",
      "expires": "June 30, 2028",
      "feature": "PAN-DB URL Filtering",
      "issued": "April 27, 2023",
      "serial": "013201027229"
    }
  }
}

How Has This Been Tested?

Tested with example scripts.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes if appropriate.
  • All new and existing tests passed.

def _get_pure_element_name(self, element):
return element[1:] if element.startswith("!") else element

def _get_config_element_by_name(self, element):
Copy link
Collaborator Author

@alperenkose alperenkose Dec 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test with:

- nics:

This is a valid yaml with empty dict.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant