-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MINOR - Better PII classification for JSON data (#17734)
* MINOR - Better PII classification for JSON data * linting
- Loading branch information
Showing
6 changed files
with
171 additions
and
57 deletions.
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
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,23 @@ | ||
# Copyright 2021 Collate | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
""" | ||
Basic Scanner ABC | ||
""" | ||
from abc import ABC, abstractmethod | ||
from typing import Any | ||
|
||
|
||
class BaseScanner(ABC): | ||
"""Basic scanner abstract class""" | ||
|
||
@abstractmethod | ||
def scan(self, data: Any): | ||
"""Scan the given data from a column""" |
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
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 |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
""" | ||
Test Column Name Scanner | ||
""" | ||
from typing import Any | ||
|
||
import pytest | ||
|
||
|
@@ -78,3 +79,65 @@ def test_get_highest_score_label(scanner): | |
"PII.NonSensitive": StringAnalysis(score=1.0, appearances=1), | ||
} | ||
) == ("PII.Sensitive", 1.0) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"data,is_json", | ||
[ | ||
("potato", (False, None)), | ||
("1", (False, None)), | ||
('{"key": "value"}', (True, {"key": "value"})), | ||
( | ||
'{"key": "value", "key2": "value2"}', | ||
(True, {"key": "value", "key2": "value2"}), | ||
), | ||
('["potato"]', (True, ["potato"])), | ||
], | ||
) | ||
def test_is_json_data(scanner, data: Any, is_json: bool): | ||
"""Assert we are flagging JSON data correctly""" | ||
assert scanner.is_json_data(data) == is_json | ||
|
||
|
||
def test_scanner_with_json(scanner): | ||
"""Test the scanner with JSON data""" | ||
|
||
assert ( | ||
scanner.scan( | ||
[ | ||
'{"email": "[email protected]", "address": {"street": "123 Main St"}}', | ||
'{"email": "potato", "age": 30, "preferences": {"newsletter": true, "notifications": "email"}}', | ||
] | ||
).tag_fqn | ||
== "PII.Sensitive" | ||
) | ||
|
||
assert ( | ||
scanner.scan( | ||
[ | ||
'{"email": "foo", "address": {"street": "bar"}}', | ||
'{"email": "potato", "age": 30, "preferences": {"newsletter": true, "notifications": "email"}}', | ||
] | ||
) | ||
is None | ||
) | ||
|
||
|
||
def test_scanner_with_lists(scanner): | ||
"""Test the scanner with list data""" | ||
|
||
assert scanner.scan(["foo", "bar", "biz"]) is None | ||
|
||
assert ( | ||
scanner.scan(["foo", "bar", "[email protected]"]).tag_fqn == "PII.Sensitive" | ||
) | ||
|
||
assert ( | ||
scanner.scan( | ||
[ | ||
'{"emails": ["[email protected]", "[email protected]"]}', | ||
'{"emails": ["foo", "bar", "biz"]}', | ||
] | ||
).tag_fqn | ||
== "PII.Sensitive" | ||
) |