diff --git a/packages/opal-client/opal_client/policy_store/opa_client.py b/packages/opal-client/opal_client/policy_store/opa_client.py index 565941d8..f7ddb4e3 100644 --- a/packages/opal-client/opal_client/policy_store/opa_client.py +++ b/packages/opal-client/opal_client/policy_store/opa_client.py @@ -34,9 +34,10 @@ def should_ignore_path(path, ignore_paths): - """Helper function to check if the policy-store should ignore the given path.""" - paths_to_ignore = [p for p in ignore_paths if not p.startswith('!')] - paths_to_not_ignore = [p[1:] for p in ignore_paths if p.startswith('!')] + """Helper function to check if the policy-store should ignore the given + path.""" + paths_to_ignore = [p for p in ignore_paths if not p.startswith("!")] + paths_to_not_ignore = [p[1:] for p in ignore_paths if p.startswith("!")] # Check if the path matches any path to not be ignored if PathUtils.glob_style_match_path_to_list(path, paths_to_not_ignore) is not None: diff --git a/packages/opal-client/opal_client/tests/opa_client_test.py b/packages/opal-client/opal_client/tests/opa_client_test.py index efc1050c..fe15cb7f 100644 --- a/packages/opal-client/opal_client/tests/opa_client_test.py +++ b/packages/opal-client/opal_client/tests/opa_client_test.py @@ -112,6 +112,7 @@ def get_badly_ordered_ops(self): order_strict_ops.get_badly_ordered_ops() ) + def test_should_not_ignore_anything_with_no_ignore_paths(): ignore_paths = [] assert should_ignore_path("myFolder", ignore_paths) == False @@ -119,6 +120,7 @@ def test_should_not_ignore_anything_with_no_ignore_paths(): assert should_ignore_path("otherFolder", ignore_paths) == False assert should_ignore_path("otherFolder/file.txt", ignore_paths) == False + def test_should_ignore_everything_with_root_as_ignore_paths(): ignore_paths = ["/"] assert should_ignore_path("myFolder", ignore_paths) == True @@ -126,6 +128,7 @@ def test_should_ignore_everything_with_root_as_ignore_paths(): assert should_ignore_path("otherFolder", ignore_paths) == True assert should_ignore_path("otherFolder/file.txt", ignore_paths) == True + def test_should_ignore_everything_with_root_and_asterisks_as_ignore_paths(): ignore_paths = ["/**"] assert should_ignore_path("myFolder", ignore_paths) == True @@ -133,6 +136,7 @@ def test_should_ignore_everything_with_root_and_asterisks_as_ignore_paths(): assert should_ignore_path("otherFolder", ignore_paths) == True assert should_ignore_path("otherFolder/file.txt", ignore_paths) == True + def test_should_ignore_path_but_not_his_contents_when_path_is_defined_without_asterisks(): ignore_paths = ["myFolder"] assert should_ignore_path("myFolder", ignore_paths) == True @@ -140,6 +144,7 @@ def test_should_ignore_path_but_not_his_contents_when_path_is_defined_without_as assert should_ignore_path("otherFolder", ignore_paths) == False assert should_ignore_path("otherFolder/file.txt", ignore_paths) == False + def test_should_ignore_path_and_his_contents_when_path_is_defined_with_asterisks(): ignore_paths = ["myFolder/**"] assert should_ignore_path("myFolder", ignore_paths) == True @@ -147,6 +152,7 @@ def test_should_ignore_path_and_his_contents_when_path_is_defined_with_asterisks assert should_ignore_path("otherFolder", ignore_paths) == False assert should_ignore_path("otherFolder/file.txt", ignore_paths) == False + def test_should_not_ignore_anything_with_not_ignore_paths_only(): ignore_paths = ["!myFolder/**"] assert should_ignore_path("myFolder", ignore_paths) == False @@ -154,6 +160,7 @@ def test_should_not_ignore_anything_with_not_ignore_paths_only(): assert should_ignore_path("otherFolder", ignore_paths) == False assert should_ignore_path("otherFolder/file.txt", ignore_paths) == False + def test_should_ignore_path_but_ones_specified_as_not_ignore_paths(): ignore_paths = ["/", "!myFolder"] assert should_ignore_path("myFolder", ignore_paths) == False @@ -161,6 +168,7 @@ def test_should_ignore_path_but_ones_specified_as_not_ignore_paths(): assert should_ignore_path("otherFolder", ignore_paths) == True assert should_ignore_path("otherFolder/file.txt", ignore_paths) == True + def test_should_ignore_path_but_ones_specified_as_not_ignore_paths_and_his_contents_when_defined_with_asterisks(): ignore_paths = ["/", "!myFolder/**"] assert should_ignore_path("myFolder", ignore_paths) == False @@ -168,42 +176,54 @@ def test_should_ignore_path_but_ones_specified_as_not_ignore_paths_and_his_conte assert should_ignore_path("otherFolder", ignore_paths) == True assert should_ignore_path("otherFolder/file.txt", ignore_paths) == True + def test_should_ignore_path_keeping_higher_priority_to_ones_defined_as_not_to_ignore_A(): ignore_paths = ["myFolder/**", "!myFolder/subFolder/**"] assert should_ignore_path("myFolder/file.txt", ignore_paths) == True assert should_ignore_path("myFolder/subFolder", ignore_paths) == False assert should_ignore_path("myFolder/subFolder/file.txt", ignore_paths) == False assert should_ignore_path("myFolder/anotherSubFolder", ignore_paths) == True - assert should_ignore_path("myFolder/anotherSubFolder/file.txt", ignore_paths) == True + assert ( + should_ignore_path("myFolder/anotherSubFolder/file.txt", ignore_paths) == True + ) assert should_ignore_path("otherFolder", ignore_paths) == False assert should_ignore_path("otherFolder/file.txt", ignore_paths) == False + def test_should_ignore_path_keeping_higher_priority_to_ones_defined_as_not_to_ignore_B(): ignore_paths = ["/", "myFolder/**", "!myFolder/subFolder/**"] assert should_ignore_path("myFolder/file.txt", ignore_paths) == True assert should_ignore_path("myFolder/subFolder", ignore_paths) == False assert should_ignore_path("myFolder/subFolder/file.txt", ignore_paths) == False assert should_ignore_path("myFolder/anotherSubFolder", ignore_paths) == True - assert should_ignore_path("myFolder/anotherSubFolder/file.txt", ignore_paths) == True + assert ( + should_ignore_path("myFolder/anotherSubFolder/file.txt", ignore_paths) == True + ) assert should_ignore_path("otherFolder", ignore_paths) == True assert should_ignore_path("otherFolder/file.txt", ignore_paths) == True + def test_should_ignore_path_keeping_higher_priority_to_ones_defined_as_not_to_ignore_C(): ignore_paths = ["!myFolder/**", "myFolder/subFolder/**"] assert should_ignore_path("myFolder/file.txt", ignore_paths) == False assert should_ignore_path("myFolder/subFolder", ignore_paths) == False assert should_ignore_path("myFolder/subFolder/file.txt", ignore_paths) == False assert should_ignore_path("myFolder/anotherSubFolder", ignore_paths) == False - assert should_ignore_path("myFolder/anotherSubFolder/file.txt", ignore_paths) == False + assert ( + should_ignore_path("myFolder/anotherSubFolder/file.txt", ignore_paths) == False + ) assert should_ignore_path("otherFolder", ignore_paths) == False assert should_ignore_path("otherFolder/file.txt", ignore_paths) == False + def test_should_ignore_path_keeping_higher_priority_to_ones_defined_as_not_to_ignore_D(): ignore_paths = ["/", "!myFolder/**", "myFolder/subFolder/**"] assert should_ignore_path("myFolder/file.txt", ignore_paths) == False assert should_ignore_path("myFolder/subFolder", ignore_paths) == False assert should_ignore_path("myFolder/subFolder/file.txt", ignore_paths) == False assert should_ignore_path("myFolder/anotherSubFolder", ignore_paths) == False - assert should_ignore_path("myFolder/anotherSubFolder/file.txt", ignore_paths) == False + assert ( + should_ignore_path("myFolder/anotherSubFolder/file.txt", ignore_paths) == False + ) assert should_ignore_path("otherFolder", ignore_paths) == True - assert should_ignore_path("otherFolder/file.txt", ignore_paths) == True \ No newline at end of file + assert should_ignore_path("otherFolder/file.txt", ignore_paths) == True