forked from ethyca/fidesops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmssql_discover.py
79 lines (69 loc) · 2.02 KB
/
mssql_discover.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import sqlalchemy
# This file is not committed to the repo, please create secrets.py with the required
# variables in the same dir as this file before running this script
from secrets import (
USER,
PASS,
IP,
PORT,
DB,
)
MASTER_MSSQL_URL = f"mssql+pyodbc://{USER}:{PASS}@{IP}:{PORT}/{DB}?driver=ODBC+Driver+17+for+SQL+Server"
SUPPORTED_DATA_TYPES = set(
[
# char types
"varchar",
"nvarchar",
"char",
"nchar",
"ntext",
"text",
# numeric types
"int",
"bigint",
"smallint",
"tinyint",
"money",
"float",
"decimal",
# date types
"date",
"datetime",
"datetime2",
"smalldatetime",
# other types
"bit",
]
)
def mssql_discover():
"""
Select all databases from the instance
Select the schema data for each data base
Check if there are any fields in the schema that Fidesops does not yet support
"""
engine = sqlalchemy.create_engine(MASTER_MSSQL_URL)
all_dbs = engine.execute("SELECT name FROM sys.databases;").all()
all_columns = []
flagged_columns = []
flagged_datatypes = set()
for db_name in all_dbs:
db_name = db_name[0]
try:
columns = engine.execute(
f"SELECT TABLE_NAME, COLUMN_NAME, DATA_TYPE FROM {db_name}.INFORMATION_SCHEMA.COLUMNS;"
).all()
except Exception:
continue
all_columns.extend(columns)
for table, column, data_type in columns:
if data_type not in SUPPORTED_DATA_TYPES:
flagged_datatypes.add(data_type)
flagged_columns.append(f"{db_name}.{table}.{column}: {data_type}")
print(f"{len(all_columns)} columns found")
print(f"{len(flagged_columns)} columns flagged")
print(f"Flagged datatypes:")
print(",\n".join(flagged_datatypes))
print(f"Flagged columns:")
print(",\n".join(flagged_columns))
if __name__ == "__main__":
mssql_discover()