aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--README.md2
-rw-r--r--config.py.sample12
-rwxr-xr-xsync.py11
3 files changed, 20 insertions, 5 deletions
diff --git a/README.md b/README.md
index 6777042..7de1e8b 100644
--- a/README.md
+++ b/README.md
@@ -23,7 +23,7 @@ git clone https://git.2e8.dk/peeringdb-simplesync
cd peeringdb-simplesync
cp config.py.sample config.py
-editor config.py # change credentials
+editor config.py # provide API-key
createdb pdbtest
psql pdbtest < schema.sql
diff --git a/config.py.sample b/config.py.sample
index 42bbac4..0f8deeb 100644
--- a/config.py.sample
+++ b/config.py.sample
@@ -1,4 +1,3 @@
-from requests.auth import HTTPBasicAuth
#
# Config keys
# ===========
@@ -9,13 +8,20 @@ from requests.auth import HTTPBasicAuth
# db_schema: database schema name
# If not specified, it defaults to 'peeringdb'
#
+# auth: 'API-key'
+# Authenticate using an API-key.
+#
+# https://docs.peeringdb.com/blog/api_keys/
+# https://github.com/peeringdb/peeringdb/blob/master/docs/api_keys.md
+#
# auth: HTTPBasicAuth('username', 'password')
-# Authenticate using username/password
+# Authenticate using username/password (DEPRECATED)
+# requires: from requests.auth import HTTPBasicAuth
#
def get_config():
return {
'db_conn_str': 'dbname=pdbtest',
'db_schema': 'peeringdb',
- 'auth': HTTPBasicAuth('use_a_dedicated_peeringdb_account', 'and_a_long_random_password'),
+ 'auth': 'obtain_an_api-key_from_peeringdb_com',
}
diff --git a/sync.py b/sync.py
index bd26211..8388d25 100755
--- a/sync.py
+++ b/sync.py
@@ -156,13 +156,22 @@ def find_spec(s, url):
return urljoin(url, m[2])
+def handle_auth(s):
+ auth = get_config().get('auth')
+ if type(auth) == str:
+ # API-Key
+ s.headers.update({'Authorization': f'Api-Key {auth:s}'})
+ else:
+ # eg. HTTPBasicAuth('username', 'password')
+ s.auth = auth
+
def main():
open_db()
s = requests.Session()
retries = Retry(total=5, backoff_factor=1, status_forcelist=[ 502, 503, 504 ])
s.mount('https://', HTTPAdapter(max_retries=retries))
- s.auth = get_config()['auth']
+ handle_auth(s)
req_agent = s.headers.get('User-Agent')
s.headers.update({'User-Agent': f'peeringdb-simplesync/0.1 {req_agent:s}'})