Skip to content

Commit ded0adf

Browse files
authored
Merge pull request #2292 from kamil-certat/docs/fast_api
DOC: Update API docs to work with rewritten API
2 parents c76bf67 + 4e753f7 commit ded0adf

File tree

2 files changed

+142
-23
lines changed

2 files changed

+142
-23
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ CHANGELOG
5959
- `intelmq.bots.outputs.smtp_batch.output`: Added a bot to gathering the events and sending them by e-mails at a stroke as CSV files (PR#2253 by Edvard Rejthar)
6060

6161
### Documentation
62+
- API: update API installation to be aligned with the rewritten API, and clarify some missing steps.
6263

6364
### Tests
6465
- New decorator `skip_installation` and environment variable `INTELMQ_TEST_INSTALLATION` to skip tests requiring an IntelMQ installation on the test host by default (PR#2370 by Sebastian Wagner, fixes #2369)

docs/user/intelmq-api.rst

Lines changed: 141 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
IntelMQ API
77
###########
88

9-
`intelmq-api` is a `hug <http://hug.rest>`_ based API for the `IntelMQ <https://github.com/certtools/intelmq/>`_ project.
9+
`intelmq-api` is a `FastAPI <https://fastapi.tiangolo.com/>`_ based API for the `IntelMQ <https://github.com/certtools/intelmq/>`_ project.
1010

1111
.. contents::
1212

@@ -23,51 +23,140 @@ For the list of supported distributions, please see the intelmq :doc:`installati
2323
Our repository page gives `installation instructions for various operating systems <https://software.opensuse.org/download.html?project=home:sebix:intelmq&package=intelmq-api>`_.
2424
No additional set-up steps are needed if you use these packages.
2525

26-
The `intelmq-api` provides the route ``/api`` for managing the IntelMQ installation.
26+
The `intelmq-api` provides the route ``/v1/api`` for managing the IntelMQ installation.
2727

28-
For development purposes and testing you can also run `intelmq-api` directly using ``hug``:
28+
For development purposes and testing, you can also run `intelmq-api` using development script
29+
from this `repository <https://github.com/certtools/intelmq-api>`_:
2930

3031
.. code-block:: bash
3132
32-
hug -m intelmq_api.serve
33+
./scripts/run_dev.sh
34+
35+
The API is then served on ``127.0.0.1:8000/v1/api``, and the interactive documentation on ``127.0.0.1:8000/docs``.
36+
Please refer to the repository README for more development tips.
3337

3438
Installation using pip
3539
^^^^^^^^^^^^^^^^^^^^^^
3640

37-
The `intelmq-api` packages ship a configuration file in ``${PREFIX}/etc/intelmq/api-config.json``, a positions configuration for the manager in ``{PREFIX}/etc/intelmq/manager/positions.conf``, a virtualhost configuration file for Apache 2 in ``${PREFIX}/etc/intelmq/api-apache.conf`` and a sudoers configuration file in ``${PREFIX}/etc/intelmq/api-sudoers.conf``.
41+
.. note::
42+
43+
Native system packages (DEB, RPM) should automatically prepare steps described in this section. If
44+
you wish to base on package from pip, you may need to do them manually, as described below.
45+
46+
To configure your system to serve the API for not-development purposes, you need to prepare a
47+
configuration for IntelMQ API, a position config for the IntelMQ Manager as well as a web application
48+
server and a (reverse)proxy. For all those steps we have prepared example configuration, intended
49+
to work with `Gunicorn <https://gunicorn.org/>`_ as the web server and `Apache 2 <https://httpd.apache.org/>`_
50+
as the proxy.
51+
52+
The `intelmq-api` package ships the following example files:
53+
- ``${PREFIX}/etc/intelmq/api-config.json`` - the API configuration,
54+
- ``${PREFIX}/etc/intelmq/manager/positions.conf`` - positions configuration for the manager,
55+
- ``${PREFIX}/etc/intelmq/api-apache.conf`` - a virtualhost configuration file for Apache 2,
56+
- ``${PREFIX}/etc/intelmq/api-sudoers.conf`` - a sudoers configuration file,
57+
- ``${PREFIX}/etc/intelmq/intelmq-api.service`` - a systemd service unit configuration for Gunicorn,
58+
- ``${PREFIX}/etc/intelmq/intelmq-api.socket`` - a systemd socket unit configuration.
59+
3860
The value of ``${PREFIX}`` depends on your environment and is something like ``/usr/local/lib/pythonX.Y/dist-packages/`` (where ``X.Y`` is your Python version).
3961

62+
.. note::
63+
64+
All configuration files have example paths to the IntelMQ API package. During the installation
65+
please ensure to update them with the right value, as the ``${PREFIX}``.
66+
67+
Installing packages
68+
~~~~~~~~~~~~~~~~~~~
69+
70+
Let's start with installing the IntelMQ API package:
71+
72+
.. code-block:: bash
73+
74+
pip install intelmq-api
75+
76+
You need to install Gunicorn and Apache2 on your own, e.g., using apt:
77+
78+
.. code-block:: bash
79+
80+
apt install gunicorn apache2
81+
82+
Then, if you didn't use it before, ensure to enable the ``proxy_http`` module for Apache:
83+
84+
.. code-block:: bash
85+
86+
a2enmod proxy_http
87+
88+
Configuring Apache
89+
~~~~~~~~~~~~~~~~~~
90+
4091
The file ``${PREFIX}/etc/intelmq/api-apache.conf`` needs to be placed in the correct place for your Apache 2 installation.
4192
- On Debian and Ubuntu, move the file to ``/etc/apache2/conf-available.d/api-apache.conf`` and then execute ``a2enconf api-apache``.
4293
- On CentOS, RHEL and Fedora, move the file to ``/etc/httpd/conf.d/``.
4394
- On openSUSE, move the file to ``/etc/apache2/conf.d/``.
44-
Don't forget to reload your webserver afterwards.
95+
96+
Don't forget to reload the Apache2 afterwards.
97+
98+
Configuring Systemd services
99+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
100+
101+
.. note::
102+
103+
This step could be also done by calling the script:
104+
105+
.. code-block:: bash
106+
107+
intelmq-api-setup-systemd
108+
109+
The systemd configuration files (``intelmq-api.service`` and ``intelmq-api.socket``) are responsible
110+
for instructing systemd daemon to start and keep running Gunicorn (that serves the API), and
111+
forwarding requests between proxy and the Gunicorn instance.
112+
113+
- Files ``${PREFIX}/etc/intelmq/intelmq-api.service`` and ``${PREFIX}/etc/intelmq/intelmq-api.socket``
114+
should be placed in ``/lib/systemd/system/`` directory. Then adapt the webserver username in
115+
``intelmq-api.service``.
116+
117+
After moving files, you can enable the service by executing ``systemctl enable intelmq-api`` to
118+
start it on the system startup.
119+
120+
Setup API configuration files
121+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
45122

46123
- The file ``${PREFIX}/etc/intelmq/api-config.json`` needs to be moved to ``/etc/intelmq/api-config.json``.
47124
- The file ``${PREFIX}/etc/intelmq/manager/positions.conf`` needs to be moved to ``/etc/intelmq/manager/positions.conf``.
48-
- Last but not least move the file ``${PREFIX}/etc/intelmq/api-sudoers.conf`` to ``/etc/sudoers.d/01_intelmq-api`` and adapt the webserver user name in this file. Set the file permissions to ``0o440``.
125+
- Last but not least move the file ``${PREFIX}/etc/intelmq/api-sudoers.conf`` to ``/etc/sudoers.d/01_intelmq-api`` and adapt the webserver username in this file. Set the file permissions to ``0o440``.
126+
127+
Afterwards, continue with the section Permissions below. When you finish the configuration,
128+
you can start the service using ``systemctl start intelmq-api``. You may need to restart the service
129+
after any configuration change.
49130

50-
Afterwards continue with the section Permissions below.
131+
Next steps
132+
~~~~~~~~~~
51133

52-
IntelMQ 2.3.1 comes with a tool ``intelmqsetup`` which performs these set-up steps automatically.
53-
Please note that the tool is very new and may not detect all situations correctly. Please report us any bugs you are observing.
54-
The tools is idempotent, you can execute it multiple times.
134+
The example Apache2 and Gunicorn configurations serve the IntelMQ API under ``/intelmq`` prefix,
135+
what means that at this moment you should be able to get, e.g., the API documentation under
136+
``/intelmq/docs`` etc.
137+
138+
Now, you should continue with the API configuration and creating users. If you didn't do it before,
139+
it's also time to configure IntelMQ itself.
140+
141+
IntelMQ 2.3.1 comes with a tool ``intelmqsetup`` which helps with performing some steps automatically.
142+
Please note that the tool is still under development and may not detect all situations correctly.
143+
Please report us any bugs you are observing. The tool is idempotent, you can execute it multiple times.
55144

56145
***********************
57146
Configuring intelmq-api
58147
***********************
59148

60-
Depending on your setup you might have to install ``sudo`` to make it possible for the ``intelmq-api`` to run the ``intelmq`` command as the user-account usually used to run ``intelmq`` (which is also often called ``intelmq``).
149+
Depending on your setup, you might have to install ``sudo`` to make it possible for the ``intelmq-api`` to run the ``intelmq`` command as the user-account usually used to run ``intelmq`` (which is also often called ``intelmq``).
61150

62151
``intelmq-api`` is configured using a configuration file in ``json`` format.
63152
``intelmq-api`` tries to load the configuration file from ``/etc/intelmq/api-config.json`` and ``${PREFIX}/etc/intelmq/api-config.json``, but you can override the path setting the environment variable ``INTELMQ_API_CONFIG``.
64-
(When using Apache, you can do this by modifying the Apache configuration file shipped with ``intelmq-api``, the file contains an example)
153+
(When using Gunicorn and systemd service, you can do this by modifying the ``intelmq-api.service`` configuration file shipped with ``intelmq-api``, the file contains an example)
65154

66-
When running the API using ``hug``, you can set the environment variable like this:
155+
When running the API using development mode, you can set the environment variable like this:
67156

68157
.. code-block:: bash
69158
70-
INTELMQ_API_CONFIG=/etc/intelmq/api-config.json hug -m intelmq_api.serve
159+
INTELMQ_API_CONFIG=/etc/intelmq/api-config.json ./scripts/run_dev.sh
71160
72161
73162
The default configuration which is shipped with the packages is also listed here for reference:
@@ -83,7 +172,7 @@ The default configuration which is shipped with the packages is also listed here
83172
}
84173
85174
86-
On Debian based systems, the default path for the ``session_store`` is ``/var/lib/dbconfig-common/sqlite3/intelmq-api/intelmqapi``, because the Debian package uses the Debian packaging tools to manage the database file.
175+
On Debian based systems, the default path for the ``session_store`` is ``/var/lib/dbconfig-common/sqlite3/intelmq-api/intelmqapi`` because the Debian package uses the Debian packaging tools to manage the database file.
87176

88177
The following configuration options are available:
89178

@@ -92,14 +181,14 @@ The following configuration options are available:
92181
This means that if the command you want to use needs parameters, they have to be separate strings.
93182
* ``allowed_path``: intelmq-api can grant **read-only** access to specific files - this setting defines the path those files can reside in.
94183
* ``session_store``: this is an optional path to a sqlite database, which is used for session storage and authentication. If it is not set (which is the default), no authentication is used!
95-
* ``session_duration``: the maximal duration of a session, its 86400 seconds by default
184+
* ``session_duration``: the maximal duration of a session, it's 86400 seconds by default
96185
* ``allow_origins``: a list of origins the responses of the API can be shared with. Allows every origin by default.
97186

98187
Permissions
99188
^^^^^^^^^^^
100189

101190
``intelmq-api`` tries to write a couple of configuration files in the ``${PREFIX}/etc/intelmq`` directory - this is only possible if you set the permissions accordingly, given that ``intelmq-api`` runs under a different user.
102-
The user the API run as also needs write access to the folder the ``session_store`` is located in, otherwise there will be an error accessing the session data.
191+
The user the API run as also needs write access to the folder the ``session_store`` is located in; otherwise there will be an error accessing the session data.
103192
If you're using the default Apache 2 setup, you might want to set the group of the files to ``www-data`` and give it write permissions (``chmod -R g+w <directoryname>``).
104193
In addition to that, the ``intelmq-manager`` tries to store the bot positions via the API into the file ``${PREFIX}/etc/intelmq/manager/positions.conf``.
105194
You should therefore create the folder ``${PREFIX}/etc/intelmq/manager`` and the file ``positions.conf`` in it.
@@ -138,7 +227,7 @@ To do so, first send a POST-Request with JSON-formatted data to http://localhost
138227
139228
{
140229
"username": "$your_username",
141-
"password: "$your_password"
230+
"password": "$your_password"
142231
}
143232
144233
With valid credentials, the JSON-formatted response contains the ``login_token``.
@@ -188,12 +277,12 @@ Access Denied / Authentication Required "Please provide valid Token verification
188277

189278
If you see the IntelMQ Manager interface and menu, but the API calls to the back-end querying configuration and status of IntelMQ fail with "Access Denied" or "Authentication Required: Please provide valid Token verification credentials" errors, you are maybe not logged in while the API requires authentication.
190279

191-
By default, the API requires authentication. Create user accounts and login with them or - if you have other protection means in place - deactivate the authentication requirement by removing or renaming the `session_store` parameter in the configuration.
280+
By default, the API requires authentication. Create user accounts and login with them, or - if you have other protection means in place - deactivate the authentication requirement by removing or renaming the `session_store` parameter in the configuration.
192281

193282
Internal Server Error
194283
^^^^^^^^^^^^^^^^^^^^^
195284

196-
There can be various reasons for internal server errors. You need to look at the error log of your web server, for example ``/var/log/apache2/error.log`` or ``/var/log/httpd/error_log`` for Apache 2. It could be that the sudo-setup is not functional, the configuration file or session database file can not be read or written or other errors in regards to the execution of the API program.
285+
There can be various reasons for internal server errors. You need to look at the error log of your web server, for example ``/var/log/apache2/error.log`` or ``/var/log/httpd/error_log`` for Apache 2. It could be that the sudo-setup is not functional, the configuration file or session database file can not be read or written or other errors in regard to the execution of the API program.
197286

198287
Can I just install it from the deb/rpm packages while installing IntelMQ from a different source?
199288
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -202,15 +291,44 @@ Yes, you can install the API and the Manager from the deb/rpm repositories, and
202291
However, knowledge about Python and system administration experience is recommended if you do so.
203292

204293
The packages install IntelMQ to ``/usr/lib/python3*/site-packages/intelmq/``.
205-
Installing with ``pip`` results in ``/usr/local/lib/python3*/site-packages/intelmq/`` (and some other accompaning resources) which overrides the installation in ``/usr/lib/``.
294+
Installing with ``pip`` results in ``/usr/local/lib/python3*/site-packages/intelmq/`` (and some other accompanying resources) which overrides the installation in ``/usr/lib/``.
206295
You probably need to adapt the configuration parameter ``intelmq_ctl_cmd`` to the ``/usr/local/bin/intelmqctl`` executable and some other tweaks.
207296

208297
sqlite3.OperationalError: attempt to write a readonly database
209298
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
210299

211-
SQLite does not only need write access to the database itself, but also the folder the database file is located in. Please check that the webserver has write permissions to the folder
300+
SQLite does not only need write access to the database itself, but also the folder the database file is located in. Please check that the webserver has `write` permissions to the folder
212301
the session file is located in.
213302

303+
sqlite3.OperationalError: unable to open database file
304+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
305+
306+
Please check the ``session_store`` in ``api-config.json`` and ensure the path is correct - the
307+
directory exists and application can write to it.
308+
309+
Gunicorn returns ``ModuleNotFoundError: No module named 'uvicorn'``, but Uvicorn is installed
310+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
311+
312+
Most probably one of them (Gunicorn and Uvicorn) were installed using different method (e.g. one
313+
from native system package, other from pip). Try to install both from one source. You may need
314+
to eventually update the Gunicorn executable path in `intelmq-api.service`.
315+
316+
Can I use other web servers or proxy?
317+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
318+
319+
Yes, the proposed setup with Gunicorn and Apache 2 is just one of many possibilities. You can
320+
refer to the `FastAPI documentation <https://fastapi.tiangolo.com/deployment/>`_ for another
321+
examples.
322+
323+
How to debug API running as system service?
324+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
325+
326+
If you experience any issues with the API, please first check the logs provided in journal:
327+
328+
.. code-block:: bash
329+
330+
journalctl -u intelmq-api
331+
214332
215333
************
216334
Getting help

0 commit comments

Comments
 (0)