Skip to content

Commit 8deebe7

Browse files
authored
Merge pull request #39 from kamil-certat/packaging_fast_api
API rewritten to FastAPI
2 parents 26a6ce8 + 962b1c2 commit 8deebe7

35 files changed

+756
-267
lines changed

.github/workflows/code_style.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Check the code style
2+
#
3+
# SPDX-FileCopyrightText: 2022 CERT.at GmbH
4+
# SPDX-License-Identifier: AGPL-3.0-or-later
5+
6+
name: "Check the code style"
7+
8+
on:
9+
push:
10+
paths-ignore:
11+
- ".github/**"
12+
pull_request:
13+
branches: [develop, maintenance]
14+
paths-ignore:
15+
- ".github/**"
16+
17+
jobs:
18+
pycodestyle:
19+
name: Run pycodestyle
20+
runs-on: ubuntu-latest
21+
22+
steps:
23+
- name: Checkout repository
24+
uses: actions/checkout@v2
25+
26+
- name: Set up Python
27+
uses: actions/setup-python@v4
28+
with:
29+
python-version: 3.8
30+
cache: "pip"
31+
cache-dependency-path: setup.py
32+
33+
- name: Install pycodestyle
34+
run: pip install pycodestyle
35+
36+
- name: Run pycodestyle
37+
run: |
38+
pycodestyle intelmq_api tests setup.py

.github/workflows/debian-package.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
name: A build on different Debian distributions
1313
strategy:
1414
matrix:
15-
codename: ['buster', 'bullseye']
15+
codename: ['bullseye'] # Only bullseye contains all dependencies
1616

1717
steps:
1818
- name: Checkout repository

.github/workflows/mypy.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
strategy:
1919
fail-fast: false
2020
matrix:
21-
python-version: [3.6, 3.7, 3.8]
21+
python-version: [3.8]
2222

2323
steps:
2424
- name: Checkout repository
@@ -30,6 +30,7 @@ jobs:
3030
- name: Install dependencies
3131
run: |
3232
python -m pip install --upgrade pip
33+
pip install .
3334
pip install mypy
3435
- name: Static type checking with mypy
3536
run: |

.github/workflows/python-unittests.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
strategy:
1919
fail-fast: false
2020
matrix:
21-
python-version: [3.6, 3.7, 3.8]
21+
python-version: [3.7, 3.8, 3.9, "3.10", 3.11]
2222

2323
steps:
2424
- name: Checkout repository
@@ -27,6 +27,9 @@ jobs:
2727
uses: actions/setup-python@v2
2828
with:
2929
python-version: ${{ matrix.python-version }}
30+
- name: Install dependencies
31+
run: |
32+
pip install .[dev]
3033
- name: Run unittests
3134
run: |
3235
python -m unittest discover -s tests/

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ build
66
dist
77
*.egg-info
88
*.sqlite
9+
10+
.vscode/

CHANGELOG.rst

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,42 @@
1-
..
1+
..
22
SPDX-FileCopyrightText: 2020-2022 Birger Schacht, Sebastian Wagner
33
SPDX-License-Identifier: AGPL-3.0-or-later
44
55
CHANGELOG
66
=========
77

8+
3.1.0 (unreleased)
9+
----------------------
10+
11+
**Removed:**
12+
13+
- Removed support for Python 3.6.
14+
- Removed duplication of the CLI commands. Adding users could be done now only by using `scripts/intelmq-api-adduser`
15+
16+
**Added:**
17+
18+
- Added code style checks to CI.
19+
- Added interactive API documentation available at `/docs` endpoint.
20+
- Added the line length limit in Python files as 100 chars.
21+
- Added the `intelmq-api-setup-systemd` script to help with initial setup.
22+
23+
**Changed:**
24+
25+
- The base API framework was changed from hug to the FastAPI. All endpoints were rewritten to use it
26+
and the structure of the files were changed to better align with the FastAPI concepts.
27+
- All endpoints returns now the proper Content-Type header.
28+
- On authentication error, in the returned JSON dictionary, the key with explanation was changed
29+
from `errors` to `error`. This is consistent with all other error messages.
30+
- The startup point of the application is now `intelmq_api.main:app`
31+
- The API is no longer a WSGI app. The Debian package is updated to run Gunicorn as a server and
32+
configure Apache2 as a proxy. If you had non-default configuration, please review new examples.
33+
34+
**Known issues:**
35+
36+
- The interactive API documentation on `/docs` doesn't respect setting `Authorization` header.
37+
- The OS native package is prepared for Debian 11 (and distribution based on it) only. For other
38+
platforms, installation using `pip` is recommended.
39+
840

941
3.1.0 RC (2022-08-02)
1042
---------------------

README.rst

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,63 @@
1-
..
1+
..
22
SPDX-FileCopyrightText: 2020 Birger Schacht
33
SPDX-License-Identifier: AGPL-3.0-or-later
44
55
###########
66
intelmq-api
77
###########
88

9-
|Build Status|
9+
|Tests Status| |Package Status|
1010

11-
.. |Build Status| image:: https://travis-ci.com/certtools/intelmq-api.svg?branch=develop
12-
:target: https://travis-ci.com/certtools/intelmq-api
11+
.. |Tests Status| image:: https://github.com/certtools/intelmq-api/actions/workflows/python-unittests.yml/badge.svg
12+
:target: https://github.com/certtools/intelmq-api/actions/workflows/python-unittests.yml
1313

14-
intelmq-api is a `hug <http://hug.rest>`_ based API for the `intelmq <https://github.com/certtools/intelmq/>`_ project.
14+
.. |Package Status| image:: https://github.com/certtools/intelmq-api/actions/workflows/debian-package.yml/badge.svg
15+
:target: https://github.com/certtools/intelmq-api/actions/workflows/debian-package.yml
16+
17+
intelmq-api is a `FastAPI <https://fastapi.tiangolo.com/>`_ based API for the `intelmq <https://github.com/certtools/intelmq/>`_ project.
1518

1619

1720
Extensive documentation regarding the installation, configuration and usage of the `intelmq-api` can be found in the `intelmq documentation <https://intelmq.readthedocs.io/en/maintenance/user/intelmq-api.html>`_.
1821

22+
*****************
23+
Development usage
24+
*****************
25+
26+
You could create a preferred virtual environment, and then install the package using:
27+
28+
.. code-block:: bash
29+
30+
pip install -e .
31+
32+
For development purposes, you can run the API using the `scripts/run_dev.sh` script. It serves the
33+
API on the local `8000` port and watches for file changes, with the automated reloading on change.
34+
35+
The interactive documentation is served on the `/docs` endpoint.
36+
37+
To set the API configuration, please export the `INTELMQ_API_CONFIG` environment variable with path
38+
to the JSON config file in the shell you want to use. For the config reference, please check the
39+
`intelmq_api/config.py` and the example from `contrib/api-config.json`.
40+
41+
If you configured the session store, you will need to authorize every request. The `/v1/login`
42+
returns the authorization token. You can use the following command to register the user:
43+
44+
.. code-block:: bash
45+
46+
./scripts/intelmq-api-adduser --user UserName
47+
48+
*************
49+
Security note
50+
*************
51+
52+
Please be careful when deploying the API. At the current stage, it is not designed to run on
53+
publicly exposed endpoints without additional pre-cautions.
1954

2055
*************
2156
Type checking
2257
*************
2358

24-
Except for the parts that directly deal with ``hug``, the code can be
25-
typechecked with ``mypy``. To run the type checker, start with the module
26-
``serve``:
59+
The code can be typechecked with ``mypy``. To run the type checker, use:
2760

2861
.. code-block:: bash
2962
30-
mypy intelmq_manager/serve.py
63+
mypy intelmq_api/

contrib/api-apache.conf

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
1-
# SPDX-FileCopyrightText: 2020 Birger Schacht
2-
#
1+
# SPDX-FileCopyrightText: 2022 CERT.at GmbH <https://cert.at/>
32
# SPDX-License-Identifier: CC0-1.0
4-
5-
#Override the default configuration file path using the
6-
#INTELMQ_API_CONFIG environment variable
7-
#SetEnv INTELMQ_API_CONFIG /etc/intelmq/api-config.json
8-
<IfModule mod_wsgi.c>
9-
WSGIApplicationGroup %{GLOBAL}
10-
WSGIPassAuthorization On
11-
WSGIScriptAlias /intelmq /usr/lib/python3/dist-packages/intelmq_api/intelmq-api.wsgi
12-
</IfModule>
133

14-
<Directory /usr/lib/python3/dist-packages/intelmq_api/>
15-
Require all granted
16-
</Directory>
4+
5+
# If you want to change default location, please align the ROOT_PATH in the service configuration
6+
<Location /intelmq/>
7+
ProxyPass unix:/usr/lib/python3/dist-packages/intelmq_api/intelmq_api.sock|http://127.0.0.1/
8+
ProxyPassReverse unix:/usr/lib/python3/dist-packages/intelmq_api/intelmq_api.sock|http://127.0.0.1/
9+
</Location>

contrib/intelmq-api.service

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# SPDX-FileCopyrightText: 2022 CERT.at GmbH <https://cert.at/>
2+
# SPDX-License-Identifier: CC0-1.0
3+
4+
[Unit]
5+
Description=Gunicorn deamon to serve the IntelMQ API
6+
Requires=intelmq-api.socket
7+
After=network.target
8+
9+
[Service]
10+
11+
# To override settings path, use e.g.:
12+
# Environment="INTELMQ_API_CONFIG=/etc/intelmq/api-config.json"
13+
14+
Environment="ROOT_PATH=/intelmq"
15+
User=www-data
16+
Group=www-data
17+
RuntimeDirectory=gunicorn
18+
WorkingDirectory=/usr/lib/python3/dist-packages/intelmq_api/
19+
ExecStart=/usr/bin/gunicorn intelmq_api.main:app --workers 4 --worker-class uvicorn.workers.UvicornWorker --bind unix:intelmq_api.sock
20+
ExecReload=/bin/kill -s HUP $MAINPID
21+
KillMode=mixed
22+
TimeoutStopSec=5
23+
PrivateTmp=true
24+
25+
[Install]
26+
WantedBy=multi-user.target

contrib/intelmq-api.socket

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# SPDX-FileCopyrightText: 2022 CERT.at GmbH <https://cert.at/>
2+
# SPDX-License-Identifier: CC0-1.0
3+
4+
[Unit]
5+
Description=The socket to handle IntelMQ API requests
6+
7+
[Socket]
8+
ListenStream=/usr/lib/python3/dist-packages/intelmq_api/intelmq_api.sock
9+
SocketUser=www-data
10+
11+
[Install]
12+
WantedBy=sockets.target

0 commit comments

Comments
 (0)