Skip to content
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

Prototype REST implementation #183

Merged
merged 34 commits into from
Jan 31, 2022
Merged

Conversation

giffels
Copy link
Member

@giffels giffels commented Apr 9, 2021

This pull request implements a prototype REST implementation to query the SqlRegistry. Currently two read-only API calls (GET /resources/state, GET /resources ) are supported. The REST API implementation uses FastApi and uvicorn as ASGI server. The REST API relies on JSON Web Token and OAuth2 scopes to support authentication and authorization.

Could be useful to avoid #172.

To activate the TARDIS REST service, simply add the following lines to your TARDISconfiguration.

Services:
  restapi:
      !TardisRestApi
      host: 127.0.0.1
      port: 1234
      secret_key: 752e003f636f402cc23728e185ce8c9eef27b7e02cf509b3015f7757e625b8e4
      algorithm: HS256
      users:
         - user_name: tardis
           hashed_password: $2b$12$c9SSllh1U6tOhIo37sDWF.kdRIU5RQAAOHL9bVYMs2.HluyFE43Uq
           scopes:
             - user:read

to enable logging I suggest to use the following logging configuration in your COBalD configuration.

logging:
  version: 1
  loggers:
    root:
      level: DEBUG
      handlers: [console, file]
    uvicorn:
      level: INFO
      handlers: [ uvicorn_file ]
      propagate: False
  formatters:
    precise:
      format: '%(name)s: %(asctime)s %(message)s'
      datefmt: '%Y-%m-%d %H:%M:%S'
  handlers:
    console:
      class : logging.StreamHandler
      formatter: precise
      stream  : ext://sys.stdout
    file:
      class : logging.handlers.RotatingFileHandler
      formatter: precise
      filename: tardis.log
      maxBytes: 10485760
      backupCount: 3
    uvicorn_file:
      class: logging.handlers.RotatingFileHandler
      formatter: precise
      filename: uvicorn.log
      maxBytes: 10485760
      backupCount: 3

Benchmarks:

Running one million requests against this service took about 11 minutes. Which is equivalent to a performance of about 1500 requests/s.

@giffels giffels added the enhancement New feature or request label Apr 9, 2021
@giffels giffels self-assigned this Apr 9, 2021
@giffels giffels added this to the 0.7.0 - Release milestone Aug 10, 2021
@giffels giffels force-pushed the add-rest-api branch 2 times, most recently from c3bedf9 to 301ffcf Compare August 24, 2021 16:18
@codecov-commenter
Copy link

codecov-commenter commented Aug 24, 2021

Codecov Report

Merging #183 (b8721c9) into master (d66c293) will decrease coverage by 0.05%.
The diff coverage is 98.96%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #183      +/-   ##
==========================================
- Coverage   99.56%   99.50%   -0.06%     
==========================================
  Files          41       53      +12     
  Lines        1823     2022     +199     
==========================================
+ Hits         1815     2012     +197     
- Misses          8       10       +2     
Impacted Files Coverage Δ
tardis/rest/hash_credentials/__main__.py 83.33% <83.33%> (ø)
tardis/rest/token_generator/__main__.py 83.33% <83.33%> (ø)
tardis/plugins/sqliteregistry.py 100.00% <100.00%> (ø)
tardis/rest/app/crud.py 100.00% <100.00%> (ø)
tardis/rest/app/database.py 100.00% <100.00%> (ø)
tardis/rest/app/main.py 100.00% <100.00%> (ø)
tardis/rest/app/routers/login.py 100.00% <100.00%> (ø)
tardis/rest/app/routers/resources.py 100.00% <100.00%> (ø)
tardis/rest/app/security.py 100.00% <100.00%> (ø)
tardis/rest/hash_credentials/hash_credentials.py 100.00% <100.00%> (ø)
... and 4 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d66c293...b8721c9. Read the comment docs.

@maxfischer2781
Copy link
Member

I've just pushed out cobald v0.12.3 that includes the required eager evaluation of YAML tags.

@giffels
Copy link
Member Author

giffels commented Nov 12, 2021

I've just pushed out cobald v0.12.3 that includes the required eager evaluation of YAML tags.

Thanks a lot.

@giffels giffels dismissed stale reviews from RHofsaess and mschnepf via ee18261 January 11, 2022 11:26
@giffels
Copy link
Member Author

giffels commented Jan 11, 2022

I have now also changed the security scopes to better match the granted permissions. We now have

  • resources:get instead of user:read
  • resources:put instead of user:write (not used yet)

The idea is that this can be extended by further scopes like resources:delete, configuration:get, configuration.put, etc.

Copy link
Member

@mschnepf mschnepf left a comment

Choose a reason for hiding this comment

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

I have one comment, but nothing critical

docs/source/services/services.rst Show resolved Hide resolved
@giffels giffels requested a review from mschnepf January 11, 2022 17:02
RHofsaess
RHofsaess previously approved these changes Jan 13, 2022
Copy link

@RHofsaess RHofsaess left a comment

Choose a reason for hiding this comment

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

👍

mschnepf
mschnepf previously approved these changes Jan 13, 2022
Copy link
Member

@mschnepf mschnepf left a comment

Choose a reason for hiding this comment

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

Thanks for all your work. It looks really good 👍

Copy link
Member

@eileen-kuehn eileen-kuehn left a comment

Choose a reason for hiding this comment

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

Great stuff 👍
I mainly left some comments on how docstrings might be improved. So fixing should be a no-brainer 😸

docs/source/services/services.rst Outdated Show resolved Hide resolved
docs/source/services/services.rst Outdated Show resolved Hide resolved
docs/source/services/services.rst Outdated Show resolved Hide resolved
Comment on lines 141 to 142
both access and error logs into a separate file. Therefore, an ``uvicorn`` logger needs to be configured. An example
can be found below.
Copy link
Member

Choose a reason for hiding this comment

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

Can we actually ensure that the example is rendered below? Maybe it is better to state something like "See the example logging configuration.".

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks, I thing this is a better.

docs/source/services/services.rst Outdated Show resolved Hide resolved
docs/source/services/services.rst Outdated Show resolved Hide resolved
tardis/rest/service.py Show resolved Hide resolved
return self._secret_key

@lru_cache(maxsize=16)
def get_user(self, user_name: str) -> [None, UserCredentials]:
Copy link
Member

Choose a reason for hiding this comment

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

Do you mind checking the return type? I would expect Optional[UserCredentials] from just reading the code below.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks, I have changed it to Optional[UserCredentials].

tardis/rest/token_generator/generate_token.py Outdated Show resolved Hide resolved
tardis/rest/token_generator/generate_token.py Outdated Show resolved Hide resolved
Co-authored-by: Eileen Kuehn <eileen.kuehn@kit.edu>
@giffels giffels dismissed stale reviews from mschnepf and RHofsaess via 5078f5f January 28, 2022 09:54
@giffels
Copy link
Member Author

giffels commented Jan 28, 2022

Thank you very much @eileen-kuehn for improving documentation and code.

Copy link
Member

@mschnepf mschnepf left a comment

Choose a reason for hiding this comment

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

Looks good to me 👍

Copy link
Member

@eileen-kuehn eileen-kuehn left a comment

Choose a reason for hiding this comment

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

Great stuff, looks good to go 🥇

@giffels giffels merged commit f978b19 into MatterMiners:master Jan 31, 2022
@giffels giffels deleted the add-rest-api branch January 31, 2022 11:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants