Skip to content

Commit

Permalink
[AUD-377] Add ip_check route (#1316)
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondjacobson committed Mar 17, 2021
1 parent bd9fb33 commit 25f4045
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
5 changes: 5 additions & 0 deletions discovery-provider/src/queries/health_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,8 @@ def ipld_block_check():
return success_response(
{"db":{"number": latest_ipld_indexed_block, "blockhash": latest_indexed_ipld_block_hash}}
)

@bp.route("/ip_check", methods=["GET"])
def ip_check():
ip = helpers.get_ip(request)
return success_response(ip, sign_response=False)
8 changes: 6 additions & 2 deletions discovery-provider/src/utils/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
from src import exceptions
from . import multihash

def get_ip(request_obj):
"""Gets the IP address from a request using the X-Forwarded-For header if present"""
ip = request_obj.headers.get('X-Forwarded-For', request_obj.remote_addr)
return ip.split(',')[0].strip()

def redis_restore(redis, key):
logger = logging.getLogger(__name__)
try:
Expand Down Expand Up @@ -179,8 +184,7 @@ def log_request(response): # pylint: disable=W0612

now = time.time()
duration = int((now - g.start) * 1000)
ip = request.headers.get('X-Forwarded-For', request.remote_addr)
ip = ip.split(',')[0].strip()
ip = get_ip(request)
host = request.host.split(':', 1)[0]
args = request.query_string.decode("utf-8")

Expand Down
7 changes: 2 additions & 5 deletions discovery-provider/src/utils/redis_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import redis
from flask.globals import request
from src.utils.config import shared_config
from src.utils.helpers import redis_set_and_dump, redis_get_or_restore
from src.utils.helpers import get_ip, redis_set_and_dump, redis_get_or_restore
from src.utils.query_params import stringify_query_params, app_name_param
from src.models import AggregateDailyUniqueUsersMetrics, AggregateDailyTotalUsersMetrics, \
AggregateMonthlyUniqueUsersMetrics, AggregateMonthlyTotalUsersMetrics, \
Expand Down Expand Up @@ -52,10 +52,7 @@ def format_ip(ip):
return ip.strip().replace(":", "_")

def get_request_ip(request_obj):
header_ips = request_obj.headers.get('X-Forwarded-For', request_obj.remote_addr)
# Use the left most IP, representing the user's IP
first_ip = header_ips.split(',')[0]
return format_ip(first_ip)
return format_ip(get_ip(request_obj))

def parse_metrics_key(key):
"""
Expand Down

0 comments on commit 25f4045

Please sign in to comment.