Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f5de147

Browse files
authored
Merge pull request #35 from Yelp/update-private-key-detector
Updating private key plugin
2 parents f2a27ef + a4dd49c commit f5de147

2 files changed

Lines changed: 46 additions & 26 deletions

File tree

detect_secrets/plugins/private_key.py

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,29 @@
1+
"""
2+
This code was extracted in part from
3+
https://github.com/pre-commit/pre-commit-hooks. Using similar heuristic logic,
4+
we adapt it to fit our plugin infrastructure, to create an organized,
5+
concerted effort in detecting all type of secrets in code.
6+
7+
Copyright (c) 2014 pre-commit dev team: Anthony Sottile, Ken Struys
8+
9+
Permission is hereby granted, free of charge, to any person obtaining a copy
10+
of this software and associated documentation files (the "Software"), to deal
11+
in the Software without restriction, including without limitation the rights
12+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
copies of the Software, and to permit persons to whom the Software is
14+
furnished to do so, subject to the following conditions:
15+
16+
The above copyright notice and this permission notice shall be included in
17+
all copies or substantial portions of the Software.
18+
19+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
THE SOFTWARE.
26+
"""
127
from __future__ import absolute_import
228

329
from .base import BasePlugin
@@ -10,35 +36,18 @@
1036
'BEGIN EC PRIVATE KEY',
1137
'BEGIN OPENSSH PRIVATE KEY',
1238
'BEGIN PRIVATE KEY',
39+
'PuTTY-User-Key-File-2',
40+
'BEGIN SSH2 ENCRYPTED PRIVATE KEY',
1341
)
1442

1543

1644
class PrivateKeyDetector(BasePlugin):
1745
"""This checks for private keys by determining whether the blacklisted
1846
lines are present in the analyzed string.
19-
20-
This is based off https://github.com/pre-commit/pre-commit-hooks.
2147
"""
2248

2349
secret_type = 'Private Key'
2450

25-
def analyze(self, file, filename):
26-
"""We override this, because we're only looking at the first line.
27-
28-
:param file: The File object itself.
29-
:param filename: string; filename of File object, used for creating
30-
PotentialSecret objects
31-
:returns dictionary representation of set (for random access by hash)
32-
{ detect_secrets.core.potential_secret.__hash__:
33-
detect_secrets.core.potential_secret }
34-
"""
35-
36-
return self.analyze_string(
37-
file.readline(),
38-
1,
39-
filename,
40-
)
41-
4251
def analyze_string(self, string, line_num, filename):
4352
output = {}
4453

tests/plugins/private_key_test.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,32 @@
11
from __future__ import absolute_import
22
from __future__ import unicode_literals
33

4+
import pytest
5+
46
from detect_secrets.plugins.private_key import PrivateKeyDetector
57
from tests.util.file_util import create_file_object_from_string
68

79

810
class TestPrivateKeyDetector(object):
911

10-
def test_analyze(self):
12+
@pytest.mark.parametrize(
13+
'file_content',
14+
[
15+
(
16+
'-----BEGIN RSA PRIVATE KEY-----\n'
17+
'super secret private key here\n'
18+
'-----END RSA PRIVATE KEY-----'
19+
),
20+
(
21+
'some text here\n'
22+
'-----BEGIN PRIVATE KEY-----\n'
23+
'yabba dabba doo'
24+
),
25+
]
26+
)
27+
def test_analyze(self, file_content):
1128
logic = PrivateKeyDetector()
1229

13-
file_content = (
14-
'-----BEGIN RSA PRIVATE KEY-----'
15-
'super secret private key here'
16-
'-----END RSA PRIVATE KEY-----'
17-
)
18-
1930
f = create_file_object_from_string(file_content)
2031
output = logic.analyze(f, 'mock_filename')
2132
assert len(output) == 1

0 commit comments

Comments
 (0)