Compare commits

..

363 Commits

Author SHA1 Message Date
maxime c808ba21ab
[fix] oubli import 2024-05-28 03:40:29 +02:00
maxime 9f908d3cee
[release] halfapi 0.6.30 2024-05-28 02:55:17 +02:00
maxime 7b6d9e994a
[deps] starlette v0.37 2024-05-28 02:54:50 +02:00
maxime 8f6330bca7
[deps] starlette 0.35 2024-05-28 02:53:58 +02:00
maxime 8506aa5322
[middleware] fix DomainMiddleware constructor for 0.35 2024-05-28 02:41:41 +02:00
maxime b683e80959
[halfapi] remove on_startup argument according to starlette 0.26 2024-05-28 02:41:19 +02:00
maxime b412d249a1
[deps] bump dependencies versions 2024-05-28 02:39:08 +02:00
Maxime Alves LIRMM f4f9a0fc66 [dockerfile] alpine 3.19 - utilisation d'un venv 2023-12-13 11:37:29 +01:00
Maxime Alves LIRMM c855cce013 [release] 0.6.29 2023-12-13 11:09:42 +01:00
Maxime Alves LIRMM e083c4386e [release] 0.6.28 2023-11-24 14:31:10 +01:00
Maxime Alves LIRMM 476ae29792 [rc] 0.6.28rc5 2023-09-01 04:59:17 +09:00
Maxime Alves LIRMM 673097adeb [half_domain] fix ACL route when checked ACL is a decorator 2023-09-01 04:58:12 +09:00
Maxime Alves LIRMM 1cc1bbd5ef [rc] 0.6.28rc4 (remove rc3) 2023-08-21 08:47:47 +02:00
Maxime Alves LIRMM 135d6e86e4 [conf] fix export SECRET variable 2023-08-21 08:47:19 +02:00
Maxime Alves LIRMM 0fcf433ec6 [ci] use alpine base image directly 2023-08-21 00:08:29 +02:00
Maxime Alves LIRMM 45cf32de2b [ci] utilisation de l'image 3.17 (pour fix le bug exceptiongroup) 2023-08-20 23:53:18 +02:00
Maxime Alves LIRMM 1b713c3816 [ci] fix image pour les tests <3.11 2023-08-20 23:42:10 +02:00
Maxime Alves LIRMM 59889e1e31 [ci] pipenv --skip-lock (has been remove) 2023-08-20 23:34:34 +02:00
Maxime Alves LIRMM 28a1a69435 [rc] 0.6.28rc3 - fix bugs and general configuration management cleanup (see changelog) 2023-08-20 23:32:50 +02:00
maxime 65ecf9817c [ci/Dockerfile] use of alpine OS instead of debian, bump python to version 3.11 2023-08-09 14:46:52 +02:00
maxime 3b7d3bda5c [rc] 0.6.28rc2 2023-08-09 14:24:32 +02:00
maxime e19f27f306 [deps] starlette 0.31 2023-08-09 14:23:09 +02:00
maxime e9c84c9f7c [rc] 0.6.28-rc1 2023-08-08 09:09:16 +02:00
maxime b1595beb14 [tests] write a schema component for dummy_domain for example 2023-08-02 13:29:57 +02:00
maxime 60ff99d0fb [domain] you can specify Schema components under the "schema_components" key of the domain dictionary 2023-08-02 12:55:04 +02:00
maxime 9657f0f9ec [ci-cd] add python 3.11 to testing releases 2023-08-02 10:57:33 +02:00
maxime f646b4d663 [ci-cd] coverage report 2023-08-02 10:49:54 +02:00
maxime 0817882558 [dev-deps] ajout de la dépendance a coverage 2023-08-02 10:43:50 +02:00
maxime 896ce58731 [pytest] do not avoid to run doctest modules in halfapi sources! 2023-08-02 10:39:06 +02:00
maxime 87856cfb42 [cli-conf] halfapi domain : the file provided as argument is a toml file of the format of .halfapi/config, + better config handling 2023-08-02 10:38:36 +02:00
maxime 4856f80b99 [rc] 0.6.28-rc0-1 2023-08-02 06:32:26 +02:00
maxime eac602f0a5 [rc] 0.6.28rc0 2023-08-01 20:50:58 +02:00
maxime 14e051bd91 [doc-schema] the "/" route on a domain now returns the OpenAPI-validated Schema (not a list of schemas), the "dummy_domain" test now validates OpenAPI specs 2023-08-01 20:31:17 +02:00
maxime 20563081f5 [doc-schema] In module-based routers, if there is a path parameter, you can specify an OpenAPI documentation for it, or a default will be used 2023-08-01 20:24:24 +02:00
maxime 7949b3206c [dev-deps] openapi-schema-validator, openapi-spec-validator 2023-08-01 19:41:49 +02:00
maxime c4583b7187 [doc] add docstrings for halfapi routes 2023-08-01 17:43:59 +02:00
maxime 2413436104 [acl] The public acls check routes use the "HEAD" method, deprecated "GET" 2023-08-01 17:32:25 +02:00
Maxime Alves LIRMM 54cc6c17c9 [release] 0.6.27 2023-06-07 11:46:53 +02:00
Maxime Alves LIRMM ff3a39c740 [rc] 0.6.27rc0 2023-06-01 15:39:57 +02:00
Maxime Alves LIRMM@home 8d254bafa0 [feature] changes in the ACLs result availability 2023-06-01 15:39:44 +02:00
Maxime Alves LIRMM 0a385661b9 [rc] 0.6.26rc0 2023-02-23 11:44:57 +01:00
Maxime Alves LIRMM e065fe04e4 [tests] test with multiple optional parameteres 2023-02-21 19:30:59 +01:00
Maxime Alves LIRMM b7c5704c95 [tests] fix tests so the data is sent in json 2023-02-21 19:23:23 +01:00
Maxime Alves LIRMM dd83a337e9 [lib.domain] route_decorator : Adds the "base_url", "cookies" and "url" to the "halfapi" argument of route definitions 2023-02-21 19:22:57 +01:00
Maxime Alves LIRMM f6d08e8309 [release] 0.6.25 2023-02-20 12:18:38 +01:00
Maxime Alves LIRMM 262de901a8 [jwtMiddleware] on expired signature error, returns Nobody and do not raise an exception 2023-02-13 11:14:24 +01:00
Maxime Alves LIRMM e5c25ede1f [rc] 0.6.25-rc0 2023-02-08 12:54:35 +01:00
Maxime Alves LIRMM b4c37ea999 [release] 0.6.24 2023-02-03 14:22:04 +01:00
Maxime Alves LIRMM 5a7e51ae94 [jwtMiddleware] clean "is_fake_user_id" code 2023-02-03 14:20:01 +01:00
Maxime Alves LIRMM 69129fd7af [doc][release] 0.7.0 2023-02-03 12:43:30 +01:00
Maxime Alves LIRMM a3fc6dc830 [authMiddleware] UN-Breaking uses either the cookie or the header names "Authorization" 2023-02-03 12:43:16 +01:00
Maxime Alves LIRMM@home 064127dc16 [fix] tests for running by implementing the dry-run option to the domain command 2023-02-03 00:02:56 +01:00
Maxime Alves LIRMM@home c2eb95533c [fix] fix check_domain test c9ecc1f8d2 2023-02-02 22:33:35 +01:00
Maxime Alves LIRMM bbb027cd0d [authMiddleware][BREAKING] read token from "JWTToken" cookie 2023-02-02 19:56:59 +01:00
Maxime Alves LIRMM c9ecc1f8d2 [cli] allow to run halfapi on a specified domain using the "halfapi domain --run DOMAIN" command 2023-02-02 19:56:11 +01:00
Maxime Alves LIRMM d1a8351997 [halfapi] if there is only one domain it is automatically enabled 2023-02-02 19:55:37 +01:00
Maxime Alves LIRMM dc29abea84 [release] 0.6.23 2023-01-14 12:03:40 +01:00
Maxime Alves LIRMM 4c966e7151 [deps] toml bumps version 0.10 2023-01-14 11:43:38 +01:00
Maxime Alves LIRMM 7deb353b4b [deps] PyYAML bumps version 6.0 2023-01-14 11:39:40 +01:00
Maxime Alves LIRMM 3986083725 [deps] PyJWT bumps version 2.6.0 2023-01-14 11:37:08 +01:00
Maxime Alves LIRMM 301b0eeab5 [deps] click bump version 8 2023-01-14 11:29:14 +01:00
Maxime Alves LIRMM cc0566542b [deps] orjson bump version 3.8.5 2023-01-14 11:23:25 +01:00
Maxime Alves LIRMM 4f4dac0ff2 [deps] Migration from starlette v0.18 to v0.23
Breaking : migrate your tests that use the TestDomain.client method
following the instructions here https://github.com/Kludex/bump-testclient

Squashed commit of the following:

commit 0417f27b3f
Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr>
Date:   Sat Jan 14 11:08:44 2023 +0100

    [deps] starlette 0.23

commit 552f00a65b
Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr>
Date:   Sat Jan 14 10:59:42 2023 +0100

    [deps] starlette 0.22

commit aefe448717
Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr>
Date:   Sat Jan 14 10:55:45 2023 +0100

    [tests][fix] compares the json interpreted value instead of the string

commit 01333a200c
Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr>
Date:   Sat Jan 14 10:55:20 2023 +0100

    [testing] changes from requests to httpx for Starlette TestClient (breaks)

commit f3784fab7f
Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr>
Date:   Sat Jan 14 10:54:10 2023 +0100

    [deps][breaking] starlette 0.21

commit 717d3f8bd6
Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr>
Date:   Sat Jan 14 10:26:31 2023 +0100

    [responses] use a wrapper function for exception handling (fix starlette 0.20)

commit d0876e45da
Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr>
Date:   Sat Jan 14 10:25:21 2023 +0100

    [deps][breaking] starlette 0.20

commit 6504191c53
Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr>
Date:   Sat Jan 14 10:12:51 2023 +0100

    [deps] starlette 0.19

commit 7b639a8dc2
Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr>
Date:   Sat Jan 14 10:11:14 2023 +0100

    [deps] starlette 0.18

commit 20bd9077a4
Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr>
Date:   Sat Jan 14 10:07:48 2023 +0100

    pipenv update
2023-01-14 11:10:13 +01:00
Maxime Alves LIRMM 8b8caa2e15 [release] 0.6.22 2022-12-02 18:11:26 +01:00
Maxime Alves LIRMM 5a70c00541 [rc] 0.6.22-rc6 2022-10-13 12:21:26 +02:00
Maxime Alves LIRMM 7723acb812 [breaking] domain description is defined in a dict, not in "__name__", "__id__", "__routers__" ... 2022-10-13 12:18:13 +02:00
Maxime Alves LIRMM fd682ba0e0 [fix] wrong "data" argument value when there is a default one 2022-09-13 19:15:45 +02:00
Maxime Alves LIRMM@home e9c57049dd [lib/domain] log.error unhandled exceptions 2022-09-06 21:51:48 +02:00
Maxime Alves LIRMM e47ffcddb9 [deps] update pyJWT>2.4.0 2022-09-06 19:58:41 +02:00
Maxime Alves LIRMM f0c898ba20 (0.6.22rc3) [halfapi] Log HTTPException with statuscode 500 as critical 2022-09-05 10:44:04 +02:00
Maxime Alves LIRMM@home b7e678e00f [rc] 0.6.22-rc2 2022-09-05 10:20:08 +02:00
Maxime Alves LIRMM@home d0ec030ce9 [fix] important fix for argument handling 2022-09-05 10:18:57 +02:00
Maxime Alves LIRMM@home 039bc2c8fe [tests][BREAK] arguments are not filtered (since version 0.6.20 probably) 2022-09-05 10:12:47 +02:00
Maxime Alves LIRMM@home 5e21d4c24f fix ret_type feature 2022-08-31 00:14:10 +02:00
Maxime Alves LIRMM@home 8bdf5cab82 [testing] MODULE and CONFIG attributes 2022-08-31 00:13:29 +02:00
Maxime Alves LIRMM 910e1e1497 [release] 0.6.22-rc0 2022-08-18 20:24:57 +02:00
Maxime Alves LIRMM 53a691d985 [responses] html and plaintext return types as ret_type argument 2022-08-18 20:22:14 +02:00
Maxime Alves LIRMM@home 5d1b88daca [release] 0.6.21 2022-08-08 20:26:31 +02:00
Maxime Alves LIRMM@home c0bd6ddc43 [release] 0.6.21 2022-08-08 20:21:05 +02:00
Maxime Alves LIRMM 779dd2d519 [deps] lock pipenv update 2022-07-18 20:35:49 +02:00
Maxime Alves LIRMM 84747e3f73 [release] 0.6.20-rc0 2022-07-18 20:34:12 +02:00
Maxime Alves LIRMM 18748808c9 [fix] argument handling 2022-07-18 18:47:38 +02:00
Maxime Alves LIRMM 34ede09fe2 [release] 0.6.19 2022-07-06 08:34:11 +02:00
Maxime Alves LIRMM 6ebdf765bd [deps] pytest v7, [ci/cd] no tests on python3.6 2022-07-06 08:29:29 +02:00
Maxime Alves LIRMM 2d47789f61 [ci/cd] parallel tests on multiple python versions 2022-07-06 08:15:49 +02:00
maxime 3ec6d7514e [release] 0.6.18 2022-06-23 15:48:58 +02:00
maxime 3c97d39cdc [config] restore dumped configuration when using HALFAPI_DOMAIN_NAME 2022-06-23 15:33:19 +02:00
maxime f96c712aa8 [release] 0.6.17 2022-06-23 11:07:41 +02:00
maxime 23a93026aa [halfdomain] fix feature HALFAPI_DOMAIN_MODULE for acls route 2022-06-23 11:03:52 +02:00
maxime 05cf37c775 [deps] ajout virtualenv dans les deps (pour build) 2022-06-23 11:00:33 +02:00
maxime a46b045ca6 [release] 0.6.16 2022-06-23 07:24:05 +02:00
maxime f68b7e59b8 handle HALFAPI_DOMAIN_MODULE environment variable 2022-06-23 07:21:49 +02:00
Maxime Alves LIRMM@home b17ce623f4 [release] 0.6.15 2022-05-17 16:19:41 +02:00
Maxime Alves LIRMM@home 7dd7d00625 [halfdomain] fix last modification 2022-05-17 16:13:33 +02:00
Maxime Alves LIRMM@home 389823db82 [deps] Pipfile lock 2022-05-17 16:02:23 +02:00
Maxime Alves LIRMM@home e70239433f [halfdomain] add ability to specify a domain's acl module path in __acl__ attribute 2022-05-17 16:01:58 +02:00
Maxime Alves LIRMM@home 739ffc9afa [release] 0.6.14 2022-05-17 10:31:34 +02:00
Maxime Alves LIRMM@home 99d4aaeb8d [lib/responses] ajout format XLSX, car ODS bug avec les dates
https://github.com/pyexcel/pyexcel-ods/issues/31
2022-05-17 10:30:15 +02:00
Maxime Alves LIRMM 0d9dc2a018 [release] 0.6.13
- (rollback from 0.6.12) Remove pytest from dependencies in Docker file and
  remove tests
- (dep) Add "packaging" dependency
- Add dependency check capability when instantiating a domain (__deps__
  variable, see in dummy_domain)
2022-03-21 09:51:04 +01:00
Maxime Alves LIRMM b63b0f52c6 [domain] checks versions specified in __deps__ variable of domain module 2022-03-21 09:45:35 +01:00
Maxime Alves LIRMM 63d6d1e8ea [release] 0.6.12 2022-03-17 17:48:41 +01:00
Maxime Alves LIRMM e8c99e6012 [ci/cd] install pytest in docker image, run pytest at launch 2022-03-17 17:38:20 +01:00
Maxime Alves LIRMM 568aea9ea8 [clena] nettoyage des fonctions non utilisées 2022-03-16 13:09:50 +01:00
Maxime Alves LIRMM 988a1e5bab [ci/cd] fix kaniko call 2022-03-14 15:24:57 +01:00
Maxime Alves LIRMM de72e469d2 [release] 0.6.11 2022-03-14 15:16:06 +01:00
Maxime Alves LIRMM f7879c6388 [release] 0.6.10
- Add "x-out" field in HTTP headers when "out" parameters are specified for a
  route
- Add "out" kwarg for not-async functions that specify it

- Hide data fields in args_check logs

- Fix testing lib for domains (add default secret and debug option)

- Domains now need to include the following variables in their __init__.py
    - __name__ (str, optional)
    - __id__ (str, optional)
- halfapi domain

- Mounts domain routers with their ACLs as decorator
- Configuration example files for systemd and a system-wide halfapi install
- Runs projects
- Handles JWT authentication middleware
2022-03-08 19:24:24 +01:00
Maxime Alves LIRMM 84179743a6 [release] 0.6.9 2022-03-02 16:46:32 +01:00
Maxime Alves LIRMM adf7f872b6 changelog 2022-02-28 10:21:01 +01:00
Maxime Alves LIRMM b96f4908c6 [release] 0.6.8 2022-02-28 10:10:06 +01:00
Maxime Alves LIRMM a388faf1d8 [testing] add "secret" and "production" variable to test_domain configuration 2022-02-28 09:58:47 +01:00
Maxime Alves LIRMM 4d6e935813 [ci/cd] fix kaniko executor 2022-02-10 16:31:26 +01:00
Maxime Alves LIRMM 4e080f805f [fix] odsresponse 2022-02-10 16:30:08 +01:00
Maxime Alves LIRMM 91dd4cbaa8 [ci/cd] check tag and not branch master 2022-02-10 16:01:38 +01:00
Maxime Alves LIRMM 209c6ef40a [ci/cd] add rules to execute build stage 2022-02-10 14:31:30 +00:00
Maxime Alves LIRMM a1c1bf04df [ci/cd] stages 2022-02-10 15:15:08 +01:00
Maxime Alves LIRMM 90203b2edf [ci] add ./tests to pytest command 2022-02-10 12:38:54 +01:00
Maxime Alves LIRMM a69d2b7639 [tests] async router renamed to async_router 2022-02-10 12:30:49 +01:00
Maxime Alves LIRMM c3153921f7 [tests] test_domain import dummy_domain inside test class 2022-02-10 11:59:40 +01:00
Maxime Alves LIRMM 97fee8ca96 [ci] execute pytest on tests folder 2022-02-10 11:56:35 +01:00
Maxime Alves LIRMM d33c82e348 [0.6.7] fix ODSResponse 2022-02-10 10:30:19 +01:00
Maxime Alves LIRMM@home 058121d985 [release] 0.6.6 2022-01-21 14:39:46 +01:00
Maxime Alves LIRMM@home 2e5680d29a [auth] dont activate authenticationMiddleware if secret is missing. NO SECRET ONLY IN FULLY PUBLIC DOMAINS!!! 2022-01-21 14:39:34 +01:00
Maxime Alves LIRMM@home 979007f287 [fix][log] active domains list logging 2022-01-21 14:38:28 +01:00
Maxime Alves LIRMM@home df555c7d26 [fix] when running one-domain mode we should set the configuration as if the domain was activated 2022-01-21 14:37:54 +01:00
Maxime Alves LIRMM@home be312d4b7a [fix][run] wont pop keys if we dont operate on a copy 2022-01-21 14:32:55 +01:00
Maxime Alves LIRMM f02a97fbf8 [testing] set starlette client in setUp 2021-12-15 12:37:49 +01:00
Maxime Alves LIRMM@home e9ffb553c8 [release] 0.6.4 2021-12-14 09:05:50 +01:00
Maxime Alves LIRMM@home 776cc8c85e [testing] fix check_routes of test_domain 2021-12-14 09:04:06 +01:00
Maxime Alves LIRMM@home 5d5ffdfb7c [tests] re-enable dummy_project route testing 2021-12-14 09:03:45 +01:00
Maxime Alves LIRMM@home 7c0f5717f4 [half_route] error handling 2021-12-14 09:01:38 +01:00
Maxime Alves LIRMM 7fb5e25411 [release] 0.6.3 2021-12-13 14:47:21 +01:00
Maxime Alves LIRMM a0dbbca04d [fix] forgot __init__.py in testing 2021-12-13 14:45:00 +01:00
Maxime Alves LIRMM 95fb267e81 [release] 0.6.2 2021-12-13 13:50:52 +01:00
Maxime Alves LIRMM e9bf94a607 [cli/domain] now instantiate HalfAPI and manually add the domain before calling schema function 2021-12-13 13:49:58 +01:00
Maxime Alves LIRMM f82cd5552b [half_domain] remove "config" argument, now uses "config" attribute from HalfAPI instance, add version and halfapi_versrion attributes, update DomainMiddleware init arguments 2021-12-13 12:45:10 +01:00
Maxime Alves LIRMM bdbad9e296 [halfapi] now inherits from Starlette class. Uses a dict to store HalfDomain instances 2021-12-13 12:43:26 +01:00
Maxime Alves LIRMM 76e942ab91 [tests] add test file for lib/domain_middleware 2021-12-13 12:42:12 +01:00
Maxime Alves LIRMM 8fff1f5372 [tests] fix tests and add "mix_stderr=False" to CliRunner instance 2021-12-13 12:41:33 +01:00
Maxime Alves LIRMM 048c9f1bab [tests] fix for schema lists 2021-12-08 16:45:00 +01:00
Maxime Alves LIRMM d5f39a7929 [schemas] always give a list of schemas 2021-12-08 16:42:12 +01:00
Maxime Alves LIRMM 648841d90f [dummy_domain] set an id to dummy_domain 2021-12-08 15:34:00 +01:00
Maxime Alves LIRMM c658815eb5 [cli] fix halfapi domain --read 2021-12-08 13:08:08 +01:00
Maxime Alves LIRMM 46e62575ae [half_domain] fix test_dummy_project 2021-12-08 12:23:47 +01:00
Maxime Alves LIRMM 7001cec86e [wip] refactor half_domain 2021-12-07 11:42:02 +01:00
Maxime Alves LIRMM@home b4157c4a7d [wip][testfail] multiple-domains app 2021-12-07 07:53:36 +01:00
Maxime Alves LIRMM@home 96f78e76c5 [tests][testfail] add default routes testing, /halfapi/acls fail 2021-12-04 09:56:14 +01:00
Maxime Alves LIRMM@home d54dcd641d [conf] fix secret tempfile creation 2021-12-04 00:28:30 +01:00
Maxime Alves LIRMM 7060d201ec [deps] optional requirements for ODSResponse 2021-12-03 17:26:14 +01:00
Maxime Alves LIRMM dbca2f28fb [conf] use of toml for halfapi configs. re-enable possibility of multiple domains 2021-12-03 17:25:57 +01:00
Maxime Alves LIRMM@home d06857bf49 [config][wip][nf] removal of "domains" and "domain" section 2021-12-03 09:49:30 +01:00
Maxime Alves LIRMM@home 3dc951c81e [tests] tidy cli tests 2021-12-03 09:20:40 +01:00
Maxime Alves LIRMM@home a8e5cfc0ff [wip][responses] allow to change return format with "format" route argument, add ODSResponse 2021-12-01 21:16:19 +01:00
Maxime Alves LIRMM@home 20cada4fa0 [halfapi] fix domain importlib 2021-12-01 21:14:17 +01:00
Maxime Alves LIRMM@home c1bb637be7 [lib.router] forbid extra-keys in routes dict (no more FQTN at same level of methods) 2021-12-01 21:13:35 +01:00
Maxime Alves LIRMM@home a37c2356d6 [lib.domain] error log when missing docstring 2021-12-01 21:12:19 +01:00
Maxime Alves LIRMM@home 038715e94a [halfapi] config option "--dry-run", used in test_domain 2021-12-01 21:11:26 +01:00
Maxime Alves LIRMM@home 2f9005a1a5 [release] 0.6.1 2021-12-01 17:02:56 +01:00
Maxime Alves LIRMM@home a2d79f49b9 [tests] dummy domain test (you can use this example in your own domain) 2021-12-01 17:02:23 +01:00
Maxime Alves LIRMM@home cf98b08fa5 [tests] mv base domain test to halfapi/testing 2021-12-01 17:01:56 +01:00
Maxime Alves LIRMM@home c1191bbb0e [deps] update deps 2021-12-01 16:25:53 +01:00
Maxime Alves LIRMM@home 837c646bc5 [ci] switch to bullseye-3.9 2021-12-01 15:49:59 +01:00
Maxime Alves LIRMM@home 49c13c56ac [lib.domain] implement domain schema - try: halfapi domain dummy_domain 2021-12-01 13:31:46 +01:00
Maxime Alves LIRMM@home 1ccfa0d10e [lib.schemas] router schema update 2021-12-01 13:07:01 +01:00
Maxime Alves LIRMM@home 238bd99bd3 [cli.routes] add json schema export 2021-12-01 13:06:00 +01:00
Maxime Alves LIRMM@home 429a90d786 [halfapi] fix typo 2021-12-01 12:21:26 +01:00
Maxime Alves LIRMM@home 1ec244b60f [lib.constants] route keys are optional, in case of empty routers 2021-12-01 12:20:32 +01:00
Maxime Alves LIRMM@home 53ecbb58fc [tests] rework some tests, avoid calling project_runner multiple times (should be tested better, but for now is just buggy imports...) 2021-12-01 12:20:01 +01:00
Maxime Alves LIRMM@home 7e7bbb3a62 [tests] fix de "project_runner" auto clean sys.path, "tree" fixture to debug projects 2021-12-01 12:16:31 +01:00
Maxime Alves LIRMM@home 5e88109b3e manipuler le sys.path : BAAAD 2021-12-01 12:14:27 +01:00
Maxime Alves LIRMM@home e293ac3867 [cli/domain] create domain function rework - creates a basic domain 2021-12-01 12:08:24 +01:00
Maxime Alves LIRMM@home a98aa27485 [pipfile] remove dependency to python3.8, TODO find a way to specify python>=3.8 2021-12-01 12:02:21 +01:00
Maxime Alves LIRMM@home 15794327f9 [tests] pytest.ini includes tests as pythonpath (because of dummy_domain) 2021-12-01 11:55:56 +01:00
Maxime Alves LIRMM@home cf20b76959 [deps] add pytest-pythonpath 2021-12-01 11:43:49 +01:00
Maxime Alves LIRMM 7e1cc21b8c [wip][testfail] update config monodomain - schema gives acls 2021-11-30 18:31:40 +01:00
Maxime Alves LIRMM ec26438340 [schemas] supression des fonctions api_routes 2021-11-30 14:40:07 +01:00
Maxime Alves LIRMM 7230316296 [ci] add pytest version 2021-11-30 12:13:17 +01:00
Maxime Alves LIRMM f5b7e3392a [tests] skip /halfapi/exception test 2021-11-30 12:07:19 +01:00
Maxime Alves LIRMM 18dbbdd584 [app] enable use of SCHEMA to run halfapi, fix tests 2021-11-30 11:20:26 +01:00
Maxime Alves LIRMM b3b32b47f8 [cli.run] halfapi run SCHEMA.csv 2021-11-30 10:42:00 +01:00
Maxime Alves LIRMM 189fcf86f7 [cli.routes] halfapi routes --export 2021-11-30 01:54:48 +01:00
Maxime Alves LIRMM 55878df260 [lib.domain] use args_check by default, even on async functions 2021-11-30 01:52:02 +01:00
Maxime Alves LIRMM a6985fa9bf [halfapi] configuration globals go lowercase 2021-11-30 01:50:27 +01:00
Maxime Alves LIRMM ed6dcb0513 [halfapi] application becomes a private attribute 2021-11-30 01:49:37 +01:00
Maxime Alves LIRMM 7017827b2b [lib] small fixes 2021-11-30 01:46:30 +01:00
Maxime Alves LIRMM 7c2bf60812 [pytest] remove halfapi from tests directory 2021-11-30 01:43:14 +01:00
Maxime Alves LIRMM 1fda2ab15d [lib.schemas] add schema_csv_dict and schema_to_csv function 2021-11-30 01:42:29 +01:00
Maxime Alves LIRMM c9639ddbc0 [constants] add lib "schema" Schemas for all items 2021-11-30 00:37:13 +01:00
Maxime Alves LIRMM 24bd3f5653 [deps] add "schema" as dependency 2021-11-29 17:01:52 +01:00
Maxime Alves LIRMM@home e203552876 [dummy_domain] add async routes, and routes that use arguments 2021-11-29 08:37:52 +01:00
Maxime Alves LIRMM@home ac4aadc2df [conf][testfail] add SCHEMA dictionary to globals 2021-11-29 06:21:48 +01:00
Maxime Alves LIRMM@home 4dae2f3676 [dummy_domain] add tests for dummy_domain acls 2021-11-29 06:09:18 +01:00
Maxime Alves LIRMM@home c27ed3a966 [routers] docstring is mandator in route methods. YAML is used for markup 2021-11-29 06:08:24 +01:00
Maxime Alves LIRMM@home 47d81c048f [lib] route_acl_decorator becomes HalfRoute.acl_decorator, creation of HalfRoute that wraps starlette.route 2021-11-29 05:42:26 +01:00
Maxime Alves LIRMM ad6877a7e9 [run] add arguments for config 2021-11-23 13:53:08 +01:00
Maxime Alves LIRMM e7e1bfed1b [conf] halfapi config now dumps the .ini of the current config 2021-11-23 13:35:12 +01:00
Maxime Alves LIRMM f0e662e060 [conf] remove is_project variable 2021-11-23 11:39:33 +01:00
Maxime Alves LIRMM ea8e7ede65 [cli.routes:export] shows a header to describe the CSV, can be deactivated 2021-11-22 20:14:05 +01:00
Maxime Alves LIRMM f4880f1f9c [lib.domain] wraps function when loading routes from modules, to get the real name 2021-11-22 20:09:57 +01:00
Maxime Alves LIRMM 4a8cb008e6 [cli.routes] add routes command, allows exporting route dict as csv 2021-11-22 20:06:50 +01:00
Maxime Alves LIRMM 049860fce5 [lib.domain] gen_router_routes yields router module also 2021-11-22 20:06:12 +01:00
Maxime Alves LIRMM 908eab5fdc [lib.user] move JWTUser, Nobody and CheckUser to lib.user 2021-11-22 20:05:17 +01:00
Maxime Alves LIRMM ce672eeb30 [cli.domain] fix list routes 2021-11-22 19:09:02 +01:00
Maxime Alves LIRMM 1f20a336e2 [conf] production now defaults to true 2021-11-22 18:30:29 +01:00
Maxime Alves LIRMM 0173eb6d72 HalfAPI class clean and rewrite 2021-11-22 18:18:06 +01:00
Maxime Alves LIRMM ad9bd45ba0 [logging] use logger from halfapi.logging 2021-11-16 17:45:40 +01:00
Maxime Alves LIRMM 00c7b5caf4 [doc] update README and add joel to license 2021-11-16 17:45:40 +01:00
Maxime Alves LIRMM 5184ab4411 [ci][wip] utilisation de devtools/kaniko du lirmm 2021-11-16 17:40:05 +01:00
Maxime Alves LIRMM 159d38cb94 [wip][ci] utilisation image officielle buildah 2021-11-10 10:46:32 +01:00
Maxime Alves LIRMM@home 0cad726f8c [ci] try buildah 2021-11-01 10:34:52 +01:00
Maxime Alves LIRMM@home 94e09a546b [dockerfile] install git 2021-10-12 18:18:54 +02:00
Maxime Alves LIRMM@home 0643af5cca [dockerfile] add gunicorn/uvicorn 2021-10-12 18:08:07 +02:00
Maxime Alves LIRMM@home 339c910c86 [wip][ci] "before_script" runs before every job 2021-10-12 14:30:11 +02:00
Maxime Alves LIRMM@home 948372fcbc [wip][ci] try to build docker image 2021-10-12 03:18:32 +02:00
Maxime Alves LIRMM@home 5a0509a114 set loglevel even if in production 2021-10-12 03:10:10 +02:00
Maxime Alves LIRMM@home c99e636d6e [0.5.13]
Squashed commit of the following:

commit 4552d85cc49fda572e54aa9c8054031554bfcb3a
Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr>
Date:   Thu Oct 7 13:57:37 2021 +0200

    [0.5.13]

commit 38032acfac559155b31c12cf12673c81b7cfdf20
Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr>
Date:   Thu Oct 7 13:57:12 2021 +0200

    add 503 error code

commit 6f516e844b0f3786aa571d1ac8d575247ff7b7fe
Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr>
Date:   Thu Oct 7 13:26:15 2021 +0200

    [ci] add run halfapi --version
2021-10-07 13:58:03 +02:00
Maxime Alves LIRMM@home 8b88d7f1b4 [0.5.12] fix production no reload 2021-10-07 13:23:53 +02:00
Maxime Alves LIRMM@home f27b68e350 [0.5.11] 2021-10-04 22:16:27 +02:00
Maxime Alves LIRMM@home f3c12f516e [cli] run does not reload modules by default 2021-10-04 20:11:32 +02:00
Maxime Alves LIRMM 55109e271c [conf] fix HALFAPI_PROD variable that is set to a string 2021-09-03 16:38:20 +02:00
Maxime Alves LIRMM 51877b271e [conf] Read "HALFAPI_SECRET" file content if the option is not specified. Do not use HALFAPI_SECRET to store the *secret* value 2021-09-03 16:22:55 +02:00
Maxime Alves LIRMM 061c966072 [lib.domain] fix mounting domains with environment variable 2021-09-03 14:56:18 +02:00
Maxime Alves LIRMM cdd2214043 [conf] halfapi becomes configurable only by environment variables (for one domain) 2021-09-03 13:17:06 +02:00
Maxime Alves LIRMM 0470f9fa89 [0.5.9] release
Squashed commit of the following:

commit 7fe3e22f5e4108b5eb149abf8d608334debc49ca
Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr>
Date:   Thu Sep 2 16:59:52 2021 +0200

    [0.5.9] release

commit c36c0fcc982388a5acf2f9f937fa8ab54a18f3de
Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr>
Date:   Thu Sep 2 16:53:13 2021 +0200

    [conf] fix #19 et ajout du test (test_dummy_project_router/test_get_config)

    configuration du domaine accessible depuis :

    l'attribut config de l'argument "halfapi" pour les fonctions
    request.scope['config'] pour les fonctions async

commit cc235eee8c6f8f5d3606dda0f88156697eac296e
Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr>
Date:   Thu Sep 2 14:59:17 2021 +0200

    [tests] don't import click two times

commit fa418478c76205bb407e536737d8e389b4bf391c
Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr>
Date:   Thu Sep 2 14:57:06 2021 +0200

    [clean] remove unused variables, remove [] as default value in fct, raise from exc
2021-09-02 17:01:36 +02:00
Maxime Alves LIRMM 74b79120ba [deps] starlette 0.16 2021-09-02 14:48:48 +02:00
Maxime Alves LIRMM bc556854ac Fix #21 by simplifying DomainMiddleware
Tests are passing, but we loose the by-domain configuration (#19)

Squashed commit of the following:

commit d75fafcb9a043ac2540b2ac135704721b002d3c0
Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr>
Date:   Thu Sep 2 14:40:05 2021 +0200

    fix #21

commit 38c59e4ea3b40bd230f2add2bb0e05772913c097
Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr>
Date:   Thu Sep 2 01:13:51 2021 +0200

    [deps] starlette 0.15 (breaks tests)

    FAILED tests/test_debug_routes.py::test_current_user - AttributeError: 'DomainMiddleware' object has no attribute 'call_next'
    FAILED tests/test_debug_routes.py::test_log - AttributeError: 'DomainMiddleware' object has no attribute 'call_next'
    FAILED tests/test_debug_routes.py::test_error - AttributeError:
    'DomainMiddleware' object has no attribute 'call_next'
    FAILED tests/test_dummy_project_router.py::test_get_route - AttributeError: 'DomainMiddleware' object has no attribute 'call_next'
    FAILED tests/test_dummy_project_router.py::test_delete_route - AttributeError: 'DomainMiddleware' object has no attribute 'call_next'
    FAILED tests/test_lib_schemas.py::test_get_api_routes - AttributeError: 'DomainMiddleware' object has no attribute 'call_next'
    FAILED tests/test_lib_schemas.py::test_get_schema_route - AttributeError: 'DomainMiddleware' object has no attribute 'call_next'
    FAILED tests/test_lib_schemas.py::test_get_api_dummy_domain_routes - AttributeError: 'DomainMiddleware' object has no attribute 'call_next'
2021-09-02 14:45:06 +02:00
Maxime Alves LIRMM 865a4dffd1 [ci] add continuous integration
Squashed commit of the following:

commit e7b303310f3726c4ec5b36db668f7a5aaee29287
Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr>
Date:   Thu Sep 2 10:35:16 2021 +0200

    fix gitlab-ci run

commit 360c03371f1b088ce883646799f829a50f7a04a8
Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr>
Date:   Thu Sep 2 08:26:51 2021 +0000

    Update .gitlab-ci.yml file
2021-09-02 10:38:19 +02:00
Maxime Alves LIRMM 5e2ccceedf [deps] "build" (python -m build --sdist; python -m build --wheel) 2021-09-02 10:20:03 +02:00
Maxime Alves LIRMM 844f6a8f14 [packaging] 0.5.8 update to PyPi 2021-09-01 15:59:47 +02:00
Maxime Alves LIRMM 43b7fe21df [license] change author 2021-09-01 15:26:56 +02:00
Maxime Alves LIRMM c9ba99c1df [readme] fix formatting 2021-09-01 15:24:53 +02:00
Maxime Alves LIRMM 18a1f71d99 [package] set source url to github 2021-09-01 15:24:21 +02:00
Maxime Alves LIRMM c2cea298bf [readme] update with instructions 2021-09-01 15:19:51 +02:00
Maxime Alves LIRMM 387fc01f44 [0.5.7] release, real fix for halfapi/acl route bug 2021-07-06 11:30:30 +02:00
Maxime Alves LIRMM c2054e9aa9 [lib.domain.domain_acls] fix ROUTES constant using read_router function 2021-07-06 11:28:23 +02:00
Maxime Alves LIRMM@home 0e669b81b0 [domain] do not raise exception when the router is missing the ROUTES variable in halfapi/acls route 2021-06-30 15:13:57 +02:00
Maxime Alves LIRMM@home b45c0bf746 [deps] Avoid starlette update to 0.15, stay in latest 0.14 2021-06-28 12:09:56 +02:00
Maxime Alves LIRMM@home c920531610 [release] 0.5.6 2021-06-25 12:21:40 +02:00
Maxime Alves LIRMM@home bb50fae186 [lib.acl] fixes #20 2021-06-25 12:21:10 +02:00
Maxime Alves LIRMM 0c3aeb532f [0.5.5] retablissement de la configuration par domaine 2021-06-23 15:04:37 +02:00
Maxime Alves LIRMM 8a9f93b9e0 [conf] lecture des sections du nom des domaines activés et ajout dans request.scope['config']['config'] 2021-06-23 15:03:00 +02:00
Maxime Alves LIRMM 9381e1582e [0.5.4] 2021-06-18 17:11:34 +02:00
Maxime Alves LIRMM a539212faf [lib.acl] fix args_check for get parameters 2021-06-18 12:08:51 +02:00
Maxime Alves LIRMM a14285475e [lib.domain] check if an HTTPException is raised, else raise an HTTPException(500) 2021-06-18 10:39:05 +02:00
Maxime Alves LIRMM@home 15d69efd60 [tests] /halfapi/schema 2021-06-17 19:14:39 +02:00
Maxime Alves LIRMM@home 2819483070 [doc] docstring for api_routes 2021-06-17 19:02:11 +02:00
Maxime Alves LIRMM@home 81f6cf8b39 [0.5.3]
Squashed commit of the following:

commit ac935db6d6
Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr>
Date:   Thu Jun 17 18:52:49 2021 +0200

    [tests] remove dummy-domain from dependencies

commit 4d50363c9b
Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr>
Date:   Thu Jun 17 18:52:18 2021 +0200

    [tests] update tests for 0.5.3

commit 6181592692
Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr>
Date:   Thu Jun 17 18:17:51 2021 +0200

    [lib.*] Refactor libs

commit ed7485a8a1
Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr>
Date:   Thu Jun 17 18:15:10 2021 +0200

    [app] Use HalfAPI class to be able to use custom configuration
    à

commit fa1ca6bf9d
Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr>
Date:   Wed Jun 16 15:34:25 2021 +0200

    [wip] tests dummy_domain

commit 86e8dd3465
Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr>
Date:   Tue Jun 15 18:12:13 2021 +0200

    [0.5.3] ajout de la config actuelle dans les arguments des routes

commit aa7ec62c7a
Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr>
Date:   Tue Jun 15 11:16:23 2021 +0200

    [lib.jwtMw] verify signature even if halfapi is in DEBUG mode

commit e208728d7e
Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr>
Date:   Tue Jun 15 10:49:46 2021 +0200

    [lib.acl] args_check doesn't check required/optional arguments if "args" is not specified in request, if the target function is not async

commit aa4c309778
Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr>
Date:   Tue Jun 15 09:45:37 2021 +0200

    [lib.domain] SUBROUTER can be a path parameter if including ":"

commit 138420461d
Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr>
Date:   Tue Jun 15 07:24:32 2021 +0200

    [gitignore] *.swp

commit 0c1e2849ba
Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr>
Date:   Tue Jun 15 07:24:14 2021 +0200

    [tests] test get route with dummy projects

commit 7227e2d7f1
Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr>
Date:   Mon Jun 14 17:18:47 2021 +0200

    [lib.domain] handle modules without ROUTES attribute

commit 78c75cd60e
Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr>
Date:   Mon Jun 14 16:34:58 2021 +0200

    [tests] add dummy_project_router tests for path-based routers (without ROUTES variable)
2021-06-17 18:53:23 +02:00
Maxime Alves LIRMM eb68d06ac0 [0.5.2] /halfapi/acls crash if acl attribute does not exist on a route 2021-06-07 19:40:50 +02:00
Maxime Alves LIRMM 3fb6fb4ded [0.5.1] Reactivation de la route "/halfapi/acls" (bug lors de multiples de domaines) 2021-06-07 19:30:00 +02:00
Maxime Alves LIRMM e5a278c84c [release] 0.5.0 2021-06-07 16:44:43 +02:00
Maxime Alves LIRMM 904783b134 [deps] 2021-05-28 22:30:39 +02:00
Maxime Alves LIRMM e0b06f51d5 [acl decorator] when check flag is activated, returns a 200 status code with empty body if no acl has passed
check function should always return 200. If we don't do this, we can't see the difference between a "refused" call and a "refused" check
2021-05-28 22:23:20 +02:00
Maxime Alves LIRMM 8ca94ab7ed [acl decorator] possibility to decorate functions with "@route_acl_decorator" 2021-05-28 22:22:05 +02:00
Maxime Alves LIRMM e4e04c6ac1 [JWTmw] add CheckUser to be used when using the "check" flag. Add "user_id" query param to check access of a specific user to a route 2021-05-28 22:18:58 +02:00
Maxime Alves LIRMM 0e5a8ede9d [test] test of lib.acl 2021-05-28 22:13:48 +02:00
Maxime Alves LIRMM a0c41d7d78 [tests] jwt, use of TestClient instead of requests 2021-05-28 22:12:53 +02:00
Maxime Alves LIRMM a82fd6def0 [acl] ajout de l'acl private (test purposes) 2021-05-28 22:02:35 +02:00
Maxime Alves LIRMM 9a9bc16bbc [tests] déplacement des fixtures jwt dans conftest 2021-05-28 21:54:45 +02:00
Maxime Alves LIRMM ea1f54cb82 [tests] JWTmw - flags check et user_id 2021-05-28 12:41:23 +02:00
Maxime Alves LIRMM 3c6713b5e2 [tests] add JWTMw test for query without tokens (must return Unauth user) 2021-05-28 12:27:35 +02:00
Maxime Alves LIRMM c7e29e399b [tests] use "isinstance" instead of "type" 2021-05-28 12:26:51 +02:00
Maxime Alves LIRMM 89a5f3aa52 [tests] nettoyage test jwt 2021-05-28 12:26:27 +02:00
Maxime Alves LIRMM@home 933f456c86 Montage des routes uniquement si les variables DOMAINDICt et SECREt sont présentes 2021-04-27 08:26:48 +02:00
Maxime Alves LIRMM@home 5276833afe [lib/domain] ajout du dossier courant dans le path pour import de librairies dans le dossier du projet 2021-04-24 23:54:18 +02:00
Maxime Alves LIRMM@home 10b1960f4e nettoyage / commentaires / renommage de variables - JWTUser.id deviens JWTUser.identity 2021-04-24 08:56:18 +02:00
Maxime Alves LIRMM@home a2fb70f84b DomainMiddleware ne vérifie plus les domaines non concernés par la première partie du path (optimisation vitesse) 2021-04-23 12:51:51 +02:00
Maxime Alves LIRMM@home 795ca3dcc0 [decorator] ajout du décorateur "args_check" pour automatiser la vérification des args requis/optionels 2021-04-22 23:36:34 +02:00
Maxime Alves LIRMM@home 5b67d938e2 Ajout du timing middleware pour le mode debug 2021-04-22 22:33:02 +02:00
Maxime Alves LIRMM@home 89ec439d3e ajout du header x-args-required et x-args-optional 2021-04-22 22:06:27 +02:00
Maxime Alves LIRMM@home b5ef4a12d1 Ajout du header http "x-acl" qui renseigne l'acl qui a passé dans une requête 2021-04-22 22:04:21 +02:00
Maxime Alves LIRMM@home 4eb23fd189 Ajout de l'option "check" permettant de connaître l'acl qui passe 2021-04-22 17:30:21 +02:00
Maxime Alves LIRMM@home e1c3d61207 Rajout de l'argument "doc" sur la route /halfapi/acls 2021-04-22 11:45:12 +02:00
Maxime Alves LIRMM@home cd0df35496 [tests][lib/responses] format des dates ORJSON + update deps (fix bug dates) 2021-03-12 19:02:23 +01:00
Maxime Alves LIRMM@home 607a288e28 [debug] ajout des routes /error/{code:str} et /exception 2021-03-12 18:59:30 +01:00
Maxime Alves LIRMM@home a3d546905c [update] fixage des versions des dépendances, fixage de python >= 3.7 2021-01-26 01:04:05 +01:00
Maxime Alves LIRMM@home fecdaa29e5 [update] mise à jour du Pipfile.lock et de jwt_middleware
Upgrade de toutes les dépendances
Suppression de la dépendance à jwt
Tests : ok
2021-01-26 00:58:59 +01:00
Joël Maïzi f36a2d8e06 [ORJSONResponse] Handle sets. 2021-01-25 10:44:07 +01:00
Maxime Alves LIRMM@home 54e215b6ff [test][nf] added basic test methods for orjsonresponse 2021-01-23 09:31:35 +01:00
Joël Maïzi a5300962ad [responses] cast set to list. 2021-01-23 08:49:07 +01:00
Maxime Alves LIRMM d21ee175e9 [responses] cast special types in ORJSONResponse 2021-01-14 10:55:02 +01:00
Maxime Alves LIRMM@home f8e546007c [conf] lower loglevel string 2020-12-04 18:12:42 +01:00
Maxime Alves LIRMM@home 8d414f2bdd [config] ajout de l'option LOGLEVEL (info|debug|warning...) 2020-12-04 10:13:54 +01:00
Maxime Alves LIRMM@home e61dd7eeaa [logs] change level de certains messages 2020-12-03 18:49:37 +01:00
Maxime Alves LIRMM@home f4ba64f186 [debug] added /halfapi/log as a debug route that generates all type of logs 2020-11-12 17:56:05 +01:00
Maxime Alves LIRMM@home d4a6bb1a04 [domainMW] set "acl_pass" variable into scope 2020-11-09 21:27:23 +01:00
Maxime Alves LIRMM@home 5d4b8d38b4 0.3.1 2020-11-09 21:26:31 +01:00
Maxime Alves LIRMM@home 9516eaa6d7 RAISES when a router import fails - test your domains routers FFS 2020-11-04 17:02:19 +01:00
Maxime Alves LIRMM@home 56657036e4 Added logging for acls 2020-11-04 13:32:33 +01:00
Maxime Alves LIRMM@home 73d49031a7 handle configuration when domain does not exist 2020-11-04 12:31:11 +01:00
Maxime Alves LIRMM@home 4782764059 add request.scope['config'] when there is a section with the domains name in the project's configuration 2020-11-04 05:01:26 +01:00
Maxime Alves LIRMM@home 61aec6871a [release][v0.3.0] Updated version to 0.3.0 2020-10-31 17:48:55 +01:00
Maxime Alves LIRMM@home 9a4f90d36b /lib/domain - try for ImportError bug 2020-10-27 13:07:49 +01:00
Maxime Alves LIRMM 64e60343bf [conf] Clean of DOMAINS global variable 2020-10-27 13:07:49 +01:00
Maxime Alves LIRMM 4df34b5d87 logging 2020-10-27 13:07:49 +01:00
Maxime Alves LIRMM 24c68b51f2 [lib] ORJSONResponse can now accept specific encoders 2020-10-26 15:12:03 +01:00
Maxime Alves LIRMM d31efe3cc4 [deps] removed half_orm from dependencies 2020-10-20 08:25:50 +02:00
Maxime Alves LIRMM e590bc31fe [doc] lib/domain, lib/domain_middlware 2020-10-07 16:13:54 +02:00
Maxime Alves LIRMM 3959e6d614 [tests] add dummy_domain as a testing dependency (because domains can't
be "not installed" in sys.path)
2020-10-07 15:51:24 +02:00
Maxime Alves LIRMM 1e1ff2fb69 [routes] skip acls that are falsy 2020-10-07 15:50:50 +02:00
Maxime Alves LIRMM 63b73a2bc1 [middleware][domain] load api and acl objects at each call (TODO: use a cache) 2020-10-07 15:49:49 +02:00
Maxime Alves LIRMM 710d390b49 [cli][conf] suppression of BASE_DIR option (now defaults to os.getcwd) 2020-10-07 15:48:48 +02:00
Maxime Alves LIRMM cb5724b4fa [doc] lib/domain_middleware 2020-10-07 13:37:31 +02:00
Maxime Alves LIRMM 23bd876c4c [conf] fix list comprehension 2020-10-07 09:55:03 +02:00
Maxime Alves LIRMM 40547ddf30 [tests][OK] Using a dummy_domain, adding it and listing it's routes 2020-10-07 09:55:03 +02:00
Maxime Alves LIRMM f5ebabbcd4 [refactor] update to avoid using global variables, and configuration are not stored in /etc/half_api anymore
Added domain_middleware
2020-10-07 09:55:03 +02:00
Maxime Alves LIRMM 781736c151 [logs] print -> logger lib.domain 2020-10-07 09:55:03 +02:00
Maxime Alves LIRMM acb0a46904 [deps] add PyYAML to dependencies 2020-10-07 09:55:03 +02:00
Maxime Alves LIRMM b651d90f0e [cli] add entry point to be just "halfapi" in Pipfile (__main__.py) 2020-10-07 09:55:03 +02:00
Maxime Alves LIRMM 0a34948b98 [cli] switch halfapi import to relative imports in cli.py 2020-10-07 09:55:03 +02:00
Maxime Alves LIRMM c41d0d8f8f [deps] jwt->PyJWT in Pipfile 2020-10-07 09:54:31 +02:00
Maxime Alves LIRMM 91ea25791b [doc] lib/domain.py annotation and comments 2020-10-07 09:54:09 +02:00
Maxime Alves LIRMM 0cd7c987e5 pylint lib/acl.py 2020-10-07 09:54:09 +02:00
Joël Maïzi 6653a0cd0a Fix wrong dependency jwt -> pyjwt. 2020-10-05 11:39:55 +02:00
Maxime Alves LIRMM 2d51f260cd [clean] remove cli/lib dir 2020-10-05 11:33:28 +02:00
Maxime Alves LIRMM 8fc1ba6c91 [doc] cli/domain.py 2020-10-05 11:31:53 +02:00
Maxime Alves LIRMM 9753f4be95 [doc] cli/run.py 2020-10-05 11:30:18 +02:00
Maxime Alves LIRMM 584e0b6584 [doc] cli/run.py 2020-10-05 11:29:17 +02:00
Maxime Alves LIRMM 82e2ccbdbc [tests] fix tests according to abfaf2e1 2020-10-05 11:28:50 +02:00
Maxime Alves LIRMM a6f2187032 [doc][style] cli/init.py 2020-10-05 11:23:44 +02:00
Maxime Alves LIRMM 1ee5da1588 [tests][jwt] fixs tests according to abfaf2e1 2020-10-05 11:18:47 +02:00
Maxime Alves LIRMM 1d0d3563cb [style] Clean cli/__init__.py 2020-10-05 11:10:34 +02:00
Maxime Alves LIRMM 91581f9ae6 [docs][style] cli/config.py 2020-10-05 11:10:00 +02:00
Maxime Alves LIRMM 6ac0b5acd9 [doc] cli/cli.py 2020-10-05 11:08:27 +02:00
Maxime Alves LIRMM 77598b8453 [doc][style] document fonctions and fix variable names 2020-10-05 11:04:32 +02:00
Maxime Alves LIRMM ad1fed7117 [doc] add docstring to conf.py 2020-10-05 10:57:40 +02:00
Maxime Alves LIRMM 59ed5884ce [style] rename exceptions (e) as exc (pep8 style) 2020-10-05 10:55:55 +02:00
Maxime Alves LIRMM 5c4e81d5d2 [jwt_mw] Refuse DEBUG tokens in PROD mode with websockets 2020-10-05 10:55:55 +02:00
Maxime Alves LIRMM 21950aa6cd [cli] remove lib/db 2020-10-05 10:55:55 +02:00
Joël Maïzi abfaf2e1ea [acls] Fix wrong key in payload for decoded JWT. 2020-10-05 10:48:00 +02:00
Maxime Alves LIRMM 333aca9e2c [app] clean code 2020-10-05 10:09:01 +02:00
Maxime Alves LIRMM b6e511a96d [lib][domain/routes] clean code, use "yield from" 2020-10-04 18:09:23 +02:00
Maxime Alves LIRMM deb41be3e8 [deps] added pylint as devdep 2020-10-04 17:27:24 +02:00
Maxime Alves LIRMM 429b26dec6 clean code + logs in conf 2020-10-04 17:27:02 +02:00
Maxime Alves LIRMM c603727190 [test] adjust test_gen_router_routes to fit the new behaviour 2020-10-04 17:26:36 +02:00
Maxime Alves LIRMM 74e0b3dc54 [acls] treat decorator as acl in route decorator 2020-09-30 10:59:03 +02:00
Joël Maïzi 31878d971e [routes] Add halfapi routes in production. 2020-09-30 10:31:52 +02:00
Maxime Alves LIRMM b89e03746f [acls] Allows decorators to be used as acls (fct=acl.public). 2020-09-30 10:31:25 +02:00
Maxime Alves LIRMM 79210e503e [acls] /halfapi/acls posé 2020-09-30 08:07:55 +02:00
Maxime Alves LIRMM 360f59b6ba [schema] fixes non-str keys in ORJSONResponse 2020-09-29 15:29:54 +02:00
Joël Maïzi 7d6bc2c181 [router][api] Fix GET / 2020-09-29 15:08:35 +02:00
Joël Maïzi 0a94f71dad [routing] Fix api representation (remove duplicates and fct). 2020-09-29 13:51:54 +02:00
Maxime Alves LIRMM e223c0791c [routing] fix route description when there is multiple verbs 2020-09-29 11:12:45 +02:00
Joël Maïzi d93fb23bba [0.2.1] Allows route parameters without acls. The route is deactivated. 2020-09-29 09:52:20 +02:00
Maxime Alves LIRMM 3530f53820 [routing] handle fqtn parameter in domain schema + get_api_routes (/) + /user -> /halfapi/current_user 2020-09-28 17:22:27 +02:00
Maxime Alves LIRMM@home f0d980e035 [0.2.0] Remove any reference to databases, clean clode, doc 2020-09-25 01:06:57 +02:00
Maxime Alves LIRMM@home 246c9224e3 [tests] all tests updated to fit new fixture 2020-09-25 01:06:21 +02:00
Maxime Alves LIRMM@home 70723ea580 [tests] add acl.py to dummy_domain 2020-09-25 01:05:27 +02:00
Maxime Alves LIRMM@home 4ef7ae377a [deps] add orjson dependency 2020-09-24 20:48:52 +02:00
Maxime Alves LIRMM@home 6a81c61649 [cli][domain] Re-implementation of list_routes, using .lib.schemas, add DOMAINSDICT constant in configuration, set a lot of default configurations 2020-09-24 19:49:16 +02:00
Maxime Alves LIRMM 2ad0b3a14b [doc][route] /schema - implementation of doc generation 2020-09-23 15:23:36 +02:00
Maxime Alves LIRMM 2610d9f089 forgot to import typ 2020-09-23 11:15:53 +02:00
Maxime Alves LIRMM 65797873da [lib][responses] include new responses in __all__ 2020-09-23 11:00:59 +02:00
Maxime Alves LIRMM d6075de2eb [lib][responses] add ORJSONResponse and update HJSONResponse 2020-09-23 10:56:32 +02:00
Maxime Alves LIRMM 39d455b682 if the route begin with a "/" we remove it 2020-09-22 16:11:32 +02:00
Maxime Alves LIRMM a78e6ebc75 [wip][routing] get_fct_name update 2020-09-22 15:46:58 +02:00
Maxime Alves LIRMM 5f7d66d4d6 [wip][routing] updated lib.domain function names 2020-09-22 14:54:45 +02:00
Maxime Alves LIRMM 38798549f6 [wip][app] adapt code to new router functions 2020-09-22 14:11:00 +02:00
Maxime Alves LIRMM ba44a01a45 [wip][routing] update routes scanner functions in lib.routes 2020-09-22 14:04:19 +02:00
Maxime Alves LIRMM c54101c3e6 [wip][routing] add routing functions in /lib/domains 2020-09-22 12:57:36 +02:00
Maxime Alves LIRMM 7337246fc1 [lib][routes] fix dict construction 2020-09-21 11:26:51 +02:00
Maxime Alves LIRMM 142ea24630 [routing] add the ACL name that returned True 2020-09-18 15:31:36 +02:00
Maxime Alves LIRMM bcc4a3e9d8 [routing] gives "keys" of the Acl row to the endpoint as kwargs 2020-09-18 11:59:44 +02:00
Maxime Alves LIRMM e8ed06a9b6 update api.sql with acl.keys 2020-09-16 14:18:37 +02:00
Joël Maïzi 31a8f68a0a [cli][domain] Remove misplaced elif in add_acls. 2020-09-16 12:21:52 +02:00
89 changed files with 6243 additions and 1628 deletions

4
.gitignore vendored
View File

@ -137,3 +137,7 @@ dmypy.json
cython_debug/
domains/
.vscode
# Vim swap files
*.swp

90
.gitlab-ci.yml Normal file
View File

@ -0,0 +1,90 @@
# This file is a template, and might need editing before it works on your project.
# To contribute improvements to CI/CD templates, please follow the Development guide at:
# https://docs.gitlab.com/ee/development/cicd/templates.html
# This specific template is located at:
# https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/ci/templates/Python.gitlab-ci.yml
# Official language image. Look for the different tagged releases at:
# https://hub.docker.com/r/library/python/tags/
image: python:alpine3.18
# Change pip's cache directory to be inside the project directory since we can
# only cache local items.
variables:
PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"
# REGISTRY_USERNAME - secret variable
# REGISTRY_PASSWORD - secret variable
# REGISTRY_SERVER - secret variable
IMAGE_NAME: ${REGISTRY_SERVER}/malves/${CI_PROJECT_NAME}
# Pip's cache doesn't store the python packages
# https://pip.pypa.io/en/stable/reference/pip_install/#caching
#
# If you want to also cache the installed packages, you have to install
# them in a virtualenv and cache it as well.
cache:
paths:
- .cache/pip
- venv/
stages:
- test
- build
.before_script_template: &test
before_script:
- python3 -V # Print out python version for debugging
- python3 -m venv /tmp/venv
- /tmp/venv/bin/pip3 install .["tests","pyexcel"]
- /tmp/venv/bin/pip3 install coverage pytest
test:
image: python:alpine${ALPINEVERSION}
stage: test
<<: *test
parallel:
matrix:
- ALPINEVERSION: ["3.16", "3.17", "3.18", "3.19"]
script:
- /tmp/venv/bin/pytest --version
- PYTHONPATH=./tests/ /tmp/venv/bin/coverage run --source halfapi -m pytest
- /tmp/venv/bin/coverage xml
- /tmp/venv/bin/halfapi --version
artifacts:
reports:
coverage_report:
coverage_format: cobertura
path: coverage.xml
build_pypi:
stage: build
script:
- python3 -V # Print out python version for debugging
- python3 -m venv /tmp/venv
- /tmp/venv/bin/pip3 install .
artifacts:
paths:
- dist/*.whl
rules:
- if: '$CI_COMMIT_TAG != ""'
variables:
TAG: $CI_COMMIT_TAG
build_container:
rules:
- if: '$CI_COMMIT_TAG != ""'
variables:
IMGTAG: $CI_COMMIT_TAG
- if: '$CI_COMMIT_REF_NAME == "devel"'
variables:
IMGTAG: "latest"
stage: build
image: $CI_REGISTRY/devtools/kaniko
script:
- echo "Will upload image halfapi:\"$IMGTAG\""
- echo "{\"auths\":{\"$CI_REGISTRY\":{\"username\":\"$CI_REGISTRY_USER\",\"password\":\"$CI_REGISTRY_PASSWORD\"}}}" > /kaniko/.docker/config.json
- /kaniko/executor --force --context $CI_PROJECT_DIR --dockerfile $CI_PROJECT_DIR/Dockerfile --destination $CI_REGISTRY/malves/halfapi:"$IMGTAG"
artifacts:
paths:
- /kaniko/.docker/config.json

View File

@ -1,5 +1,233 @@
# HalfAPI
## 0.6.30
Dependencies updates
- pyYAML v6.0.1
- starlette v0.37.2
Warning : the on_startup halfAPI argument is now removed, use the lifeSpan
## 0.6.29
### Dependencies
Starlette version bumped to 0.33.
## 0.6.28
### Dependencies
Starlette version bumped to 0.31 (had to disable a test to make it work but
seems not important).
### Development dependencies
Python 3.7 is no longer supported (openapi_spec_validator is not compatible).
If you are a developper, you should update dev dependencies in your virtual
environment.
### OpenAPI schemas
This release improves OpenAPI specification in routes, and gives a default
"parameters" field for routes that have path parameters.
Also, if you use halfAPI for multi-domain setups, you may be annoyed by the
change in the return value of the "/" route that differs from "/domain" route.
An HalfAPI instance should return one and only one OpenAPI Schema, so you can
rely on it to connect to other software.
The version number that is contained under the "info" dictionnary is now the "version"
of the Api domain, as specified in the domain dictionnary specified at the root
of the Domain.
The title field of the "info" dictionnary is now the Domain's name.
The ACLs list is now available under the "info.x-acls" attribute of the schema.
It is still accessible from the "/halfapi/acls" route.
#### Schema Components
You can now specify a dict in the domain's metadata dict that follows the
"components" key of an OpenAPI schema.
Use it to define models that are used in your API. You can see an exemple of
it's use in the "tests/dummy_domain/__init__.py" file.
### ACLs
The use of an "HEAD" request to check an ACL is now the norm. Please change all
the occurrences of your calls on theses routes with the GET method.
### CLI
Domain command update :
The `--conftest` flag is now allowed when running the `domain` command, it dumps the current configuration as a TOML string.
`halfapi domain --conftest my_domain`
The `--dry-run` flag was buggy and is now fixed when using the `domai ` command with the `--run` flag.
### Configuration
The `port` option in a `domain.my_domain` section in the TOML config file is now prefered to the one in the `project` section.
The `project` section is used as a default section for the whole configuration file. - Tests still have to be written -
The standard configuration precedence is fixed, in this order from the hight to the lower :
- Argument value (i.e. : --log-level)
- Environment value (i.e. : HALFAPI_LOGLEVEL)
- Configuration value under "domain" key
- Configuration value under "project" key
- Default configuration value given in the "DEFAULT_CONF" dictionary of halfapi/conf.py
### Logs
Small cleanup of the logs levels. If you don't want the config to be dumped, just set the HALFAPI_LOGLEVEL to something different than "DEBUG".
### Fixes
- Check an ACL based on a decorator on "/halfapi/acls/MY_ACL"
## 0.6.27
### Breaking changes
- ACLs definition can now include a "public" parameter that defines if there should be an automatic creation of a route to check this acls
- /halfapi/acls does not return the "result", it just returns if there is a public route to check the ACL on /halfapi/acls/acl_name
=======
argument of starlette instead.
>>>>>>> a8c59c6 ([release] halfapi 0.6.27)
## 0.6.26
- Adds the "base_url", "cookies" and "url" to the "halfapi" argument of route definitions
## 0.6.25
- Deletes the "Authorization" cookie on authentication error
- Do not raise an exception on signature expiration, use "Nobody" user instead
## 0.6.24
- Uses the "Authorization" cookie to read authentication token additionnaly to the "Authorization" header
- CLI : allows to run a single domain using the "halfapi domain --run domain_name" command
## 0.6.23
Dependency update version
- starlette v0.23
- orjson v3.8.5
- click v8
- pyJWT v2.6
- pyYAML v6
- toml v0.10
## 0.6.22
- IMPORTANT : Fix bug introduced with 0.6.20 (fix arguments handling)
- BREAKING : A domain should now include it's meta datas in a "domain" dictionary located in the __init__.py file of the domain's root. Consider looking in 'tests/dummy_domain/__init__.py'
- Add *html* return type as default argument ret_type
- Add *txt* return type
- Log unhandled exceptions
- Log HTTPException with statuscode 500 as critical
- PyJWT >=2.4.0,<2.5.0
## 0.6.21
- Store only domain's config in halfapi['config']
- Should run halfapi domain with config_file argument
- Testing : You can specify a "MODULE" attribute to point out the path to the Api's base module
- Testing : You can specify a "CONFIG" attribute to set the domain's testing configuration
- Environment : HALFAPI_DOMAIN_MODULE can be set to specify Api's base module
- Config : 'module' attribute can be set to specify Api's base module
## 0.6.20
- Fix arguments handling
## 0.6.19
- Allow file sending in multipart request (#32)
- Add python-multipart dependency
## 0.6.18
- Fix config coming from .halfapi/config when using HALFAPI_DOMAIN_NAME environment variable
## 0.6.17
- Fix 0.6.16
- Errata : HALFAPI_DOMAIN is HALFAPI_DOMAIN_NAME
- Testing : You can now specify "MODULE" class attribute for your "test_domain"
subclasses
## 0.6.16
- The definition of "HALFAPI_DOMAIN_MODULE" environment variable allows to
specify the base module for a domain structure. It is formatted as a python
import path.
The "HALFAPI_DOMAIN" specifies the "name" of the module
## 0.6.15
- Allows to define a "__acl__" variable in the API module's __init__.py, to
specify how to import the acl lib. You can also specify "acl" in the domain's
config
## 0.6.14
- Add XLSXResponse (with format argument set to "xlsx"), to return .xlsx files
## 0.6.13
- (rollback from 0.6.12) Remove pytest from dependencies in Docker file and
remove tests
- (dep) Add "packaging" dependency
- Add dependency check capability when instantiating a domain (__deps__
variable, see in dummy_domain)
## 0.6.12
- Installs pytest with dependencies in Docker image, tests when building image
## 0.6.11
- Fix "request" has no "path_params" attribute bug
## 0.6.10
- Add "x-out" field in HTTP headers when "out" parameters are specified for a
route
- Add "out" kwarg for not-async functions that specify it
## 0.6.9
- Hide data fields in args_check logs
## 0.6.8
- Fix testing lib for domains (add default secret and debug option)
## 0.6.2
- Domains now need to include the following variables in their __init__.py
- __name__ (str, optional)
- __id__ (str, optional)
- halfapi domain
## 0.1.0
- Mounts domain routers with their ACLs as decorator

11
Dockerfile Normal file
View File

@ -0,0 +1,11 @@
# syntax=docker/dockerfile:1
FROM python:alpine3.19
COPY . /halfapi
WORKDIR /halfapi
ENV VENV_DIR=/opt/venv
RUN mkdir -p $VENV_DIR
RUN python -m venv $VENV_DIR
RUN $VENV_DIR/bin/pip install gunicorn uvicorn
RUN $VENV_DIR/bin/pip install .
RUN ln -s $VENV_DIR/bin/halfapi /usr/local/bin/
CMD $VENV_DIR/bin/gunicorn halfapi.app

View File

@ -1,4 +1,5 @@
Copyright (c) 2015-2020 Joël Maïzi <joel.maizi@collorg.org>
Copyright (c) 2020-2021 Maxime ALVES <maxime@freepoteries.fr>, Joël Maïzi
<joel.maizi@collorg.org>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by

33
Pipfile
View File

@ -4,18 +4,33 @@ url = "https://pypi.org/simple"
verify_ssl = true
[dev-packages]
pytest = "*"
pytest = ">=7,<8"
requests = "*"
pytest-asyncio = "*"
pylint = "*"
build = "*"
twine = "*"
pyflakes = "*"
vulture = "*"
virtualenv = "*"
httpx = "*"
openapi-schema-validator = "*"
openapi-spec-validator = "*"
coverage = "*"
[packages]
click = "*"
starlette = "*"
uvicorn = "*"
jwt = "*"
[requires]
python_version = "3.7"
click = ">=8,<9"
starlette = ">=0.37,<0.38"
uvicorn = ">=0.13,<1"
orjson = ">=3.8.5,<4"
pyjwt = ">=2.6.0,<2.7.0"
pyyaml = ">=6.0.1,<7"
timing-asgi = ">=0.2.1,<1"
schema = ">=0.7.4,<1"
toml = ">=0.10,<0.11"
pip = "*"
packaging = ">=19.0"
python-multipart = "*"
[scripts]
halfapi = "halfapi.cli.cli:cli"
halfapi = "python -m halfapi"

1415
Pipfile.lock generated

File diff suppressed because it is too large Load Diff

128
README.md
View File

@ -1,18 +1,9 @@
# HalfAPI
Base tools to develop complex API with rights management.
This Python-based ASGI application aims to provide the core functionality to
multiple API domains.
It's developed at the [LIRMM](https://lirmm.fr) in Montpellier, France.
The name "halfAPI" comes from the deep relationship between it and
[halfORM](https://gite.lirmm.fr/newsi/halfORM), a project authored by
[Joël Maizi](https://gite.lirmm.fr/maizi).
You'll need a database with the API details. You can find the database model in halfapi/models/api.sql
**TODO :** include a token generation tool for testing purpose.
This project was developped by Maxime Alves and Joël Maïzi. The name was chosen
to reference [HalfORM](https://github.com/collorg/halfORM), a project written by Joël Maïzi.
## Dependencies
@ -20,67 +11,94 @@ You'll need a database with the API details. You can find the database model in
- python3
- python3-pip
- libgit2-dev
- starlette
- PyJWT
- click
- uvicorn
- orjson
- pyyaml
## Installing
## Configuration
Configure HalfAPI in the file : .halfapi/config .
It's a **toml** file that contains at least two sections, project and domains.
https://toml.io/en/
With local folder :
### Project
`pip3 install -r requirements.txt .[cli]`
The main configuration options without which HalfAPI cannot be run.
**secret** : The file containing the secret to decode the user's tokens.
**port** : The port for the test server.
**loglevel** : The log level (info, debug, critical, ...)
From the repository :
`pip3 install git+ssh://git@gite.lirmm.fr:malves/halfapi.git[cli]`
## CLI usage
## Running
### Development mode
In the project's folder :
`halfapi run`
### Production
The production server may use different init systems. As our main server is Debian-based, we use systemd services to manage the api server. Find the right service files and configure them properly in order to make your production setup perfect.
### Domains
Specify the domains configurations in the following form :
```
cp conf/systemd/lirmm_api* /etc/systemd/system/
systemctl daemon-reload
systemctl start lirmm_api
[domains.DOMAIN_NAME]
name = "DOMAIN_NAME"
enabled = true
prefix = "/prefix"
module = "domain_name.path.to.api.root"
port = 1002
```
To make it start at boot :
`systemctl enable lirmm_api`
To get the logs :
Specific configuration can be done under the "config" section :
```
journalctl -f --unit lirmm_api
[domains.DOMAIN_NAME.config]
boolean_option = false
string_value = "String"
answer = 42
listylist = ["hello", "world"]
```
And can be accessed through the app's "config" dictionnary.
## Testing
### Installing
## Usage
pip3 install .[cli][tests]
Develop an HalfAPI domain by following the examples located in
tests/dummy_domain . An HalfAPI domain should be an importable python module
that is available in the python path.
### Running
Run the project by using the `halfapi run` command.
pytest -v
You can try the dummy_domain with the following command.
```
PYTHONPATH=$PWD/tests python -m halfapi domain dummy_domain
```
### CLI documentation
Use the CLI help.
```
python -m halfapi --help
python -m halfapi domain --help
```
## API Testing
@TODO
### Example
Check out the [sample project](https://github.com/halfAPI/halfapi_sample_project)
that helps you to build your own domain.
## Development
@TODO

View File

@ -1,5 +1,5 @@
#!/usr/bin/env python3
__version__ = '0.1.0'
__version__ = '0.6.30'
def version():
return f'HalfAPI version:{__version__}'

3
halfapi/__main__.py Normal file
View File

@ -0,0 +1,3 @@
from .cli.cli import cli
if __name__ == '__main__':
cli()

View File

@ -1,45 +1,7 @@
#!/usr/bin/env python3
# asgi framework
from starlette.applications import Starlette
from starlette.authentication import UnauthenticatedUser
from starlette.middleware import Middleware
from starlette.responses import Response, JSONResponse
from starlette.routing import Route
from starlette.middleware.authentication import AuthenticationMiddleware
import os
from .halfapi import HalfAPI
from .logging import logger
# typing
from typing import Any, Awaitable, Callable, MutableMapping
# module libraries
from halfapi.conf import HOST, PORT, DB_NAME, SECRET, PRODUCTION
from halfapi.lib.jwt_middleware import JWTAuthenticationBackend
from halfapi.lib.responses import *
from halfapi.lib.routes import get_routes
debug_routes = [
Route('/', lambda request, *args, **kwargs: PlainTextResponse('It Works!')),
Route('/user', lambda request, *args, **kwargs:
JSONResponse({'user':request.user.json})
if type(request.user) != UnauthenticatedUser
else JSONResponse({'user':False})),
Route('/payload', lambda request, *args, **kwargs:
JSONResponse({'payload':str(request.payload)}))
] if not PRODUCTION else []
application = Starlette(
debug=not PRODUCTION,
routes=debug_routes + get_routes(),
middleware=[
Middleware(AuthenticationMiddleware,
backend=JWTAuthenticationBackend(secret_key=SECRET))
],
exception_handlers={
401: UnauthorizedResponse,
404: NotFoundResponse,
500: InternalServerErrorResponse,
501: NotImplementedResponse
}
)
def application():
from .conf import CONFIG
return HalfAPI(CONFIG).application

View File

@ -1 +0,0 @@
#!/usr/bin/env

View File

@ -1,20 +1,28 @@
#!/usr/bin/env python3
"""
cli/cli.py Main entry point of halfapi cli tool
The init command is the only command loaded if not in a *project dir*, and it is
not loaded otherwise.
"""
# builtins
import click
from halfapi.conf import IS_PROJECT
@click.group(invoke_without_command=True)
@click.option('--version', is_flag=True)
@click.pass_context
def cli(ctx, version):
"""
HalfAPI Cli entry point
It uses the Click library
"""
if version:
import halfapi
return click.echo(halfapi.version())
from halfapi import version
click.echo(version())
if IS_PROJECT:
import halfapi.cli.config
import halfapi.cli.domain
import halfapi.cli.run
else:
import halfapi.cli.init
from . import config
from . import domain
from . import run
from . import init
from . import routes

View File

@ -1,34 +1,31 @@
#!/usr/bin/env python3
"""
cli/config.py Contains the .halfapi/config
Defines the "halfapi config" command
"""
import click
from .cli import cli
from halfapi.conf import (
PROJECT_NAME,
DOMAINS,
CONF_DIR,
HOST,
PORT,
DB_NAME,
PRODUCTION,
BASE_DIR
)
from ..conf import CONFIG
CONF_STR=f"""
DOMAIN_CONF_STR="""
[domain]
name = {name}
router = {router}
"""
CONF_STR="""
[project]
name = {PROJECT_NAME}
host = {HOST}
port = {PORT}
production = {PRODUCTION}
base_dir = {BASE_DIR}
host = {host}
port = {port}
production = {production}
"""
[domains]"""
for dom in DOMAINS:
CONF_STR = '\n'.join((CONF_STR, dom))
@cli.command()
def config():
"""
Lists config parameters and their values
"""
click.echo(CONF_STR)
click.echo(CONF_STR.format(**CONFIG))

View File

@ -1,306 +1,242 @@
#!/usr/bin/env python3
"""
cli/domain.py Defines the "halfapi domain" cli commands
"""
# builtins
import os
import sys
import logging
import click
import importlib
from .cli import cli
from halfapi.conf import DOMAINS, BASE_DIR
from halfapi.db import (
Domain,
APIRouter,
APIRoute,
AclFunction,
Acl)
import subprocess
logger = logging.getLogger('halfapi')
import json
import toml
import click
import orjson
import uvicorn
from .cli import cli
from ..conf import CONFIG
from ..half_domain import HalfDomain
from ..lib.routes import api_routes
from ..lib.responses import ORJSONResponse
from ..conf import CONFIG, PROJECT_LEVEL_KEYS
from ..logging import logger
#################
# domain create #
#################
def create_domain():
sys.exit(0)
def create_domain(domain_name: str, module_path: str):
logger.info('Will add **%s** (%s) to current halfAPI project',
domain_name, module_path)
#if domain_name in DOMAINSDICT():
# logger.warning('Domain **%s** is already in project', domain_name)
# sys.exit(1)
def domain_tree_create():
def create_init(path):
with open(os.path.join(os.getcwd(), path, '__init__.py'), 'w') as f:
f.writelines([
'"""',
f'name: {domain_name}',
f'router: {module_path}',
'"""'
])
logger.debug('created %s', os.path.join(os.getcwd(), path, '__init__.py'))
def create_acl(path):
with open(os.path.join(path, 'acl.py'), 'w') as f:
f.writelines([
'from halfapi.lib.acl import public, ACLS',
])
logger.debug('created %s', os.path.join(path, 'acl.py'))
os.mkdir(domain_name)
create_init(domain_name)
router_path = os.path.join(domain_name, 'routers')
create_acl(domain_name)
os.mkdir(router_path)
create_init(router_path)
# TODO: Generate config file
domain_tree_create()
"""
try:
importlib.import_module(module_path)
except ImportError:
logger.error('cannot import %s', domain_name)
domain_tree_create()
"""
"""
try:
importlib.import_module(domain_name)
except ImportError:
click.echo('Error in domain creation')
logger.debug('%s', subprocess.run(['tree', 'a', os.getcwd()]))
raise Exception('cannot create domain {}'.format(domain_name))
"""
###############
# domain read #
###############
def list_routes(domain):
click.echo(f'\nDomain : {domain}')
routers = APIRouter(domain=domain)
for router in routers.select():
routes = APIRoute(domain=domain, router=router['name'])
click.echo('# /{name}'.format(**router))
for route in routes.select():
route.pop('fct_name')
acls = ', '.join([ acl['acl_fct_name'] for acl in Acl(**route).select() ])
route['acls'] = acls
click.echo('- [{http_verb}] {path} ({acls})'.format(**route))
def list_routes(domain, m_dom):
"""
Echoes the list of the **m_dom** active routes
"""
#################
# domain update #
#################
def update_db(domain):
click.echo(f'\nDomain : {domain}\n')
def add_domain():
"""
Inserts Domain into database
"""
new_domain = Domain(name=domain)
if len(new_domain) == 0:
click.echo(f'New domain {domain}')
new_domain.insert()
routes = api_routes(m_dom)[0]
if len(routes):
for key, item in routes.items():
methods = '|'.join(list(item.keys()))
click.echo(f'\t{key} : {methods}')
else:
click.echo('\t**No ROUTES**')
raise Exception('Routeless domain')
def add_router(name):
"""
Inserts Router into database
Parameters:
- name (str): The Router's name
"""
router = APIRouter()
router.name = name
router.domain = domain
def list_api_routes():
"""
Echoes the list of all active domains.
if len(router) == 0:
router.insert()
def add_acl_fct(fct):
"""
Inserts ACL function into database
Parameters:
- fct (Callable): The ACL function reference
"""
acl = AclFunction()
acl.domain = domain
acl.name = fct.__name__
if len(acl) == 0:
acl.insert()
def add_acls(acls, **route):
"""
Inserts ACL into database
Parameters:
- acls [(Callable, [str])]: List of the Route's ACL's
- route (dict): The Route
"""
route.pop('fct_name')
acl = Acl(**route)
for fct, keys in acls:
acl.acl_fct_name = fct.__name__
if len(acl) != 0:
raise Exception(
'An ACL row for this route with this function already exists. Check your routers')
else:
acl.keys = keys
add_acl_fct(fct)
acl.insert()
elif fct is None:
acl.delete()
def get_fct_name(http_verb, path):
"""
Returns the predictable name of the function for a route
Parameters:
- http_verb (str): The Route's HTTP method (GET, POST, ...)
- path (str): A path beginning by '/' for the route
Returns:
str: The *unique* function name for a route and it's verb
Examples:
>>> get_fct_name('foo', 'bar')
Traceback (most recent call last):
...
Exception: Malformed path
>>> get_fct_name('get', '/')
'get_'
>>> get_fct_name('GET', '/')
'get_'
>>> get_fct_name('POST', '/foo')
'post_foo'
>>> get_fct_name('POST', '/foo/bar')
'post_foo_bar'
>>> get_fct_name('DEL', '/foo/{boo}/{far}/bar')
'del_foo_BOO_FAR_bar'
>>> get_fct_name('DEL', '/foo/{boo:zoo}')
'del_foo_BOO'
"""
if path[0] != '/':
raise Exception('Malformed path')
elts = path[1:].split('/')
fct_name = [http_verb.lower()]
for elt in elts:
if elt and elt[0] == '{':
fct_name.append(elt[1:-1].split(':')[0].upper())
else:
fct_name.append(elt)
return '_'.join(fct_name)
def add_route(http_verb, path, router, acls):
"""
Inserts Route into database
Parameters:
- http_verb (str): The Route's HTTP method (GET, POST, ...)
- path (str): A path beginning by '/' for the route
- router (str): The Route's Router name
- acls (List[Callable]): The list of ACL functions for this Route
"""
click.echo(f'Adding route /{domain}/{router}{path}')
route = APIRoute()
# Route definition
route.http_verb = http_verb
route.path = path
route.fct_name = get_fct_name(http_verb, path)
route.router = router
route.domain = domain
if len(route) == 0:
route.insert()
add_acls(acls, **route.to_dict())
sys.path.insert(0, BASE_DIR)
# Reset Domain relations
delete_domain(domain)
acl_set = set()
try:
# Module retrieval
dom_mod = importlib.import_module(domain)
except ImportError:
# Domain is not available in current PYTHONPATH
click.echo(f"Can't import *{domain}*", err=True)
return False
try:
add_domain()
except Exception as e:
# Could not insert Domain
# @TODO : Insertion exception handling
click.echo(e)
click.echo(f"Could not insert *{domain}*", err=True)
return False
# add sub routers
try:
ROUTERS = dom_mod.ROUTERS
except AttributeError:
# No ROUTERS variable in current domain, check domain/__init__.py
click.echo(f'The domain {domain} has no *ROUTERS* variable', err=True)
for router_name in dom_mod.ROUTERS:
try:
router_mod = getattr(dom_mod.routers, router_name)
except AttributError:
# Missing router, continue
click.echo(f'The domain {domain} has no *{router_name}* router', err=True)
continue
try:
add_router(router_name)
except Exception as e:
# Could not insert Router
# @TODO : Insertion exception handling
print(e)
continue
for route_path, route_params in router_mod.ROUTES.items():
for http_verb, acls in route_params.items():
try:
# Insert a route and it's ACLS
add_route(http_verb, route_path, router_name, acls)
except Exception as e:
# Could not insert route
# @TODO : Insertion exception handling
print(e)
continue
#################
# domain delete #
#################
def delete_domain(domain):
d = Domain(name=domain)
if len(d) != 1:
return False
d.delete(delete_all=True)
return True
TODO: Rewrite function
"""
click.echo('# API Routes')
# for domain, m_dom in DOMAINSDICT().items():
# list_routes(domain, m_dom)
@click.option('--devel',default=None, is_flag=True)
@click.option('--watch',default=False, is_flag=True)
@click.option('--production',default=None, is_flag=True)
@click.option('--port',default=None, type=int)
@click.option('--log-level',default=None, type=str)
@click.option('--dry-run',default=False, is_flag=True)
@click.option('--run',default=False, is_flag=True)
@click.option('--read',default=False, is_flag=True)
@click.option('--conftest',default=False, is_flag=True)
@click.option('--create',default=False, is_flag=True)
@click.option('--update',default=False, is_flag=True)
@click.option('--delete',default=False, is_flag=True)
@click.option('--domains',default=None)
@click.argument('config_file', type=click.File(mode='rb'), required=False)
@click.argument('domain',default=None, required=False)
@cli.command()
def domain(domains, delete, update, create, read): #, domains, read, create, update, delete):
def domain(domain, config_file, delete, update, create, conftest, read, run, dry_run, log_level, port, production, watch, devel):
"""
Lists routes for the specified domains, or update them in the database
The "halfapi domain" command
Parameters:
domain (List[str]|None): The list of the domains to list/update
domain (str|None): The domain name
The parameter has a misleading name as it is a multiple option
but this would be strange to use it several times named as "domains"
update (boolean): If set, update the database for the selected domains
"""
if not domains:
if not domain:
if create:
return create_domain()
# TODO: Connect to the create_domain function
raise NotImplementedError
raise Exception('Missing domain name')
if config_file:
ARG_CONFIG = toml.load(config_file.name)
if 'project' in ARG_CONFIG:
for key, value in ARG_CONFIG['project'].items():
if key in PROJECT_LEVEL_KEYS:
CONFIG[key] = value
if 'domain' in ARG_CONFIG and domain in ARG_CONFIG['domain']:
for key, value in ARG_CONFIG['domain'][domain].items():
if key in PROJECT_LEVEL_KEYS:
CONFIG[key] = value
CONFIG['domain'].update(ARG_CONFIG['domain'])
if create:
raise NotImplementedError
elif update:
raise NotImplementedError
elif delete:
raise NotImplementedError
elif read:
from ..halfapi import HalfAPI
halfapi = HalfAPI(CONFIG)
click.echo(orjson.dumps(
halfapi.domains[domain].schema(),
option=orjson.OPT_NON_STR_KEYS,
default=ORJSONResponse.default_cast)
)
domains = DOMAINS
else:
domains_ = []
for domain_name in domains.split(','):
if domain_name in DOMAINS:
domains.append(domain_name)
continue
if dry_run:
CONFIG['dryrun'] = True
click.echo(
f'Domain {domain_name}s is not activated in the configuration')
domains = CONFIG.get('domain')
for key in domains.keys():
if key != domain:
domains[key]['enabled'] = False
else:
domains[key]['enabled'] = True
domains = domains_
for domain in domains:
if update:
update_db(domain)
if delete:
delete_domain(domain)
if not log_level:
log_level = CONFIG.get('domain', {}).get('loglevel', CONFIG.get('loglevel', False))
else:
list_routes(domain)
CONFIG['loglevel'] = log_level
if not port:
port = CONFIG.get('domain', {}).get('port', CONFIG.get('port', False))
else:
CONFIG['port'] = port
if devel is None and production is not None and (production is False or production is True):
CONFIG['production'] = production
if devel is not None:
CONFIG['production'] = False
CONFIG['loglevel'] = 'debug'
if conftest:
click.echo(
toml.dumps(CONFIG)
)
else:
# domain section port is preferred, if it doesn't exist we use the global one
uvicorn_kwargs = {}
if CONFIG.get('port'):
uvicorn_kwargs['port'] = CONFIG['port']
if CONFIG.get('loglevel'):
uvicorn_kwargs['log_level'] = CONFIG['loglevel'].lower()
if watch:
uvicorn_kwargs['reload'] = True
uvicorn.run(
'halfapi.app:application',
factory=True,
**uvicorn_kwargs
)
sys.exit(0)

View File

@ -1,40 +1,43 @@
#!/usr/bin/env python3
"""
cli/init.py Defines the "halfapi init" cli commands
Helps the user to create a new project
"""
# builtins
import os
import sys
import re
import click
import logging
from halfapi import __version__
from halfapi.cli.lib.db import ProjectDB
import click
from .. import __version__
from ..conf import CONF_DIR
from .cli import cli
logger = logging.getLogger('halfapi')
from ..logging import logger
TMPL_HALFAPI_ETC = """[project]
name = {project}
host = 127.0.0.1
host = 127.0.0.1
port = 8000
secret = /path/to/secret_file
production = False
base_dir = {base_dir}
"""
def format_halfapi_etc(project, path):
return TMPL_HALFAPI_ETC.format(
project=project,
base_dir=path
)
TMPL_HALFAPI_CONFIG = """[project]
name = {name}
halfapi_version = {halfapi_version}
[domain]
"""
@click.argument('project')
@cli.command()
def init(project):
"""
The "halfapi init" command
"""
if not re.match('^[a-z0-9_]+$', project, re.I):
click.echo('Project name must match "^[a-z0-9_]+$", retry.', err=True)
sys.exit(1)
@ -44,28 +47,15 @@ def init(project):
sys.exit(1)
click.echo(f'create directory {project}')
logger.debug('Create directory %s', project)
os.mkdir(project)
try:
pdb = ProjectDB(project)
pdb.init()
except Exception as e:
logger.warning(e)
logger.debug(os.environ.get('HALFORM_CONF_DIR'))
raise e
logger.debug('Create directory %s/.halfapi', project)
os.mkdir(f'{project}/.halfapi')
os.mkdir(os.path.join(project, '.halfapi'))
open(os.path.join(project, '.halfapi', 'domains'), 'w').write('[domains]\n')
config_file = os.path.join(project, '.halfapi', 'config')
with open(config_file, 'w') as f:
f.write(TMPL_HALFAPI_CONFIG.format(
name=project,
halfapi_version=__version__
))
with open(f'{project}/.halfapi/config', 'w') as conf_file:
conf_file.write(TMPL_HALFAPI_CONFIG.format(
halfapi_version=__version__))
click.echo(f'Insert this into the HALFAPI_CONF_DIR/{project} file')
click.echo(format_halfapi_etc(
project,
os.path.abspath(project)))
click.echo(f'Configure halfapi project in {CONF_DIR}/{project}')

View File

@ -1,116 +0,0 @@
import click
from half_orm.model import Model, CONF_DIR
from half_orm.model_errors import MissingConfigFile
import psycopg2
import subprocess
_DB_SCHEMA = """
create schema api;
create type verb as enum ('POST', 'GET', 'PUT', 'DELETE');
create table api.domain (
name text,
primary key (name)
);
create table api.router (
name text,
domain text,
primary key (name, domain)
);
alter table api.router add constraint router_domain_fkey foreign key (domain) references api.domain(name) on update cascade on delete cascade;
create table api.route (
http_verb verb,
path text, -- relative to /<domain>/<router>
fct_name text,
router text,
domain text,
primary key (http_verb, path, router, domain)
);
alter table api.route add constraint route_router_fkey foreign key (router, domain) references api.router(name, domain) on update cascade on delete cascade;
create table api.acl_function (
name text,
description text,
domain text,
primary key (name, domain)
);
alter table api.acl_function add constraint acl_function_domain_fkey foreign key (domain) references api.domain(name) on update cascade on delete cascade;
create table api.acl (
http_verb verb,
path text not null,
router text,
domain text,
acl_fct_name text,
keys text[],
primary key (http_verb, path, router, domain, acl_fct_name)
);
alter table api.acl add constraint acl_route_fkey foreign key (http_verb, path,
router, domain) references api.route(http_verb, path, router, domain) on update cascade on delete cascade;
alter table api.acl add constraint acl_function_fkey foreign key (acl_fct_name, domain) references api.acl_function(name, domain) on update cascade on delete cascade;
create schema "api.view";
create view "api.view".route as
select
route.*,
'/'::text || route.domain || '/'::text || route.router || route.path AS abs_path
from
api.route
join api.domain on
route.domain = domain.name
;
create view "api.view".acl as
select
acl.*,
'/'::text || route.domain || '/'::text || route.router || route.path AS abs_path
from
api.acl
join api.acl_function on
acl.acl_fct_name = acl_function.name
join api.route on
acl.domain = route.domain
and acl.router = route.router
and acl.path = route.path;
"""
HOP_CONF = """[database]
name = {}
"""
class ProjectDB:
def __init__(self, project_name):
self.__project_name = project_name
self.__db_name = f'halfapi_{self.__project_name}'
self.__db = self._get_db()
def _get_db(self):
from subprocess import PIPE
hop_conf_file = f'{CONF_DIR}/{self.__db_name}'
try:
return Model(self.__db_name)
except psycopg2.OperationalError as err:
"créer la base de données"
ret = subprocess.run(['/usr/bin/createdb', self.__db_name])
if ret.returncode != 0:
raise Exception(f"Can't create {self.__db_name}")
except MissingConfigFile:
print(f"demande validation de {CONF_DIR} {self.__db_name}")
print("demande validation création de fichier de CONF")
open(hop_conf_file, 'w').write(HOP_CONF.format(self.__db_name))
return self._get_db()
def init(self):
"""
"""
self.__db.execute_query(_DB_SCHEMA)
self.__db._connection.close()

54
halfapi/cli/routes.py Normal file
View File

@ -0,0 +1,54 @@
#!/usr/bin/env python3
"""
cli/routes.py Defines the "halfapi routes" cli commands
"""
# builtins
import sys
import importlib
from pprint import pprint
import orjson
import click
from .cli import cli
from ..logging import logger
# from ..lib.domain import domain_schema_dict
from ..lib.constants import DOMAIN_SCHEMA, ROUTE_SCHEMA
from ..lib.responses import ORJSONResponse
@click.argument('module', required=True)
@click.option('--export', default=False, is_flag=True)
@click.option('--validate', default=False, is_flag=True)
@click.option('--check', default=False, is_flag=True)
@click.option('--noheader', default=False, is_flag=True)
@click.option('--schema', default=False, is_flag=True)
@cli.command()
def routes(module, export=False, validate=False, check=False, noheader=False, schema=False):
"""
The "halfapi routes" command
"""
# try:
#  mod = importlib.import_module(module)
# except ImportError as exc:
#  raise click.BadParameter('Cannot import this module', param=module) from exc
# if export:
#  click.echo(schema_to_csv(module, header=not noheader))
# if schema:
#  routes_d = domain_schema_dict(mod)
#  ROUTE_SCHEMA.validate(routes_d)
#  click.echo(orjson.dumps(routes_d,
#  option=orjson.OPT_NON_STR_KEYS,
#  default=ORJSONResponse.default_cast))
# if validate:
#  routes_d = domain_schema_dict(mod)
#  try:
#  ROUTE_SCHEMA.validate(routes_d)
#  except Exception as exc:
#  raise exc

View File

@ -1,36 +1,87 @@
#!/usr/bin/env python3
"""
cli/domain.py Defines the "halfapi run" cli command
"""
import os
import sys
import click
import uvicorn
from .cli import cli
from halfapi.cli.domain import list_routes
from halfapi.conf import (HOST, PORT,
PRODUCTION, BASE_DIR, DOMAINS)
from .domain import list_api_routes
from ..conf import CONFIG, SCHEMA
from ..logging import logger
from ..lib.schemas import schema_csv_dict
from ..half_domain import HalfDomain
@click.option('--host', default=None)
@click.option('--port', default=None)
@click.option('--host', default=CONFIG.get('host'))
@click.option('--port', default=CONFIG.get('port'))
@click.option('--reload', default=False)
@click.option('--secret', default=CONFIG.get('secret'))
@click.option('--production', default=CONFIG.get('secret'))
@click.option('--loglevel', default=CONFIG.get('loglevel'))
@click.option('--prefix', default='/')
@click.option('--check', default=True)
@click.option('--dryrun', default=False, is_flag=True)
@click.argument('schema', type=click.File('r'), required=False)
@click.argument('domain', required=False)
@cli.command()
def run(host, port):
if not host:
host = HOST
if not port:
port = PORT
def run(host, port, reload, secret, production, loglevel, prefix, check, dryrun,
schema, domain):
"""
The "halfapi run" command
"""
logger.debug('[run] host=%s port=%s reload=%s secret=%s production=%s loglevel=%s prefix=%s schema=%s',
host, port, reload, secret, production, loglevel, prefix, schema
)
port = int(port)
debug = reload = not PRODUCTION
log_level = 'info' if PRODUCTION else 'debug'
if production and reload:
reload = False
raise Exception('Can\'t use live code reload in production')
click.echo('Launching application')
click.echo(f'Launching application')
sys.path.insert(0, BASE_DIR)
if secret:
CONFIG['secret'] = secret
[ list_routes(domain) for domain in DOMAINS ]
if schema:
# Populate the SCHEMA global with the data from the given file
for key, val in schema_csv_dict(schema, prefix).items():
SCHEMA[key] = val
if domain:
# If we specify a domain to run as argument
if 'domain' not in CONFIG:
CONFIG['domain'] = {}
# Disable all domains
keys = list(CONFIG.get('domain').keys())
for key in keys:
CONFIG['domain'].pop(key)
# And activate the desired one, mounted without prefix
CONFIG['domain'][domain] = {
'name': domain,
'prefix': False,
'enabled': True
}
# list_api_routes()
click.echo(f'uvicorn.run("halfapi.app:application"\n' \
f'host: {host}\n' \
f'port: {port}\n' \
f'log_level: {loglevel}\n' \
f'reload: {reload}\n'
)
if dryrun:
CONFIG['dryrun'] = True
uvicorn.run('halfapi.app:application',
host=host,
port=int(port),
log_level=log_level,
log_level=loglevel,
reload=reload)

View File

@ -1,61 +1,205 @@
#!/usr/bin/env python3
"""
conf.py reads the current configuration files
It uses the following environment variables :
- HALFAPI_CONF_DIR (default: /etc/half_api)
It defines the following globals :
- PROJECT_NAME (str) - HALFAPI_PROJECT_NAME
- PRODUCTION (bool) - HALFAPI_PRODUCTION
- LOGLEVEL (str) - HALFAPI_LOGLEVEL
- BASE_DIR (str) - HALFAPI_BASE_DIR
- HOST (str) - HALFAPI_HOST
- PORT (int) - HALFAPI_PORT
- CONF_DIR (str) - HALFAPI_CONF_DIR
- DRYRUN (bool) - HALFAPI_DRYRUN
It reads the following ressource :
- ./.halfapi/config
It follows the following format :
[project]
halfapi_version = HALFAPI_VERSION
[domain.domain_name]
name = domain_name
routers = routers
[domain.domain_name.config]
option = Argh
"""
from .logging import logger
import os
from os import environ
import sys
from configparser import ConfigParser
import importlib
import tempfile
import uuid
IS_PROJECT = os.path.isfile('.halfapi/config')
import toml
if IS_PROJECT:
SCHEMA = {}
default_config = {
'project': {
'host': '127.0.0.1',
'port': '8000',
'secret': '',
'base_dir': '',
'production': 'no'
}
DEFAULT_CONF = {
# Default configuration values
'SECRET': tempfile.mkstemp()[1],
'PROJECT_NAME': os.getcwd().split('/')[-1],
'PRODUCTION': True,
'HOST': '127.0.0.1',
'PORT': 3000,
'LOGLEVEL': 'info',
'BASE_DIR': os.getcwd(),
'CONF_FILE': '.halfapi/config',
'CONF_DIR': '/etc/half_api',
'DRYRUN': None
}
PROJECT_LEVEL_KEYS = {
# Allowed keys in "project" section of configuration file
'project_name',
'production',
'secret',
'host',
'port',
'loglevel',
'dryrun'
}
DOMAIN_LEVEL_KEYS = PROJECT_LEVEL_KEYS | {
# Allowed keys in "domain" section of configuration file
'name',
'module',
'prefix',
'enabled'
}
CONF_FILE = os.environ.get('HALFAPI_CONF_FILE', DEFAULT_CONF['CONF_FILE'])
CONF_DIR = os.environ.get('HALFAPI_CONF_DIR', DEFAULT_CONF['CONF_DIR'])
HALFAPI_ETC_FILE=os.path.join(
CONF_DIR, 'config'
)
BASE_DIR = os.environ.get('HALFAPI_BASE_DIR', DEFAULT_CONF['BASE_DIR'])
HALFAPI_DOT_FILE=os.path.join(
BASE_DIR, '.halfapi', 'config')
HALFAPI_CONFIG_FILES = []
try:
with open(HALFAPI_ETC_FILE, 'r'):
HALFAPI_CONFIG_FILES.append(HALFAPI_ETC_FILE)
except FileNotFoundError:
logger.info('Cannot find a configuration file under %s', HALFAPI_ETC_FILE)
try:
with open(HALFAPI_DOT_FILE, 'r'):
HALFAPI_CONFIG_FILES.append(HALFAPI_DOT_FILE)
except FileNotFoundError:
logger.info('Cannot find a configuration file under %s', HALFAPI_DOT_FILE)
ENVIRONMENT = {}
# Load environment variables allowed in configuration
if 'HALFAPI_DRYRUN' in os.environ:
ENVIRONMENT['dryrun'] = True
if 'HALFAPI_PROD' in os.environ:
ENVIRONMENT['production'] = bool(os.environ.get('HALFAPI_PROD'))
if 'HALFAPI_LOGLEVEL' in os.environ:
ENVIRONMENT['loglevel'] = os.environ.get('HALFAPI_LOGLEVEL').lower()
if 'HALFAPI_SECRET' in os.environ:
ENVIRONMENT['secret'] = os.environ.get('HALFAPI_SECRET')
if 'HALFAPI_HOST' in os.environ:
ENVIRONMENT['host'] = os.environ.get('HALFAPI_HOST')
if 'HALFAPI_PORT' in os.environ:
ENVIRONMENT['port'] = int(os.environ.get('HALFAPI_PORT'))
def read_config(filenames=HALFAPI_CONFIG_FILES):
"""
The highest index in "filenames" are the highest priorty
"""
d_res = {}
logger.info('Reading config files %s', filenames)
for CONF_FILE in filenames:
if os.path.isfile(CONF_FILE):
conf_dict = toml.load(CONF_FILE)
d_res.update(conf_dict)
logger.info('Read config files (result) %s', d_res)
return { **d_res.get('project', {}), 'domain': d_res.get('domain', {}) }
CONFIG = read_config()
CONFIG.update(**ENVIRONMENT)
PROJECT_NAME = CONFIG.get('project_name',
os.environ.get('HALFAPI_PROJECT_NAME', DEFAULT_CONF['PROJECT_NAME']))
if os.environ.get('HALFAPI_DOMAIN_NAME'):
# Force enabled domain by environment variable
DOMAIN_NAME = os.environ.get('HALFAPI_DOMAIN_NAME')
if 'domain' in CONFIG and DOMAIN_NAME in CONFIG['domain'] \
and 'config' in CONFIG['domain'][DOMAIN_NAME]:
domain_config = CONFIG['domain'][DOMAIN_NAME]['config']
else:
domain_config = {}
CONFIG['domain'] = {}
CONFIG['domain'][DOMAIN_NAME] = {
'enabled': True,
'name': DOMAIN_NAME,
'prefix': False
}
config = ConfigParser(allow_no_value=True)
config.read_dict(default_config)
config.read(filenames=['.halfapi/config'])
CONFIG['domain'][DOMAIN_NAME]['config'] = domain_config
PROJECT_NAME = config.get('project', 'name')
if os.environ.get('HALFAPI_DOMAIN_MODULE'):
# Specify the pythonpath to import the specified domain (defaults to global)
dom_module = os.environ.get('HALFAPI_DOMAIN_MODULE')
CONFIG['domain'][DOMAIN_NAME]['module'] = dom_module
if len(PROJECT_NAME) == 0:
raise Exception('Need a project name as argument')
if len(CONFIG.get('domain', {}).keys()) == 0:
logger.info('No domains')
DOMAINS = [domain for domain, _ in config.items('domains')] \
if config.has_section('domains') \
else []
CONF_DIR = environ.get('HALFAPI_CONF_DIR', '/etc/half_api')
# Secret
if 'secret' not in CONFIG:
# TODO: Create a temporary secret
CONFIG['secret'] = DEFAULT_CONF['SECRET']
with open(CONFIG['secret'], 'w') as secret_file:
secret_file.write(str(uuid.uuid4()))
HALFAPI_CONF_FILE=os.path.join(
CONF_DIR,
PROJECT_NAME
)
if not os.path.isfile(HALFAPI_CONF_FILE):
print(f'Missing {HALFAPI_CONF_FILE}, exiting')
sys.exit(1)
config.read(filenames=[HALFAPI_CONF_FILE])
try:
with open(CONFIG['secret'], 'r') as secret_file:
CONFIG['secret'] = CONFIG['secret'].strip()
except FileNotFoundError as exc:
logger.warning('Running without secret file: %s', CONFIG['secret'] or 'no file specified')
HOST = config.get('project', 'host')
PORT = config.getint('project', 'port')
DB_NAME = f'halfapi_{PROJECT_NAME}'
CONFIG.setdefault('project_name', DEFAULT_CONF['PROJECT_NAME'])
CONFIG.setdefault('production', DEFAULT_CONF['PRODUCTION'])
CONFIG.setdefault('host', DEFAULT_CONF['HOST'])
CONFIG.setdefault('port', DEFAULT_CONF['PORT'])
CONFIG.setdefault('loglevel', DEFAULT_CONF['LOGLEVEL'])
CONFIG.setdefault('dryrun', DEFAULT_CONF['DRYRUN'])
try:
with open(config.get('project', 'secret')) as secret_file:
SECRET = secret_file.read()
# Set the secret so we can use it in domains
os.environ['HALFAPI_SECRET'] = SECRET
except FileNotFoundError:
print('There is no file like {}'.format(config.get('project', 'secret')))
sys.exit(1)
PRODUCTION = config.getboolean('project', 'production') or False
os.environ['HALFAPI_PROD'] = str(PRODUCTION)
BASE_DIR = config.get('project', 'base_dir')
# !!!TO REMOVE!!!
SECRET = CONFIG.get('secret')
PRODUCTION = CONFIG.get('production')
# !!!

View File

@ -1,12 +0,0 @@
#!/usr/bin/env python3
from halfapi.conf import DB_NAME
# DB
from half_orm.model import Model
db = Model(DB_NAME)
Domain = db.get_relation_class('api.domain')
APIRouter = db.get_relation_class('api.router')
APIRoute = db.get_relation_class('api.route')
AclFunction = db.get_relation_class('api.acl_function')
Acl = db.get_relation_class('api.acl')
RouteACL = db.get_relation_class('api.view.acl')

513
halfapi/half_domain.py Normal file
View File

@ -0,0 +1,513 @@
import importlib
import inspect
import os
import re
from packaging.specifiers import SpecifierSet
from packaging.version import Version
from typing import Coroutine, Dict, Iterator, List, Tuple
from types import ModuleType, FunctionType
from schema import SchemaError
from starlette.applications import Starlette
from starlette.middleware import Middleware
from starlette.routing import Router, Route
from starlette.schemas import SchemaGenerator
from .lib.acl import AclRoute
from .lib.responses import ORJSONResponse
import yaml
from . import __version__
from .lib.constants import API_SCHEMA_DICT, ROUTER_SCHEMA, VERBS
from .half_route import HalfRoute
from .lib import acl as lib_acl
from .lib.responses import PlainTextResponse
from .lib.routes import JSONRoute
from .lib.schemas import param_docstring_default
from .lib.domain import MissingAclError, PathError, UnknownPathParameterType, \
UndefinedRoute, UndefinedFunction, get_fct_name, route_decorator
from .lib.domain_middleware import DomainMiddleware
from .logging import logger
class HalfDomain(Starlette):
def __init__(self, domain, module=None, router=None, acl=None, app=None):
"""
Parameters:
domain (str): Module name (should be importable)
router (str): Router name (should be importable from domain module
defaults to __router__ variable from domain module)
app (HalfAPI): The app instance
"""
self.app = app
self.m_domain = importlib.import_module(domain) if module is None else module
self.d_domain = getattr(self.m_domain, 'domain', domain)
self.name = self.d_domain['name']
self.id = self.d_domain['id']
self.version = self.d_domain['version']
self.halfapi_version = self.d_domain.get('halfapi_version', __version__)
self.deps = self.d_domain.get('deps', tuple())
self.schema_components = self.d_domain.get('schema_components', dict())
if not router:
self.router = self.d_domain.get('routers', '.routers')
else:
self.router = router
self.m_router = None
try:
self.m_router = importlib.import_module(self.router, self.m_domain.__package__)
except AttributeError:
raise Exception('no router module')
self.m_acl = HalfDomain.m_acl(self.m_domain, acl)
self.config = { **app.config }
logger.info('HalfDomain creation %s %s', domain, self.config)
for elt in self.deps:
package, version = elt
specifier = SpecifierSet(version)
package_module = importlib.import_module(package)
if Version(package_module.__version__) not in specifier:
raise Exception(
'Wrong version for package {} version {} (excepting {})'.format(
package, package_module.__version__, specifier
))
super().__init__(
routes=self.gen_domain_routes(),
middleware=[
Middleware(
DomainMiddleware,
domain={
'name': self.name,
'id': self.id,
'version': self.version,
'halfapi_version': self.halfapi_version,
'config': self.config.get('domain', {}).get(self.name, {}).get('config', {})
}
)
]
)
@staticmethod
def name(module):
""" Returns the name declared in the 'domain' dict at the root of the package
"""
return module.domain['name']
@staticmethod
def m_acl(module, acl=None):
""" Returns the imported acl module for the domain module
"""
if not acl:
acl = getattr(module, '__acl__', '.acl')
return importlib.import_module(acl, module.__package__)
@staticmethod
def acls(module, acl=None):
""" Returns the ACLS constant for the given domain
"""
m_acl = HalfDomain.m_acl(module, acl)
try:
return [
lib_acl.ACL(*elt)
for elt in getattr(m_acl, 'ACLS')
]
except AttributeError as exc:
logger.error(exc)
raise Exception(
f'Missing acl.ACLS constant in module {m_acl.__package__}') from exc
@staticmethod
def acls_route(domain, module_path=None, acl=None):
""" Dictionary of acls
Format :
{
[acl_name]: {
callable: fct_reference,
docs: fct_docstring,
}
}
"""
d_res = {}
module = importlib.import_module(domain) \
if module_path is None \
else importlib.import_module(module_path)
m_acl = HalfDomain.m_acl(module, acl)
for elt in HalfDomain.acls(module, acl=acl):
fct = getattr(m_acl, elt.name)
d_res[elt.name] = {
'callable': fct,
'docs': elt.documentation
}
return d_res
@staticmethod
def acls_router(domain, module_path=None, acl=None):
""" Returns a Router object with the following routes :
/ : The "acls" field of the API metadatas
/{acl_name} : If the ACL is defined as public, a route that returns either status code 200 or 401 on HEAD/GET request
"""
routes = []
d_res = {}
module = importlib.import_module(domain) \
if module_path is None \
else importlib.import_module(module_path)
m_acl = HalfDomain.m_acl(module, acl)
for elt in HalfDomain.acls(module, acl=acl):
fct = getattr(m_acl, elt.name)
d_res[elt.name] = {
'callable': fct,
'docs': elt.documentation,
'public': elt.public
}
if elt.public:
try:
if inspect.iscoroutinefunction(fct):
logger.warning('async decorator are not yet supported')
else:
inner = fct()
if inspect.iscoroutinefunction(fct) or callable(inner):
fct = inner
except TypeError:
# Fct is not a decorator or is not well called (has no default arguments)
# We can ignore this
pass
routes.append(
AclRoute(f'/{elt.name}', fct, elt)
)
d_res_under_domain_name = {}
d_res_under_domain_name[HalfDomain.name(module)] = d_res
routes.append(
Route(
'/',
JSONRoute(d_res_under_domain_name),
methods=['GET']
)
)
return Router(routes)
@staticmethod
def gen_routes(m_router: ModuleType,
verb: str,
path: List[str],
params: List[Dict],
path_param_docstrings: Dict[str, str] = {}) -> Tuple[FunctionType, Dict]:
"""
Returns a tuple of the function associatied to the verb and path arguments,
and the dictionary of it's acls
Parameters:
- m_router (ModuleType): The module containing the function definition
- verb (str): The HTTP verb for the route (GET, POST, ...)
- path (List): The route path, as a list (each item being a level of
deepness), from the lowest level (domain) to the highest
- params (Dict): The acl list of the following format :
[{'acl': Function, 'args': {'required': [], 'optional': []}}]
Returns:
(Function, Dict): The destination function and the acl dictionary
"""
if len(params) == 0:
raise MissingAclError('[{}] {}'.format(verb, '/'.join(path)))
if len(path) == 0:
logger.error('Empty path for [{%s}]', verb)
raise PathError()
fct_name = get_fct_name(verb, path[-1])
if hasattr(m_router, fct_name):
fct = getattr(m_router, fct_name)
fct_docstring_obj = yaml.safe_load(fct.__doc__)
if 'parameters' not in fct_docstring_obj and path_param_docstrings:
fct_docstring_obj['parameters'] = list(map(
yaml.safe_load,
path_param_docstrings.values()))
fct.__doc__ = yaml.dump(fct_docstring_obj)
else:
raise UndefinedFunction('{}.{}'.format(m_router.__name__, fct_name or ''))
if not inspect.iscoroutinefunction(fct):
return route_decorator(fct), params
# TODO: Remove when using only sync functions
return lib_acl.args_check(fct), params
@staticmethod
def gen_router_routes(m_router, path: List[str], PATH_PARAMS={}) -> \
Iterator[Tuple[str, str, ModuleType, Coroutine, List]]:
"""
Recursive generator that parses a router (or a subrouter)
and yields from gen_routes
Parameters:
- m_router (ModuleType): The currently treated router module
- path (List[str]): The current path stack
Yields:
(str, str, ModuleType, Coroutine, List): A tuple containing the path, verb,
router module, function reference and parameters of the route.
Function and parameters are yielded from then gen_routes function,
that decorates the endpoint function.
"""
for subpath, params in HalfDomain.read_router(m_router).items():
path.append(subpath)
for verb in VERBS:
if verb not in params:
continue
yield ('/'.join(filter(lambda x: len(x) > 0, path)),
verb,
m_router,
*HalfDomain.gen_routes(m_router, verb, path, params[verb], PATH_PARAMS)
)
for subroute in params.get('SUBROUTES', []):
subroute_module = importlib.import_module(f'.{subroute}', m_router.__name__)
param_match = re.fullmatch('^([A-Z_]+)_([a-z]+)$', subroute)
parameter_name = None
if param_match is not None:
try:
parameter_name = param_match.groups()[0].lower()
if parameter_name in PATH_PARAMS:
raise Exception(f'Duplicate parameter name in same path! {subroute} : {parameter_name}')
parameter_type = param_match.groups()[1]
path.append('{{{}:{}}}'.format(
parameter_name,
parameter_type,
)
)
try:
PATH_PARAMS[parameter_name] = subroute_module.param_docstring
except AttributeError as exc:
PATH_PARAMS[parameter_name] = param_docstring_default(parameter_name, parameter_type)
except AssertionError as exc:
raise UnknownPathParameterType(subroute) from exc
else:
path.append(subroute)
try:
yield from HalfDomain.gen_router_routes(
subroute_module,
path,
PATH_PARAMS
)
except ImportError as exc:
logger.error('Failed to import subroute **{%s}**', subroute)
raise exc
path.pop()
if parameter_name:
PATH_PARAMS.pop(parameter_name)
path.pop()
@staticmethod
def read_router(m_router: ModuleType) -> Dict:
"""
Reads a module and returns a router dict
If the module has a "ROUTES" constant, it just returns this constant,
Else, if the module has an "ACLS" constant, it builds the accurate dict
TODO: May be another thing, may be not a part of halfAPI
"""
m_path = None
try:
if not hasattr(m_router, 'ROUTES'):
routes = {'':{}}
acls = getattr(m_router, 'ACLS') if hasattr(m_router, 'ACLS') else None
if acls is not None:
for method in acls.keys():
if method not in VERBS:
raise Exception(
'This method is not handled: {}'.format(method))
routes[''][method] = []
routes[''][method] = acls[method].copy()
routes['']['SUBROUTES'] = []
if hasattr(m_router, '__path__'):
""" Module is a package
"""
m_path = getattr(m_router, '__path__')
if isinstance(m_path, list) and len(m_path) == 1:
routes['']['SUBROUTES'] = [
elt.name
for elt in os.scandir(m_path[0])
if elt.is_dir()
]
else:
routes = getattr(m_router, 'ROUTES')
try:
ROUTER_SCHEMA.validate(routes)
except SchemaError as exc:
logger.error(routes)
raise exc
return routes
except ImportError as exc:
# TODO: Proper exception handling
raise exc
except FileNotFoundError as exc:
# TODO: Proper exception handling
logger.error(m_path)
raise exc
def gen_domain_routes(self):
"""
Yields the Route objects for a domain
Parameters:
m_domains: ModuleType
Returns:
Generator(HalfRoute)
"""
yield HalfRoute('/',
self.schema_openapi(),
[{'acl': lib_acl.public}],
'GET'
)
for path, method, m_router, fct, params in HalfDomain.gen_router_routes(self.m_router, []):
yield HalfRoute(f'/{path}', fct, params, method)
def schema_dict(self) -> Dict:
""" gen_router_routes return values as a dict
Parameters:
m_router (ModuleType): The domain routers' module
Returns:
Dict: Schema of dict is halfapi.lib.constants.DOMAIN_SCHEMA
@TODO: Should be a "router_schema_dict" function
"""
d_res = {}
for path, verb, m_router, fct, parameters in HalfDomain.gen_router_routes(self.m_router, []):
if path not in d_res:
d_res[path] = {}
if verb not in d_res[path]:
d_res[path][verb] = {}
d_res[path][verb]['callable'] = f'{m_router.__name__}:{fct.__name__}'
try:
d_res[path][verb]['docs'] = yaml.safe_load(fct.__doc__)
except AttributeError:
logger.error(
'Cannot read docstring from fct (fct=%s path=%s verb=%s', fct.__name__, path, verb)
d_res[path][verb]['acls'] = list(map(lambda elt: { **elt, 'acl': elt['acl'].__name__ },
parameters))
return d_res
def schema(self) -> Dict:
schema = { **API_SCHEMA_DICT }
schema['domain'] = {
'name': self.name,
'id': self.id,
'version': getattr(self.m_domain, '__version__', ''),
'patch_release': getattr(self.m_domain, '__patch_release__', ''),
'routers': self.m_router.__name__,
'acls': tuple(getattr(self.m_acl, 'ACLS', ()))
}
schema['paths'] = self.schema_dict()
return schema
def schema_openapi(self) -> Route:
schema = SchemaGenerator(
{
'openapi': '3.0.0',
'info': {
'title': self.name,
'version': self.version,
'x-acls': tuple(getattr(self.m_acl, 'ACLS', ())),
**({
f'x-{key}': value
for key, value in self.d_domain.items()
}),
},
'components': self.schema_components
}
)
async def inner(request, *args, **kwargs):
"""
description: |
Returns the current API routes description (OpenAPI v3)
as a JSON object
responses:
200:
description: API Schema in OpenAPI v3 format
"""
return ORJSONResponse(
schema.get_schema(routes=request.app.routes))
return inner

109
halfapi/half_route.py Normal file
View File

@ -0,0 +1,109 @@
""" HalfRoute
Child class of starlette.routing.Route
"""
from functools import partial, wraps
from typing import Callable, Coroutine, List, Dict
from types import FunctionType
from starlette.requests import Request
from starlette.responses import PlainTextResponse
from starlette.routing import Route
from starlette.exceptions import HTTPException
from .logging import logger
from .lib.domain import MissingAclError, PathError, UnknownPathParameterType, \
UndefinedRoute, UndefinedFunction
class HalfRoute(Route):
""" HalfRoute
"""
def __init__(self, path: List[str], fct: Callable, params: List[Dict], method: str):
logger.info('HalfRoute creation: %s %s %s %s', path, fct, params, method)
if len(params) == 0:
raise MissingAclError('[{}] {}'.format(method, '/'.join(path)))
if len(path) == 0:
logger.error('Empty path for [{%s}]', method)
raise PathError()
super().__init__(
path,
HalfRoute.acl_decorator(
fct,
params
),
methods=[method])
@staticmethod
def acl_decorator(fct: Callable = None, params: List[Dict] = None) -> Coroutine:
"""
Decorator for async functions that calls pre-conditions functions
and appends kwargs to the target function
Parameters:
fct (Callable):
The function to decorate
params List[Dict]:
A list of dicts that have an "acl" key that points to a function
Returns:
async function
"""
if not params:
params = []
if not fct:
return partial(HalfRoute.acl_decorator, params=params)
@wraps(fct)
async def caller(req: Request, *args, **kwargs):
for param in params:
if param.get('acl'):
passed = param['acl'](req, *args, **kwargs)
if isinstance(passed, FunctionType):
passed = param['acl']()(req, *args, **kwargs)
if not passed:
logger.debug(
'ACL FAIL for current route (%s - %s)', fct, param.get('acl'))
continue
logger.debug(
'ACL OK for current route (%s - %s)', fct, param.get('acl'))
req.scope['acl_pass'] = param['acl'].__name__
if 'args' in param:
req.scope['args'] = param['args']
logger.debug(
'Args for current route (%s)', param.get('args'))
if 'out' in param:
req.scope['out'] = param['out']
if 'out' in param:
req.scope['out'] = param['out'].copy()
if 'check' in req.query_params:
return PlainTextResponse(param['acl'].__name__)
logger.debug('acl_decorator %s', param)
logger.debug('calling %s:%s %s %s', fct.__module__, fct.__name__, args, kwargs)
return await fct(
req, *args,
**{
**kwargs,
})
if 'check' in req.query_params:
return PlainTextResponse('')
raise HTTPException(401)
return caller

316
halfapi/halfapi.py Normal file
View File

@ -0,0 +1,316 @@
#!/usr/bin/env python3
"""
app.py is the file that is read when launching the application using an asgi
runner.
It defines the following globals :
- routes (contains the Route objects for the application)
- application (the asgi application itself - a starlette object)
"""
import sys
import logging
import time
import importlib
from datetime import datetime
# asgi framework
from starlette.applications import Starlette
from starlette.authentication import UnauthenticatedUser
from starlette.exceptions import HTTPException
from starlette.middleware import Middleware
from starlette.routing import Router, Route, Mount
from starlette.requests import Request
from starlette.responses import Response, PlainTextResponse
from starlette.middleware.authentication import AuthenticationMiddleware
from timing_asgi import TimingMiddleware
from timing_asgi.integrations import StarletteScopeToName
# module libraries
from .lib.constants import API_SCHEMA_DICT
from .lib.domain_middleware import DomainMiddleware
from .lib.timing import HTimingClient
from .lib.jwt_middleware import JWTAuthenticationBackend, on_auth_error
from .lib.responses import (ORJSONResponse, UnauthorizedResponse,
NotFoundResponse, InternalServerErrorResponse, NotImplementedResponse,
ServiceUnavailableResponse, gen_exception_route)
from .lib.domain import NoDomainsException
from .lib.routes import gen_schema_routes, JSONRoute
from .lib.schemas import schema_json
from .logging import logger, config_logging
from .half_domain import HalfDomain
from halfapi import __version__
class HalfAPI(Starlette):
def __init__(self,
config,
d_routes=None):
# Set log level (defaults to debug)
config_logging(
getattr(logging, config.get('loglevel', 'DEBUG').upper(), 'DEBUG')
)
self.config = config
SECRET = self.config.get('secret')
PRODUCTION = self.config.get('production', True)
DRYRUN = self.config.get('dryrun', False)
TIMINGMIDDLEWARE = self.config.get('timingmiddleware', False)
if DRYRUN:
logger.info('HalfAPI starting in dry-run mode')
else:
logger.info('HalfAPI starting')
self.PRODUCTION = PRODUCTION
self.SECRET = SECRET
# Domains
""" HalfAPI routes (if not PRODUCTION, includes debug routes)
"""
routes = []
routes.append(
Mount('/halfapi', routes=list(self.halfapi_routes()))
)
logger.debug('Config: %s', self.config)
domains = {
key: elt
for key, elt in self.config.get('domain', {}).items()
if elt.get('enabled', False)
}
logger.debug('Active domains: %s', domains)
if d_routes:
# Mount the routes from the d_routes argument - domain-less mode
logger.info('Domain-less mode : the given schema defines the activated routes')
for route in gen_schema_routes(d_routes):
routes.append(route)
else:
pass
startup_fcts = []
if DRYRUN:
startup_fcts.append(
HalfAPI.wait_quit()
)
super().__init__(
debug=not PRODUCTION,
routes=routes,
exception_handlers={
401: gen_exception_route(UnauthorizedResponse),
404: gen_exception_route(NotFoundResponse),
500: gen_exception_route(HalfAPI.exception),
501: gen_exception_route(NotImplementedResponse),
503: gen_exception_route(ServiceUnavailableResponse)
}
)
schemas = []
self.__domains = {}
for key, domain in domains.items():
if not isinstance(domain, dict):
continue
dom_name = domain.get('name', key)
if not domain.get('enabled', False):
continue
if not domain.get('prefix', False):
if len(domains.keys()) > 1:
raise Exception('Cannot use multiple domains and set prefix to false')
path = '/'
else:
path = f'/{dom_name}'
logger.debug('Mounting domain %s on %s', domain.get('name'), path)
domain_key = domain.get('name', key)
add_domain_args = {
**domain,
'path': path
}
self.add_domain(**add_domain_args)
schemas.append(self.__domains[domain_key].schema())
self.add_route('/', JSONRoute(schemas))
if SECRET:
self.add_middleware(
AuthenticationMiddleware,
backend=JWTAuthenticationBackend(),
on_error=on_auth_error
)
if not PRODUCTION and TIMINGMIDDLEWARE:
self.add_middleware(
TimingMiddleware,
client=HTimingClient(),
metric_namer=StarletteScopeToName(prefix="halfapi",
starlette_app=self)
)
@property
def version(self):
return __version__
async def version_async(self, request, *args, **kwargs):
"""
description: Version route
responses:
200:
description: Currently running HalfAPI's version
"""
return Response(self.version)
@staticmethod
async def exception(request: Request, exc: HTTPException):
logger.critical(exc, exc_info=True)
return InternalServerErrorResponse()
@property
def application(self):
return self
def halfapi_routes(self):
""" Halfapi default routes
"""
async def get_user(request, *args, **kwargs):
"""
description: WhoAmI route
responses:
200:
description: The currently logged-in user
content:
application/json:
schema:
type: object
"""
return ORJSONResponse({'user':request.user})
yield Route('/whoami', get_user)
yield Route('/schema', schema_json)
yield Mount('/acls', self.acls_router())
yield Route('/version', self.version_async)
""" Halfapi debug routes definition
"""
if self.PRODUCTION:
return
""" Debug routes
"""
async def debug_log(request: Request, *args, **kwargs):
logger.debug('debuglog# %s', {datetime.now().isoformat()})
logger.info('debuglog# %s', {datetime.now().isoformat()})
logger.warning('debuglog# %s', {datetime.now().isoformat()})
logger.error('debuglog# %s', {datetime.now().isoformat()})
logger.critical('debuglog# %s', {datetime.now().isoformat()})
return Response('')
yield Route('/log', debug_log)
async def error_code(request: Request, *args, **kwargs):
code = request.path_params['code']
raise HTTPException(code)
yield Route('/error/{code:int}', error_code)
async def exception(request: Request, *args, **kwargs):
raise Exception('Test exception')
yield Route('/exception', exception)
@staticmethod
def api_schema(domain):
pass
@staticmethod
def wait_quit():
""" sleeps 1 second and quits. used in dry-run mode
"""
import time
import sys
time.sleep(1)
sys.exit(0)
def acls_router(self):
mounts = {}
for domain, domain_conf in self.config.get('domain', {}).items():
if isinstance(domain_conf, dict) and domain_conf.get('enabled', False):
mounts['domain'] = HalfDomain.acls_router(
domain,
module_path=domain_conf.get('module'),
acl=domain_conf.get('acl')
)
if len(mounts) > 1:
return Router([
Mount(f'/{domain}', acls_router)
for domain, acls_router in mounts.items()
])
elif len(mounts) == 1:
return Mount('/', mounts.popitem()[1])
else:
return Router()
@property
def domains(self):
return self.__domains
def add_domain(self, **kwargs):
if not kwargs.get('enabled'):
raise Exception(f'Domain not enabled ({kwargs})')
name = kwargs['name']
self.config['domain'][name] = kwargs.get('config', {})
if not kwargs.get('module'):
module = name
else:
module = kwargs.get('module')
try:
self.__domains[name] = HalfDomain(
name,
module=importlib.import_module(module),
router=kwargs.get('router'),
acl=kwargs.get('acl'),
app=self
)
except ImportError as exc:
print(
'Cannot instantiate HalfDomain {} with module {}'.format(
name,
module
))
raise exc
self.mount(kwargs.get('path', name), self.__domains[name])
return self.__domains[name]
def __main__():
return HalfAPI(CONFIG).application
if __name__ == '__main__':
__main__()

View File

@ -1,24 +1,178 @@
#!/usr/bin/env python3
from functools import wraps
from starlette.authentication import UnauthenticatedUser
""" Base ACL module that contains generic functions for domains ACL
"""
Base ACL module that contains generic functions for domains ACL
"""
from dataclasses import dataclass
from functools import wraps
from json import JSONDecodeError
import yaml
from starlette.authentication import UnauthenticatedUser
from starlette.exceptions import HTTPException
from starlette.routing import Route
from starlette.responses import Response
def connected(func):
""" Decorator that checks if the user object of the request has been set
"""
@wraps(func)
def caller(req, *args, **kwargs):
if (not hasattr(req, 'user')
or type(req.user) == UnauthenticatedUser
or not hasattr(req.user, 'is_authenticated')):
return False
return func(req, **{**kwargs, **req.path_params})
return caller
from ..logging import logger
def public(*args, **kwargs) -> bool:
"Unlimited access"
return True
def private(*args, **kwargs) -> bool:
"Forbidden access"
return False
def connected(fct=public):
""" Decorator that checks if the user object of the request has been set
"""
@wraps(fct)
def caller(req, *args, **kwargs):
if (not hasattr(req, 'user')
or isinstance(req.user, UnauthenticatedUser)
or not hasattr(req.user, 'is_authenticated')):
return False
if hasattr(req, 'path_params'):
return fct(req, **{**kwargs, **req.path_params})
return fct(req, **{**kwargs})
return caller
def args_check(fct):
""" Decorator that puts required and optional arguments in scope
For GET requests it uses the query_params
For POST requests it uses the body as JSON
If "check" is present in the query params, nothing is done.
If some required arguments are missing, a 400 status code is sent.
"""
@wraps(fct)
async def caller(req, *args, **kwargs):
if 'check' in req.query_params:
# Check query param should not read the "args"
return await fct(req, *args, **kwargs)
data_ = {}
if req.method == 'GET':
data_ = dict(req.query_params)
elif req.method in ['POST', 'PATCH', 'PUT', 'DELETE']:
if req.scope.get('headers'):
if b'content-type' not in dict(req.scope.get('headers')):
data_ = {}
else:
content_type = dict(req.scope.get('headers')).get(b'content-type').decode().split(';')[0]
if content_type == 'application/json':
try:
data_ = await req.json()
except JSONDecodeError as exc:
logger.debug('Posted data was not JSON')
pass
elif content_type in [
'multipart/form-data', 'application/x-www-form-urlencoded']:
data_ = dict(await req.form())
else:
data_ = await req.body()
def plural(array: list) -> str:
return 's' if len(array) > 1 else ''
def comma_list(array: list) -> str:
return ', '.join(array)
args_d = req.scope.get('args')
if args_d is not None and isinstance(data_, dict):
required = args_d.get('required', set())
missing = []
data = {}
for key in required:
data[key] = data_.get(key, None)
if data[key] is None:
missing.append(key)
if missing:
raise HTTPException(
400,
f"Missing value{plural(missing)} for: {comma_list(missing)}!")
optional = args_d.get('optional', set())
for key in optional:
if key in data_:
data[key] = data_[key]
else:
""" Unsafe mode, without specified arguments, or plain text mode
"""
data = data_
kwargs['data'] = data
out_s = req.scope.get('out')
if out_s:
kwargs['out'] = list(out_s)
return await fct(req, *args, **kwargs)
return caller
# ACLS list for doc and priorities
# Write your own constant in your domain or import this one
# Format : (acl_name: str, acl_documentation: str, priority: int, [public=False])
#
# The 'priority' integer is greater than zero and the lower values means more
# priority. For a route, the order of declaration of the ACLs should respect
# their priority.
#
# When the 'public' boolean value is True, a route protected by this ACL is
# defined on the "/halfapi/acls/acl_name", that returns an empty response and
# the status code 200 or 401.
ACLS = (
('private', private.__doc__, 0, True),
('public', public.__doc__, 999, True)
)
@dataclass
class ACL():
name: str
documentation: str
priority: int
public: bool = False
class AclRoute(Route):
def __init__(self, path, acl_fct, acl: ACL):
self.acl_fct = acl_fct
self.name = acl.name
self.description = acl.documentation
self.docstring = yaml.dump({
'description': f'{self.name}: {self.description}',
'responses': {
'200': {
'description': 'ACL OK'
},
'401': {
'description': 'ACL FAIL'
}
}
})
async def endpoint(request, *args, **kwargs):
if request.method == 'GET':
logger.warning('Deprecated since 0.6.28, use HEAD method since now')
if self.acl_fct(request, *args, **kwargs) is True:
return Response(status_code=200)
return Response(status_code=401)
endpoint.__doc__ = self.docstring
return super().__init__(path, methods=['HEAD', 'GET'], endpoint=endpoint)

71
halfapi/lib/constants.py Normal file
View File

@ -0,0 +1,71 @@
import re
from schema import Schema, Optional, Or
from .. import __version__
VERBS = ('GET', 'POST', 'PUT', 'PATCH', 'DELETE')
ITERABLE_STR = Or([ str ], { str }, ( str ))
ACLS_SCHEMA = Schema([{
'acl': str,
Optional('args'): {
Optional('required'): ITERABLE_STR,
Optional('optional'): ITERABLE_STR
},
Optional('out'): ITERABLE_STR
}])
ROUTER_ACLS_SCHEMA = Schema([{
'acl': lambda n: callable(n),
Optional('args'): {
Optional('required'): ITERABLE_STR,
Optional('optional'): ITERABLE_STR
},
Optional('out'): ITERABLE_STR
}])
is_callable_dotted_notation = lambda x: re.match(
r'^(([a-zA-Z_])+\.?)*:[a-zA-Z_]+$', 'ab_c.TEST:get')
ROUTE_SCHEMA = Schema({
Optional(str): { # path - Optional when no routes
str: { # method
'callable': is_callable_dotted_notation,
'docs': lambda n: True, # Should validate an openAPI spec
'acls': ACLS_SCHEMA
}
}
})
DOMAIN_SCHEMA = Schema({
'name': str,
'id': str,
Optional('routers'): str,
Optional('version'): str,
Optional('patch_release'): str,
Optional('acls'): [
[str, str, int, Optional(bool)]
]
})
API_SCHEMA_DICT = {
'openapi': '3.0.0',
'info': {
'title': 'HalfAPI',
'version': __version__
},
}
API_SCHEMA = Schema({
**API_SCHEMA_DICT,
'domain': DOMAIN_SCHEMA,
'paths': ROUTE_SCHEMA
})
ROUTER_SCHEMA = Schema({
Or('', str): {
# Optional('GET'): [],#ACLS_SCHEMA,
Optional(Or(*VERBS)): ROUTER_ACLS_SCHEMA,
Optional('SUBROUTES'): [Optional(str)]
}
})

192
halfapi/lib/domain.py Normal file
View File

@ -0,0 +1,192 @@
#!/usr/bin/env python3
"""
lib/domain.py The domain-scoped utility functions
"""
import re
import sys
import importlib
import inspect
from functools import wraps
from types import ModuleType, FunctionType
from typing import Coroutine, Generator
from typing import Dict, List, Tuple
import yaml
from starlette.exceptions import HTTPException
from halfapi.lib import acl
from halfapi.lib.responses import ORJSONResponse, ODSResponse, XLSXResponse, PlainTextResponse, HTMLResponse
# from halfapi.lib.router import read_router
from halfapi.lib.constants import VERBS
from ..logging import logger
class MissingAclError(Exception):
""" Exception to use when no acl are specified for a route
"""
pass
class PathError(Exception):
""" Exception to use when the path for a route is malformed
"""
pass
class UnknownPathParameterType(Exception):
""" Exception to use when the path parameter for a route is not supported
"""
pass
class UndefinedRoute(Exception):
""" Exception to use when the route definition cannot be found
"""
pass
class UndefinedFunction(Exception):
""" Exception to use when a function definition cannot be found
"""
pass
class NoDomainsException(Exception):
""" The exception that is raised when HalfAPI is called without domains
"""
pass
def route_decorator(fct: FunctionType) -> Coroutine:
""" Returns an async function that can be mounted on a router
"""
@wraps(fct)
@acl.args_check
async def wrapped(request, *args, **kwargs):
fct_args_spec = inspect.getfullargspec(fct).args
fct_args_defaults = inspect.getfullargspec(fct).defaults or []
fct_args_defaults_dict = dict(list(zip(
reversed(fct_args_spec),
reversed(fct_args_defaults)
)))
fct_args = request.path_params.copy()
if 'halfapi' in fct_args_spec:
fct_args['halfapi'] = {
'user': request.user if
'user' in request else None,
'config': request.scope.get('config', {}),
'domain': request.scope.get('domain', 'unknown'),
'cookies': request.cookies,
'base_url': request.base_url,
'url': request.url
}
if 'data' in fct_args_spec:
if 'data' in fct_args_defaults_dict:
fct_args['data'] = fct_args_defaults_dict['data']
else:
fct_args['data'] = {}
fct_args['data'].update(kwargs.get('data', {}))
if 'out' in fct_args_spec:
fct_args['out'] = kwargs.get('out')
""" If format argument is specified (either by get, post param or function argument)
"""
if 'ret_type' in fct_args_defaults_dict:
ret_type = fct_args_defaults_dict['ret_type']
else:
ret_type = fct_args.get('data', {}).get('format', 'json')
logger.debug('Return type {} (defaults: {})'.format(ret_type,
fct_args_defaults_dict))
try:
logger.debug('FCT_ARGS***** %s', fct_args)
if ret_type == 'json':
return ORJSONResponse(fct(**fct_args))
if ret_type == 'ods':
res = fct(**fct_args)
assert isinstance(res, list)
for elt in res:
assert isinstance(elt, dict)
return ODSResponse(res)
if ret_type == 'xlsx':
res = fct(**fct_args)
assert isinstance(res, list)
for elt in res:
assert isinstance(elt, dict)
return XLSXResponse(res)
if ret_type in ['html', 'xhtml']:
res = fct(**fct_args)
assert isinstance(res, str)
return HTMLResponse(res)
if ret_type in 'txt':
res = fct(**fct_args)
assert isinstance(res, str)
return PlainTextResponse(res)
raise NotImplementedError
except NotImplementedError as exc:
raise HTTPException(501) from exc
except Exception as exc:
# TODO: Write tests
logger.error(exc, exc_info=True)
if not isinstance(exc, HTTPException):
raise HTTPException(500) from exc
raise exc
return wrapped
def get_fct_name(http_verb: str, path: str) -> str:
"""
Returns the predictable name of the function for a route
Parameters:
- http_verb (str): The Route's HTTP method (GET, POST, ...)
- path (str): The functions path
Returns:
str: The *unique* function name for a route and it's verb
Examples:
>>> get_fct_name('get', '')
'get'
>>> get_fct_name('GET', '')
'get'
>>> get_fct_name('POST', 'foo')
'post_foo'
>>> get_fct_name('POST', 'bar')
'post_bar'
>>> get_fct_name('DEL', 'foo/{boo}')
'del_foo_BOO'
>>> get_fct_name('DEL', '{boo:zoo}/far')
'del_BOO_far'
"""
if path and path[0] == '/':
path = path[1:]
fct_name = [http_verb.lower()]
for elt in path.split('/'):
if elt and elt[0] == '{':
fct_name.append(elt[1:-1].split(':')[0].upper())
elif elt:
fct_name.append(elt)
return '_'.join(fct_name)

View File

@ -0,0 +1,80 @@
"""
DomainMiddleware
"""
from starlette.datastructures import URL
from starlette.middleware.base import (BaseHTTPMiddleware,
RequestResponseEndpoint)
from starlette.requests import Request
from starlette.responses import Response
from ..logging import logger
class DomainMiddleware(BaseHTTPMiddleware):
"""
DomainMiddleware adds the api routes and acls to the following scope keys :
- api
- acl
"""
def __init__(self, app, domain=None):
""" app: HalfAPI instance
"""
logger.info('DomainMiddleware app:%s domain:%s', app, domain)
super().__init__(app)
self.domain = domain
self.request = None
async def dispatch(self, request: Request,
call_next: RequestResponseEndpoint) -> Response:
"""
Call of the route fonction (decorated or not)
"""
request.scope['domain'] = self.domain['name']
if hasattr(request.app, 'config') \
and isinstance(request.app.config, dict):
# Set the config scope to the domain's config
request.scope['config'] = request.app.config.get(
'domain', {}
).get(
self.domain['name'], {}
).copy()
# TODO: Remove in 0.7.0
config = request.scope['config'].copy()
request.scope['config']['domain'] = {}
request.scope['config']['domain'][self.domain['name']] = {}
request.scope['config']['domain'][self.domain['name']]['config'] = config
else:
logger.debug('%s', request.app)
logger.debug('%s', getattr(request.app, 'config', None))
response = await call_next(request)
if 'acl_pass' in request.scope:
# Set the http header "x-acl" if an acl was used on the route
response.headers['x-acl'] = request.scope['acl_pass']
if 'args' in request.scope:
# Set the http headers "x-args-required" and "x-args-optional"
if len(request.scope['args'].get('required', set())):
response.headers['x-args-required'] = \
','.join(request.scope['args']['required'])
if len(request.scope['args'].get('optional', set())):
response.headers['x-args-optional'] = \
','.join(request.scope['args']['optional'])
if len(request.scope.get('out', set())):
response.headers['x-out'] = \
','.join(request.scope['out'])
response.headers['x-domain'] = self.domain['name']
return response

View File

@ -1,149 +1,162 @@
__LICENSE__ = """
BSD 3-Clause License
"""
JWT Middleware module
Copyright (c) 2018, Amit Ripshtos
All rights reserved.
Classes:
- JWTUser : goes in request.user
- JWTAuthenticationBackend
- JWTWebSocketAuthenticationBackend
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Raises:
Exception: If configuration has no SECRET
"""
from os import environ
import jwt
import typing
from uuid import UUID
from http.cookies import SimpleCookie
import jwt
from starlette.authentication import (
AuthenticationBackend, AuthenticationError, BaseUser, AuthCredentials,
UnauthenticatedUser)
from starlette.requests import HTTPConnection, Request
from starlette.exceptions import HTTPException
import logging
logger = logging.getLogger('halfapi')
from .user import CheckUser, JWTUser, Nobody
from ..logging import logger
from ..conf import CONFIG
from ..lib.responses import ORJSONResponse
SECRET=None
try:
from ..conf import PRODUCTION
except ImportError:
logger.warning('Could not import PRODUCTION variable from conf module,'\
' using HALFAPI_PROD environment variable')
PRODUCTION = bool(environ.get('HALFAPI_PROD', False))
with open(CONFIG.get('secret', ''), 'r') as secret_file:
SECRET = secret_file.read().strip()
except FileNotFoundError:
logger.error('Could not import SECRET variable from conf module,'\
' using HALFAPI_SECRET environment variable')
try:
from ..conf import SECRET
except ImportError:
logger.warning('Could not import SECRET variable from conf module,'\
' using HALFAPI_SECRET environment variable')
SECRET = environ.get('HALFAPI_SECRET', False)
if not SECRET:
raise Exception('Missing HALFAPI_SECRET variable')
def cookies_from_scope(scope):
cookie = dict(scope.get("headers") or {}).get(b"cookie")
if not cookie:
return {}
simple_cookie = SimpleCookie()
simple_cookie.load(cookie.decode("utf8"))
return {key: morsel.value for key, morsel in simple_cookie.items()}
class JWTUser(BaseUser):
def __init__(self, id: UUID, token: str, payload: dict) -> None:
self.__id = id
self.token = token
self.payload = payload
def __str__(self):
return str(self.json)
@property
def json(self):
return {
'id' : str(self.__id),
'token': self.token,
'payload': self.payload
}
@property
def is_authenticated(self) -> bool:
return True
@property
def id(self) -> str:
return self.__id
def on_auth_error(request: Request, exc: Exception):
response = ORJSONResponse({"error": str(exc)}, status_code=401)
response.delete_cookie('Authorization')
return response
class JWTAuthenticationBackend(AuthenticationBackend):
def __init__(self, secret_key: str = SECRET,
algorithm: str = 'HS256', prefix: str = 'JWT'):
if secret_key is None:
raise Exception('Missing secret_key argument for JWTAuthenticationBackend')
raise Exception('Missing secret_key argument for JWTAuthenticationBackend')
self.secret_key = secret_key
self.algorithm = algorithm
self.prefix = prefix
async def authenticate(self, request):
if "Authorization" not in request.headers:
return None
@property
def id(self) -> str:
return self.__id
async def authenticate(
self, conn: HTTPConnection
) -> typing.Optional[typing.Tuple['AuthCredentials', 'BaseUser']]:
# Standard way to authenticate via API
# https://datatracker.ietf.org/doc/html/rfc7235#section-4.2
token = conn.headers.get('Authorization')
if not token:
token = cookies_from_scope(conn.scope).get('Authorization')
is_check_call = 'check' in conn.query_params
PRODUCTION = conn.scope['app'].debug == False
if not token and not is_check_call:
return AuthCredentials(), Nobody()
token = request.headers["Authorization"]
try:
payload = jwt.decode(token,
key=self.secret_key,
algorithms=self.algorithm,
verify=True)
if token:
payload = jwt.decode(token,
key=self.secret_key,
algorithms=[self.algorithm],
options={
'verify_signature': True
})
if is_check_call:
if token:
return AuthCredentials(), CheckUser(payload['user_id'])
return AuthCredentials(), Nobody()
if PRODUCTION and 'debug' in payload.keys() and payload['debug']:
raise AuthenticationError(
'Trying to connect using *DEBUG* token in *PRODUCTION* mode')
except jwt.InvalidTokenError as e:
raise AuthenticationError(str(e))
except Exception as e:
print(e)
raise e
except jwt.ExpiredSignatureError as exc:
return AuthCredentials(), Nobody()
except jwt.InvalidTokenError as exc:
raise AuthenticationError(str(exc)) from exc
except Exception as exc:
logger.error('Authentication error : %s', exc)
raise exc
return AuthCredentials(["authenticated"]), JWTUser(
id=payload['id'], token=token, payload=payload)
user_id=payload['user_id'], token=token, payload=payload)
class JWTWebSocketAuthenticationBackend(AuthenticationBackend):
def __init__(self, secret_key: str, algorithm: str = 'HS256', query_param_name: str = 'jwt',
id: UUID = None, audience = None, options = {}):
user_id: UUID = None, audience = None):
self.secret_key = secret_key
self.algorithm = algorithm
self.query_param_name = query_param_name
self.id = id
self.__id = user_id
self.audience = audience
self.options = options
async def authenticate(self, request):
if self.query_param_name not in request.query_params:
return AuthCredentials(), UnauthenticatedUser()
async def authenticate(
self, conn: HTTPConnection
) -> typing.Optional[typing.Tuple["AuthCredentials", "BaseUser"]]:
token = request.query_params[self.query_param_name]
if self.query_param_name not in conn.query_params:
return AuthCredentials(), Nobody()
token = conn.query_params[self.query_param_name]
try:
payload = jwt.decode(token, key=self.secret_key, algorithms=self.algorithm,
audience=self.audience, options=self.options)
except jwt.InvalidTokenError as e:
raise AuthenticationError(str(e))
payload = jwt.decode(
token,
key=self.secret_key,
algorithms=[self.algorithm],
audience=self.audience,
options={
'verify_signature': bool(PRODUCTION)
})
return AuthCredentials(["authenticated"]), JWTUser(id = payload['id'],
token=token, payload=payload)
if PRODUCTION and 'debug' in payload.keys() and payload['debug']:
raise AuthenticationError(
'Trying to connect using *DEBUG* token in *PRODUCTION* mode')
except jwt.InvalidTokenError as exc:
raise AuthenticationError(str(exc)) from exc
return (
AuthCredentials(["authenticated"]),
JWTUser(
user_id=payload['id'],
token=token,
payload=payload)
)

View File

@ -1,21 +1,25 @@
#!/usr/bin/env python3
from starlette.exceptions import HTTPException
"""
This is the *query* library that contains all the useful functions to treat our
queries
Fonction:
- parse_query
"""
def parse_query(q: str = ""):
from starlette.exceptions import HTTPException
def parse_query(q_string: str = ""):
"""
Returns the fitting Response object according to query parameters.
The parse_query function handles the following arguments in the query
The parse_query function handles the following arguments in the query
string : format, limit, and offset
It returns a callable function that returns the desired Response object.
Parameters:
q (str): The query string "q" parameter, in the format
q_string (str): The query string "q" parameter, in the format
key0:value0|...|keyN:valueN
Returns:
@ -52,25 +56,23 @@ def parse_query(q: str = ""):
>>> parse_query('limit:10')
<function parse_query.<locals>.select at 0x...>
>>> parse_query('limit=10')
Traceback (most recent call last):
...
starlette.exceptions.HTTPException: 400
# >>> parse_query('limit=10')
# starlette.exceptions.HTTPException: 400
"""
params = {}
if len(q) > 0:
if len(q_string) > 0:
try:
split_ = lambda x : x.split(':')
params = dict(map(split_, q.split('|')))
except ValueError:
raise HTTPException(400)
params = dict(map(split_, q_string.split('|')))
except ValueError as exc:
raise HTTPException(400) from exc
split_ = lambda x : x.split(':')
params = dict(map(split_, q.split('|')))
params = dict(map(split_, q_string.split('|')))
def select(obj, fields = []):
def select(obj, fields):
if 'limit' in params and int(params['limit']) > 0:
obj.limit(int(params['limit']))
@ -78,6 +80,6 @@ def parse_query(q: str = ""):
if 'offset' in params and int(params['offset']) > 0:
obj.offset(int(params['offset']))
return [elt for elt in obj.select(*fields)]
return list(obj.select(*fields))
return select

View File

@ -1,69 +1,167 @@
#!/usr/bin/env python3
# builtins
import numbers
import csv
""" Response module
Contains some base response classes
Classes :
- HJSONResponse
- InternalServerErrorResponse
- NotFoundResponse
- NotImplementedResponse
- ORJSONResponse
- PlainTextResponse
- ServiceUnavailableResponse
- UnauthorizedResponse
- ODSResponse
"""
from datetime import date
from io import TextIOBase, StringIO
from half_orm.null import NULL
import decimal
import typing
from io import BytesIO
import orjson
# asgi framework
from starlette.responses import PlainTextResponse, Response, JSONResponse
from starlette.responses import PlainTextResponse, Response, JSONResponse, HTMLResponse
from starlette.requests import Request
from starlette.exceptions import HTTPException
from .user import JWTUser, Nobody
from ..logging import logger
__all__ = [
'HJSONResponse',
'InternalServerErrorResponse',
'NotFoundResponse',
'NotImplementedResponse',
'ORJSONResponse',
'PlainTextResponse',
'ServiceUnavailableResponse',
'UnauthorizedResponse']
class InternalServerErrorResponse(Response):
""" The 500 Internal Server Error default Response
""" The 500 Internal Server Error default Response
"""
def __init__(self, *args, **kwargs):
super().__init__(status_code=500)
class NotFoundResponse(Response):
""" The 404 Not Found default Response
""" The 404 Not Found default Response
"""
def __init__(self, *args, **kwargs):
super().__init__(status_code=404)
class NotImplementedResponse(Response):
""" The 501 Not Implemented default Response
""" The 501 Not Implemented default Response
"""
def __init__(self, *args, **kwargs):
super().__init__(status_code=501)
class ServiceUnavailableResponse(Response):
""" The 503 Service Unavailable default Response
"""
def __init__(self, *args, **kwargs):
super().__init__(status_code=503)
class UnauthorizedResponse(Response):
""" The 401 Not Found default Response
""" The 401 Not Found default Response
"""
def __init__(self, *args, **kwargs):
super().__init__(status_code = 401)
class HJSONResponse(JSONResponse):
def __init__(self, obj):
obj = self.__serialize(obj)
super().__init__(
content=obj,
status_code = 200)
def __serialize(self, obj):
if isinstance(obj, dict):
robj = dict()
for key, value in obj.items():
robj[key] = self.__serialize(value)
return robj
if isinstance(obj, list):
robj = []
for value in obj:
robj.append(self.__serialize(value))
return robj
if isinstance(obj, numbers.Number) or isinstance(obj, str):
return obj
if obj == NULL:
return None
return str(obj)
class ORJSONResponse(JSONResponse):
""" The response that encodes data into JSON
"""
def __init__(self, content, default=None, **kwargs):
self.default = default if default is not None else ORJSONResponse.default_cast
super().__init__(content, **kwargs)
def render(self, content: typing.Any) -> bytes:
return orjson.dumps(content,
option=orjson.OPT_NON_STR_KEYS,
default=self.default)
@staticmethod
def default_cast(typ):
""" Cast the data in JSON-serializable type
"""
str_types = {
decimal.Decimal
}
list_types = {
set
}
jsonable_types = {
JWTUser, Nobody
}
if callable(typ):
return typ.__name__
if type(typ) in str_types:
return str(typ)
if type(typ) in list_types:
return list(typ)
if type(typ) in jsonable_types:
return typ.json
raise TypeError(f'Type {type(typ)} is not handled by ORJSONResponse')
class HJSONResponse(ORJSONResponse):
""" The response that encodes generator data into JSON
"""
def render(self, content: typing.Generator):
return super().render(list(content))
class ODSResponse(Response):
file_type = 'ods'
def __init__(self, d_rows: typing.List[typing.Dict]):
try:
import pyexcel as pe
except ImportError:
""" ODSResponse is not handled
"""
super().__init__(content=
'pyexcel is not installed, ods format not available'
)
return
with BytesIO() as ods_file:
rows = []
if len(d_rows):
rows_names = list(d_rows[0].keys())
for elt in d_rows:
rows.append(list(elt.values()))
rows.insert(0, rows_names)
self.sheet = pe.Sheet(rows)
self.sheet.save_to_memory(
file_type=self.file_type,
stream=ods_file)
filename = f'{date.today()}.{self.file_type}'
super().__init__(
content=ods_file.getvalue(),
headers={
'Content-Type': 'application/vnd.oasis.opendocument.spreadsheet; charset=UTF-8',
'Content-Disposition': f'attachment; filename="{filename}"'},
status_code = 200)
class XLSXResponse(ODSResponse):
file_type = 'xlsx'
def gen_exception_route(response_cls):
async def exception_route(req: Request, exc: HTTPException):
return response_cls()
return exception_route

12
halfapi/lib/router.py Normal file
View File

@ -0,0 +1,12 @@
import os
import sys
import subprocess
from types import ModuleType
from typing import Dict
from pprint import pprint
from schema import SchemaError
from .constants import VERBS, ROUTER_SCHEMA
from ..logging import logger

View File

@ -1,84 +1,136 @@
#!/usr/bin/env python3
from functools import wraps
import importlib
import sys
"""
Routes module
from halfapi.conf import (PROJECT_NAME, DB_NAME, HOST, PORT,
PRODUCTION, DOMAINS)
Classes :
- JSONRoute
from halfapi.db import (
Domain,
APIRouter,
APIRoute,
AclFunction,
Acl)
from halfapi.lib.responses import *
from starlette.exceptions import HTTPException
from starlette.routing import Mount, Route
from starlette.requests import Request
Fonctions :
- gen_domain_routes
- gen_schema_routes
- api_routes
Exception :
- DomainNotFoundError
"""
import inspect
from typing import Coroutine, Dict, Generator, Tuple, Any
from types import ModuleType, FunctionType
import yaml
# from .domain import gen_router_routes, domain_acls, route_decorator, domain_schema
from .responses import ORJSONResponse
from .acl import args_check
from ..half_route import HalfRoute
from . import acl
from ..logging import logger
class DomainNotFoundError(Exception):
pass
def get_routes(domains=None):
""" Procedure to mount the registered domains on their prefixes
Parameters:
- app (ASGIApp): The Starlette instance
- domains (list): The domains to mount, retrieved from the database
with their attributes "name"
Returns: Nothing
""" Exception when a domain is not importable
"""
def JSONRoute(data: Any) -> Coroutine:
"""
Returns a route function that returns the data as JSON
def route_decorator(fct, acls_mod, acls):
@wraps(fct)
async def caller(req: Request, *args, **kwargs):
for acl_fct_name in acls:
acl_fct = getattr(acls_mod, acl_fct_name)
if acl_fct(req, *args, **kwargs):
return await fct(req, *args, **kwargs)
Parameters:
data (Any):
The data to return
raise HTTPException(401)
Returns:
async function
"""
async def wrapped(request, *args, **kwargs):
return ORJSONResponse(data)
return caller
return wrapped
app_routes = []
for domain_name in DOMAINS:
def gen_domain_routes(m_domain: ModuleType):
"""
Yields the Route objects for a domain
Parameters:
m_domains: ModuleType
Returns:
Generator(HalfRoute)
"""
yield HalfRoute('/',
JSONRoute(domain_schema(m_domain)),
[{'acl': acl.public}],
'GET'
)
for path, method, m_router, fct, params in gen_router_routes(m_domain, []):
yield HalfRoute(f'/{path}', fct, params, method)
def gen_schema_routes(schema: Dict):
"""
Yields the Route objects according to a given schema
"""
for path, methods in schema.items():
for verb, definition in methods.items():
fct = definition.pop('fct')
acls = definition.pop('acls')
# TODO: Check what to do with gen_routes, it is almost the same function
if not inspect.iscoroutinefunction(fct):
yield HalfRoute(path, route_decorator(fct), acls, verb)
else:
yield HalfRoute(path, args_check(fct), acls, verb)
def api_routes(m_dom: ModuleType) -> Tuple[Dict, Dict]:
"""
Yields the description objects for HalfAPI app routes
Parameters:
m_dom (ModuleType): the halfapi module
Returns:
(Dict, Dict)
"""
d_acls = {}
def str_acl(params):
l_params = []
for param in params:
if 'acl' not in param.keys() or not param['acl']:
continue
l_params.append(param.copy())
l_params[-1]['acl'] = param['acl'].__name__
if param['acl'] not in d_acls.keys():
d_acls[param['acl'].__name__] = param['acl']
return l_params
d_res = {}
for path, verb, m_router, fct, params in gen_router_routes(m_dom, []):
try:
domain = next(Domain(name=domain_name).select())
except StopIteration:
raise DomainNotFoundError(
f"Domain '{domain_name}' not found in '{DB_NAME}' database!")
domain_acl_mod = importlib.import_module(f'{domain["name"]}.acl')
domain_routes = []
for router in APIRouter(domain=domain['name']).select():
router_routes = []
if path not in d_res:
d_res[path] = {}
router_mod = importlib.import_module(
'{domain}.routers.{name}'.format(**router))
d_res[path][verb] = {
'docs': yaml.load(fct.__doc__, Loader=yaml.FullLoader),
'acls': str_acl(params)
}
except Exception as exc:
logger.error("""Error in route generation
path:%s
verb:%s
router:%s
fct:%s
params:%s """, path, verb, m_router, fct, params)
raise exc
with APIRoute(domain=domain['name'],
router=router['name']) as routes:
for route in routes.select():
fct_name = route.pop('fct_name')
acls = [ list(elt.values()).pop()
for elt in Acl(**route).select('acl_fct_name') ]
router_routes.append(
Route(route['path'],
route_decorator(
getattr(router_mod, fct_name),
domain_acl_mod,
acls
), methods=[route['http_verb']])
)
domain_routes.append(
Mount('/{name}'.format(**router), routes=router_routes))
app_routes.append(Mount('/{name}'.format(**domain),
routes=domain_routes))
return app_routes
return d_res, d_acls

137
halfapi/lib/schemas.py Normal file
View File

@ -0,0 +1,137 @@
""" Schemas module
Functions :
- schema_json
- schema_dict_dom
- get_acls
Constant :
SCHEMAS (starlette.schemas.SchemaGenerator)
"""
import os
import importlib
from typing import Dict, Coroutine, List
from types import ModuleType
import yaml
from starlette.schemas import SchemaGenerator
from .. import __version__
from ..logging import logger
from .routes import api_routes
from .responses import ORJSONResponse
SCHEMAS = SchemaGenerator(
{"openapi": "3.0.0", "info": {"title": "HalfAPI", "version": __version__}}
)
async def schema_json(request, *args, **kwargs):
"""
description: |
Returns the current API routes description (OpenAPI v3)
as a JSON object
responses:
200:
description: API Schema in OpenAPI v3 format
"""
return ORJSONResponse(
SCHEMAS.get_schema(routes=request.app.routes))
def schema_csv_dict(csv: List[str], prefix='/') -> Dict:
package = None
schema_d = {}
modules_d = {}
acl_modules_d = {}
for line in csv:
if not line:
continue
path, verb, router, acl_fct_name, args_req, args_opt, out = line.strip().split(';')
logger.info('schema_csv_dict %s %s %s', path, args_req, args_opt)
path = f'{prefix}{path}'
if path not in schema_d:
schema_d[path] = {}
if verb not in schema_d[path]:
mod_str = router.split(':')[0]
fct_str = router.split(':')[1]
if mod_str not in modules_d:
modules_d[mod_str] = importlib.import_module(mod_str)
if not hasattr(modules_d[mod_str], fct_str):
raise Exception(
'Missing function in module. module:{} function:{}'.format(
router, fct_str
)
)
fct = getattr(modules_d[mod_str], fct_str)
schema_d[path][verb] = {
'module': modules_d[mod_str],
'fct': fct,
'acls': []
}
if package and router.split('.')[0] != package:
raise Exception('Multi-domain is not allowed in that mode')
package = router.split('.')[0]
if not len(package):
raise Exception(
'Empty package name (router=%s)'.format(router))
acl_package = f'{package}.acl'
if acl_package not in acl_modules_d:
if acl_package not in modules_d:
modules_d[acl_package] = importlib.import_module(acl_package)
if not hasattr(modules_d[acl_package], acl_fct_name):
raise Exception(
'Missing acl function in module. module:{} acl:{}'.format(
acl_package, acl_fct_name
)
)
acl_modules_d[acl_package] = {}
acl_modules_d[acl_package][acl_fct_name] = getattr(modules_d[acl_package], acl_fct_name)
schema_d[path][verb]['acls'].append({
'acl': acl_modules_d[acl_package][acl_fct_name],
'args': {
'required': set(args_req.split(',')) if len(args_req) else set(),
'optional': set(args_opt.split(',')) if len(args_opt) else set()
}
})
return schema_d
def param_docstring_default(name, type):
""" Returns a default docstring in OpenAPI format for a path parameter
"""
type_map = {
'str': 'string',
'uuid': 'string',
'path': 'string',
'int': 'number',
'float': 'number'
}
return yaml.dump({
'name': name,
'in': 'path',
'description': f'default description for path parameter {name}',
'required': True,
'schema': {
'type': type_map[type]
}
})

23
halfapi/lib/timing.py Normal file
View File

@ -0,0 +1,23 @@
"""
Timing module
Helpers to gathers stats on requests timing
class HTimingClient
"""
import logging
from timing_asgi import TimingClient
from ..logging import logger
class HTimingClient(TimingClient):
""" Used to redefine TimingClient.timing
"""
def timing(self, metric_name, timing, tags):
tags_d = dict(map(lambda elt: elt.split(':'), tags))
logger.debug('[TIME:%s][%s] %s %s - %sms',
tags_d['time'], metric_name,
tags_d['http_method'], tags_d['http_status'],
round(timing*1000, 2))

79
halfapi/lib/user.py Normal file
View File

@ -0,0 +1,79 @@
from uuid import UUID
from starlette.authentication import BaseUser, UnauthenticatedUser
class Nobody(UnauthenticatedUser):
""" Nobody class
The default class when no token is passed
"""
@property
def json(self):
return {
'id' : '',
'token': '',
'payload': ''
}
class JWTUser(BaseUser):
""" JWTUser class
Is used to store authentication informations
"""
def __init__(self, user_id: UUID, token: str, payload: dict) -> None:
self.__id = user_id
self.token = token
self.payload = payload
def __str__(self):
return str(self.json)
@property
def json(self):
return {
'id' : str(self.__id),
'token': self.token,
'payload': self.payload
}
@property
def is_authenticated(self) -> bool:
return True
@property
def display_name(self) -> str:
return ' '.join(
(self.payload.get('name'), self.payload.get('firstname')))
@property
def id(self) -> str:
return self.__id
class CheckUser(BaseUser):
""" CheckUser class
Is used to call checks with give user_id, to know if it passes the ACLs for
the given route.
It should never be able to run a route function.
"""
def __init__(self, user_id: UUID) -> None:
self.__id = user_id
@property
def is_authenticated(self) -> bool:
return True
@property
def display_name(self) -> str:
return 'check_user'
@property
def id(self) -> str:
return self.__id

32
halfapi/logging.py Normal file
View File

@ -0,0 +1,32 @@
import logging
default_level = logging.DEBUG
default_format = '%(asctime)s [%(process)d] [%(levelname)s] %(message)s'
default_datefmt = '[%Y-%m-%d %H:%M:%S %z]'
def config_logging(level=default_level, format=default_format, datefmt=default_datefmt):
# When run by 'uvicorn ...', a root handler is already
# configured and the basicConfig below does nothing.
# To get the desired formatting:
logging.getLogger().handlers.clear()
# 'uvicorn --log-config' is broken so we configure in the app.
# https://github.com/encode/uvicorn/issues/511
logging.basicConfig(
# match gunicorn format
format=format,
datefmt=datefmt,
level=level)
# When run by 'gunicorn -k uvicorn.workers.UvicornWorker ...',
# These loggers are already configured and propogating.
# So we have double logging with a root logger.
# (And setting propagate = False hurts the other usage.)
logging.getLogger('uvicorn.asgi').handlers.clear()
logging.getLogger('uvicorn.access').handlers.clear()
logging.getLogger('uvicorn.error').handlers.clear()
logging.getLogger('uvicorn.asgi').propagate = True
logging.getLogger('uvicorn.access').propagate = True
logging.getLogger('uvicorn.error').propagate = True
logger = logging.getLogger()

View File

@ -1,74 +0,0 @@
create schema api;
create type verb as enum ('POST', 'GET', 'PUT', 'DELETE');
create table api.domain (
name text,
primary key (name)
);
create table api.router (
name text,
domain text,
primary key (name, domain)
);
alter table api.router add constraint router_domain_fkey foreign key (domain) references api.domain(name) on update cascade on delete cascade;
create table api.route (
http_verb verb,
path text, -- relative to /<domain>/<router>
fct_name text,
router text,
domain text,
primary key (http_verb, path, router, domain)
);
alter table api.route add constraint route_router_fkey foreign key (router, domain) references api.router(name, domain) on update cascade on delete cascade;
create table api.acl_function (
name text,
description text,
domain text,
primary key (name, domain)
);
alter table api.acl_function add constraint acl_function_domain_fkey foreign key (domain) references api.domain(name) on update cascade on delete cascade;
create table api.acl (
http_verb verb,
path text not null,
router text,
domain text,
acl_fct_name text,
primary key (http_verb, path, router, domain, acl_fct_name)
);
alter table api.acl add constraint acl_route_fkey foreign key (http_verb, path,
router, domain) references api.route(http_verb, path, router, domain) on update cascade on delete cascade;
alter table api.acl add constraint acl_function_fkey foreign key (acl_fct_name, domain) references api.acl_function(name, domain) on update cascade on delete cascade;
create schema "api.view";
create view "api.view".route as
select
route.*,
'/'::text || route.domain || '/'::text || route.router || route.path AS abs_path
from
api.route
join api.domain on
route.domain = domain.name
;
create view "api.view".acl as
select
acl.*,
'/'::text || route.domain || '/'::text || route.router || route.path AS abs_path
from
api.acl
join api.acl_function on
acl.acl_fct_name = acl_function.name
join api.route on
acl.domain = route.domain
and acl.router = route.router
and acl.path = route.path;

View File

@ -0,0 +1,152 @@
import importlib
import functools
import os
import sys
import json
from json.decoder import JSONDecodeError
import toml
from unittest import TestCase
from starlette.testclient import TestClient
from click.testing import CliRunner
from ..cli.cli import cli
from ..halfapi import HalfAPI
from ..half_domain import HalfDomain
from ..conf import DEFAULT_CONF
from pprint import pprint
import tempfile
class TestDomain(TestCase):
@property
def domain_name(self):
return getattr(self, 'DOMAIN')
@property
def module_name(self):
return getattr(self, 'MODULE', self.domain_name)
@property
def acl_path(self):
return getattr(self, 'ACL', '.acl')
@property
def router_path(self):
return getattr(self, 'ROUTERS', '.routers')
@property
def router_module(self):
return '.'.join((self.module_name, self.ROUTERS))
def setUp(self):
# CLI
class_ = CliRunner
def invoke_wrapper(f):
"""Augment CliRunner.invoke to emit its output to stdout.
This enables pytest to show the output in its logs on test
failures.
"""
@functools.wraps(f)
def wrapper(*args, **kwargs):
echo = kwargs.pop('echo', False)
result = f(*args, **kwargs)
if echo is True:
sys.stdout.write(result.output)
return result
return wrapper
class_.invoke = invoke_wrapper(class_.invoke)
self.runner = class_(mix_stderr=False)
# HTTP
# Fake default values of default configuration
self.halfapi_conf = {
'secret': 'testsecret',
'production': False,
'domain': {}
}
self.halfapi_conf['domain'][self.domain_name] = {
'name': self.domain_name,
'router': self.router_path,
'acl': self.acl_path,
'module': self.module_name,
'prefix': False,
'enabled': True,
'config': getattr(self, 'CONFIG', {})
}
_, self.config_file = tempfile.mkstemp()
with open(self.config_file, 'w') as fh:
fh.write(toml.dumps(self.halfapi_conf))
self.halfapi = HalfAPI(self.halfapi_conf)
self.client = TestClient(self.halfapi.application)
self.module = importlib.import_module(
self.module_name
)
def tearDown(self):
pass
def check_domain(self):
result = None
try:
result = self.runner.invoke(cli, '--version')
self.assertEqual(result.exit_code, 0)
result = self.runner.invoke(cli, ['domain', '--read', self.DOMAIN, self.config_file])
self.assertEqual(result.exit_code, 0)
result_d = json.loads(result.stdout)
result = self.runner.invoke(cli, ['run', '--help'])
self.assertEqual(result.exit_code, 0)
# result = self.runner.invoke(cli, ['run', '--dryrun', self.DOMAIN])
# self.assertEqual(result.exit_code, 0)
except AssertionError as exc:
print(f'Result {result}')
print(f'Stdout {result.stdout}')
print(f'Stderr {result.stderr}')
raise exc
except JSONDecodeError as exc:
print(f'Result {result}')
print(f'Stdout {result.stdout}')
raise exc
return result_d
def check_routes(self):
r = self.client.request('get', '/')
assert r.status_code == 200
schema = r.json()
assert isinstance(schema, dict)
assert 'openapi' in schema
assert 'info' in schema
assert 'paths' in schema
r = self.client.request('get', '/halfapi/acls')
assert r.status_code == 200
d_r = r.json()
assert isinstance(d_r, dict)
assert self.domain_name in d_r.keys()
ACLS = HalfDomain.acls(self.module, self.acl_path)
assert len(ACLS) == len(d_r[self.domain_name])
for acl_rule in ACLS:
assert len(acl_rule.name) > 0
assert acl_rule.name in d_r[self.domain_name]
assert len(acl_rule.documentation) > 0
assert isinstance(acl_rule.priority, int)
assert acl_rule.priority >= 0
if acl_rule.public is True:
r = self.client.request('get', f'/halfapi/acls/{acl_rule.name}')
assert r.status_code in [200, 401]

View File

@ -0,0 +1,3 @@
pyexcel>=0.6.3,<1
pyexcel-ods>=0.5.6,<1
pyexcel-xlsx=0.6.0,<1

View File

@ -1,4 +1,5 @@
[pytest]
testpaths = tests halfapi
addopts = --doctest-modules -rP --log-level debug
doctest_optionflags = ELLIPSIS
addopts = --doctest-modules
doctest_optionflags = ELLIPSIS IGNORE_EXCEPTION_DETAIL
pythonpath = ./tests

View File

@ -1,6 +1,61 @@
click
starlette
uvicorn
PyJWT
half_orm @ git+ssh://git@gite.lirmm.fr/newsi/halfORM.git
pygit2==0.28.2
alog==0.9.13
anyio==3.4.0
asgiref==3.4.1
astroid==2.9.0
attrs==21.2.0
bleach==4.1.0
build==0.7.0
certifi==2021.10.8
cffi==1.15.0
charset-normalizer==2.0.9
click==7.1.2
colorama==0.4.4
contextlib2==21.6.0
cryptography==36.0.1
docutils==0.18.1
h11==0.12.0
idna==3.3
importlib-metadata==4.8.2
iniconfig==1.1.1
isort==5.10.1
jeepney==0.7.1
keyring==23.4.0
lazy-object-proxy==1.7.1
mccabe==0.6.1
orjson==3.6.5
packaging==21.3
pep517==0.12.0
pkginfo==1.8.2
platformdirs==2.4.0
pluggy==1.0.0
py==1.11.0
pycparser==2.21
pyflakes==2.4.0
Pygments==2.10.0
PyJWT==2.3.0
pylint==2.12.2
pyparsing==3.0.6
pytest==6.2.5
pytest-asyncio==0.16.0
pytest-pythonpath==0.7.3
PyYAML==5.4.1
readme-renderer==32.0
requests==2.26.0
requests-toolbelt==0.9.1
rfc3986==1.5.0
schema==0.7.5
SecretStorage==3.3.1
six==1.16.0
sniffio==1.2.0
starlette==0.17.1
timing-asgi==0.2.1
toml==0.10.2
tomli==2.0.0
tqdm==4.62.3
twine==3.7.1
urllib3==1.26.7
uvicorn==0.16.0
vulture==2.3
webencodings==0.5.1
wrapt==1.13.3
zipp==3.6.0

1
scripts/whoami.sh Normal file
View File

@ -0,0 +1 @@
http 127.0.0.1:3000/halfapi/whoami Authorization:$(http 127.0.0.1:3000/authentication/check email=malves password=papa|jq -r '.token')

View File

@ -8,6 +8,9 @@ from setuptools import setup, find_packages
import pathlib
here = pathlib.Path(__file__).parent.resolve()
long_description = (here / 'README.md').read_text(encoding='utf-8')
def get_version(package):
"""
Return package version as listed in `__version__` in `init.py`.
@ -16,13 +19,6 @@ def get_version(package):
return re.search("__version__ = ['\"]([^'\"]+)['\"]", f.read()).group(1)
def get_long_description():
"""
Return the README.
"""
here = pathlib.Path(__file__).parent.resolve()
long_description = (here / 'README.md').read_text(encoding='utf-8')
def get_packages(package):
"""
Return root package and all sub-packages.
@ -37,27 +33,62 @@ module_name="halfapi"
setup(
name=module_name,
version=get_version(module_name),
url="https://gite.lirmm.fr/malves/halfapi",
long_description=get_long_description(),
url="https://github.com/halfAPI/halfapi",
description="Core to write deep APIs using a module's tree",
author="Maxime ALVES",
author_email="maxime@freepoteries.fr",
license="GPLv3",
long_description=long_description,
long_description_content_type="text/markdown",
packages=get_packages(module_name),
python_requires=">=3.7",
python_requires=">=3.8",
install_requires=[
"PyJWT>=1.7.1",
"starlette>=0.13,<1",
"half_orm @ git+ssh://git@gite.lirmm.fr/maizi/halfORM.git",
"click>=7.1,<8",
"uvicorn>=0.11,<1"
"PyJWT>=2.6.0,<2.7.0",
"starlette>=0.33,<0.34",
"click>=8,<9",
"uvicorn>=0.13,<1",
"orjson>=3.8.5,<4",
"pyyaml>=6,<7",
"timing-asgi>=0.2.1,<1",
"schema>=0.7.4,<1",
"toml>=0.10,<0.11",
"packaging>=19.0",
"python-multipart"
],
classifiers=[
"Development Status :: 3 - Alpha",
"License :: OSI Approved :: GNU General Public License v3 (GPLv3)",
"Intended Audience :: Developers",
"Topic :: Software Development :: Libraries :: Application Frameworks",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10"
],
extras_require={
"tests":[
"pytest",
"requests"
"pytest>=7,<8",
"pytest-asyncio",
"pylint",
"requests",
"httpx",
"openapi-schema-validator",
"openapi-spec-validator",
"coverage"
],
"pyexcel":[
"pyexcel",
"pyexcel-ods3",
"pyexcel-xlsx"
]
},
entry_points={
"console_scripts":[
"halfapi=halfapi.cli.cli:cli"
]
},
keywords="web-api development boilerplate",
project_urls={
"Source": "https://github.com/halfAPI/halfapi",
}
)

93
tests/cli/test_cli.py Normal file
View File

@ -0,0 +1,93 @@
#!/usr/bin/env python3
import os
import subprocess
import importlib
import tempfile
from unittest.mock import patch
import pytest
from click.testing import CliRunner
from configparser import ConfigParser
from halfapi import __version__
from halfapi.cli import cli
Cli = cli.cli
PROJNAME = os.environ.get('PROJ','tmp_api')
def test_options(runner):
# Wrong command
with runner.isolated_filesystem():
r = runner.invoke(Cli, ['foobar'])
assert r.exit_code == 2
# Test existing commands
with runner.isolated_filesystem():
r = runner.invoke(Cli, ['--help'])
assert r.exit_code == 0
with runner.isolated_filesystem():
r = runner.invoke(Cli, ['--version'])
assert r.exit_code == 0
with runner.isolated_filesystem():
r = runner.invoke(Cli, ['init', '--help'])
assert r.exit_code == 0
@pytest.mark.skip
def test_init_project_fail(runner):
# Missing argument (project)
testproject = 'testproject'
r = runner.invoke(Cli, ['init'])
assert r.exit_code == 2
with runner.isolated_filesystem():
# Fail : Wrong project name
r = runner.invoke(Cli, ['init', 'test*-project'])
assert r.exit_code == 1
with runner.isolated_filesystem():
# Fail : Already existing folder
os.mkdir(testproject)
r = runner.invoke(Cli, ['init', testproject])
assert r.exit_code == 1
with runner.isolated_filesystem():
# Fail : Already existing nod
os.mknod(testproject)
r = runner.invoke(Cli, ['init', testproject])
assert r.exit_code == 1
@pytest.mark.skip
def test_init_project(runner):
"""
"""
cp = ConfigParser()
with runner.isolated_filesystem():
env = {
'HALFAPI_CONF_DIR': '.halfapi'
}
res = runner.invoke(Cli, ['init', PROJNAME], env=env)
try:
assert os.path.isdir(PROJNAME)
assert os.path.isdir(os.path.join(PROJNAME, '.halfapi'))
# .halfapi/config check
assert os.path.isfile(os.path.join(PROJNAME, '.halfapi', 'config'))
cp.read(os.path.join(PROJNAME, '.halfapi', 'config'))
assert cp.has_section('project')
assert cp.has_option('project', 'name')
assert cp.get('project', 'name') == PROJNAME
assert cp.get('project', 'halfapi_version') == __version__
# removal of domain section (0.6)
# assert cp.has_section('domain')
except AssertionError as exc:
subprocess.run(['tree', '-a', os.getcwd()])
raise exc
assert res.exit_code == 0
assert res.exception is None

View File

@ -0,0 +1,9 @@
from halfapi.cli.cli import cli
from configparser import ConfigParser
def test_config(cli_runner):
with cli_runner.isolated_filesystem():
result = cli_runner.invoke(cli, ['config'])
cp = ConfigParser()
cp.read_string(result.output)
assert cp.has_section('project')

View File

@ -0,0 +1,84 @@
#!/usr/bin/env python3
import os
import subprocess
import importlib
import tempfile
from unittest.mock import patch
import json
import toml
import pytest
from click.testing import CliRunner
from configparser import ConfigParser
from halfapi.conf import DEFAULT_CONF, PROJECT_LEVEL_KEYS, DOMAIN_LEVEL_KEYS
PROJNAME = os.environ.get('PROJ','tmp_api')
class TestCliProj():
def test_cmds(self, project_runner):
assert project_runner('--help').exit_code == 0
#assert project_runner('run', '--help').exit_code == 0
#assert project_runner('domain', '--help').exit_code == 0
def test_domain_commands(self, project_runner):
""" TODO: Test create command
"""
test_conf = {
'project': {
'port': '3010',
'loglevel': 'warning'
},
'domain': {
'dummy_domain': {
'port': 4242,
'name': 'dummy_domain',
'enabled': True
}
}
}
r = project_runner('domain')
print(r.stdout)
assert r.exit_code == 1
_, tmp_conf = tempfile.mkstemp()
with open(tmp_conf, 'w') as fh:
fh.write(
toml.dumps(test_conf)
)
r = project_runner(f'domain dummy_domain --conftest {tmp_conf}')
assert r.exit_code == 0
r_conf = toml.loads(r.stdout)
for key, value in r_conf.items():
if key == 'domain':
continue
assert key in PROJECT_LEVEL_KEYS
if key == 'port':
assert value == test_conf['domain']['dummy_domain']['port']
elif key == 'loglevel':
assert value == test_conf['project']['loglevel']
else:
assert value == DEFAULT_CONF[key.upper()]
assert json.dumps(test_conf['domain']) == json.dumps(r_conf['domain'])
for key in test_conf['domain']['dummy_domain'].keys():
assert key in DOMAIN_LEVEL_KEYS
# Default command "run"
r = project_runner(f'domain dummy_domain --dry-run {tmp_conf}')
print(r.stdout)
assert r.exit_code == 0
def test_config_commands(self, project_runner):
try:
r = project_runner('config')
assert r.exit_code == 0
except AssertionError as exc:
subprocess.call(['tree', '-a'])
raise exc

33
tests/cli/test_cli_run.py Normal file
View File

@ -0,0 +1,33 @@
import pytest
from click.testing import CliRunner
from halfapi.cli.cli import cli
import os
from unittest.mock import patch
@pytest.mark.skip
def test_run_noproject(cli_runner):
with cli_runner.isolated_filesystem():
result = cli_runner.invoke(cli, ['config'])
print(result.stdout)
assert result.exit_code == 0
result = cli_runner.invoke(cli, ['run', '--dryrun'])
try:
assert result.exit_code == 0
except AssertionError as exc:
print(result.stdout)
raise exc
"""
def test_run_empty_project(cli_runner):
with cli_runner.isolated_filesystem():
os.mkdir('dummy_domain')
result = cli_runner.invoke(cli, ['run', './dummy_domain'])
assert result.exit_code == 1
def test_run_dummy_project(project_runner):
with patch('uvicorn.run', autospec=True) as runMock:
result = project_runner.invoke(cli, ['run'])
runMock.assert_called_once()
"""

View File

@ -1,57 +1,120 @@
#!/usr/bin/env python3
import logging
import functools
import re
import os
import subprocess
import importlib
import tempfile
from unittest.mock import patch
from typing import Dict, Tuple
import pytest
from uuid import uuid1
from uuid import uuid1, uuid4, UUID
from click.testing import CliRunner
import jwt
import sys
from unittest.mock import patch
import pytest
from starlette.applications import Starlette
from starlette.responses import PlainTextResponse
from starlette.middleware.authentication import AuthenticationMiddleware
from starlette.testclient import TestClient
from halfapi import __version__
from halfapi.cli import cli
from halfapi.cli.init import format_halfapi_etc
Cli = cli.cli
from halfapi.halfapi import HalfAPI
from halfapi.cli.cli import cli
from halfapi.cli.init import init
from halfapi.cli.domain import domain, create_domain
from halfapi.lib.responses import ORJSONResponse
from halfapi.lib.jwt_middleware import JWTAuthenticationBackend
logger = logging.getLogger()
PROJNAME = os.environ.get('PROJ','tmp_api')
SECRET = 'dummysecret'
from halfapi.lib.jwt_middleware import (
JWTUser, JWTAuthenticationBackend,
JWTWebSocketAuthenticationBackend)
@pytest.fixture
def dummy_domain():
yield {
'name': 'dummy_domain',
'router': '.routers',
'enabled': True,
'prefix': False,
'config': {
'test': True
}
}
@pytest.fixture
def token_builder():
yield jwt.encode({
'name':'xxx',
'user_id': str(uuid4())},
key=SECRET
)
@pytest.fixture
def token_debug_false_builder():
yield jwt.encode({
'name':'xxx',
'user_id': str(uuid4()),
'debug': False},
key=SECRET
)
@pytest.fixture
def token_debug_true_builder():
yield jwt.encode({
'name':'xxx',
'user_id': str(uuid4()),
'debug': True},
key=SECRET
)
@pytest.fixture
def runner():
return CliRunner()
return CliRunner(mix_stderr=False)
@pytest.fixture
def dropdb():
p = subprocess.Popen(['dropdb', f'halfapi_{PROJNAME}'])
p.wait()
yield
def cli_runner():
"""Yield a click.testing.CliRunner to invoke the CLI."""
class_ = CliRunner
p = subprocess.Popen(['dropdb', f'halfapi_{PROJNAME}'])
p.wait()
def invoke_wrapper(f):
"""Augment CliRunner.invoke to emit its output to stdout.
This enables pytest to show the output in its logs on test
failures.
"""
@functools.wraps(f)
def wrapper(*args, **kwargs):
echo = kwargs.pop('echo', False)
result = f(*args, **kwargs)
if echo is True:
sys.stdout.write(result.output)
return result
return wrapper
class_.invoke = invoke_wrapper(class_.invoke)
cli_runner_ = class_(mix_stderr=False)
yield cli_runner_
@pytest.fixture
def createdb():
p = subprocess.Popen(['createdb', f'halfapi_{PROJNAME}'])
p.wait()
return
def halfapicli(cli_runner):
def caller(*args):
return cli_runner.invoke(cli, ' '.join(args))
def confdir(dirname):
d = os.environ.get(dirname)
if not d:
os.environ[dirname] = tempfile.mkdtemp(prefix='halfapi_')
return os.environ.get(dirname)
if not os.path.isdir(d):
os.mkdir(d)
return d
yield caller
@pytest.fixture
def halform_conf_dir():
return confdir('HALFORM_CONF_DIR')
@pytest.fixture
def halfapi_conf_dir():
return confdir('HALFAPI_CONF_DIR')
# store history of failures per test class name and per index in parametrize (if
# parametrize used)
@ -103,26 +166,157 @@ def pytest_runtest_setup(item):
pytest.xfail("previous test failed ({})".format(test_name))
@pytest.fixture
def project_runner(runner, dropdb, createdb, halform_conf_dir, halfapi_conf_dir):
env = {
'HALFORM_CONF_DIR': halform_conf_dir,
'HALFAPI_CONF_DIR': halfapi_conf_dir
}
def project_runner(runner, halfapicli, tree):
with runner.isolated_filesystem():
res = runner.invoke(Cli, ['init', PROJNAME],
env=env,
catch_exceptions=True)
assert res.exit_code == 0
res = halfapicli('init', PROJNAME)
os.chdir(PROJNAME)
fs_path = os.getcwd()
sys.path.insert(0, fs_path)
secret = tempfile.mkstemp()
SECRET_PATH = secret[1]
with open(SECRET_PATH, 'w') as f:
f.write(str(uuid1()))
with open(os.path.join(halfapi_conf_dir, PROJNAME), 'w') as f:
"""
with open(os.path.join('.halfapi', PROJNAME), 'w') as halfapi_etc:
PROJ_CONFIG = re.sub('secret = .*', f'secret = {SECRET_PATH}',
format_halfapi_etc(PROJNAME, os.getcwd()))
f.write(PROJ_CONFIG)
halfapi_etc.write(PROJ_CONFIG)
"""
yield lambda args: runner.invoke(Cli, args, env=env)
###
# add dummy domain
###
create_domain('test_domain', 'test_domain.routers')
###
yield halfapicli
while fs_path in sys.path:
sys.path.remove(fs_path)
@pytest.fixture
def dummy_app():
app = Starlette()
app.add_route('/',
lambda request, *args, **kwargs: PlainTextResponse('Hello test!'))
app.add_middleware(
AuthenticationMiddleware,
backend=JWTAuthenticationBackend(secret_key='dummysecret')
)
return app
@pytest.fixture
def dummy_debug_app():
app = Starlette(debug=True)
app.add_route('/',
lambda request, *args, **kwargs: PlainTextResponse('Hello test!'))
app.add_middleware(
AuthenticationMiddleware,
backend=JWTAuthenticationBackend(secret_key='dummysecret')
)
return app
@pytest.fixture
def test_client(dummy_app):
return TestClient(dummy_app)
@pytest.fixture
def create_route():
def wrapped(domain_path, method, path):
stack = [domain_path, *path.split('/')[1:]]
for i in range(len(stack)):
if len(stack[i]) == 0:
continue
path = os.path.join(*stack[0:i+1])
if os.path.isdir(os.path.join(path)):
continue
os.mkdir(path)
init_path = os.path.join(*stack, '__init__.py')
with open(init_path, 'a+') as f:
f.write(f'\ndef {method}():\n raise NotImplementedError')
return wrapped
@pytest.fixture
def dummy_project():
halfapi_config = tempfile.mktemp()
halfapi_secret = tempfile.mktemp()
domain = 'dummy_domain'
with open(halfapi_config, 'w') as f:
f.writelines([
'[project]\n',
'name = lirmm_api\n',
'halfapi_version = 0.5.0\n',
f'secret = {halfapi_secret}\n',
'port = 3050\n',
'loglevel = debug\n',
'[domain.dummy_domain]\n',
f'name = {domain}\n',
'router = dummy_domain.routers\n',
f'[domain.dummy_domain.config]\n',
'test = True'
])
with open(halfapi_secret, 'w') as f:
f.write('turlututu')
return (halfapi_config, 'dummy_domain', 'dummy_domain.routers')
@pytest.fixture
def application_debug(project_runner):
halfAPI = HalfAPI({
'secret':'turlututu',
'production':False,
'domain': {
'dummy_domain': {
'name': 'dummy_domain',
'router': '.routers',
'enabled': True,
'prefix': False,
'config':{
'test': True
}
}
},
})
assert isinstance(halfAPI, HalfAPI)
yield halfAPI.application
@pytest.fixture
def application_domain(dummy_domain):
return HalfAPI({
'secret':'turlututu',
'production':True,
'domain': {
'dummy_domain': {
**dummy_domain,
'config': {
'test': True
}
}
}
}).application
@pytest.fixture
def tree():
def wrapped(path):
list_dirs = os.walk(path)
for root, dirs, files in list_dirs:
for d in dirs:
print(os.path.join(root, d))
for f in files:
print(os.path.join(root, f))
return wrapped

View File

@ -0,0 +1,27 @@
from halfapi import __version__ as halfapi_version
domain = {
'name': 'dummy_domain',
'version': '0.0.0',
'id': '8b88e60a625369235b36c2d6d70756a0c02c1c7fb169fcee6dc820bcf9723f5a',
'deps': (
('halfapi', '=={}'.format(halfapi_version)),
),
'schema_components': {
'schemas': {
'Pinnochio': {
'type': 'object',
'required': {
'id',
'name',
'nose_size'
},
'properties': {
'id': {'type': 'string'},
'name': {'type': 'string'},
'nose_size': {'type': 'number'}
}
}
}
}
}

13
tests/dummy_domain/acl.py Normal file
View File

@ -0,0 +1,13 @@
from halfapi.lib import acl
from halfapi.lib.acl import public, private, ACLS
from random import randint
def random(*args):
""" Random access ACL
"""
return randint(0,1) == 1
ACLS = (
*ACLS,
('random', random.__doc__, 10)
)

View File

View File

@ -0,0 +1,59 @@
from halfapi.lib import acl
from halfapi.lib.responses import ORJSONResponse
ACLS = {
'GET': [{'acl':acl.public}],
'POST': [{'acl':acl.public}],
'PATCH': [{'acl':acl.public}],
'PUT': [{'acl':acl.public}],
'DELETE': [{'acl':acl.public}]
}
async def get(test):
"""
description:
returns the path parameter
responses:
200:
description: test response
"""
return ORJSONResponse(str(test))
def post(test):
"""
description:
returns the path parameter
responses:
200:
description: test response
"""
return str(test)
def patch(test):
"""
description:
returns the path parameter
responses:
200:
description: test response
"""
return str(test)
def put(test):
"""
description:
returns the path parameter
responses:
200:
description: test response
"""
return str(test)
def delete(test):
"""
description:
returns the path parameter
responses:
200:
description: test response
"""
return str(test)

View File

@ -0,0 +1,14 @@
from starlette.responses import PlainTextResponse
from halfapi.lib import acl
ACLS = {
'GET': [{'acl':acl.public}]
}
async def get(request, *args, **kwargs):
"""
responses:
200:
description: dummy abc.alphabet route
"""
return PlainTextResponse('True')

View File

@ -0,0 +1,21 @@
from uuid import uuid4
from halfapi.lib import acl
ACLS = {
'GET' : [{'acl':acl.public}]
}
def get():
"""
description: The pinnochio guy
responses:
200:
description: test response
content:
application/json:
schema:
$ref: "#/components/schemas/Pinnochio"
"""
return {
'id': str(uuid4()),
'name': 'pinnochio',
'nose_size': 42
}

View File

@ -0,0 +1,79 @@
from ... import acl
from halfapi.logging import logger
ACLS = {
'GET' : [
{
'acl':acl.public,
'args': {
'required': {
'foo', 'bar'
},
'optional': {
'x'
}
}
},
{
'acl':acl.random,
'args': {
'required': {
'foo', 'baz'
},
'optional': {
'truebidoo'
}
}
},
],
'POST' : [
{
'acl':acl.private,
'args': {
'required': {
'foo', 'bar'
},
'optional': {
'x'
}
}
},
{
'acl':acl.public,
'args': {
'required': {
'foo', 'baz'
},
'optional': {
'truebidoo',
'z'
}
}
},
]
}
def get(data):
"""
description:
returns the arguments passed in
responses:
200:
description: test response
"""
logger.error('%s', data['foo'])
return data
def post(data):
"""
description:
returns the arguments passed in
responses:
200:
description: test response
"""
logger.error('%s', data)
return data

View File

@ -0,0 +1,69 @@
from halfapi.lib.responses import ORJSONResponse, NotImplementedResponse
from ... import acl
ROUTES = {
'abc/alphabet/{test:uuid}': {
'GET': [{'acl': acl.public}]
},
'abc/pinnochio': {
'GET': [{'acl': acl.public}]
},
'config': {
'GET': [{'acl': acl.public}]
},
'arguments': {
'GET': [{
'acl': acl.public,
'args': {
'required': {'foo', 'bar'},
'optional': set()
}
}]
},
}
async def get_abc_alphabet_TEST(request, *args, **kwargs):
"""
description: Not implemented
responses:
200:
description: test response
parameters:
- name: test
in: path
description: Test parameter in route with "ROUTES" constant
required: true
schema:
type: string
"""
return NotImplementedResponse()
async def get_abc_pinnochio(request, *args, **kwargs):
"""
description: Not implemented
responses:
200:
description: test response
"""
return NotImplementedResponse()
async def get_config(request, *args, **kwargs):
"""
description: Not implemented
responses:
200:
description: test response
"""
return NotImplementedResponse()
async def get_arguments(request, *args, **kwargs):
"""
description: Liste des datatypes.
responses:
200:
description: test response
"""
return ORJSONResponse({
'foo': kwargs.get('data').get('foo'),
'bar': kwargs.get('data').get('bar')
})

View File

@ -0,0 +1,33 @@
from ... import acl
from halfapi.logging import logger
ACLS = {
'GET' : [
{'acl':acl.public},
{'acl':acl.random},
]
}
def get(halfapi):
"""
description:
returns the configuration of the domain
responses:
200:
description: test response
"""
logger.error('%s', halfapi)
# TODO: Remove in 0.7.0
try:
assert 'test' in halfapi['config']['domain']['dummy_domain']['config']
except AssertionError as exc:
logger.error('No TEST in halfapi[config][domain][dummy_domain][config]')
raise exc
try:
assert 'test' in halfapi['config']
except AssertionError as exc:
logger.error('No TEST in halfapi[config]')
raise exc
return halfapi['config']

View File

@ -0,0 +1,8 @@
param_docstring = """
name: second
in: path
description: second parameter description test
required: true
schema:
type: string
"""

View File

@ -0,0 +1,20 @@
from uuid import UUID
from halfapi.lib import acl
ACLS = {
'GET': [{'acl': acl.public}]
}
def get(first, second, third):
"""
description: a Test route for path parameters
responses:
200:
description: The test passed!
500:
description: The test did not pass :(
"""
assert isintance(first, str)
assert isintance(second, UUID)
assert isintance(third, int)
return ''

View File

@ -0,0 +1,13 @@
from halfapi.lib import acl
ACLS = {
'GET': [{'acl':acl.public}]
}
def get(ext, ret_type='html'):
"""
responses:
200:
description: dummy abc.alphabet route
"""
return '\n'.join(('trololo', '', 'ololotr'))

View File

@ -0,0 +1,31 @@
from halfapi.lib import acl
ACLS = {
'GET': [{'acl':acl.public}],
'POST': [
{
'acl':acl.public,
'args': {
'required': {'trou'},
'optional': {'troo'}
}
}
]
}
def get(ext, halfapi={}, ret_type='html'):
"""
responses:
200:
description: dummy abc.alphabet route
"""
return '\n'.join(('trololo', '', 'ololotr'))
def post(ext, data={'troo': 'fidget'}, halfapi={}, ret_type='html'):
"""
responses:
200:
description: dummy abc.alphabet route
"""
print(data)
return '\n'.join(('trololo', '', 'ololotr'))

View File

@ -0,0 +1,13 @@
from halfapi.lib import acl
ACLS = {
'GET': [{'acl':acl.public}]
}
def get(ret_type='html'):
"""
responses:
200:
description: dummy abc.alphabet route
"""
return '\n'.join(('trololo', '', 'ololotr'))

View File

@ -0,0 +1,4 @@
from . import get
def test_get():
assert isinstance(get(), str)

25
tests/setup.py Normal file
View File

@ -0,0 +1,25 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
def get_packages(package):
"""
Return root package and all sub-packages.
"""
return [
dirpath
for dirpath, dirnames, filenames in os.walk(package)
if os.path.exists(os.path.join(dirpath, "__init__.py"))
]
from setuptools import setup, find_packages
module_name="dummy_domain"
setup(
name=module_name,
version='0',
url="https://gite.lirmm.fr/malves/halfapi",
packages=get_packages(module_name),
python_requires=">=3.7",
install_requires=[]
)

37
tests/test_acl.py Normal file
View File

@ -0,0 +1,37 @@
import pytest
from starlette.responses import PlainTextResponse
from starlette.testclient import TestClient
from halfapi.half_route import HalfRoute
from halfapi.lib import acl
def test_acl_Check(dummy_app, token_debug_false_builder):
"""
A request with ?check should always return a 200 status code
"""
@HalfRoute.acl_decorator(params=[{'acl':acl.public}])
async def test_route_public(request, **kwargs):
raise Exception('Should not raise')
return PlainTextResponse('ok')
dummy_app.add_route('/test_public', test_route_public)
test_client = TestClient(dummy_app)
resp = test_client.request('get', '/test_public?check')
assert resp.status_code == 200
@HalfRoute.acl_decorator(params=[{'acl':acl.private}])
async def test_route_private(request, **kwargs):
raise Exception('Should not raise')
return PlainTextResponse('ok')
dummy_app.add_route('/test_private', test_route_private)
test_client = TestClient(dummy_app)
resp = test_client.request('get', '/test_private')
assert resp.status_code == 401
resp = test_client.request('get', '/test_private?check')
assert resp.status_code == 200

15
tests/test_app.py Normal file
View File

@ -0,0 +1,15 @@
import os
from starlette.applications import Starlette
from unittest.mock import MagicMock, patch
from halfapi.halfapi import HalfAPI
from halfapi.lib.domain import NoDomainsException
def test_halfapi_dummy_domain(dummy_domain):
with patch('starlette.applications.Starlette') as mock:
mock.return_value = MagicMock()
config = {}
config['domain'] = {}
config['domain'][dummy_domain['name']] = dummy_domain
print(config)
halfapi = HalfAPI(config)

View File

@ -1,96 +0,0 @@
#!/usr/bin/env python3
import os
import subprocess
import importlib
import tempfile
from unittest.mock import patch
import pytest
from click.testing import CliRunner
from configparser import ConfigParser
from halfapi import __version__
from halfapi.cli import cli
Cli = cli.cli
PROJNAME = os.environ.get('PROJ','tmp_api')
@pytest.mark.incremental
class TestCli():
def test_options(self, runner, dropdb, createdb):
# Wrong command
with runner.isolated_filesystem():
r = runner.invoke(Cli, ['foobar'])
assert r.exit_code == 2
# Test existing commands
with runner.isolated_filesystem():
r = runner.invoke(Cli, ['--help'])
assert r.exit_code == 0
with runner.isolated_filesystem():
r = runner.invoke(Cli, ['--version'])
assert r.exit_code == 0
with runner.isolated_filesystem():
r = runner.invoke(Cli, ['init', '--help'])
assert r.exit_code == 0
def test_init_project_fail(self, runner, dropdb):
# Missing argument (project)
testproject = 'testproject'
r = runner.invoke(Cli, ['init'])
assert r.exit_code == 2
with runner.isolated_filesystem():
# Fail : Wrong project name
r = runner.invoke(Cli, ['init', 'test*-project'])
assert r.exit_code == 1
with runner.isolated_filesystem():
# Fail : Already existing folder
os.mkdir(testproject)
r = runner.invoke(Cli, ['init', testproject])
assert r.exit_code == 1
with runner.isolated_filesystem():
# Fail : Already existing nod
os.mknod(testproject)
r = runner.invoke(Cli, ['init', testproject])
assert r.exit_code == 1
def test_init_project(self, runner, dropdb, createdb, halform_conf_dir, halfapi_conf_dir):
cp = ConfigParser()
with runner.isolated_filesystem():
env = {
'HALFORM_CONF_DIR': halform_conf_dir,
'HALFAPI_CONF_DIR': halfapi_conf_dir
}
res = runner.invoke(Cli, ['init', PROJNAME], env=env)
try:
assert os.path.isdir(PROJNAME)
assert os.path.isdir(os.path.join(PROJNAME, '.halfapi'))
# .halfapi/config check
assert os.path.isfile(os.path.join(PROJNAME, '.halfapi', 'config'))
cp.read(os.path.join(PROJNAME, '.halfapi', 'config'))
assert cp.has_section('project')
assert cp.has_option('project', 'name')
assert cp.get('project', 'name') == PROJNAME
assert cp.get('project', 'halfapi_version') == __version__
# .halfapi/domains check
assert os.path.isfile(os.path.join(PROJNAME, '.halfapi', 'domains'))
cp.read(os.path.join(PROJNAME, '.halfapi', 'domains'))
assert cp.has_section('domains')
except AssertionError as e:
subprocess.run(['tree', '-a', os.getcwd()])
raise e
assert res.exit_code == 0
assert res.exception is None

View File

@ -1,44 +0,0 @@
#!/usr/bin/env python3
import os
import subprocess
import importlib
import tempfile
from unittest.mock import patch
import pytest
from click.testing import CliRunner
from configparser import ConfigParser
PROJNAME = os.environ.get('PROJ','tmp_api')
@pytest.fixture
def subproc(project_runner):
def caller(cmd):
proc = subprocess.Popen(cmd.split(' '))
return proc.wait()
return caller
@pytest.mark.incremental
class TestCliProj():
def test_cmds(self, subproc):
assert subproc('halfapi run --help') == 0
assert subproc('halfapi domain --help') == 0
def test_config_commands(self, subproc):
res = subproc('halfapi config pr00t')
assert res == 2
res = subproc('halfapi config --help')
assert res == 0
res = subproc('halfapi config')
assert res == 0
def test_domain_commands(self, subproc):
res = subproc('halfapi domain foobar')
assert res == 2
res = subproc('halfapi domain --help')
assert res == 0
def test_domain_create(self, subproc):
DOMNAME='tmp_domain'
res = subproc(f'halfapi domain --create')
assert res == 0

52
tests/test_conf.py Normal file
View File

@ -0,0 +1,52 @@
from unittest import TestCase
import sys
import pytest
from halfapi.halfapi import HalfAPI
class TestConf(TestCase):
def setUp(self):
self.args = {
'domain': {
'dummy_domain': {
'name': 'dummy_domain',
'router': '.routers',
'enabled': True,
'prefix': False,
}
}
}
def tearDown(self):
pass
def test_conf_production_default(self):
halfapi = HalfAPI({
**self.args
})
assert halfapi.PRODUCTION is True
def test_conf_production_true(self):
halfapi = HalfAPI({
**self.args,
'production': True,
})
assert halfapi.PRODUCTION is True
def test_conf_production_false(self):
halfapi = HalfAPI({
**self.args,
'production': False,
})
assert halfapi.PRODUCTION is False
def test_conf_variables(self):
from halfapi.conf import (
CONFIG,
SCHEMA,
)
assert isinstance(CONFIG, dict)
assert isinstance(CONFIG.get('project_name'), str)
assert isinstance(SCHEMA, dict)
assert isinstance(CONFIG.get('secret'), str)
assert isinstance(CONFIG.get('host'), str)
assert isinstance(CONFIG.get('port'), int)

14
tests/test_constants.py Normal file
View File

@ -0,0 +1,14 @@
from schema import Schema
def test_constants():
from halfapi.lib.constants import (
VERBS,
ACLS_SCHEMA,
ROUTE_SCHEMA,
DOMAIN_SCHEMA,
API_SCHEMA)
assert isinstance(VERBS, tuple)
assert isinstance(ACLS_SCHEMA, Schema)
assert isinstance(ROUTE_SCHEMA, Schema)
assert isinstance(DOMAIN_SCHEMA, Schema)
assert isinstance(API_SCHEMA, Schema)

View File

@ -2,14 +2,66 @@
import pytest
from starlette.authentication import UnauthenticatedUser
from starlette.testclient import TestClient
from halfapi.app import app
import subprocess
import json
import os
import sys
import pprint
import openapi_spec_validator
import logging
logger = logging.getLogger()
def test_itworks():
c = TestClient(app)
r = c.get('/')
assert r.text == 'It Works!'
from halfapi.lib.constants import API_SCHEMA
def test_user():
c = TestClient(app)
r = c.get('/user')
def test_halfapi_whoami(application_debug):
# @TODO : If we use isolated filesystem multiple times that creates a bug.
# So we use a single function with fixture "application debug"
c = TestClient(application_debug)
r = c.request('get', '/halfapi/whoami')
assert r.status_code == 200
def test_halfapi_log(application_debug):
# @TODO : If we use isolated filesystem multiple times that creates a bug.
# So we use a single function with fixture "application debug"
c = TestClient(application_debug)
r = c.request('get', '/halfapi/log')
assert r.status_code == 200
def test_halfapi_error_400(application_debug):
# @TODO : If we use isolated filesystem multiple times that creates a bug.
# So we use a single function with fixture "application debug"
c = TestClient(application_debug)
r = c.request('get', '/halfapi/error/400')
assert r.status_code == 400
def test_halfapi_error_404(application_debug):
# @TODO : If we use isolated filesystem multiple times that creates a bug.
# So we use a single function with fixture "application debug"
c = TestClient(application_debug)
r = c.request('get', '/halfapi/error/404')
assert r.status_code == 404
def test_halfapi_error_500(application_debug):
# @TODO : If we use isolated filesystem multiple times that creates a bug.
# So we use a single function with fixture "application debug"
c = TestClient(application_debug)
r = c.request('get', '/halfapi/error/500')
assert r.status_code == 500
def test_schema(application_debug):
c = TestClient(application_debug)
r = c.request('get', '/')
schema = r.json()
assert isinstance(schema, dict)
openapi_spec_validator.validate_spec(schema)

106
tests/test_domain.py Normal file
View File

@ -0,0 +1,106 @@
import pytest
from halfapi.testing.test_domain import TestDomain
from pprint import pprint
import logging
logger = logging.getLogger()
class TestDummyDomain(TestDomain):
from .dummy_domain import domain
__name__ = domain.get('name')
__routers__ = domain.get('routers')
DOMAIN = __name__
CONFIG = {'test': True}
def test_domain(self):
self.check_domain()
def test_routes(self):
self.check_routes()
def test_html_route(self):
res = self.client.request('get', '/ret_type')
assert res.status_code == 200
assert isinstance(res.content.decode(), str)
assert res.headers['content-type'].split(';')[0] == 'text/html'
res = self.client.request('get', '/ret_type/h24')
assert res.status_code == 200
assert isinstance(res.content.decode(), str)
assert res.headers['content-type'].split(';')[0] == 'text/html'
res = self.client.request('get', '/ret_type/h24/config')
assert res.status_code == 200
assert isinstance(res.content.decode(), str)
assert res.headers['content-type'].split(';')[0] == 'text/html'
res = self.client.request('post', '/ret_type/h24/config', json={
'trou': 'glet'
})
assert res.status_code == 200
assert isinstance(res.content.decode(), str)
assert res.headers['content-type'].split(';')[0] == 'text/html'
def test_arguments__get_routes(self):
res = self.client.request('post', '/arguments?foo=1&x=3')
assert res.status_code == 400
arg_dict = {'foo': '1', 'bar': '2', 'x': '3'}
res = self.client.request('get', '/arguments?foo=1&bar=2&x=3')
assert res.json() == arg_dict
res = self.client.request('get', '/arguments?foo=1&bar=2&x=3&y=4')
assert res.json() == arg_dict
def test_arguments_post_routes(self):
arg_dict = {}
res = self.client.request('post', '/arguments', json=arg_dict)
assert res.status_code == 400
arg_dict = {'foo': '1', 'bar': '3'}
res = self.client.request('post', '/arguments', json=arg_dict)
assert res.status_code == 400
arg_dict = {'foo': '1', 'baz': '3'}
res = self.client.request('post', '/arguments', json=arg_dict)
assert res.json() == arg_dict
arg_dict = {'foo': '1', 'baz': '3', 'truebidoo': '4'}
res = self.client.request('post', '/arguments', json=arg_dict)
assert res.json() == arg_dict
res = self.client.request('post', '/arguments', json={ **arg_dict, 'y': '4'})
assert res.json() == arg_dict
res = self.client.request('post', '/arguments', json={ **arg_dict, 'z': True})
assert res.json() == {**arg_dict, 'z': True}
def test_schema_path_params(self):
res = self.client.request('get', '/halfapi/schema')
schema = res.json()
logger.debug(schema)
assert len(schema['paths']) > 0
route = schema['paths']['/path_params/{first}/one/{second}/two/{third}']
assert 'parameters' in route['get']
parameters = route['get']['parameters']
assert len(parameters) == 3
param_map = {
elt['name']: elt
for elt in parameters
}
assert param_map['second']['description'] == 'second parameter description test'

View File

@ -0,0 +1,19 @@
import importlib
from halfapi.testing.test_domain import TestDomain
def test_dummy_domain():
from . import dummy_domain
from .dummy_domain import acl
assert acl.public() is True
assert isinstance(acl.random(), int)
assert acl.private() is False
from .dummy_domain import routers
from .dummy_domain.routers.arguments import get
from .dummy_domain.routers.abc.alphabet.TEST_uuid import get
from .dummy_domain.routers.abc.pinnochio import get
from .dummy_domain.routers.config import get
from .dummy_domain.routers.config import get
from .dummy_domain.routers import async_router
from .dummy_domain.routers.async_router import ROUTES, get_abc_alphabet_TEST, get_abc_pinnochio, get_config, get_arguments

View File

@ -0,0 +1,109 @@
import os
import sys
import importlib
import subprocess
import time
import pytest
import json
from pprint import pprint
from starlette.routing import Route
from starlette.testclient import TestClient
def test_get_config_route(dummy_project, application_domain):
c = TestClient(application_domain)
r = c.request('get', '/')
assert r.status_code == 200
pprint(r.json())
r = c.request('get', '/config')
assert r.status_code == 200
pprint(r.json())
assert 'test' in r.json()
def test_get_route(dummy_project, application_domain):
c = TestClient(application_domain)
path = verb = params = None
dummy_domain_routes = [
('config','GET'),
('config','GET'),
('async_router/abc/pinnochio','GET'),
('async_router/config','GET'),
# ('abc/pinnochio','GET'),
# ('abc/alphabet','GET'),
]
for route_def in dummy_domain_routes:
path, verb = route_def[0], route_def[1]
route_path = '/{}'.format(path)
print(route_path)
try:
if verb.lower() == 'get':
r = c.request('get', route_path)
elif verb.lower() == 'post':
r = c.request('post', route_path)
elif verb.lower() == 'patch':
r = c.request('patch', route_path)
elif verb.lower() == 'put':
r = c.request('put', route_path)
elif verb.lower() == 'delete':
r = c.request('delete', route_path)
else:
raise Exception(verb)
try:
assert r.status_code in [200, 501]
except AssertionError as exc:
print('{} [{}] {}'.format(str(r.status_code), verb, route_path))
raise exc from exc
except NotImplementedError:
pass
dummy_domain_path_routes = [
('abc/alphabet/{test}','GET'),
]
#for route_def in dummy_domain_path_routes:
for route_def in []:#dummy_domain_routes:
from uuid import uuid4
test_uuid = uuid4()
for route_def in dummy_domain_path_routes:
path, verb = route_def[0], route_def[1]
path = path.format(test=str(test_uuid))
route_path = f'/{path}'
if verb.lower() == 'get':
r = c.request('get', f'{route_path}')
assert r.status_code == 200
def test_delete_route(dummy_project, application_domain):
c = TestClient(application_domain)
from uuid import uuid4
arg = str(uuid4())
r = c.request('delete', f'/abc/alphabet/{arg}')
assert r.status_code == 200
assert isinstance(r.json(), str)
def test_arguments_route(dummy_project, application_domain):
c = TestClient(application_domain)
path = '/arguments'
r = c.request('get', path)
assert r.status_code == 400
r = c.request('get', path, params={'foo':True})
assert r.status_code == 400
arg = {'foo':True, 'bar':True}
r = c.request('get', path, params=arg)
assert r.status_code == 200
for key, val in arg.items():
assert json.loads(r.json()[key]) == val
path = '/async_router/arguments'
r = c.request('get', path)
assert r.status_code == 400
r = c.request('get', path, params={'foo':True})
assert r.status_code == 400
arg = {'foo':True, 'bar':True}
r = c.request('get', path, params=arg)
assert r.status_code == 200
for key, val in arg.items():
assert json.loads(r.json()[key]) == val

6
tests/test_halfapi.py Normal file
View File

@ -0,0 +1,6 @@
from halfapi.halfapi import HalfAPI
def test_methods():
assert 'application' in dir(HalfAPI)
assert 'version' in dir(HalfAPI)
assert 'version_async' in dir(HalfAPI)

View File

@ -14,131 +14,12 @@ from starlette.testclient import TestClient
from starlette.authentication import (
AuthenticationBackend, AuthenticationError, BaseUser, AuthCredentials,
UnauthenticatedUser)
from starlette.responses import PlainTextResponse
#from halfapi.app import app
os.environ['HALFAPI_PROD'] = 'True'
os.environ['HALFAPI_SECRET'] = 'randomsecret'
from halfapi.lib.jwt_middleware import (PRODUCTION, SECRET,
from halfapi.lib.jwt_middleware import (
JWTUser, JWTAuthenticationBackend,
JWTWebSocketAuthenticationBackend)
def test_constants():
assert PRODUCTION == bool(os.environ['HALFAPI_PROD'])
assert SECRET == os.environ['HALFAPI_SECRET']
@pytest.fixture
def token():
# This fixture needs to have a running auth-lirmm on 127.0.0.1:3000
# Sets a valid token
r = requests.post('http://127.0.0.1:3000/',
data={'email':'maizi', 'password':'a'})
if len(r.text) <= 0:
raise Exception('No result in token retrieval')
try:
res = json.loads(r.text)
except JSONDecodeError:
raise Exception('Malformed response from token retrieval')
if 'token' not in res.keys():
raise Exception('Missing token in token request')
return res['token']
@pytest.fixture
def token_builder():
yield jwt.encode({
'name':'xxx',
'id': str(uuid4())},
key=SECRET
)
@pytest.fixture
def token_debug_false_builder():
yield jwt.encode({
'name':'xxx',
'id': str(uuid4()),
'debug': False},
key=SECRET
)
@pytest.fixture
def token_debug_true_builder():
yield jwt.encode({
'name':'xxx',
'id': str(uuid4()),
'debug': True},
key=SECRET
)
@pytest.fixture
def token_dirser():
# This fixture needs to have a running auth-lirmm on 127.0.0.1:3000
# Sets a valid token
r = requests.post('http://127.0.0.1:3000/',
data={'email':'dhenaut', 'password':'a'})
if len(r.text) <= 0:
raise Exception('No result in token retrieval')
try:
res = json.loads(r.text)
except JSONDecodeError:
raise Exception('Malformed response from token retrieval')
if 'token' not in res.keys():
raise Exception('Missing token in token request')
return res['token']
"""
def test_token(token):
client = TestClient(app)
r = client.get('/user', headers={'Authorization':token})
res = False
try:
res = json.loads(r.text)
except JSONDecodeError:
raise Exception('Malformed response from /user request')
assert 'user' in res.keys()
assert 'id' in res['user'].keys()
assert 'token' in res['user'].keys()
assert 'payload' in res['user'].keys()
def test_labopers(token, token_dirser):
res = requests.get('http://127.0.0.1:8080/api/v4/organigramme/laboratoire/personnel',
params={
'q': 'limit:10|format:csv'
},
headers={
'Authorization': token
})
assert res.status_code == 401
res = requests.get('http://127.0.0.1:8080/api/v4/organigramme/laboratoire/personnel',
params={
'q': 'limit:10|format:csv'
},
headers={
'Authorization': token_dirser
})
assert res.status_code == 200
"""
def test_JWTUser():
uid = uuid4()
@ -150,47 +31,102 @@ def test_JWTUser():
assert user.payload == payload
assert user.is_authenticated == True
@pytest.mark.asyncio
async def test_JWTAuthenticationBackend(token_builder):
backend = JWTAuthenticationBackend()
assert backend.secret_key == SECRET
def test_jwt_NoToken(dummy_app):
async def test_route(request):
assert isinstance(request.user, UnauthenticatedUser)
return PlainTextResponse('ok')
req = Request(
dummy_app.add_route('/test', test_route)
test_client = TestClient(dummy_app)
resp = test_client.request('get', '/test')
assert resp.status_code == 200
def test_jwt_Token(dummy_app, token_builder):
async def test_route(request):
assert isinstance(request.user, JWTUser)
print(request.scope['app'].debug)
return PlainTextResponse('ok')
dummy_app.add_route('/test', test_route)
test_client = TestClient(dummy_app)
resp = test_client.request('get', '/test',
cookies={
'Authorization': token_builder
})
assert resp.status_code == 200
resp = test_client.request('get', '/test',
headers={
'Authorization': token_builder
})
credentials, user = await backend.authenticate(req)
assert type(user) == JWTUser
assert type(credentials) == AuthCredentials
assert resp.status_code == 200
@pytest.mark.asyncio
async def test_JWTAuthenticationBackend_DebugFalse(token_debug_false_builder):
backend = JWTAuthenticationBackend()
assert backend.secret_key == SECRET
req = Request(
def test_jwt_DebugFalse(dummy_app, token_debug_false_builder):
async def test_route(request):
assert isinstance(request.user, JWTUser)
return PlainTextResponse('ok')
dummy_app.add_route('/test', test_route)
test_client = TestClient(dummy_app)
resp = test_client.request('get', '/test',
cookies={
'Authorization': token_debug_false_builder
})
assert resp.status_code == 200
resp = test_client.request('get', '/test',
headers={
'Authorization': token_debug_false_builder
})
credentials, user = await backend.authenticate(req)
assert type(user) == JWTUser
assert type(credentials) == AuthCredentials
assert resp.status_code == 200
@pytest.mark.asyncio
async def test_JWTAuthenticationBackend_DebugTrue(token_debug_true_builder):
backend = JWTAuthenticationBackend()
assert backend.secret_key == SECRET
def test_jwt_DebugTrue(dummy_app, token_debug_true_builder):
"""
A debug token should return a 400 status code with a non debug app
"""
async def test_route(request):
return PlainTextResponse('ok')
req = Request(
dummy_app.add_route('/test', test_route)
test_client = TestClient(dummy_app)
resp = test_client.request('get', '/test',
cookies={
'Authorization': token_debug_true_builder
})
assert resp.status_code == 400
resp = test_client.request('get', '/test',
headers={
'Authorization': token_debug_true_builder
})
assert resp.status_code == 400
try:
await backend.authenticate(req)
except Exception as e:
assert type(e) == AuthenticationError
def test_jwt_DebugTrue_DebugApp(dummy_debug_app, token_debug_true_builder):
"""
A debug token should return a 200 status code with a debug app
"""
async def test_route(request):
assert isinstance(request.user, JWTUser)
return PlainTextResponse('ok')
dummy_debug_app.add_route('/test', test_route)
test_client = TestClient(dummy_debug_app)
resp = test_client.request('get', '/test',
cookies={
'Authorization': token_debug_true_builder
})
assert resp.status_code == 200
resp = test_client.request('get', '/test',
headers={
'Authorization': token_debug_true_builder
})
assert resp.status_code == 200

View File

@ -1,54 +0,0 @@
import os
import jwt
from requests import Request
import pytest
from unittest.mock import patch
import json
from json.decoder import JSONDecodeError
import sys
from hashlib import sha256
from base64 import b64decode
from uuid import uuid4, UUID
from starlette.testclient import TestClient
from starlette.authentication import (
AuthenticationBackend, AuthenticationError, BaseUser, AuthCredentials,
UnauthenticatedUser)
#from halfapi.app import app
os.environ['HALFAPI_PROD'] = ''
os.environ['HALFAPI_SECRET'] = 'randomsecret'
from halfapi.lib.jwt_middleware import (PRODUCTION, SECRET,
JWTUser, JWTAuthenticationBackend,
JWTWebSocketAuthenticationBackend)
def test_constants():
assert PRODUCTION == bool(os.environ['HALFAPI_PROD'])
assert SECRET == os.environ['HALFAPI_SECRET']
@pytest.fixture
def token_debug_builder():
yield jwt.encode({
'name':'xxx',
'id': str(uuid4()),
'debug': True},
key=SECRET
)
@pytest.mark.asyncio
async def test_JWTAuthenticationBackend_debug(token_debug_builder):
backend = JWTAuthenticationBackend()
req = Request(
headers={
'Authorization': token_debug_builder
})
auth = await backend.authenticate(req)
assert(len(auth) == 2)
assert type(auth[0]) == AuthCredentials
assert type(auth[1]) == JWTUser

124
tests/test_lib_domain.py Normal file
View File

@ -0,0 +1,124 @@
# #!/usr/bin/env python3
# import importlib
# from halfapi.lib.domain import VERBS, gen_routes, gen_router_routes, \
#  MissingAclError, domain_schema_dict, domain_schema_list
# 
# from types import FunctionType
# 
# 
# def test_gen_router_routes():
#  from .dummy_domain import routers
#  for path, verb, m_router, fct, params in gen_router_routes(routers, ['dummy_domain']):
#  assert isinstance(path, str)
#  assert verb in VERBS
#  assert len(params) > 0
#  assert hasattr(fct, '__call__')
#  assert len(m_router.__file__) > 0
# 
# 
# def test_gen_routes():
#  from .dummy_domain.routers.abc.alphabet import TEST_uuid
#  try:
#  gen_routes(
#  TEST_uuid,
#  'get',
#  ['abc', 'alphabet', 'TEST_uuid', ''],
#  [])
#  except MissingAclError:
#  assert True
# 
#  fct, params = gen_routes(
#  TEST_uuid,
#  'get',
#  ['abc', 'alphabet', 'TEST_uuid', ''],
#  TEST_uuid.ACLS['GET'])
# 
#  assert isinstance(fct, FunctionType)
#  assert isinstance(params, list)
#  assert len(TEST_uuid.ACLS['GET']) == len(params)
# 
# def test_domain_schema_dict():
#  from .dummy_domain import routers
#  d_res = domain_schema_dict(routers)
# 
#  assert isinstance(d_res, dict)
# 
# def test_domain_schema_list():
#  from .dummy_domain import routers
#  res = domain_schema_list(routers)
# 
#  assert isinstance(res, list)
#  assert len(res) > 0
from starlette.testclient import TestClient
from starlette.responses import Response
from starlette.routing import Router, Route
from halfapi.lib.domain import route_decorator
from halfapi.lib.user import Nobody
def test_route_decorator():
""" It should decorate an async function that fullfills its arguments
"""
def route(halfapi, data, out, ret_type='txt'):
for key in ['user', 'config', 'domain', 'cookies', 'base_url', 'url']:
assert key in halfapi
assert halfapi['user'] is None
assert isinstance(halfapi['config'], dict)
assert len(halfapi['config']) == 0
assert isinstance(halfapi['domain'], str)
assert halfapi['domain'] == 'unknown'
assert isinstance(halfapi['cookies'], dict)
assert len(halfapi['cookies']) == 0
assert len(str(halfapi['base_url'])) > 0
assert str(halfapi['base_url']) == 'http://testserver/'
assert len(str(halfapi['url'])) > 0
assert str(halfapi['url']) == 'http://testserver/'
assert isinstance(data, dict)
assert len(data) == 0
assert out is None
assert ret_type is 'txt'
return ''
async_route = route_decorator(route)
app = Router([Route('/', endpoint=async_route, methods=['GET'])])
client = TestClient(app)
response = client.get('/')
assert response.is_success
assert response.content.decode() == ''
def route(data, out, ret_type='txt'):
assert isinstance(data, dict)
assert len(data) == 0
assert out is None
assert ret_type is 'txt'
return ''
async_route = route_decorator(route)
app = Router([Route('/', endpoint=async_route, methods=['GET'])])
client = TestClient(app)
response = client.get('/')
assert response.is_success
assert response.content.decode() == ''
def route(data):
assert isinstance(data, dict)
assert len(data) == 2
assert data['toto'] == 'tata'
assert data['bouboul'] == True
return ''
async_route = route_decorator(route)
app = Router([Route('/', endpoint=async_route, methods=['POST'])])
client = TestClient(app)
response = client.post('/', json={'toto': 'tata', 'bouboul': True})
assert response.is_success
assert response.json() == ''

View File

@ -0,0 +1,38 @@
from starlette.testclient import TestClient
from starlette.middleware.base import BaseHTTPMiddleware
from unittest.mock import patch
from halfapi.lib.domain_middleware import DomainMiddleware
def test_init():
with patch('starlette.middleware.base.BaseHTTPMiddleware.__init__') as init:
mw = DomainMiddleware('app', 'domain')
init.assert_called_once_with('app')
assert mw.domain == 'domain'
assert mw.request == None
def test_call(application_debug):
c = TestClient(application_debug)
r = c.request('get', '/abc/alphabet')
assert r.status_code == 200
assert r.headers['x-domain'] == 'dummy_domain'
assert r.headers['x-acl'] == 'public'
r = c.request('get', '/arguments')
assert r.status_code == 400
assert r.headers['x-domain'] == 'dummy_domain'
assert r.headers['x-acl'] == 'public'
assert 'foo' in r.headers['x-args-required'].split(',')
assert 'bar' in r.headers['x-args-required'].split(',')
assert r.headers['x-args-optional'] == 'x'
c = TestClient(application_debug)
r = c.request('post', '/arguments')
assert r.status_code == 400
assert r.headers['x-domain'] == 'dummy_domain'
assert r.headers['x-acl'] == 'public'
assert 'foo' in r.headers['x-args-required'].split(',')
assert 'baz' in r.headers['x-args-required'].split(',')
assert 'truebidoo' in r.headers['x-args-optional'].split(',')
assert 'z' in r.headers['x-args-optional'].split(',')

View File

@ -0,0 +1,62 @@
import json
import decimal
import datetime
from starlette.responses import Response
from halfapi.lib.responses import *
def test_orjson():
test_obj = {
"ok": "ko",
"dec": decimal.Decimal(42),
"set": {0, 4, 2},
"date": datetime.date(1,1,1),
"datetime": datetime.datetime(1,1,1),
}
resp = ORJSONResponse(test_obj)
body = resp.body.decode()
test_obj_dec = json.loads(body)
print(test_obj_dec)
assert 'ok' in test_obj_dec.keys()
assert isinstance(test_obj_dec['ok'], str)
assert isinstance(test_obj_dec['dec'], str)
assert isinstance(test_obj_dec['set'], list)
assert isinstance(test_obj_dec['date'], str)
assert test_obj_dec['date'] == '0001-01-01'
assert test_obj_dec['datetime'] == '0001-01-01T00:00:00'
def test_responses():
resp = HJSONResponse('')
assert isinstance(resp, Response)
assert resp.status_code == 200
resp = ORJSONResponse('')
assert isinstance(resp, Response)
assert resp.status_code == 200
resp = PlainTextResponse()
assert isinstance(resp, Response)
assert resp.status_code == 200
def test_errors():
resp = ServiceUnavailableResponse()
assert isinstance(resp, Response)
assert resp.status_code == 503
resp = UnauthorizedResponse()
assert isinstance(resp, Response)
assert resp.status_code == 401
resp = InternalServerErrorResponse()
assert isinstance(resp, Response)
assert resp.status_code == 500
resp = NotFoundResponse()
assert isinstance(resp, Response)
assert resp.status_code == 404
resp = NotImplementedResponse()
assert isinstance(resp, Response)
assert resp.status_code == 501

57
tests/test_lib_router.py Normal file
View File

@ -0,0 +1,57 @@
# import os
# import pytest
# from schema import SchemaError
# from halfapi.lib.router import read_router
# from halfapi.lib.constants import ROUTER_SCHEMA, ROUTER_ACLS_SCHEMA
# 
# def test_read_router_routers():
#  from .dummy_domain import routers
# 
#  router_d = read_router(routers)
#  assert '' in router_d
#  assert 'SUBROUTES' in router_d['']
#  assert isinstance(router_d['']['SUBROUTES'], list)
# 
#  for elt in os.scandir(routers.__path__[0]):
#  if elt.is_dir():
#  assert elt.name in router_d['']['SUBROUTES']
# 
# def test_read_router_abc():
#  from .dummy_domain.routers import abc
#  router_d = read_router(abc)
# 
#  assert '' in router_d
#  assert 'SUBROUTES' in router_d['']
#  assert isinstance(router_d['']['SUBROUTES'], list)
# 
# def test_read_router_alphabet():
#  from .dummy_domain.routers.abc import alphabet
#  router_d = read_router(alphabet)
# 
#  assert '' in router_d
#  assert 'SUBROUTES' in router_d['']
#  assert isinstance(router_d['']['SUBROUTES'], list)
# 
#  ROUTER_SCHEMA.validate(router_d)
# 
#  with pytest.raises(SchemaError):
#  """ Test that we cannot specify wrong method in ROUTES or ACLS
# 
#  TODO: Write more errors
#  """
#  router_d['']['TEG'] = {}
#  ROUTER_SCHEMA.validate(router_d)
# 
# def test_read_router_TEST():
#  from .dummy_domain.routers.abc.alphabet import TEST_uuid
#  router_d = read_router(TEST_uuid)
# 
#  print(router_d)
#  assert '' in router_d
#  assert 'SUBROUTES' in router_d['']
#  assert isinstance(router_d['']['GET'], list)
#  assert isinstance(router_d['']['POST'], list)
#  assert isinstance(router_d['']['PATCH'], list)
#  assert isinstance(router_d['']['PUT'], list)
# 
# 

34
tests/test_lib_routes.py Normal file
View File

@ -0,0 +1,34 @@
# from starlette.routing import Route
# from halfapi.lib.routes import gen_starlette_routes, gen_router_routes
# 
# def test_gen_starlette_routes():
#  from .dummy_domain import routers
#  for route in gen_starlette_routes({
#  'dummy_domain': routers }):
# 
#  assert isinstance(route, Route)
# 
# import pytest
# 
# @pytest.mark.skip
# def test_api_routes():
#  from . import dummy_domain
#  d_res, d_acls = api_routes(dummy_domain)
#  assert isinstance(d_res, dict)
#  assert isinstance(d_acls, dict)
# 
#  yielded = False
# 
#  for path, verb, m_router, fct, params in gen_router_routes(dummy_domain, []):
#  if not yielded:
#  yielded = True
# 
#  assert path in d_res
#  assert verb in d_res[path]
#  assert 'docs' in d_res[path][verb]
#  assert 'acls' in d_res[path][verb]
#  assert isinstance(d_res[path][verb]['docs'], dict)
#  assert isinstance(d_res[path][verb]['acls'], list)
#  assert len(d_res[path][verb]['acls']) == len(params)
# 
#  assert yielded is True