|
| 1 | +from dblinter.configuration_model import Context |
| 2 | +from dblinter.database_connection import DatabaseConnection |
| 3 | +from dblinter.function_library import FunctionLibrary |
| 4 | +from dblinter.sarif_document import SarifDocument |
| 5 | + |
| 6 | + |
| 7 | +def test_table_with_sensitive_column(postgres_instance_args) -> None: |
| 8 | + args = postgres_instance_args |
| 9 | + db = DatabaseConnection(args) |
| 10 | + CHECK_EXTENSION = "select count(*) as nb from pg_extension where extname='anon'" |
| 11 | + anon = db.query(CHECK_EXTENSION)[0][0] |
| 12 | + if anon == 0: |
| 13 | + assert True |
| 14 | + return |
| 15 | + context = Context( |
| 16 | + desc="Base on the extension anon (https://postgresql-anonymizer.readthedocs.io/en/stable/detection), show sensitive column.", |
| 17 | + fixes=[ |
| 18 | + "Install extension anon, and create some masking rules on.", |
| 19 | + ], |
| 20 | + message="{0} have column {1} (category {2}) that can be consider has sensitive. It should be masked for non data-operator users.", |
| 21 | + ) |
| 22 | + function_library = FunctionLibrary() |
| 23 | + db.query("select anon.init()") |
| 24 | + db.query("CREATE TABLE test (id integer, creditcard text)") |
| 25 | + sarif_document = SarifDocument() |
| 26 | + function_library.get_function_by_function_name("table_with_sensible_column")( |
| 27 | + function_library, db, [], context, ("public", "test"), sarif_document |
| 28 | + ) |
| 29 | + assert ( |
| 30 | + sarif_document.sarif_doc.runs[0].results[0].message.text |
| 31 | + == "postgres.public.test have column creditcard (category creditcard) that can be consider has sensitive. It should be masked for non data-operator users." |
| 32 | + ) |
| 33 | + assert ( |
| 34 | + sarif_document.sarif_doc.runs[0].results[1].message.text |
| 35 | + == "postgres.public.test have column id (category account_id) that can be consider has sensitive. It should be masked for non data-operator users." |
| 36 | + ) |
| 37 | + |
| 38 | + |
| 39 | +def test_table_without_sensitive_column(postgres_instance_args) -> None: |
| 40 | + args = postgres_instance_args |
| 41 | + db = DatabaseConnection(args) |
| 42 | + CHECK_EXTENSION = "select count(*) as nb from pg_extension where extname='anon'" |
| 43 | + anon = db.query(CHECK_EXTENSION)[0][0] |
| 44 | + if anon == 0: |
| 45 | + assert True |
| 46 | + return |
| 47 | + context = Context( |
| 48 | + desc="Base on the extension anon (https://postgresql-anonymizer.readthedocs.io/en/stable/detection), show sensitive column.", |
| 49 | + fixes=[ |
| 50 | + "Install extension anon, and create some masking rules on.", |
| 51 | + ], |
| 52 | + message="{0} have column {1} (category {2}) that can be consider has sensitive. It should be masked for non data-operator users.", |
| 53 | + ) |
| 54 | + function_library = FunctionLibrary() |
| 55 | + db.query("select anon.init()") |
| 56 | + db.query("CREATE TABLE test (test_id integer, description text)") |
| 57 | + sarif_document = SarifDocument() |
| 58 | + function_library.get_function_by_function_name("table_with_sensible_column")( |
| 59 | + function_library, db, [], context, ("public", "test"), sarif_document |
| 60 | + ) |
| 61 | + assert sarif_document.sarif_doc.runs[0].results == [] |
0 commit comments