-
Notifications
You must be signed in to change notification settings - Fork 293
Expand file tree
/
Copy pathimporter.py
More file actions
executable file
·1330 lines (1126 loc) · 49.2 KB
/
importer.py
File metadata and controls
executable file
·1330 lines (1126 loc) · 49.2 KB
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""OSV Importer."""
# TODO: Refactor per
# https://github.com/google/osv.dev/pull/2030#discussion_r1513861856
import argparse
import concurrent.futures
from collections import namedtuple
import datetime
import json
import logging
import os
import requests
from google.protobuf import json_format
from requests.adapters import HTTPAdapter
import shutil
import threading
import time
from urllib3.util import Retry
import atexit
from typing import List, Tuple, Optional
from google.cloud import ndb
from google.cloud import pubsub_v1
from google.cloud import storage
from google.cloud.storage import retry
from google.cloud.exceptions import NotFound
import pygit2.enums
import osv
import osv.logs
from osv import vulnerability_pb2
DEFAULT_WORK_DIR = '/work'
DEFAULT_PUBLIC_LOGGING_BUCKET = 'osv-public-import-logs'
_BUG_REDO_DAYS = 14
_TASKS_TOPIC = 'tasks'
_OSS_FUZZ_EXPORT_BUCKET = 'oss-fuzz-osv-vulns'
_EXPORT_WORKERS = 32
_NO_UPDATE_MARKER = 'OSV-NO-UPDATE'
_BUCKET_THREAD_COUNT = 20
_HTTP_LAST_MODIFIED_FORMAT = '%a, %d %b %Y %H:%M:%S %Z'
_TIMEOUT_SECONDS = 60
_NDB_PUT_BATCH_SIZE = 500
_client_store = threading.local()
def modify_storage_client_adapters(storage_client: storage.Client,
pool_connections: int = 128,
max_retries: int = 3,
pool_block: bool = True) -> storage.Client:
"""Returns a modified google.cloud.storage.Client object.
Due to many concurrent GCS connections, the default connection pool can become
overwhelmed, introducing delays.
Solution described in https://github.com/googleapis/python-storage/issues/253
These affect the urllib3.HTTPConnectionPool underpinning the storage.Client's
HTTP requests.
Args:
storage_client: an existing google.cloud.storage.Client object
pool_connections: number of pool_connections desired
max_retries: maximum retries
pool_block: blocking behaviour when pool is exhausted
Returns:
the google.cloud.storage.Client appropriately modified.
"""
adapter = HTTPAdapter(
pool_connections=pool_connections,
max_retries=max_retries,
pool_block=pool_block)
# pylint: disable=protected-access
storage_client._http.mount('https://', adapter)
storage_client._http._auth_request.session.mount('https://', adapter)
return storage_client
def _is_vulnerability_file(source_repo, file_path):
"""Return whether or not the file is a Vulnerability entry."""
if (source_repo.directory_path and
not file_path.startswith(source_repo.directory_path.rstrip('/') + '/')):
return False
if source_repo.ignore_file(file_path):
return False
return file_path.endswith(source_repo.extension)
def aestnow() -> datetime.datetime:
"""Get the current AEST time"""
# To retain the original timezone-unaware behaviour of this function,
# returns the current AEST time with a UTC timezone.
# i.e. returns (utcnow() + 10 hours) UTC
return utcnow().astimezone(datetime.timezone(
datetime.timedelta(hours=10))).replace(tzinfo=datetime.UTC)
def utcnow() -> datetime.datetime:
"""utcnow() for mocking."""
return datetime.datetime.now(datetime.UTC)
def replace_importer_log(client: storage.Client, source_name: str,
bucket_name: str, import_failure_logs: List[str]):
"""Replace the public importer logs with the new one."""
bucket: storage.Bucket = client.bucket(bucket_name)
upload_string = f'--- {datetime.datetime.now(datetime.UTC).isoformat()} ---\n'
upload_string += '\n'.join(import_failure_logs)
bucket.blob(source_name).upload_from_string(
upload_string, retry=retry.DEFAULT_RETRY)
def log_run_duration(start: float):
"""Log the elapsed wallclock duration at the end of the program.
This enables a log-based metric to be created.
Args:
start: the time the program started.
"""
elapsed = time.time() - start
logging.info('Importer run duration: %d', elapsed)
class Importer:
"""Importer."""
def __init__(self,
ssh_key_public_path,
ssh_key_private_path,
work_dir,
public_log_bucket,
oss_fuzz_export_bucket,
strict_validation: bool,
delete: bool,
deletion_safety_threshold_pct: float = 10.0):
self._ssh_key_public_path = ssh_key_public_path
self._ssh_key_private_path = ssh_key_private_path
self._work_dir = work_dir
self._publisher = pubsub_v1.PublisherClient()
project = os.environ['GOOGLE_CLOUD_PROJECT']
self._tasks_topic = self._publisher.topic_path(project, _TASKS_TOPIC)
self._public_log_bucket = public_log_bucket
self._oss_fuzz_export_bucket = oss_fuzz_export_bucket
self._sources_dir = os.path.join(self._work_dir, 'sources')
self._strict_validation = strict_validation
self._delete = delete
self._deletion_safety_threshold_pct = deletion_safety_threshold_pct
os.makedirs(self._sources_dir, exist_ok=True)
def _git_callbacks(self, source_repo):
"""Get git auth callbacks."""
return osv.GitRemoteCallback(source_repo.repo_username,
self._ssh_key_public_path,
self._ssh_key_private_path)
def _request_analysis_external(self,
source_repo,
original_sha256,
path,
deleted=False,
source_timestamp=None):
"""Request analysis."""
# TODO(michaelkedar): Making a distinction for oss-fuzz updates so we can
# track the logic flow for our eventual decoupling of the special logic.
src_ts = ''
if source_timestamp is not None:
src_ts = str(int(source_timestamp.timestamp()))
task_type = 'update'
if source_repo.name == 'oss-fuzz':
task_type = 'update-oss-fuzz'
self._publisher.publish(
self._tasks_topic,
data=b'',
type=task_type,
source=source_repo.name,
path=path,
original_sha256=original_sha256,
deleted=str(deleted).lower(),
req_timestamp=str(int(time.time())),
src_timestamp=src_ts)
def _request_internal_analysis(self, bug):
"""Request internal analysis."""
self._publisher.publish(
self._tasks_topic,
data=b'',
type='impact',
source_id=bug.source_id,
allocated_id=bug.key.id(),
req_timestamp=str(int(time.time())))
def _infer_id_from_invalid_data(self, name: str, content: bytes) -> str:
"""Best effort infer the vulnerability ID for data that failed to parse.
First try and extract something that looks like an "id" field, and failing
that, try to infer from the filename.
Args:
name: the name associated with the data
content: the data itself
Returns:
str: the inferred identifer
"""
# First try without strict validation
extension = os.path.splitext(name)[1]
try:
vulns = osv.parse_vulnerabilities_from_data(
content, extension, strict=False, source_name=name)
if vulns:
return vulns[0].id
except RuntimeError:
# Happens if filename extension is unsupported.
pass
except Exception:
# This function is called from an Exception handler.
# Do not cause further exceptions.
pass
# TODO(apollock): Then try by poking around at the data.
# Then use the filename
return os.path.splitext(os.path.basename(name))[0]
def _record_quality_finding(
self,
source: osv.SourceRepository.name,
bug_id: str,
maybe_new_finding: osv.ImportFindings = osv.ImportFindings.INVALID_JSON):
"""Record the quality finding about a record in Datastore.
Args:
source: the name of the source of the vulnerability record
bug_id: the ID of the vulnerability
maybe_new_finding: the finding to record
Sets the finding's last_attempt to now, and adds the finding to the list of
findings for the record (if any already exist)
"""
# Get any current findings for this record.
findingtimenow = utcnow()
if existing_finding := osv.ImportFinding.get_by_id(bug_id):
if maybe_new_finding not in existing_finding.findings:
existing_finding.findings.append(maybe_new_finding)
existing_finding.last_attempt: findingtimenow
existing_finding.put()
else:
osv.ImportFinding(
bug_id=bug_id,
source=source,
findings=[maybe_new_finding],
first_seen=findingtimenow,
last_attempt=findingtimenow).put()
def run(self):
"""Run importer."""
for source_repo in osv.SourceRepository.query():
if source_repo.name == 'oss-fuzz':
continue
try:
self.validate_source_repo(source_repo)
if not self._delete:
self.process_updates(source_repo)
if self._delete:
self.process_deletions(source_repo)
except Exception as e:
logging.exception(e)
def checkout(self, source_repo):
"""Check out a source repo."""
return osv.ensure_updated_checkout(
source_repo.repo_url,
os.path.join(self._sources_dir, source_repo.name),
git_callbacks=self._git_callbacks(source_repo),
branch=source_repo.repo_branch,
force_update=True)
def _vuln_ids_from_gcs_blob(self, client: storage.Client,
source_repo: osv.SourceRepository,
blob: storage.Blob) -> Optional[Tuple[str]]:
"""Returns a list of the vulnerability IDs from a parsable OSV file in GCS.
Usually an OSV file has a single vulnerability in it, but it is permissible
to have more than one, hence it returns a list.
This is runnable in parallel using concurrent.futures.ThreadPoolExecutor
Args:
client: a storage.Client() to use for retrieval of the blob
source_repo: the osv.SourceRepository the blob relates to
blob: the storage.Blob object to operate on
Raises:
jsonschema.exceptions.ValidationError when self._strict_validation is True
input fails OSV JSON Schema validation
Returns:
a list of one or more vulnerability IDs (from the Vulnerability proto) or
None when the blob has an unexpected name or fails to retrieve
"""
if not _is_vulnerability_file(source_repo, blob.name):
return None
# Download in a blob generation agnostic way to cope with the blob
# changing between when it was listed and now (if the generation doesn't
# match, retrieval fails otherwise).
try:
blob_bytes = storage.Blob(
blob.name, blob.bucket, generation=None).download_as_bytes(client)
except NotFound:
# The file can disappear between bucket listing and blob retrieval.
return None
vuln_ids = []
# When self._strict_validation is True,
# this *may* raise a jsonschema.exceptions.ValidationError
vulns = osv.parse_vulnerabilities_from_data(
blob_bytes,
os.path.splitext(blob.name)[1],
strict=source_repo.strict_validation and self._strict_validation,
source_name=blob.name)
for vuln in vulns:
vuln_ids.append(vuln.id)
return vuln_ids
def _convert_blob_to_vuln(
self, storage_client: storage.Client, ndb_client: ndb.Client,
source_repo: osv.SourceRepository, blob: storage.Blob,
ignore_last_import_time: bool
) -> None | Tuple[str, str, None | datetime.datetime,
List[vulnerability_pb2.Vulnerability]]:
"""Parse a GCS blob into a tuple of hash, path, updated, and Vulnerability
list.
Criteria for returning a tuple:
- any record in the blob is new (i.e. a new ID) or modified since last run,
and the hash for the blob has changed
- the importer is reimporting the entire source
- ignore_last_import_time is True
- the record passes OSV JSON Schema validation
Usually an OSV file has a single vulnerability in it, but it is permissible
to have more than one, hence it returns a list of tuples.
This is runnable in parallel using concurrent.futures.ThreadPoolExecutor
Args:
storage_client: a storage.Client() to use for retrieval of the blob
ndb_client: an ndb.Client() to use for Data Store access
source_repo: the osv.SourceRepository the blob relates to
blob: the storage.Blob object to operate on
Raises:
jsonschema.exceptions.ValidationError when self._strict_validation is True
input fails OSV JSON Schema validation
Returns:
a tuple of (hash, path, updated, vulnerability_list) or None when the blob
has an unexpected name or doesn't meet the import criteria.
"""
if not _is_vulnerability_file(source_repo, blob.name):
return None
utc_last_update_date = source_repo.last_update_date
if (not ignore_last_import_time and blob.updated and
blob.updated <= utc_last_update_date):
return None
# The record in GCS appears to be new/changed, examine further.
logging.info('Bucket entry triggered for %s/%s', source_repo.bucket,
blob.name)
# Download in a blob generation agnostic way to cope with the blob
# changing between when it was listed and now (if the generation doesn't
# match, retrieval fails otherwise).
blob_bytes = storage.Blob(
blob.name, blob.bucket,
generation=None).download_as_bytes(storage_client)
blob_hash = osv.sha256_bytes(blob_bytes)
# When self._strict_validation is True,
# this *may* raise a jsonschema.exceptions.ValidationError
vulns = osv.parse_vulnerabilities_from_data(
blob_bytes,
os.path.splitext(blob.name)[1],
strict=self._strict_validation,
source_name=blob.name)
# TODO(andrewpollock): integrate with linter here.
# This is the atypical execution path (when reimporting is triggered)
if ignore_last_import_time:
# do not log updated date for reimports
return blob_hash, blob.name, None, vulns
# If being run under test, reuse existing NDB client.
ndb_ctx = ndb.context.get_context(False)
if ndb_ctx is None:
# Production. Use the NDB client passed in.
ndb_ctx = ndb_client.context(cache_policy=False)
else:
# Unit testing. Reuse the unit test's existing NDB client to avoid
# "RuntimeError: Context is already created for this thread."
ndb_ctx = ndb_ctx.use()
# This is the typical execution path (when reimporting not triggered)
with ndb_ctx:
for vuln in vulns:
v = osv.Vulnerability.get_by_id(vuln.id)
# The vuln already exists and has been modified since last import
if (v is None or
v.modified_raw != vuln.modified.ToDatetime(datetime.UTC)):
return blob_hash, blob.name, blob.updated, vulns
return None
def _sync_from_previous_commit(self, source_repo, repo):
"""Sync the repository from the previous commit.
This was refactored out of _process_updates_git() due to excessive
indentation.
Args:
source_repo: the Git source repository.
repo: the checked out Git source repository.
Returns:
changed_entries: the dict of {path: timestamp} that have changed.
deleted_entries: the dict of {path: timestamp} that have been deleted.
"""
changed_entries = {}
deleted_entries = {}
walker = repo.walk(repo.head.target, pygit2.enums.SortMode.TOPOLOGICAL)
walker.hide(source_repo.last_synced_hash)
for commit in walker:
if commit.author.email == osv.AUTHOR_EMAIL:
continue
if _NO_UPDATE_MARKER in commit.message:
logging.info('Skipping commit %s as no update marker found.', commit.id)
continue
logging.info('Processing commit %s from %s', commit.id,
commit.author.email)
for parent in commit.parents:
diff = repo.diff(parent, commit)
for delta in diff.deltas:
if delta.old_file and _is_vulnerability_file(source_repo,
delta.old_file.path):
if delta.status == pygit2.enums.DeltaStatus.DELETED:
deleted_entries[
delta.old_file.path] = datetime.datetime.fromtimestamp(
commit.commit_time, datetime.UTC)
continue
changed_entries[
delta.old_file.path] = datetime.datetime.fromtimestamp(
commit.commit_time, datetime.UTC)
if delta.new_file and _is_vulnerability_file(source_repo,
delta.new_file.path):
changed_entries[
delta.new_file.path] = datetime.datetime.fromtimestamp(
commit.commit_time, datetime.UTC)
return changed_entries, deleted_entries
def _process_updates_git(self, source_repo: osv.SourceRepository):
"""Process updates for a git source_repo."""
logging.info("Begin processing git: %s", source_repo.name)
repo = self.checkout(source_repo)
# Get list of changed files since last sync.
changed_entries = {}
if source_repo.last_synced_hash:
# Syncing from a previous commit.
changed_entries, _ = self._sync_from_previous_commit(source_repo, repo)
else:
# First sync from scratch.
logging.info('Syncing repo from scratch')
for root, _, filenames in os.walk(osv.repo_path(repo)):
for filename in filenames:
path = os.path.join(root, filename)
rel_path = os.path.relpath(path, osv.repo_path(repo))
if _is_vulnerability_file(source_repo, rel_path):
# set the timestamp to None when syncing from scratch
changed_entries[rel_path] = None
import_failure_logs = []
changed_entries_to_process = []
# Create tasks for changed files.
for changed_entry, ts in changed_entries.items():
path = os.path.join(osv.repo_path(repo), changed_entry)
if not os.path.exists(path):
# Path no longer exists. It must have been deleted in another commit.
continue
try:
vuln = osv.parse_vulnerability(
path,
key_path=source_repo.key_path,
strict=source_repo.strict_validation and self._strict_validation)
except osv.sources.KeyPathError:
# Key path doesn't exist in the vulnerability.
# No need to log a full error, as this is expected result.
logging.info('Entry does not have an OSV entry: %s', changed_entry)
continue
except Exception as e:
logging.error('Failed to parse %s: %s', changed_entry, str(e))
with open(path, "rb") as f:
content = f.read()
vuln_id = self._infer_id_from_invalid_data(
os.path.basename(path), content)
self._record_quality_finding(source_repo.name, vuln_id)
# Don't include error stack trace as that might leak sensitive info
import_failure_logs.append('Failed to parse vulnerability "' + path +
'"')
continue
logging.info('Re-analysis triggered for %s', changed_entry)
original_sha256 = osv.sha256(path)
# Collect for batch processing
changed_entries_to_process.append(
(vuln, path, ts, original_sha256, changed_entry))
if changed_entries_to_process:
put_if_newer_batch(
[(v, p) for v, p, _, _, _ in changed_entries_to_process],
source_repo.name)
for vuln, path, ts, orig_sha256, entry in changed_entries_to_process:
self._request_analysis_external(
source_repo, orig_sha256, entry, source_timestamp=ts)
replace_importer_log(storage.Client(), source_repo.name,
self._public_log_bucket, import_failure_logs)
source_repo.last_synced_hash = str(repo.head.target)
source_repo.put()
logging.info('Finished processing git: %s', source_repo.name)
def _process_updates_bucket(self, source_repo: osv.SourceRepository):
"""Process updates from bucket."""
# TODO(ochang): Use Pub/Sub change notifications for more efficient
# processing.
logging.info("Begin processing bucket for updates: %s", source_repo.name)
# Record import time at the start to avoid race conditions
# where a new record is added to the bucket while we are processing.
import_time_now = utcnow()
if not source_repo.last_update_date:
source_repo.last_update_date = datetime.datetime.min.replace(
tzinfo=datetime.UTC)
ignore_last_import_time = source_repo.ignore_last_import_time
if ignore_last_import_time:
source_repo.ignore_last_import_time = False
source_repo.put()
storage_client = modify_storage_client_adapters(storage.Client())
# Get all of the existing records in the GCS bucket
logging.info(
'Listing blobs in gs://%s',
os.path.join(source_repo.bucket,
('' if source_repo.directory_path is None else
source_repo.directory_path)))
# Convert to list to retrieve all information into memory
# This makes its concurrent use later faster
listed_blobs = list(
storage_client.list_blobs(
source_repo.bucket,
prefix=source_repo.directory_path,
retry=retry.DEFAULT_RETRY))
import_failure_logs = []
# Get the hash and the parsed vulnerability from every GCS object that
# parses as an OSV record. Do this in parallel for a degree of expedience.
with concurrent.futures.ThreadPoolExecutor(
max_workers=_BUCKET_THREAD_COUNT) as executor:
logging.info('Parallel-parsing %d blobs in %s', len(listed_blobs),
source_repo.name)
datastore_client = ndb.Client()
future_to_blob = {
executor.submit(self._convert_blob_to_vuln, storage_client,
datastore_client, source_repo, blob,
ignore_last_import_time):
blob for blob in listed_blobs
}
converted_vulns = []
logging.info('Processing %d parallel-parsed blobs in %s',
len(future_to_blob), source_repo.name)
for future in concurrent.futures.as_completed(future_to_blob):
blob = future_to_blob[future]
try:
if result := future.result():
converted_vulns.append(result)
except Exception as e:
# Don't include error stack trace as that might leak sensitive info
logging.error('Failed to parse vulnerability %s: %s', blob.name, e)
# TODO(apollock): log finding here
# This feels gross to redownload it again.
vuln_id = self._infer_id_from_invalid_data(blob.name,
blob.download_as_bytes())
self._record_quality_finding(source_repo.name, vuln_id)
import_failure_logs.append(
'Failed to parse vulnerability (when considering for import) "' +
blob.name + '"')
for cv_result in converted_vulns:
if not cv_result:
continue
blob_hash, blob_name, source_timestamp, vulns = cv_result
logging.info('Requesting analysis of bucket entry: %s/%s',
source_repo.bucket, blob_name)
put_if_newer_batch([(v, blob_name) for v in vulns], source_repo.name)
self._request_analysis_external(
source_repo,
blob_hash,
blob_name,
source_timestamp=source_timestamp)
replace_importer_log(storage_client, source_repo.name,
self._public_log_bucket, import_failure_logs)
source_repo.last_update_date = import_time_now
source_repo.put()
logging.info('Finished processing bucket: %s', source_repo.name)
def _process_deletions_bucket(self,
source_repo: osv.SourceRepository,
threshold: float = 10.0):
"""Process deletions from a GCS bucket source.
This validates the continued existence of every Vulnerability in Datastore
(for the given source) against every vulnerability currently in that
source's GCS bucket, calculating the delta. The vulnerabilities determined
to have been deleted from GCS are then flagged for treatment by the worker.
If the delta is too large, something undesirable has been assumed to have
happened and further processing is aborted.
Args:
source_repo: the osv.SourceRepository being operated on
threshold: the percentage delta considered safe to delete
"""
logging.info('Begin processing bucket for deletions: %s', source_repo.name)
# Get all the existing non-withdrawn Vulnerability IDs for
# source_repo.name in Datastore
query = osv.Vulnerability.query()
# everything with source_id starting with 'name:'
query = query.filter(osv.Vulnerability.source_id > source_repo.name + ':',
osv.Vulnerability.source_id < source_repo.name + ';')
result = list(query.fetch(keys_only=False))
result.sort(key=lambda r: r.key.id())
VulnAndSource = namedtuple('VulnAndSource', ['id', 'path'])
logging.info('Retrieved %s results from query', len(result))
vuln_ids_for_source = [
VulnAndSource(id=r.key.id(), path=r.source_id.partition(':')[2])
for r in result
if not r.is_withdrawn
]
logging.info(
'Counted %d Vulnerabilities for %s in Datastore',
len(vuln_ids_for_source),
source_repo.name,
extra={'json_fields': {
'source_repo': source_repo.name,
}})
storage_client = storage.Client()
# Get all of the existing records in the GCS bucket
# (to get their IDs for checking against Datastore)
logging.info(
'Listing blobs in gs://%s',
os.path.join(source_repo.bucket,
('' if source_repo.directory_path is None else
source_repo.directory_path)))
listed_blobs = list(
storage_client.list_blobs(
source_repo.bucket,
prefix=source_repo.directory_path,
retry=retry.DEFAULT_RETRY))
import_failure_logs = []
# Get the vulnerability ID from every GCS object that parses as an OSV
# record. Do this in parallel for a degree of expedience.
with concurrent.futures.ThreadPoolExecutor(
max_workers=_BUCKET_THREAD_COUNT) as executor:
logging.info('Parallel-parsing %d blobs in %s', len(listed_blobs),
source_repo.name)
future_to_blob = {
executor.submit(self._vuln_ids_from_gcs_blob, storage_client,
source_repo, blob):
blob for blob in listed_blobs
}
vuln_ids_in_gcs = []
logging.info('Processing %d parallel-parsed blobs in %s',
len(future_to_blob), source_repo.name)
for future in concurrent.futures.as_completed(future_to_blob):
blob = future_to_blob[future]
try:
if future.result():
vuln_ids_in_gcs.extend(
[vuln_id for vuln_id in future.result() if vuln_id])
except Exception as e:
# Don't include error stack trace as that might leak sensitive info
logging.error('Failed to parse vulnerability %s: %s', blob.name, e)
# List.append() is atomic and threadsafe.
import_failure_logs.append(
'Failed to parse vulnerability (when considering for deletion)"' +
blob.name + '"')
logging.info('Counted %d parsed vulnerabilities (from %d blobs) for %s',
len(vuln_ids_in_gcs), len(listed_blobs), source_repo.name)
# diff what's in Datastore with what was seen in GCS.
vulns_to_delete = [
v for v in vuln_ids_for_source if v.id not in vuln_ids_in_gcs
]
logging.info(
'%d Vulnerabilities in Datastore considered deleted from GCS for %s',
len(vulns_to_delete), source_repo.name)
if len(vulns_to_delete) == 0:
logging.info('No vulnerabilities to delete from GCS for %s',
source_repo.name)
replace_importer_log(storage_client, source_repo.name,
self._public_log_bucket, import_failure_logs)
return
# sanity check: deleting a lot/all of the records for source in Datastore is
# probably worth flagging for review.
if (len(vulns_to_delete) / len(vuln_ids_for_source) * 100) >= threshold:
logging.error(
'Cowardly refusing to delete %d missing records from '
'GCS for: %s',
len(vulns_to_delete),
source_repo.name,
extra={})
vulns = [v.id for v in vulns_to_delete]
logging.info('Vulnerabilities to delete: %s', vulns)
return
# Request deletion.
for v in vulns_to_delete:
logging.info('Requesting deletion of bucket entry: %s/%s for %s',
source_repo.bucket, v.path, v.id)
self._request_analysis_external(
source_repo, original_sha256='', path=v.path, deleted=True)
replace_importer_log(storage_client, source_repo.name,
self._public_log_bucket, import_failure_logs)
def _process_updates_rest(self, source_repo: osv.SourceRepository):
"""Process updates from REST API.
To find new updates, first makes a HEAD request to check the 'Last-Modified'
header, and skips processing if it's before the source's last_modified_date
(and ignore_last_import_time isn't set).
Otherwise, GETs the list of vulnerabilities and requests updates for
vulnerabilities modified after last_modified_date.
last_modified_date is updated to the HEAD's 'Last-Modified' time, or the
latest vulnerability's modified date if 'Last-Modified' was missing/invalid.
"""
logging.info('Begin processing REST: %s', source_repo.name)
last_update_date = (
source_repo.last_update_date or
datetime.datetime.min.replace(tzinfo=datetime.UTC))
ignore_last_import = source_repo.ignore_last_import_time
if ignore_last_import:
last_update_date = datetime.datetime.min.replace(tzinfo=datetime.UTC)
source_repo.ignore_last_import_time = False
source_repo.put()
s = requests.Session()
adapter = HTTPAdapter(
max_retries=Retry(
total=3, status_forcelist=[502, 503, 504], backoff_factor=1))
s.mount('http://', adapter)
s.mount('https://', adapter)
try:
request = s.head(source_repo.rest_api_url, timeout=_TIMEOUT_SECONDS)
except Exception:
logging.exception('Exception querying REST API:')
return
if request.status_code != 200:
logging.error('Failed to fetch REST API: %s for %s', request.status_code,
source_repo.rest_api_url)
return
request_last_modified = None
if last_modified := request.headers.get('Last-Modified'):
try:
# strptime discards timezone information - assume UTC
request_last_modified = datetime.datetime.strptime(
last_modified,
_HTTP_LAST_MODIFIED_FORMAT).replace(tzinfo=datetime.UTC)
# Check whether endpoint has been modified since last update
if request_last_modified <= last_update_date:
logging.info('No changes since last update.')
return
except ValueError:
logging.error('Invalid Last-Modified header: "%s"', last_modified)
try:
request = s.get(source_repo.rest_api_url, timeout=_TIMEOUT_SECONDS)
except Exception:
logging.exception('Exception querying REST API:')
return
data = json.loads(request.text)
vulns = []
for datum in data:
vulnerability = vulnerability_pb2.Vulnerability()
json_format.ParseDict(datum, vulnerability, ignore_unknown_fields=True)
if not vulnerability.id:
raise ValueError('Missing id field. Invalid vulnerability.')
if not vulnerability.modified:
raise ValueError('Missing modified field. Invalid vulnerability.')
vulns.append(vulnerability)
vulns_last_modified = last_update_date
logging.info('%d records to consider', len(vulns))
# Create tasks for changed files.
vulns_to_process = []
for vuln in vulns:
import_failure_logs = []
vuln_modified = vuln.modified.ToDatetime(datetime.UTC)
if request_last_modified and vuln_modified > request_last_modified:
logging.warning('%s was modified (%s) after Last-Modified header (%s)',
vuln.id, vuln_modified, request_last_modified)
vulns_last_modified = max(vulns_last_modified, vuln_modified)
if vuln_modified <= last_update_date:
continue
try:
# TODO(jesslowe): Use a ThreadPoolExecutor to parallelize this
single_vuln = s.get(
source_repo.link + vuln.id + source_repo.extension,
timeout=_TIMEOUT_SECONDS)
# Validate the individual request and parse the vulnerability.
try:
v = osv.parse_vulnerability_from_dict(
single_vuln.json(),
source_repo.key_path,
strict=source_repo.strict_validation and self._strict_validation)
except Exception as e:
logging.error('Failed to parse %s: %s', str(single_vuln.content),
str(e))
vuln_id = self._infer_id_from_invalid_data(
source_repo.link + vuln.id + source_repo.extension,
single_vuln.content)
self._record_quality_finding(source_repo.name, vuln_id)
continue
ts = None if ignore_last_import else vuln_modified
vulns_to_process.append((v, vuln.id + source_repo.extension, ts,
osv.sha256_bytes(single_vuln.text.encode())))
except osv.sources.KeyPathError:
# Key path doesn't exist in the vulnerability.
# No need to log a full error, as this is expected result.
logging.info('Entry does not have an OSV entry: %s', vuln.id)
continue
except Exception as e:
logging.exception('Failed to parse %s: error type: %s, details: %s',
vuln.id, e.__class__.__name__, e)
import_failure_logs.append(f'Failed to parse vulnerability "{vuln.id}"')
continue
if vulns_to_process:
put_if_newer_batch([(v, p) for v, p, _, _ in vulns_to_process],
source_repo.name)
for v, path, ts, sha256 in vulns_to_process:
self._request_analysis_external(
source_repo, sha256, path, source_timestamp=ts)
replace_importer_log(storage.Client(), source_repo.name,
self._public_log_bucket, import_failure_logs)
source_repo.last_update_date = request_last_modified or vulns_last_modified
source_repo.put()
logging.info('Finished processing REST: %s', source_repo.name)
def _process_deletions_rest(self,
source_repo: osv.SourceRepository,
threshold: float = 10.0):
"""Process deletions from a REST bucket source.
This validates the continued existence of every Vulnerability in Datastore
(for the given source) against every vulnerability currently in that
source's REST API, calculating the delta. The vulnerabilities determined
to have been deleted from the REST API are then flagged for treatment by
the worker.
If the number of deletions exceeds the safety threshold (default 10%),
the operation is aborted unless ignore_deletion_threshold is set on the
SourceRepository.
"""
logging.info('Begin processing REST for deletions: %s', source_repo.name)
# Get all the existing non-withdrawn Vulnerability IDs for
# source_repo.name in Datastore
query = osv.Vulnerability.query()
# everything with source_id starting with 'name:'
query = query.filter(osv.Vulnerability.source_id > source_repo.name + ':',
osv.Vulnerability.source_id < source_repo.name + ';')
result = list(query.fetch(keys_only=False))
result.sort(key=lambda r: r.key.id())
VulnAndSource = namedtuple('VulnAndSource', ['id', 'path'])
logging.info('Retrieved %s results from query', len(result))
vuln_ids_for_source = [
VulnAndSource(id=r.key.id(), path=r.source_id.partition(':')[2])
for r in result
if not r.is_withdrawn
]
logging.info(
'Counted %d Vulnerabilities for %s in Datastore',
len(vuln_ids_for_source),
source_repo.name,
extra={'json_fields': {
'source_repo': source_repo.name,
}})
s = requests.Session()
adapter = HTTPAdapter(
max_retries=Retry(
total=3, status_forcelist=[502, 503, 504], backoff_factor=1))
s.mount('http://', adapter)
s.mount('https://', adapter)
try:
request = s.get(source_repo.rest_api_url, timeout=_TIMEOUT_SECONDS)
except Exception:
logging.exception('Exception querying REST API:')
return
if request.status_code != 200:
logging.error('Failed to fetch REST API: %s for %s', request.status_code,
source_repo.rest_api_url)
return
data = json.loads(request.text)
vuln_ids_in_rest = []
for datum in data:
vulnerability = vulnerability_pb2.Vulnerability()
json_format.ParseDict(datum, vulnerability, ignore_unknown_fields=True)
if not vulnerability.id:
logging.warning('Missing id field in REST response. Skipping.')
continue
vuln_ids_in_rest.append(vulnerability.id)