Add query endpoint for trusted users#97
Open
MialLewis wants to merge 7 commits into
Open
Conversation
c19a8df to
2105678
Compare
758239d to
719ed0c
Compare
b5b3818 to
4bb05c5
Compare
for more information, see https://pre-commit.ci
warunawickramasingha
left a comment
Contributor
There was a problem hiding this comment.
Proposed a few suggestions to improve the code, style and better handle exceptions + rate limiting rule at nginx level. Please check and apply as necessary.
| def verify_token(request) -> bool: | ||
| token = get_bearer_token(request) | ||
| secret = environ.get("QUERY_SECRET_KEY", "") | ||
| return token == secret |
Contributor
There was a problem hiding this comment.
Not a huge concern in this context. But for secret validations, it is recommended to use something like compare_digest instead of == to avoid timing attacks.
Suggested change
| return token == secret | |
| from hmac import compare_digest | |
| return compare_digest(token, secret) |
| # Create your views here. | ||
| from django.views.decorators.cache import cache_page | ||
| from services.models import Message, Usage, FeatureUsage, Location | ||
| from rest_framework import response, viewsets |
Contributor
There was a problem hiding this comment.
Suggested change
| from rest_framework import response, viewsets, status |
| @api_view(("POST",)) | ||
| def query(request, format=None): | ||
| if not verify_token(request): | ||
| return response.Response(status=401, data="UNAUTHORIZED") |
Contributor
There was a problem hiding this comment.
For improved readability
Suggested change
| return response.Response(status=401, data="UNAUTHORIZED") | |
| return response.Response(status=status.HTTP_401_UNAUTHORIZED, data="UNAUTHORIZED") |
Comment on lines
+339
to
+345
| if sql_err: | ||
| return response.Response(status=400, data=f"Invalid Parameters: {sql_err}") | ||
| conn = connections["readonly"] | ||
| with conn.cursor() as cur: | ||
| cur.execute(sql) | ||
| res = cur.fetchall() | ||
| return response.Response(res) |
Contributor
There was a problem hiding this comment.
Exception handling for db operations
Suggested change
| if sql_err: | |
| return response.Response(status=400, data=f"Invalid Parameters: {sql_err}") | |
| conn = connections["readonly"] | |
| with conn.cursor() as cur: | |
| cur.execute(sql) | |
| res = cur.fetchall() | |
| return response.Response(res) | |
| if sql_err: | |
| return response.Response(status=status.HTTP_400_BAD_REQUEST, data=f"Invalid Parameters: {sql_err}") | |
| try: | |
| conn = connections["readonly"] | |
| with conn.cursor() as cur: | |
| cur.execute(sql) | |
| res = cur.fetchall() | |
| except Exception: | |
| return Response({"error": "Query failed"}, status=status.HTTP_400_BAD_REQUEST) | |
| return response.Response(res) |
| err = "" | ||
| if not val: | ||
| err = f"No {param} parameter provided" | ||
| return err, val |
Contributor
There was a problem hiding this comment.
To work better with --data-urlencode "sql="
Suggested change
| return err, val | |
| def get_parameter(request, param): | |
| val = request.POST.get(param) | |
| if val is None or val.strip() == "": | |
| return f"No {param} parameter provided", None | |
| return None, val |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR adds an endpoint that allows trusted users to query the report database.
To test (contact me for the token, it's on keeper):
Error on write:
Read action:
Closes #95