mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-04 19:26:52 +00:00
Detect OpenSSH private keys
This commit is contained in:
parent
9d1ac3273b
commit
68a7729327
5 changed files with 64 additions and 0 deletions
33
pre_commit_hooks/detect_private_key.py
Normal file
33
pre_commit_hooks/detect_private_key.py
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
from __future__ import print_function
|
||||
|
||||
import io
|
||||
import sys
|
||||
|
||||
import argparse
|
||||
|
||||
|
||||
def detect_private_key(argv=None):
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('filenames', nargs='*', help='Filenames to check')
|
||||
args = parser.parse_args(argv)
|
||||
|
||||
private_key_files = []
|
||||
|
||||
for filename in args.filenames:
|
||||
with io.open(filename, 'r') as f:
|
||||
content = f.read()
|
||||
if 'BEGIN RSA PRIVATE KEY' in content:
|
||||
private_key_files.append(content)
|
||||
if 'BEGIN DSA PRIVATE KEY' in content:
|
||||
private_key_files.append(content)
|
||||
|
||||
if private_key_files:
|
||||
for private_key_file in private_key_files:
|
||||
print('Private key found: {0}'.format(private_key_file))
|
||||
return 1
|
||||
else:
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(detect_private_key())
|
||||
Loading…
Add table
Add a link
Reference in a new issue