Skip to content

Commit 717a189

Browse files
authored
Merge pull request #1061 from dmytrotsko/api_key_v2
Improved API keys logic
2 parents 0d81699 + 7a890b4 commit 717a189

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+996
-300
lines changed

.dockerignore

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/delphi-epidata
2-
/.mypy_cache
2+
**/.mypy_cache
33
/.github
44
/docs
5-
__pycache__
6-
/node_modules
5+
**/__pycache__
6+
**/.pytest_cache
7+
**/node_modules

.env.example

+5-15
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,8 @@
11
FLASK_DEBUG=True
22
SQLALCHEMY_DATABASE_URI=sqlite:///test.db
33
FLASK_SECRET=abc
4-
SECRET_TWITTER=abc
5-
SECRET_GHT=abc
6-
SECRET_FLUVIEW=abc
7-
SECRET_CDC=abc
8-
SECRET_SENSORS=abc
9-
SECRET_SENSOR_TWTR=abc
10-
SECRET_SENSOR_GFT=abc
11-
SECRET_SENSOR_GHT=abc
12-
SECRET_SENSOR_GHTJ=abc
13-
SECRET_SENSOR_CDC=abc
14-
SECRET_SENSOR_QUID=abc
15-
SECRET_SENSOR_WIKI=abc
16-
SECRET_QUIDEL=abc
17-
SECRET_NOROSTAT=abc
18-
SECRET_AFHSB=abc
4+
#API_REQUIRED_STARTING_AT=2021-07-30
5+
API_KEY_ADMIN_PASSWORD=abc
6+
API_KEY_REGISTER_WEBHOOK_TOKEN=abc
7+
RECAPTCHA_SITE_KEY
8+
RECAPTCHA_SECRET_KEY

.github/workflows/ci.yaml

+3-1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ jobs:
5353
run: |
5454
docker build -t delphi_database_epidata -f ./repos/delphi/delphi-epidata/dev/docker/database/epidata/Dockerfile .
5555
docker build -t delphi_web_python -f repos/delphi/delphi-epidata/dev/docker/python/Dockerfile .
56+
sudo docker build -t delphi_redis_instance -f repos/delphi/delphi-epidata/dev/docker/redis/Dockerfile .
5657
cd ./repos/delphi/delphi-epidata
5758
docker build -t delphi_web_epidata -f ./devops/Dockerfile .
5859
cd ../../../
@@ -63,7 +64,8 @@ jobs:
6364
run: |
6465
docker network create --driver bridge delphi-net
6566
docker run --rm -d -p 13306:3306 --network delphi-net --name delphi_database_epidata --cap-add=sys_nice delphi_database_epidata
66-
docker run --rm -d -p 10080:80 --env "MODULE_NAME=delphi.epidata.server.main" --env "SQLALCHEMY_DATABASE_URI=mysql+mysqldb://user:pass@delphi_database_epidata:3306/epidata" --env "FLASK_SECRET=abc" --env "FLASK_PREFIX=/epidata" --network delphi-net --name delphi_web_epidata delphi_web_epidata
67+
docker run --rm -d -p 10080:80 --env "MODULE_NAME=delphi.epidata.server.main" --env "SQLALCHEMY_DATABASE_URI=mysql+mysqldb://user:pass@delphi_database_epidata:3306/epidata" --env "FLASK_SECRET=abc" --env "FLASK_PREFIX=/epidata" --env "RATELIMIT_STORAGE_URL=redis://delphi_redis_instance:6379" --env "API_KEY_REGISTER_WEBHOOK_TOKEN=abc" --env "API_KEY_ADMIN_PASSWORD=test_admin_password" --network delphi-net --name delphi_web_epidata delphi_web_epidata
68+
docker run --rm -p 6379:6379 --network delphi-net --name delphi_redis_instance delphi_redis_instance
6769
docker ps
6870
6971
- run: |

dev/docker/python/Dockerfile

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ WORKDIR /usr/src/app
55
COPY repos repos
66
COPY repos/delphi/delphi-epidata/dev/docker/python/setup.sh .
77

8+
89
RUN ln -s -f /usr/share/zoneinfo/America/New_York /etc/localtime && \
910
chmod -R o+r repos/ && \
1011
bash setup.sh && \

dev/docker/redis/Dockerfile

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
FROM redis
2+
3+
CMD ["redis-server"]

devops/Dockerfile

-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ RUN ln -s -f /usr/share/zoneinfo/America/New_York /etc/localtime \
2323
# the file /tmp/requirements.txt is created in the parent docker definition. (see:
2424
# https://github.com/tiangolo/meinheld-gunicorn-docker/blob/master/docker-images/python3.8.dockerfile#L5 )
2525
# this combined requirements installation ensures all version constrants are accounted for.
26-
2726
# disable python stdout buffering
2827
ENV PYTHONUNBUFFERED 1
2928

integrations/acquisition/covid_hosp/facility/test_scenarios.py

+3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ def setUp(self):
2929

3030
# use the local instance of the Epidata API
3131
Epidata.BASE_URL = 'http://delphi_web_epidata/epidata/api.php'
32+
Epidata.auth = ('epidata', 'key')
3233

3334
# use the local instance of the epidata database
3435
secrets.db.host = 'delphi_database_epidata'
@@ -40,6 +41,8 @@ def setUp(self):
4041
cur.execute('truncate table covid_hosp_facility')
4142
cur.execute('truncate table covid_hosp_facility_key')
4243
cur.execute('truncate table covid_hosp_meta')
44+
cur.execute('delete from api_user')
45+
cur.execute('insert into api_user(api_key, tracking, registered) values ("key", 1, 1)')
4346

4447
@freeze_time("2021-03-16")
4548
def test_acquire_dataset(self):

integrations/acquisition/covid_hosp/state_daily/test_scenarios.py

+3
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def setUp(self):
3333

3434
# use the local instance of the Epidata API
3535
Epidata.BASE_URL = 'http://delphi_web_epidata/epidata/api.php'
36+
Epidata.auth = ('epidata', 'key')
3637

3738
# use the local instance of the epidata database
3839
secrets.db.host = 'delphi_database_epidata'
@@ -43,6 +44,8 @@ def setUp(self):
4344
with db.new_cursor() as cur:
4445
cur.execute('truncate table covid_hosp_state_timeseries')
4546
cur.execute('truncate table covid_hosp_meta')
47+
cur.execute('delete from api_user')
48+
cur.execute('insert into api_user(api_key, tracking, registered) values("key", 1, 1)')
4649

4750
@freeze_time("2021-03-16")
4851
def test_acquire_dataset(self):

integrations/acquisition/covid_hosp/state_timeseries/test_scenarios.py

+3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ def setUp(self):
2929

3030
# use the local instance of the Epidata API
3131
Epidata.BASE_URL = 'http://delphi_web_epidata/epidata/api.php'
32+
Epidata.auth = ('epidata', 'key')
3233

3334
# use the local instance of the epidata database
3435
secrets.db.host = 'delphi_database_epidata'
@@ -39,6 +40,8 @@ def setUp(self):
3940
with db.new_cursor() as cur:
4041
cur.execute('truncate table covid_hosp_state_timeseries')
4142
cur.execute('truncate table covid_hosp_meta')
43+
cur.execute('delete from api_user')
44+
cur.execute('insert into api_user(api_key, tracking, registered) values ("key", 1, 1)')
4245

4346
@freeze_time("2021-03-17")
4447
def test_acquire_dataset(self):

integrations/acquisition/covidcast/test_covidcast_meta_caching.py

+10-8
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,20 @@ def setUp(self):
6060

6161
# use the local instance of the Epidata API
6262
Epidata.BASE_URL = BASE_URL
63+
Epidata.auth = ('epidata', 'key')
6364

6465
def tearDown(self):
6566
"""Perform per-test teardown."""
6667
self.cur.close()
6768
self.cnx.close()
6869

70+
@staticmethod
71+
def _make_request():
72+
params = {'endpoint': 'covidcast_meta', 'cached': 'true'}
73+
response = requests.get(Epidata.BASE_URL, params=params, auth=Epidata.auth)
74+
response.raise_for_status()
75+
return response.json()
76+
6977
def test_caching(self):
7078
"""Populate, query, cache, query, and verify the cache."""
7179

@@ -147,10 +155,7 @@ def test_caching(self):
147155
self.cnx.commit()
148156

149157
# fetch the cached version (manually)
150-
params = {'endpoint': 'covidcast_meta', 'cached': 'true'}
151-
response = requests.get(BASE_URL, params=params)
152-
response.raise_for_status()
153-
epidata4 = response.json()
158+
epidata4 = self._make_request()
154159

155160
# make sure the cache was actually served
156161
self.assertEqual(epidata4, {
@@ -170,10 +175,7 @@ def test_caching(self):
170175
self.cnx.commit()
171176

172177
# fetch the cached version (manually)
173-
params = {'endpoint': 'covidcast_meta', 'cached': 'true'}
174-
response = requests.get(BASE_URL, params=params)
175-
response.raise_for_status()
176-
epidata5 = response.json()
178+
epidata5 = self._make_request()
177179

178180
# make sure the cache was returned anyhow
179181
self.assertEqual(epidata4, epidata5)

integrations/acquisition/covidcast/test_csv_uploading.py

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ def setUp(self):
5757

5858
# use the local instance of the Epidata API
5959
Epidata.BASE_URL = 'http://delphi_web_epidata/epidata/api.php'
60+
Epidata.auth = ('epidata', 'key')
6061

6162
def tearDown(self):
6263
"""Perform per-test teardown."""

integrations/acquisition/covidcast_nowcast/test_csv_uploading.py

+3
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ def setUp(self):
4141
database='epidata')
4242
cur = cnx.cursor()
4343
cur.execute('truncate table covidcast_nowcast')
44+
cur.execute('delete from api_user')
45+
cur.execute('insert into api_user(api_key, tracking, registered) values ("key", 1, 1)')
4446
cnx.commit()
4547
cur.close()
4648

@@ -54,6 +56,7 @@ def setUp(self):
5456

5557
# use the local instance of the Epidata API
5658
Epidata.BASE_URL = 'http://delphi_web_epidata/epidata/api.php'
59+
Epidata.auth = ('epidata', 'key')
5760

5861
def tearDown(self):
5962
"""Perform per-test teardown."""

integrations/client/test_delphi_epidata.py

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ def localSetUp(self):
4141

4242
# use the local instance of the Epidata API
4343
Epidata.BASE_URL = 'http://delphi_web_epidata/epidata/api.php'
44+
Epidata.auth = ('epidata', 'key')
4445

4546
# use the local instance of the epidata database
4647
secrets.db.host = 'delphi_database_epidata'

integrations/client/test_nowcast.py

+3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ def setUp(self):
2828
cur = cnx.cursor()
2929

3030
cur.execute('truncate table covidcast_nowcast')
31+
cur.execute('delete from api_user')
32+
cur.execute('insert into api_user(api_key, tracking, registered) values ("key", 1, 1)')
3133

3234
cnx.commit()
3335
cur.close()
@@ -38,6 +40,7 @@ def setUp(self):
3840

3941
# use the local instance of the Epidata API
4042
Epidata.BASE_URL = 'http://delphi_web_epidata/epidata/api.php'
43+
Epidata.auth = ('epidata', 'key')
4144

4245
# use the local instance of the epidata database
4346
secrets.db.host = 'delphi_database_epidata'

integrations/server/test_covid_hosp.py

+3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ def setUp(self):
1717

1818
# use the local instance of the Epidata API
1919
Epidata.BASE_URL = 'http://delphi_web_epidata/epidata/api.php'
20+
Epidata.auth = ('epidata', 'key')
2021

2122
# use the local instance of the epidata database
2223
secrets.db.host = 'delphi_database_epidata'
@@ -27,6 +28,8 @@ def setUp(self):
2728
with db.new_cursor() as cur:
2829
cur.execute('truncate table covid_hosp_state_timeseries')
2930
cur.execute('truncate table covid_hosp_meta')
31+
cur.execute('delete from api_user')
32+
cur.execute('insert into api_user(api_key, tracking, registered) values ("key", 1, 1)')
3033

3134

3235
def insert_issue(self, cur, issue, value, record_type):

integrations/server/test_covidcast.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,12 @@
66

77
# third party
88
import mysql.connector
9-
import requests
109

1110
# first party
1211
from delphi_utils import Nans
1312
from delphi.epidata.acquisition.covidcast.test_utils import CovidcastBase, CovidcastTestRow
1413
from delphi.epidata.client.delphi_epidata import Epidata
1514

16-
# use the local instance of the Epidata API
17-
BASE_URL = 'http://delphi_web_epidata/epidata/api.php'
1815

1916
class CovidcastTests(CovidcastBase):
2017
"""Tests the `covidcast` endpoint."""
@@ -25,7 +22,9 @@ def localSetUp(self):
2522

2623
def request_based_on_row(self, row: CovidcastTestRow, **kwargs):
2724
params = self.params_from_row(row, endpoint='covidcast', **kwargs)
28-
Epidata.BASE_URL = BASE_URL
25+
# use the local instance of the Epidata API
26+
Epidata.BASE_URL = 'http://delphi_web_epidata/epidata/api.php'
27+
Epidata.auth = ('epidata', 'key')
2928
response = Epidata.covidcast(**params)
3029

3130
return response

integrations/server/test_covidcast_endpoints.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
# use the local instance of the Epidata API
1616
BASE_URL = "http://delphi_web_epidata/epidata/covidcast"
1717
BASE_URL_OLD = "http://delphi_web_epidata/epidata/api.php"
18+
AUTH = ('epidata', 'key')
1819

1920

2021
class CovidcastEndpointTests(CovidcastBase):
@@ -36,7 +37,7 @@ def _fetch(self, endpoint="/", is_compatibility=False, **params):
3637
params.setdefault("data_source", params.get("source"))
3738
else:
3839
url = f"{BASE_URL}{endpoint}"
39-
response = requests.get(url, params=params)
40+
response = requests.get(url, params=params, auth=AUTH)
4041
response.raise_for_status()
4142
return response.json()
4243

0 commit comments

Comments
 (0)