Rotating API keys for Mapbox

This commit is contained in:
Antonio
2018-01-11 10:18:01 +01:00
parent b10f48166d
commit 7dc87ad38e
14 changed files with 177 additions and 39 deletions
@@ -2,6 +2,35 @@
-- Complain if script is sourced in psql, rather than via CREATE EXTENSION
\echo Use "ALTER EXTENSION cdb_dataservices_server UPDATE TO '0.30.0'" to load this file. \quit
CREATE OR REPLACE FUNCTION cdb_dataservices_server._cdb_mapbox_apikey(
username TEXT,
orgname TEXT,
service TEXT)
RETURNS TEXT AS $$
import json
from cartodb_services.mapbox.types import MAPBOX_ROUTING_APIKEY_ROUNDRROBIN, MAPBOX_GEOCODER_APIKEY_ROUNDRROBIN, MAPBOX_ISOLINES_APIKEY_ROUNDRROBIN
if service == 'routing':
round_robin_service = MAPBOX_ROUTING_APIKEY_ROUNDRROBIN
api_keys = GD["user_routing_config_{0}".format(username)].mapbox_api_keys
elif service == 'geocoder':
round_robin_service = MAPBOX_GEOCODER_APIKEY_ROUNDRROBIN
api_keys = GD["user_geocoder_config_{0}".format(username)].mapbox_api_keys
elif service == 'isolines':
round_robin_service = MAPBOX_ISOLINES_APIKEY_ROUNDRROBIN
api_keys = GD["user_isolines_routing_config_{0}".format(username)].mapbox_matrix_api_keys
else:
return None
round_robin = GD[round_robin_service] if round_robin_service in GD else 0
api_key = api_keys[round_robin]
GD[round_robin_service] = round_robin + 1 if round_robin < len(api_keys) - 1 else 0
return api_key
$$ LANGUAGE plpythonu SECURITY DEFINER STABLE PARALLEL RESTRICTED;
-- HERE goes your code to upgrade/downgrade
CREATE OR REPLACE FUNCTION cdb_dataservices_server._cdb_mapbox_route_with_waypoints(
username TEXT,
@@ -28,8 +57,11 @@ RETURNS cdb_dataservices_server.simple_route AS $$
if not quota_service.check_user_quota():
raise Exception('You have reached the limit of your quota')
mapbox_apikey_query = plpy.prepare("SELECT cdb_dataservices_server._cdb_mapbox_apikey($1, $2, $3) as apikey;", ["text", "text", "text"])
mapbox_apikey = plpy.execute(mapbox_apikey_query, [username, orgname, 'routing'])
try:
client = MapboxRouting(user_routing_config.mapbox_api_key, logger, user_routing_config.mapbox_service_params)
client = MapboxRouting(mapbox_apikey[0]['apikey'], logger, user_routing_config.mapbox_service_params)
if not waypoints or len(waypoints) < 2:
logger.info("Empty origin or destination")
@@ -237,9 +269,12 @@ RETURNS Geometry AS $$
if not quota_service.check_user_quota():
raise Exception('You have reached the limit of your quota')
mapbox_apikey_query = plpy.prepare("SELECT cdb_dataservices_server._cdb_mapbox_apikey($1, $2, $3) as apikey;", ["text", "text", "text"])
mapbox_apikey = plpy.execute(mapbox_apikey_query, [username, orgname, 'geocoder'])
with metrics('cdb_mapbox_geocode_street_point', user_geocoder_config, logger):
try:
geocoder = MapboxGeocoder(user_geocoder_config.mapbox_api_key, logger, user_geocoder_config.mapbox_service_params)
geocoder = MapboxGeocoder(mapbox_apikey[0]['apikey'], logger, user_geocoder_config.mapbox_service_params)
country_iso3 = None
if country:
@@ -344,9 +379,12 @@ RETURNS Geometry AS $$
if not quota_service.check_user_quota():
raise Exception('You have reached the limit of your quota')
mapbox_apikey_query = plpy.prepare("SELECT cdb_dataservices_server._cdb_mapbox_apikey($1, $2, $3) as apikey;", ["text", "text", "text"])
mapbox_apikey = plpy.execute(mapbox_apikey_query, [username, orgname, 'geocoder'])
with metrics('cdb_geocode_namedplace_point', user_geocoder_config, logger):
try:
geocoder = MapboxGeocoder(user_geocoder_config.mapbox_api_key, logger)
geocoder = MapboxGeocoder(mapbox_apikey[0]['apikey'], logger)
country_iso3 = None
if country_name:
@@ -442,8 +480,11 @@ RETURNS SETOF cdb_dataservices_server.isoline AS $$
if not quota_service.check_user_quota():
raise Exception('You have reached the limit of your quota')
mapbox_apikey_query = plpy.prepare("SELECT cdb_dataservices_server._cdb_mapbox_apikey($1, $2, $3) as apikey;", ["text", "text", "text"])
mapbox_apikey = plpy.execute(mapbox_apikey_query, [username, orgname, 'isolines'])
try:
client = MapboxMatrixClient(user_isolines_routing_config.mapbox_matrix_api_key, logger, user_isolines_routing_config.mapbox_matrix_service_params)
client = MapboxMatrixClient(mapbox_apikey[0]['apikey'], logger, user_isolines_routing_config.mapbox_matrix_service_params)
mapbox_isolines = MapboxIsolines(client, logger)
if source:
@@ -514,8 +555,11 @@ RETURNS SETOF cdb_dataservices_server.isoline AS $$
if not quota_service.check_user_quota():
raise Exception('You have reached the limit of your quota')
mapbox_apikey_query = plpy.prepare("SELECT cdb_dataservices_server._cdb_mapbox_apikey($1, $2, $3) as apikey;", ["text", "text", "text"])
mapbox_apikey = plpy.execute(mapbox_apikey_query, [username, orgname, 'isolines'])
try:
client = MapboxMatrixClient(user_isolines_routing_config.mapbox_matrix_api_key, logger, user_isolines_routing_config.mapbox_matrix_service_params)
client = MapboxMatrixClient(mapbox_apikey[0]['apikey'], logger, user_isolines_routing_config.mapbox_matrix_service_params)
mapbox_isolines = MapboxIsolines(client, logger)
if source:
@@ -3,6 +3,7 @@
\echo Use "ALTER EXTENSION cdb_dataservices_server UPDATE TO '0.29.0'" to load this file. \quit
-- HERE goes your code to upgrade/downgrade
DROP FUNCTION IF EXISTS cdb_dataservices_server._cdb_mapbox_apikey(username TEXT, orgname TEXT, service TEXT);
DROP FUNCTION IF EXISTS cdb_dataservices_server._cdb_mapbox_route_with_waypoints(username TEXT, orgname TEXT, waypoints geometry(Point, 4326)[], mode TEXT);
CREATE OR REPLACE FUNCTION cdb_dataservices_server.cdb_route_point_to_point(
@@ -32,8 +32,11 @@ RETURNS cdb_dataservices_server.simple_route AS $$
if not quota_service.check_user_quota():
raise Exception('You have reached the limit of your quota')
mapbox_apikey_query = plpy.prepare("SELECT cdb_dataservices_server._cdb_mapbox_apikey($1, $2, $3) as apikey;", ["text", "text", "text"])
mapbox_apikey = plpy.execute(mapbox_apikey_query, [username, orgname, 'routing'])
try:
client = MapboxRouting(user_routing_config.mapbox_api_key, logger, user_routing_config.mapbox_service_params)
client = MapboxRouting(mapbox_apikey[0]['apikey'], logger, user_routing_config.mapbox_service_params)
if not waypoints or len(waypoints) < 2:
logger.info("Empty origin or destination")
@@ -1770,6 +1773,36 @@ RETURNS boolean AS $$
GD[cache_key] = obs_config
return True
$$ LANGUAGE plpythonu SECURITY DEFINER STABLE PARALLEL RESTRICTED;
CREATE OR REPLACE FUNCTION cdb_dataservices_server._cdb_mapbox_apikey(
username TEXT,
orgname TEXT,
service TEXT)
RETURNS TEXT AS $$
import json
from cartodb_services.mapbox.types import MAPBOX_ROUTING_APIKEY_ROUNDRROBIN, MAPBOX_GEOCODER_APIKEY_ROUNDRROBIN, MAPBOX_ISOLINES_APIKEY_ROUNDRROBIN
if service == 'routing':
round_robin_service = MAPBOX_ROUTING_APIKEY_ROUNDRROBIN
api_keys = GD["user_routing_config_{0}".format(username)].mapbox_api_keys
elif service == 'geocoder':
round_robin_service = MAPBOX_GEOCODER_APIKEY_ROUNDRROBIN
api_keys = GD["user_geocoder_config_{0}".format(username)].mapbox_api_keys
elif service == 'isolines':
round_robin_service = MAPBOX_ISOLINES_APIKEY_ROUNDRROBIN
api_keys = GD["user_isolines_routing_config_{0}".format(username)].mapbox_matrix_api_keys
else:
return None
round_robin = GD[round_robin_service] if round_robin_service in GD else 0
api_key = api_keys[round_robin]
GD[round_robin_service] = round_robin + 1 if round_robin < len(api_keys) - 1 else 0
return api_key
$$ LANGUAGE plpythonu SECURITY DEFINER STABLE PARALLEL RESTRICTED;
DO $$
BEGIN
IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'service_type') THEN
@@ -2079,9 +2112,12 @@ RETURNS Geometry AS $$
if not quota_service.check_user_quota():
raise Exception('You have reached the limit of your quota')
mapbox_apikey_query = plpy.prepare("SELECT cdb_dataservices_server._cdb_mapbox_apikey($1, $2, $3) as apikey;", ["text", "text", "text"])
mapbox_apikey = plpy.execute(mapbox_apikey_query, [username, orgname, 'geocoder'])
with metrics('cdb_mapbox_geocode_street_point', user_geocoder_config, logger):
try:
geocoder = MapboxGeocoder(user_geocoder_config.mapbox_api_key, logger, user_geocoder_config.mapbox_service_params)
geocoder = MapboxGeocoder(mapbox_apikey[0]['apikey'], logger, user_geocoder_config.mapbox_service_params)
country_iso3 = None
if country:
@@ -2460,9 +2496,12 @@ RETURNS Geometry AS $$
if not quota_service.check_user_quota():
raise Exception('You have reached the limit of your quota')
mapbox_apikey_query = plpy.prepare("SELECT cdb_dataservices_server._cdb_mapbox_apikey($1, $2, $3) as apikey;", ["text", "text", "text"])
mapbox_apikey = plpy.execute(mapbox_apikey_query, [username, orgname, 'geocoder'])
with metrics('cdb_geocode_namedplace_point', user_geocoder_config, logger):
try:
geocoder = MapboxGeocoder(user_geocoder_config.mapbox_api_key, logger)
geocoder = MapboxGeocoder(mapbox_apikey[0]['apikey'], logger)
country_iso3 = None
if country_name:
@@ -3123,8 +3162,11 @@ RETURNS SETOF cdb_dataservices_server.isoline AS $$
if not quota_service.check_user_quota():
raise Exception('You have reached the limit of your quota')
mapbox_apikey_query = plpy.prepare("SELECT cdb_dataservices_server._cdb_mapbox_apikey($1, $2, $3) as apikey;", ["text", "text", "text"])
mapbox_apikey = plpy.execute(mapbox_apikey_query, [username, orgname, 'isolines'])
try:
client = MapboxMatrixClient(user_isolines_routing_config.mapbox_matrix_api_key, logger, user_isolines_routing_config.mapbox_matrix_service_params)
client = MapboxMatrixClient(mapbox_apikey[0]['apikey'], logger, user_isolines_routing_config.mapbox_matrix_service_params)
mapbox_isolines = MapboxIsolines(client, logger)
if source:
@@ -3258,8 +3300,11 @@ RETURNS SETOF cdb_dataservices_server.isoline AS $$
if not quota_service.check_user_quota():
raise Exception('You have reached the limit of your quota')
mapbox_apikey_query = plpy.prepare("SELECT cdb_dataservices_server._cdb_mapbox_apikey($1, $2, $3) as apikey;", ["text", "text", "text"])
mapbox_apikey = plpy.execute(mapbox_apikey_query, [username, orgname, 'isolines'])
try:
client = MapboxMatrixClient(user_isolines_routing_config.mapbox_matrix_api_key, logger, user_isolines_routing_config.mapbox_matrix_service_params)
client = MapboxMatrixClient(mapbox_apikey[0]['apikey'], logger, user_isolines_routing_config.mapbox_matrix_service_params)
mapbox_isolines = MapboxIsolines(client, logger)
if source:
+4 -1
View File
@@ -29,8 +29,11 @@ RETURNS cdb_dataservices_server.simple_route AS $$
if not quota_service.check_user_quota():
raise Exception('You have reached the limit of your quota')
mapbox_apikey_query = plpy.prepare("SELECT cdb_dataservices_server._cdb_mapbox_apikey($1, $2, $3) as apikey;", ["text", "text", "text"])
mapbox_apikey = plpy.execute(mapbox_apikey_query, [username, orgname, 'routing'])
try:
client = MapboxRouting(user_routing_config.mapbox_api_key, logger, user_routing_config.mapbox_service_params)
client = MapboxRouting(mapbox_apikey[0]['apikey'], logger, user_routing_config.mapbox_service_params)
if not waypoints or len(waypoints) < 2:
logger.info("Empty origin or destination")
+29
View File
@@ -117,3 +117,32 @@ RETURNS boolean AS $$
GD[cache_key] = obs_config
return True
$$ LANGUAGE plpythonu SECURITY DEFINER STABLE PARALLEL RESTRICTED;
CREATE OR REPLACE FUNCTION cdb_dataservices_server._cdb_mapbox_apikey(
username TEXT,
orgname TEXT,
service TEXT)
RETURNS TEXT AS $$
import json
from cartodb_services.mapbox.types import MAPBOX_ROUTING_APIKEY_ROUNDRROBIN, MAPBOX_GEOCODER_APIKEY_ROUNDRROBIN, MAPBOX_ISOLINES_APIKEY_ROUNDRROBIN
if service == 'routing':
round_robin_service = MAPBOX_ROUTING_APIKEY_ROUNDRROBIN
api_keys = GD["user_routing_config_{0}".format(username)].mapbox_api_keys
elif service == 'geocoder':
round_robin_service = MAPBOX_GEOCODER_APIKEY_ROUNDRROBIN
api_keys = GD["user_geocoder_config_{0}".format(username)].mapbox_api_keys
elif service == 'isolines':
round_robin_service = MAPBOX_ISOLINES_APIKEY_ROUNDRROBIN
api_keys = GD["user_isolines_routing_config_{0}".format(username)].mapbox_matrix_api_keys
else:
return None
round_robin = GD[round_robin_service] if round_robin_service in GD else 0
api_key = api_keys[round_robin]
GD[round_robin_service] = round_robin + 1 if round_robin < len(api_keys) - 1 else 0
return api_key
$$ LANGUAGE plpythonu SECURITY DEFINER STABLE PARALLEL RESTRICTED;
+4 -1
View File
@@ -202,9 +202,12 @@ RETURNS Geometry AS $$
if not quota_service.check_user_quota():
raise Exception('You have reached the limit of your quota')
mapbox_apikey_query = plpy.prepare("SELECT cdb_dataservices_server._cdb_mapbox_apikey($1, $2, $3) as apikey;", ["text", "text", "text"])
mapbox_apikey = plpy.execute(mapbox_apikey_query, [username, orgname, 'geocoder'])
with metrics('cdb_mapbox_geocode_street_point', user_geocoder_config, logger):
try:
geocoder = MapboxGeocoder(user_geocoder_config.mapbox_api_key, logger, user_geocoder_config.mapbox_service_params)
geocoder = MapboxGeocoder(mapbox_apikey[0]['apikey'], logger, user_geocoder_config.mapbox_service_params)
country_iso3 = None
if country:
+4 -1
View File
@@ -77,9 +77,12 @@ RETURNS Geometry AS $$
if not quota_service.check_user_quota():
raise Exception('You have reached the limit of your quota')
mapbox_apikey_query = plpy.prepare("SELECT cdb_dataservices_server._cdb_mapbox_apikey($1, $2, $3) as apikey;", ["text", "text", "text"])
mapbox_apikey = plpy.execute(mapbox_apikey_query, [username, orgname, 'geocoder'])
with metrics('cdb_geocode_namedplace_point', user_geocoder_config, logger):
try:
geocoder = MapboxGeocoder(user_geocoder_config.mapbox_api_key, logger)
geocoder = MapboxGeocoder(mapbox_apikey[0]['apikey'], logger)
country_iso3 = None
if country_name:
+8 -2
View File
@@ -148,8 +148,11 @@ RETURNS SETOF cdb_dataservices_server.isoline AS $$
if not quota_service.check_user_quota():
raise Exception('You have reached the limit of your quota')
mapbox_apikey_query = plpy.prepare("SELECT cdb_dataservices_server._cdb_mapbox_apikey($1, $2, $3) as apikey;", ["text", "text", "text"])
mapbox_apikey = plpy.execute(mapbox_apikey_query, [username, orgname, 'isolines'])
try:
client = MapboxMatrixClient(user_isolines_routing_config.mapbox_matrix_api_key, logger, user_isolines_routing_config.mapbox_matrix_service_params)
client = MapboxMatrixClient(mapbox_apikey[0]['apikey'], logger, user_isolines_routing_config.mapbox_matrix_service_params)
mapbox_isolines = MapboxIsolines(client, logger)
if source:
@@ -283,8 +286,11 @@ RETURNS SETOF cdb_dataservices_server.isoline AS $$
if not quota_service.check_user_quota():
raise Exception('You have reached the limit of your quota')
mapbox_apikey_query = plpy.prepare("SELECT cdb_dataservices_server._cdb_mapbox_apikey($1, $2, $3) as apikey;", ["text", "text", "text"])
mapbox_apikey = plpy.execute(mapbox_apikey_query, [username, orgname, 'isolines'])
try:
client = MapboxMatrixClient(user_isolines_routing_config.mapbox_matrix_api_key, logger, user_isolines_routing_config.mapbox_matrix_service_params)
client = MapboxMatrixClient(mapbox_apikey[0]['apikey'], logger, user_isolines_routing_config.mapbox_matrix_service_params)
mapbox_isolines = MapboxIsolines(client, logger)
if source:
@@ -31,7 +31,7 @@ SELECT cartodb.cdb_conf_setconf('mapzen_conf', '{"routing": {"api_key": "routing
(1 row)
SELECT cartodb.cdb_conf_setconf('mapbox_conf', '{"routing": {"api_key": "routing_dummy_api_key", "monthly_quota": 1500000}, "geocoder": {"api_key": "geocoder_dummy_api_key", "monthly_quota": 1500000}, "matrix": {"api_key": "matrix_dummy_api_key", "monthly_quota": 1500000}}');
SELECT cartodb.cdb_conf_setconf('mapbox_conf', '{"routing": {"api_keys": ["routing_dummy_api_key"], "monthly_quota": 1500000}, "geocoder": {"api_keys": ["geocoder_dummy_api_key"], "monthly_quota": 1500000}, "matrix": {"api_keys": ["matrix_dummy_api_key"], "monthly_quota": 1500000}}');
cdb_conf_setconf
------------------
@@ -13,7 +13,7 @@ SELECT cartodb.cdb_conf_setconf('redis_metrics_config', '{"redis_host": "localho
SELECT cartodb.cdb_conf_setconf('redis_metadata_config', '{"redis_host": "localhost", "redis_port": 6379, "timeout": 0.1, "redis_db": 5}');
SELECT cartodb.cdb_conf_setconf('heremaps_conf', '{"geocoder": {"app_id": "dummy_id", "app_code": "dummy_code", "geocoder_cost_per_hit": 1}, "isolines": {"app_id": "dummy_id", "app_code": "dummy_code"}}');
SELECT cartodb.cdb_conf_setconf('mapzen_conf', '{"routing": {"api_key": "routing_dummy_api_key", "monthly_quota": 1500000}, "geocoder": {"api_key": "geocoder_dummy_api_key", "monthly_quota": 1500000}, "matrix": {"api_key": "matrix_dummy_api_key", "monthly_quota": 1500000}}');
SELECT cartodb.cdb_conf_setconf('mapbox_conf', '{"routing": {"api_key": "routing_dummy_api_key", "monthly_quota": 1500000}, "geocoder": {"api_key": "geocoder_dummy_api_key", "monthly_quota": 1500000}, "matrix": {"api_key": "matrix_dummy_api_key", "monthly_quota": 1500000}}');
SELECT cartodb.cdb_conf_setconf('mapbox_conf', '{"routing": {"api_keys": ["routing_dummy_api_key"], "monthly_quota": 1500000}, "geocoder": {"api_keys": ["geocoder_dummy_api_key"], "monthly_quota": 1500000}, "matrix": {"api_keys": ["matrix_dummy_api_key"], "monthly_quota": 1500000}}');
SELECT cartodb.cdb_conf_setconf('logger_conf', '{"geocoder_log_path": "/dev/null"}');
SELECT cartodb.cdb_conf_setconf('data_observatory_conf', '{"connection": {"whitelist": ["ethervoid"], "production": "host=localhost port=5432 dbname=contrib_regression user=geocoder_api", "staging": "host=localhost port=5432 dbname=dataservices_db user=geocoder_api"}, "monthly_quota": 100000}');