Skip to content

Commit 1821a23

Browse files
authored
feat(api): make prod api use new datastore entities (#4058)
Switch production over to use new `AffectedVersions` for matching. There's now a bunch of unused code in server.py that I will remove in a follow-up PR.
1 parent a574cee commit 1821a23

File tree

5 files changed

+25
-55
lines changed

5 files changed

+25
-55
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ run-website-emulator:
7171
run-api-server:
7272
test -f $(HOME)/.config/gcloud/application_default_credentials.json || (echo "GCP Application Default Credentials not set, try 'gcloud auth login --update-adc'"; exit 1)
7373
cd gcp/api && docker build -f Dockerfile.esp -t osv/esp:latest .
74-
cd gcp/api && $(install-cmd) && GOOGLE_CLOUD_PROJECT=oss-vdb $(run-cmd) python test_server.py $(HOME)/.config/gcloud/application_default_credentials.json $(ARGS)# Run with `make run-api-server ARGS=--no-backend` to launch esp without backend.
74+
cd gcp/api && $(install-cmd) && GOOGLE_CLOUD_PROJECT=oss-vdb OSV_VULNERABILITIES_BUCKET=osv-vulnerabilities $(run-cmd) python test_server.py $(HOME)/.config/gcloud/application_default_credentials.json $(ARGS)# Run with `make run-api-server ARGS=--no-backend` to launch esp without backend.
7575

7676
run-api-server-test:
7777
test -f $(HOME)/.config/gcloud/application_default_credentials.json || (echo "GCP Application Default Credentials not set, try 'gcloud auth login --update-adc'"; exit 1)

deployment/clouddeploy/osv-api/run-prod.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ spec:
1010
spec:
1111
containers:
1212
- image: osv-server
13+
env:
14+
- name: OSV_VULNERABILITIES_BUCKET
15+
value: osv-vulnerabilities
1316
resources:
1417
limits:
1518
cpu: 2

gcp/api/integration_tests.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -173,17 +173,20 @@ def test_query_version(self):
173173
timeout=_TIMEOUT)
174174
self.assert_results_equal({'vulns': [self._VULN_744]}, response.json())
175175

176-
response = requests.post(
177-
_api() + _BASE_QUERY,
178-
data=json.dumps({
179-
'version': '2.1.2-rc',
180-
'package': {
181-
'name': 'mruby',
182-
}
183-
}),
184-
timeout=_TIMEOUT)
185-
186-
self.assert_results_equal({'vulns': [self._VULN_744]}, response.json())
176+
# NOTE(michaelkedar): version queries without ecosystem specified is not
177+
# officially supported. Since our change to matching logic, this test
178+
# would now return >50 vulnerabilities across 4 ecosystems.
179+
# response = requests.post(
180+
# _api() + _BASE_QUERY,
181+
# data=json.dumps({
182+
# 'version': '2.1.2-rc',
183+
# 'package': {
184+
# 'name': 'mruby',
185+
# }
186+
# }),
187+
# timeout=_TIMEOUT)
188+
189+
# self.assert_results_equal({'vulns': [self._VULN_744]}, response.json())
187190
# self.assertEqual(
188191
# response.text,
189192
# '{"code":3,"message":"Ecosystem not specified"}')
@@ -262,6 +265,7 @@ def test_query_semver(self):
262265
go_2021_0052,
263266
ghsa_3vp4_m3rf_835h,
264267
]
268+
expected_vulns.sort(key=lambda x: x['id'])
265269

266270
# Test that a SemVer (believed to be vulnerable) version and an ecosystem
267271
# returns expected vulnerabilities.

gcp/api/run_tests.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ if [ $# -lt 1 ]; then
1818
exit 1
1919
fi
2020

21-
export GOOGLE_CLOUD_PROJECT=oss-vdb
21+
export GOOGLE_CLOUD_PROJECT=oss-vdb OSV_VULNERABILITIES_BUCKET=osv-vulnerabilities
2222
service docker start
2323

2424
# Set -e later as service docker start should be able to successfully fail

gcp/api/server.py

Lines changed: 5 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -173,28 +173,10 @@ def GetVulnById(self, request, context: grpc.ServicerContext):
173173
context.abort(grpc.StatusCode.INVALID_ARGUMENT, 'ID too long')
174174
return None
175175

176-
if get_gcp_project() in ('oss-vdb-test', 'test-osv'):
177-
# Get vuln from GCS
178-
try:
179-
return osv.gcs.get_by_id(request.id)
180-
except exceptions.NotFound:
181-
# Check for aliases
182-
alias_group = yield osv.AliasGroup.query(
183-
osv.AliasGroup.bug_ids == request.id).get_async()
184-
if alias_group:
185-
alias_string = ' '.join([
186-
f'{alias}' for alias in alias_group.bug_ids if alias != request.id
187-
])
188-
context.abort(
189-
grpc.StatusCode.NOT_FOUND,
190-
f'Bug not found, but the following aliases were: {alias_string}')
191-
return None
192-
context.abort(grpc.StatusCode.NOT_FOUND, 'Bug not found.')
193-
return None
194-
195-
bug = yield osv.Bug.query(osv.Bug.db_id == request.id).get_async()
196-
197-
if not bug:
176+
# Get vuln from GCS
177+
try:
178+
return osv.gcs.get_by_id(request.id)
179+
except exceptions.NotFound:
198180
# Check for aliases
199181
alias_group = yield osv.AliasGroup.query(
200182
osv.AliasGroup.bug_ids == request.id).get_async()
@@ -209,17 +191,6 @@ def GetVulnById(self, request, context: grpc.ServicerContext):
209191
context.abort(grpc.StatusCode.NOT_FOUND, 'Bug not found.')
210192
return None
211193

212-
if bug.status == osv.BugStatus.UNPROCESSED:
213-
context.abort(grpc.StatusCode.NOT_FOUND, 'Bug not found.')
214-
return None
215-
216-
if not bug.public:
217-
context.abort(grpc.StatusCode.PERMISSION_DENIED, 'Permission denied.')
218-
return None
219-
220-
resp = yield bug_to_response(bug, include_details=True)
221-
return resp
222-
223194
@ndb_context
224195
@trace_filter.log_trace
225196
@ndb.synctasklet
@@ -876,18 +847,10 @@ def to_response(b: osv.Bug):
876847
return None
877848

878849
bugs = yield query_by_commit(context, commit_bytes, to_response=to_response)
879-
elif package_name and get_gcp_project() in ('oss-vdb-test', 'test-osv'):
850+
elif package_name:
880851
# New Database table & GCS querying
881852
bugs = yield query_package(context, package_name, ecosystem, version,
882853
include_details)
883-
# Version query needs to include a package.
884-
elif package_name and version:
885-
bugs = yield query_by_version(
886-
context, package_name, ecosystem, version, to_response=to_response)
887-
elif package_name and ecosystem:
888-
# Package specified without version.
889-
bugs = yield query_by_package(
890-
context, package_name, ecosystem, to_response=to_response)
891854
else:
892855
context.service_context.abort(grpc.StatusCode.INVALID_ARGUMENT,
893856
'Invalid query.')

0 commit comments

Comments
 (0)