33import json
44import os
55from typing import Union
6-
6+ from pathlib import Path
77
88SECRET_PATTERNS = [
99 re .compile (r'[\'"]?subscription_id[\'"]?\s*[:=]\s*[\'"][0-9a-f\-]{36}[\'"]' , re .IGNORECASE ),
2222def check_ipynb_for_secrets (filename : Union [str , os .PathLike ]) -> bool :
2323 """Jupyter notebooks can't be parsed directly - need to convert to JSON first"""
2424 try :
25- with open (filename , "r" , encoding = "utf-8" ) as file :
25+ with Path (filename ). open ( "r" , encoding = "utf-8" ) as file :
2626 notebook_data = json .load (file )
2727 failed = False
2828 for cell in notebook_data .get ("cells" , []):
@@ -38,23 +38,22 @@ def check_ipynb_for_secrets(filename: Union[str, os.PathLike]) -> bool:
3838 return True
3939
4040
41- def main ():
41+ def main () -> None :
4242 failed = False
4343
4444 for filename in sys .argv [1 :]:
4545 if filename .endswith ((".py" , ".yaml" , ".yml" , ".md" )):
4646 try :
47- with open (filename , "r" , encoding = "utf-8" ) as file :
47+ with Path (filename ). open ( "r" , encoding = "utf-8" ) as file :
4848 for line_number , line in enumerate (file , start = 1 ):
4949 for pattern in SECRET_PATTERNS :
5050 if pattern .search (line ):
5151 print (f"Secret detected in { filename } on line { line_number } : { line .strip ()} " )
5252 failed = True
5353 except UnicodeDecodeError :
5454 print (f"Failed to read { filename } . Skipping secrets check." )
55- elif filename .endswith (".ipynb" ):
56- if check_ipynb_for_secrets (filename ):
57- failed = True
55+ elif filename .endswith (".ipynb" ) and check_ipynb_for_secrets (filename ):
56+ failed = True
5857
5958 if failed :
6059 sys .exit (1 )
0 commit comments