Skip to content

Api key #1011

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 92 commits into from
Mar 29, 2023
Merged

Api key #1011

Changes from 1 commit
Commits
Show all changes
92 commits
Select commit Hold shift + click to select a range
c7b0120
Cleanup unused imports && added role definition and security
Oct 18, 2022
a700ef2
Simple api role checking
dmytrotsko Oct 18, 2022
75509cb
Render api key warnings
dmytrotsko Oct 18, 2022
a796e2c
Move tests
dmytrotsko Oct 18, 2022
8d48b20
Add api user ddl
dmytrotsko Oct 18, 2022
c753f73
tests
dmytrotsko Oct 18, 2022
a15a36e
improve dockerignore
dmytrotsko Oct 18, 2022
4fc210d
support api date via env
dmytrotsko Oct 18, 2022
ee0c087
start with admin interface
dmytrotsko Oct 18, 2022
0446ea9
start with simple admin interface
dmytrotsko Oct 18, 2022
c7ca927
very basic admin interface
dmytrotsko Oct 18, 2022
d066a90
list all known roles
dmytrotsko Oct 18, 2022
8b09387
remove log statement
dmytrotsko Oct 18, 2022
e940f8b
fixup tests
dmytrotsko Oct 18, 2022
8bb2789
add record flag to user
dmytrotsko Oct 18, 2022
3161d2e
basic record based on flag logging
dmytrotsko Oct 18, 2022
9ab8978
rename column to tracking
dmytrotsko Oct 18, 2022
54dc83c
add webhook endpoint for google forms and simplify table
dmytrotsko Oct 18, 2022
62122cf
tests for simplified api_user
dmytrotsko Oct 18, 2022
32096ac
prepare for sending mails with mailgun
dmytrotsko Oct 18, 2022
45414bc
disable security check for test setup
dmytrotsko Oct 18, 2022
60f7cd6
fix tests
dmytrotsko Oct 18, 2022
dac3fcb
add registered flag
dmytrotsko Oct 18, 2022
d880795
request a simple api key form with recaptcha check
dmytrotsko Oct 18, 2022
e76cde4
implement rate limiting
dmytrotsko Oct 18, 2022
f1f008e
registered flag not saved
dmytrotsko Oct 18, 2022
98315c6
mark all tests no rate limit
dmytrotsko Oct 18, 2022
f897e28
add link to registration form within api key form
dmytrotsko Oct 18, 2022
b345f76
elevate sqlalchemy log level to ERROR for test purpose
dmytrotsko Oct 18, 2022
5ce96bd
add structlog to requirements
dmytrotsko Oct 18, 2022
f851d5b
add structured logging module
dmytrotsko Oct 18, 2022
38abd83
implement api key scrubbing logs and getter functions for User class
dmytrotsko Oct 18, 2022
d65acb8
minor commenting change
dmytrotsko Oct 18, 2022
5aee08f
change logging to kward format and add a get function for API key
dmytrotsko Oct 18, 2022
7378e8d
move mask_apikey function out of User class
dmytrotsko Oct 18, 2022
02a535b
minor change in variable name
dmytrotsko Oct 18, 2022
c18a122
Fixed test
dmytrotsko Oct 18, 2022
da947fd
added missing import
dmytrotsko Oct 18, 2022
4732467
Fixed _is_public_route method. Added URL_PREFIX to list of routes
dmytrotsko Oct 24, 2022
ae1e6a1
Merge pull request #2 from dmytrotsko/fix_admin_endpoint_access
dmytrotsko Oct 24, 2022
16a154c
Added email column to User table
dmytrotsko Oct 25, 2022
4b5fa65
Merge branch 'api_key' of github.com:dmytrotsko/delphi-epidata into a…
dmytrotsko Oct 25, 2022
a8011fd
Fixed tests
dmytrotsko Oct 25, 2022
6775384
Added Dockerfile for Redis
dmytrotsko Oct 25, 2022
7e11435
Made email optional
dmytrotsko Oct 25, 2022
74fd290
Added email address validaiton
dmytrotsko Oct 26, 2022
8d496ba
Fixed API key creation via /create_key endpoint
dmytrotsko Oct 27, 2022
3bc7095
Fixed /register endpoint after google script refactoring
dmytrotsko Oct 27, 2022
e39c82a
Fixed registration
dmytrotsko Oct 28, 2022
be80550
Reverted some changes in _config.py
dmytrotsko Oct 28, 2022
8f21122
Reverted changes in .env.example
dmytrotsko Nov 2, 2022
71bb186
Code refactoring
dmytrotsko Nov 2, 2022
aea77eb
Removed unused variables
dmytrotsko Nov 2, 2022
b7a6078
Code cleaning
dmytrotsko Nov 2, 2022
635be31
Removed unused variables
dmytrotsko Nov 3, 2022
93b056e
Removed default redis configuration file (it is not needed for now)
dmytrotsko Nov 3, 2022
df11183
Moved BASE_URL and auth to setUP, formatted code
dmytrotsko Nov 3, 2022
b63faa3
Moved BASE_URL and auth to setUP(), formatted code
dmytrotsko Nov 3, 2022
9ed758b
Revert "Moved BASE_URL and auth to setUP(), formatted code"
dmytrotsko Nov 3, 2022
02083a0
Formatted code & removed unused imports.
dmytrotsko Nov 4, 2022
99f75a6
Created staticmethod _make_rqeuest() and reused it in test_caching() …
dmytrotsko Nov 7, 2022
f94ec4d
Returned BASE_URL and auth back, because if we use default BASE_URL f…
dmytrotsko Nov 7, 2022
0480172
Completely removed patch_flask_config() function as it is useless. Th…
dmytrotsko Nov 7, 2022
3586bcf
Removed duplicated _logger.py file (this is exact copy of src/acquisi…
dmytrotsko Nov 7, 2022
1ee16f7
Fixed test
dmytrotsko Nov 8, 2022
7ce411b
Removed unused method
dmytrotsko Nov 8, 2022
68240ca
Added missing changes in Dockerfiles for copying logger
dmytrotsko Nov 8, 2022
d350347
Fixed misspelled filename
dmytrotsko Nov 9, 2022
4bca6b4
Added missing variable in CSVPrinter
dmytrotsko Nov 9, 2022
3cf15da
Formatted code
dmytrotsko Nov 9, 2022
ad00db5
Removed tests for admin endpoint for now
dmytrotsko Nov 9, 2022
4285b87
Moved config variables from _security to _config. Removed duplicated …
dmytrotsko Nov 9, 2022
ec67149
Added missing changes
dmytrotsko Nov 15, 2022
7a364d9
Replaced db.execute delete and update with built-it sqlalchemy functi…
dmytrotsko Nov 15, 2022
e4ae70c
Removed email field. Edited google form link to prefill tracking cons…
dmytrotsko Nov 15, 2022
c21689d
Removed api_user ddl
dmytrotsko Nov 24, 2022
1ebff22
Changed user roles to use m2m table instead of string in api_user table
dmytrotsko Nov 28, 2022
c49c1f8
Added new variable for api key expiration date
dmytrotsko Nov 28, 2022
c54412c
Updated admin endpoint. Removed email from user details and added cre…
dmytrotsko Nov 28, 2022
6c50703
Updated integration tests
dmytrotsko Nov 28, 2022
3e34e3c
Updated endpoints to register user role on endpoint load
dmytrotsko Nov 28, 2022
8fbae7d
Removed user_role FK in api_user table.
dmytrotsko Nov 30, 2022
95507c2
Updated according to new structure
dmytrotsko Nov 30, 2022
597eeb4
Removed API_KEY_EXPIRE_AFTER variable
dmytrotsko Nov 30, 2022
08bd2b6
Removed creation_date, expiration_date and last_api_access_date from …
dmytrotsko Nov 30, 2022
c436b0a
Added roles registraion on endpoints load
dmytrotsko Nov 30, 2022
199d434
Removed role registration statement
dmytrotsko Nov 30, 2022
4422a5e
Fixed integration tests. Actually reverted latest changes as api_user…
dmytrotsko Nov 30, 2022
806d6e4
Added new ddls
dmytrotsko Nov 30, 2022
82a0e25
Merge branch 'dev' into api_key
dmytrotsko Nov 30, 2022
58f715d
Fixed wrong import
dmytrotsko Dec 1, 2022
73efe70
Fixed issue after resolving conflicts
dmytrotsko Dec 1, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add structured logging module
  • Loading branch information
dmytrotsko committed Oct 18, 2022
commit f851d5bdd1cb2bb938a35e98a9051222ce66fdab
94 changes: 94 additions & 0 deletions src/server/_logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
"""Structured logger utility for creating JSON logs in Delphi pipelines."""
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wtf, this is an exact duplicate of src/acquisition/covidcast/logger.py ! please delete it. this copy seems to only be used by _security.py and ill make a note there too.

Copy link
Contributor Author

@dmytrotsko dmytrotsko Nov 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, it is an exact duplicate of that src/acquisition/covidcast/logger.py as we have only src/server in delphi_web_epidata container so we don't have access to that existing logger. And this duplicate is for logging user's api usage inside that container.
Can we use app.logger? - Yep, but then we will need to parse somehow that app logs to separate logs.
Possible solutions:

  1. Leave as it is (we will have duplicated files in project)
  2. Add 1 line to Dockerfile to copy src/acquisition/covidcast/logger.py to delphi_web_epdiata (the most efficient solution)
  3. Implement custom logging level and handler to collect api usage logs (will take more time than No2, not efficient, needs additional tests)

Copy link
Contributor Author

@dmytrotsko dmytrotsko Nov 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UPD:
Need to add COPY logger to devops/Dockerfile and dev/ docker/python/Dockerfile because delphi_web_python also needs logger.py in order to run tests.
But from that we have one issue :)
After that we are not able to use --mount option to mount src/server to delphi_web_epidata because it will replace all the files inside /app/app folder in container logger.py file will not be available.

import logging
import sys
import threading
import structlog


def handle_exceptions(logger):
"""Handle exceptions using the provided logger."""
def exception_handler(etype, value, traceback):
logger.exception("Top-level exception occurred",
exc_info=(etype, value, traceback))

def multithread_exception_handler(args):
exception_handler(args.exc_type, args.exc_value, args.exc_traceback)

sys.excepthook = exception_handler
threading.excepthook = multithread_exception_handler


def get_structured_logger(name=__name__,
filename=None,
log_exceptions=True):
"""Create a new structlog logger.

Use the logger returned from this in indicator code using the standard
wrapper calls, e.g.:

logger = get_structured_logger(__name__)
logger.warning("Error", type="Signal too low").

The output will be rendered as JSON which can easily be consumed by logs
processors.

See the structlog documentation for details.

Parameters
---------
name: Name to use for logger (included in log lines), __name__ from caller
is a good choice.
filename: An (optional) file to write log output.
"""
# Configure the underlying logging configuration
handlers = [logging.StreamHandler()]
if filename:
handlers.append(logging.FileHandler(filename))

logging.basicConfig(
format="%(message)s",
level=logging.INFO,
handlers=handlers
)



# Configure structlog. This uses many of the standard suggestions from
# the structlog documentation.
structlog.configure(
processors=[
# Filter out log levels we are not tracking.
structlog.stdlib.filter_by_level,
# Include logger name in output.
structlog.stdlib.add_logger_name,
# Include log level in output.
structlog.stdlib.add_log_level,
# Allow formatting into arguments e.g., logger.info("Hello, %s",
# name)
structlog.stdlib.PositionalArgumentsFormatter(),
# Add timestamps.
structlog.processors.TimeStamper(fmt="iso"),
# Match support for exception logging in the standard logger.
structlog.processors.StackInfoRenderer(),
structlog.processors.format_exc_info,
# Decode unicode characters
structlog.processors.UnicodeDecoder(),
# Render as JSON
structlog.processors.JSONRenderer()
],
# Use a dict class for keeping track of data.
context_class=dict,
# Use a standard logger for the actual log call.
logger_factory=structlog.stdlib.LoggerFactory(),
# Use a standard wrapper class for utilities like log.warning()
wrapper_class=structlog.stdlib.BoundLogger,
# Cache the logger
cache_logger_on_first_use=True,
)

logger = structlog.get_logger(name)

if log_exceptions:
handle_exceptions(logger)

return logger