-
Notifications
You must be signed in to change notification settings - Fork 486
/
Copy pathsecrets_collection.py
318 lines (254 loc) · 11.8 KB
/
secrets_collection.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import multiprocessing as mp
import os
from collections import defaultdict
from typing import Any
from typing import Dict
from typing import Generator
from typing import List
from typing import Optional
from typing import Set
from typing import Tuple
from . import scan
from ..util.path import convert_local_os_path
from .potential_secret import PotentialSecret
from detect_secrets.settings import configure_settings_from_baseline
from detect_secrets.settings import get_settings
class PatchedFile:
"""This exists so that we can do typecasting, without importing unidiff."""
path: str
def __iter__(self) -> Generator:
pass
class SecretsCollection:
def __init__(self, root: str = '') -> None:
"""
:param root: if specified, will scan as if the root was the value provided,
rather than the current working directory. We still store results as if
relative to root, since we're running as if it was in a different directory,
rather than scanning a different directory.
"""
self.data: Dict[str, Set[PotentialSecret]] = defaultdict(set)
self.root = root
@classmethod
def load_from_baseline(cls, baseline: Dict[str, Any]) -> 'SecretsCollection':
output = cls()
for filename in baseline['results']:
for item in baseline['results'][filename]:
secret = PotentialSecret.load_secret_from_dict({'filename': filename, **item})
output[convert_local_os_path(filename)].add(secret)
return output
@property
def files(self) -> Set[str]:
return set(self.data.keys())
def scan_files(self, *filenames: str, num_processors: Optional[int] = None) -> None:
"""Just like scan_file, but optimized through parallel processing."""
if len(filenames) == 1:
self.scan_file(filenames[0])
return
if not num_processors:
num_processors = mp.cpu_count()
child_process_settings = get_settings().json()
with mp.Pool(
processes=num_processors,
initializer=configure_settings_from_baseline,
initargs=(child_process_settings,),
) as p:
for secrets in p.imap_unordered(
_scan_file_and_serialize,
[os.path.join(self.root, filename) for filename in filenames],
):
for secret in secrets:
self[os.path.relpath(secret.filename, self.root)].add(secret)
def scan_file(self, filename: str) -> None:
for secret in scan.scan_file(os.path.join(self.root, convert_local_os_path(filename))):
self[convert_local_os_path(filename)].add(secret)
def scan_diff(self, diff: str) -> None:
"""
:raises: UnidiffParseError
"""
try:
for secret in scan.scan_diff(diff):
self[secret.filename].add(secret)
except ImportError: # pragma: no cover
raise NotImplementedError(
'SecretsCollection.scan_diff requires `unidiff` to work. Try pip '
'installing that package, and try again.',
)
def merge(self, old_results: 'SecretsCollection') -> None:
"""
We operate under an assumption that the latest results are always more accurate,
assuming that the baseline is created on the same repository. However, we cannot
merely discard the old results in favor of the new, since there is valuable information
that ought to be preserved: verification of secrets, both automated and manual.
Therefore, this function serves to extract this information from the old results,
and amend the new results with it.
"""
for filename in old_results.files:
if filename not in self.files:
continue
# This allows us to obtain the same secret, by accessing the hash.
mapping = {
secret: secret
for secret in self.data[filename]
}
for old_secret in old_results.data[filename]:
if old_secret not in mapping:
continue
# Only override if there's no newer value.
if mapping[old_secret].is_secret is None:
mapping[old_secret].is_secret = old_secret.is_secret
# If the old value is false, it won't make a difference.
if not mapping[old_secret].is_verified:
mapping[old_secret].is_verified = old_secret.is_verified
def trim(
self,
scanned_results: Optional['SecretsCollection'] = None,
filelist: Optional[List[str]] = None,
) -> None:
"""
Removes invalid entries in the current SecretsCollection.
This behaves *kinda* like set intersection and left-join. That is, for matching files,
a set intersection is performed. For non-matching files, only the files in `self` will
be kept.
This is because we may not be constructing the other SecretsCollection with the same
information as we are with the current SecretsCollection, and we cannot infer based on
incomplete information. As such, we will keep the status quo.
Assumptions:
1. Both `scanned_results` and the current SecretsCollection are constructed using
the same settings (otherwise, we can't determine whether a missing secret is due
to newly filtered secrets, or actually removed).
:param scanned_results: if None, will just clear out non-existent files.
:param filelist: files without secrets are not present in `scanned_results`. Therefore,
by supplying this additional filelist, we can assert that if an entry is missing in
`scanned_results`, it must not have secrets in it.
"""
if scanned_results is None:
scanned_results = SecretsCollection()
filelist = [
filename
for filename in self.files
if not os.path.exists(filename)
]
if not filelist:
fileset = set()
else:
fileset = set(filelist)
# Unfortunately, we can't merely do a set intersection since we want to update the line
# numbers (if applicable). Therefore, this does it manually.
result: Dict[str, Set[PotentialSecret]] = defaultdict(set)
for filename in scanned_results.files:
if filename not in self.files:
continue
# We construct this so we can get O(1) retrieval of secrets.
existing_secret_map = {secret: secret for secret in self[filename]}
for secret in scanned_results[filename]:
if secret not in existing_secret_map:
continue
# Currently, we assume that the `scanned_results` have no labelled data, so
# we only want to obtain the latest line number from it.
existing_secret = existing_secret_map[secret]
if existing_secret.line_number:
# Only update line numbers if we're tracking them.
existing_secret.line_number = secret.line_number
result[filename].add(existing_secret)
for filename in self.files:
# If this is already populated by scanned_results, then the set intersection
# is already completed.
if filename in result:
continue
# All secrets relating to that file was removed.
# We know this because:
# 1. It's a file that was scanned (in filelist)
# 2. It would have been in the baseline, if there were secrets...
# 3. ...but it isn't.
if filename in fileset:
continue
result[filename] = self[filename]
self.data = result
def json(self) -> Dict[str, Any]:
"""Custom JSON encoder"""
output = defaultdict(list)
for filename, secret in self:
output[filename].append(secret.json())
return dict(output)
def exactly_equals(self, other: Any) -> bool:
return self.__eq__(other, strict=True) # type: ignore
def __getitem__(self, filename: str) -> Set[PotentialSecret]:
return self.data[filename]
def __setitem__(self, filename: str, value: Set[PotentialSecret]) -> None:
self.data[filename] = value
def __iter__(self) -> Generator[Tuple[str, PotentialSecret], None, None]:
for filename in sorted(self.files):
secrets = self[filename]
# NOTE: If line numbers aren't supplied, they are supposed to default to 0.
for secret in sorted(
secrets,
key=lambda secret: (
getattr(secret, 'line_number', 0),
secret.secret_hash,
secret.type,
),
):
yield filename, secret
def __bool__(self) -> bool:
# This checks whether there are secrets, rather than just empty files.
# Empty files can occur with SecretsCollection subtraction.
return bool(list(self))
def __eq__(self, other: Any, strict: bool = False) -> bool:
"""
:param strict: if strict, will return False even if secrets match
(e.g. if line numbers are different)
"""
if not isinstance(other, SecretsCollection):
raise NotImplementedError
if self.files != other.files:
return False
for filename in self.files:
self_mapping = {
(secret.secret_hash, secret.type): secret for secret in self[filename]
}
other_mapping = {
(secret.secret_hash, secret.type): secret for secret in other[filename]
}
# Since PotentialSecret is hashable, we compare their identities through this.
if set(self_mapping.values()) != set(other_mapping.values()):
return False
if not strict:
continue
for secretA in self_mapping.values():
secretB = other_mapping[(secretA.secret_hash, secretA.type)]
valuesA = vars(secretA)
valuesA.pop('secret_value')
valuesB = vars(secretB)
valuesB.pop('secret_value')
if valuesA['line_number'] == 0 or valuesB['line_number'] == 0:
# If line numbers are not provided (for either one), then don't compare
# line numbers.
valuesA.pop('line_number')
valuesB.pop('line_number')
if valuesA != valuesB:
return False
return True
def __ne__(self, other: Any) -> bool:
return not self.__eq__(other)
def __sub__(self, other: Any) -> 'SecretsCollection':
"""This behaves like set subtraction."""
if not isinstance(other, SecretsCollection):
raise NotImplementedError
# We want to create a copy to follow convention and adhere to the principle
# of least surprise.
output = SecretsCollection()
for filename in other.files:
if filename not in self.files:
continue
output[filename] = self[filename] - other[filename]
for filename in self.files:
if filename in other.files:
continue
output[filename] = self[filename]
return output
def __len__(self) -> int:
"""Returns the total number of secrets in the collection."""
return sum(len(secrets) for secrets in self.data.values())
def _scan_file_and_serialize(filename: str) -> List[PotentialSecret]:
"""Used for multiprocessing, since lambdas can't be serialized."""
return list(scan.scan_file(filename))