60 Commits

Author SHA1 Message Date
Jan Čermák
9f3767b23d
Return cursor of the first host logs entry via headers (#5333)
* Return cursor of the first host logs entry via headers

Return first entry's cursor via custom `X-First-Cursor` header that can
be consumed by the client and used for continual requesting of the
historic logs. Once the first fetch returns data, the cursor can be
supplied as the first argument to the Range header in another call,
fetching accurate slice of the journal with the previous log entries
using the `Range: entries=cursor[[:num_skip]:num_entries]` syntax.

Let's say we fetch logs with the Range header `entries=:-19:20` (to
fetch very last 20 lines of the logs, see below why not
`entries:-20:20`) and we get `cursor50` as the reply (the actual value
will be much more complex and with no guaranteed format). To fetch
previous slice of the logs, we use `entries=cursor50:-20:20`, which
would return 20 lines previous to `cursor50` and `cursor30` in the
cursor header. This way we can go all the way back to the history.

One problem with the cursor is that it's not possible to determine when
the negative num_skip points beyond the first log entry. In that case
the client either needs to know what the first entry is (via
`entries=:0:1`) or can iterate naively and stop once two subsequent
requests return the same first cursor.

Another caveat, even though it's unlikely it will be hit in real usage,
is that it's not possible to fetch the last line only - if no cursor is
provided, negative num_skip argument is needed, and in that case we're
pointing one record back from the current cursor, which is the previous
record. The least we can return without knowing any cursor is thus
`entries=👎2` (where the `2` can be omitted, however with
`entries=👎1` we would lose the last line). This also explains why
different `num_skip` and `num_entries` must be used for the first fetch.

* Fix typo (fallback->callback)

* Refactor journal_logs_reader to always return the cursor

* Update tests for new cursor handling
2024-10-09 20:27:29 +02:00
Stefan Agner
f6faa18409
Bump pre-commit ruff to 0.5.7 and reformat (#5242)
It seems that the codebase is not formatted with the latest ruff
version. This PR reformats the codebase with ruff 0.5.7.
2024-08-13 20:53:56 +02:00
Jan Čermák
0814552b2a
Use Journal Export Format for host (advanced) logs (#4963)
* Use Journal Export Format for host (advanced) logs

Add methods for handling Journal Export Format and use it for fetching
of host logs. This is foundation for colored streaming logs for other
endpoints as well.

* Make pylint happier - remove extra pass statement

* Rewrite journal gateway tests to mock ClientResponse's StreamReader

* Handle connection refused error when connecting to journal-gatewayd

* Use SYSTEMD_JOURNAL_GATEWAYD_SOCKET global path also for connection

* Use parsing algorithm suggested by @agners in review

* Fix timestamps in formatting, always use UTC for now

* Add tests for Accept header in host logs

* Apply suggestions from @agners

Co-authored-by: Stefan Agner <stefan@agner.ch>

* Bail out of parsing earlier if field is not in required fields

* Fix parsing issue discovered in the wild and add test case

* Make verbose formatter more tolerant

* Use some bytes' native functions for some minor optimizations

* Move MalformedBinaryEntryError to exceptions module, add test for it

---------

Co-authored-by: Stefan Agner <stefan@agner.ch>
2024-03-20 09:00:45 +01:00
J. Nick Koston
eb85be2770
Improve json performance by porting core orjson utils (#4816)
* Improve json performance by porting core orjson utils

* port relevant tests

* pylint

* add test for read_json_file

* add test for read_json_file

* remove workaround for core issue we do not have here

---------

Co-authored-by: Pascal Vizeli <pvizeli@syshack.ch>
2024-01-13 19:19:01 +01:00
Stefan Agner
883e54f989
Make check_port an async function (#4677)
* Make check_port asyncio

This requires to change the ingress_port property to a async method.

* Avoid using wait_for

* Add missing async

* Really await

* Set dynamic ingress port on add-on installation/update

* Fix pytest issue

* Rename async_check_port back to check_port

* Raise RuntimeError in case port is not set

* Make sure port gets set on add-on restore

* Drop unnecessary async

* Simplify check_port by using asyncio.get_running_loop()
2023-12-05 15:49:35 -05:00
Stefan Agner
9088810b49
Improve D-Bus error handling for NetworkManager (#4720)
* Improve D-Bus error handling for NetworkManager

There are quite some errors captured which are related by seemingly a
suddenly missing NetworkManager. Errors appear as:
23-11-21 17:42:50 ERROR (MainThread) [supervisor.dbus.network] Error while processing /org/freedesktop/NetworkManager/Devices/10: Remote peer disconnected
...
23-11-21 17:42:50 ERROR (MainThread) [supervisor.dbus.network] Error while processing /org/freedesktop/NetworkManager/Devices/35: The name is not activatable

Both errors seem to already happen at introspection time, however
the current code doesn't converts these errors to Supervisor issues.
This PR uses the already existing `DBus.from_dbus_error()`.

Furthermore this adds a new Exception `DBusNoReplyError` for the
`ErrorType.NO_REPLY` (or `org.freedesktop.DBus.Error.NoReply` in
D-Bus terms, which is the type of the first of the two issues above).

And finally it separates the `ErrorType.SERVICE_UNKNOWN` (or
`org.freedesktop.DBus.Error.ServiceUnknown` in D-Bus terms, which is
the second of the above issue) from `DBusInterfaceError` into a new
`DBusServiceUnkownError`.

This allows to handle errors more specifically.

To avoid too much churn, all instances where `DBusInterfaceError`
got handled, we are now also handling `DBusServiceUnkownError`.

The `DBusNoReplyError` and `DBusServiceUnkownError` appear when
the NetworkManager service stops or crashes. Instead of retrying
every interface we know, just give up if one of these issues appear.
This should significantly lower error messages users are seeing
and Sentry events.

* Remove unnecessary statement

* Fix pytests

* Make sure error strings are compared correctly

* Fix typo/remove unnecessary pylint exception

* Fix DBusError typing

* Add pytest for from_dbus_error

* Revert "Make sure error strings are compared correctly"

This reverts commit 10dc2e4c3887532921414b4291fe3987186db408.

* Add test cases

---------

Co-authored-by: Mike Degatano <michael.degatano@gmail.com>
2023-11-27 23:32:11 +01:00
J. Nick Koston
5c579e557c
Port core async safe logging to supervisor (#4716)
fixes #4715
2023-11-20 20:33:36 +01:00
Stefan Agner
928aff342f
Address pytest warnings (#4695) 2023-11-15 10:45:36 +01:00
Pascal Vizeli
de1f3555b1
Disable codenotary until rework of cosign (#4217)
* Disable codenotary until rework of cosign

* Skip code notary tests

---------

Co-authored-by: Mike Degatano <michael.degatano@gmail.com>
2023-03-31 11:11:29 +02:00
Mike Degatano
c0b75edfb7
Format data disk in Supervisor instead of OS Agent (#4212)
* Supervisor formats data disk instead of os agent

* Fix issues occurring during tests

* Can't migrate if target is too small
2023-03-30 14:15:07 -04:00
Mike Degatano
a6caccd845
Use session dbus mocks for all tests (#4198)
* Use session dbus mocks for all tests

* func instead of fn for pylint
2023-03-21 07:30:31 +01:00
Mike Degatano
417ee418f2
Handle UnicodeDecodeError (#4110) 2023-01-21 17:59:55 +01:00
Mike Degatano
0f79ba5a3d
AppArmor tests and fix profile name in flag (#4109) 2023-01-20 23:19:33 +01:00
Pascal Vizeli
2809f23391
Make dbus-fast calls more robust (#4005)
* Make dbus-fast calls more robust

* Handle all exceptions and add test

* DBus minimal can't return commands list

Co-authored-by: Mike Degatano <michael.degatano@gmail.com>
2022-11-15 22:19:38 +01:00
Mike Degatano
bde5c938a7
Ignore veth changes (#3955)
* Reduce log noise for unmanaged interfaces

* Ignore signals with veth changes only

* Fix test and add one
2022-10-16 11:06:35 +02:00
Mike Degatano
48e666e1fc
Bad introspection causes DBus error not KeyError (#3898)
* Bad introspection causes DBus error not KeyError

* Fix none error on disconnect race
2022-09-26 18:27:08 +02:00
Mike Degatano
c24b811180
Use dbus-fast unpack_variants option (#3885)
* Use dbus-fast unpack_variants option

* More readable log on signals
2022-09-21 20:52:06 +02:00
Mike Degatano
0958cd0c06
Switch from dbus-next to dbus-fast (#3882)
* Switch to dbus-fast

* dbus-fast==1.6.0
2022-09-21 09:24:04 +02:00
Mike Degatano
2072370ccc
Revert "Switch to dbus-fast (#3877)" (#3878)
This reverts commit 615758a1dfde1f91ff1dc7e3e4ecc826a0ed2b58.
2022-09-19 17:18:25 -04:00
Mike Degatano
615758a1df
Switch to dbus-fast (#3877) 2022-09-19 21:55:22 +02:00
Mike Degatano
d195f19fa8
Refactor to dbus-next proxy interfaces (#3862)
* Refactor to dbus-next proxy interfaces

* Fix tests mocking dbus methods

* Fix call dbus
2022-09-13 13:45:28 -04:00
Mike Degatano
b71c6c60da
Share MessageBus across dbus objects (#3860)
* Share MessageBus across dbus objects

* Fix connect calls in tests
2022-09-12 20:10:12 +02:00
Pascal Vizeli
e445a8aabf
Enable CAS for Add-ons (#3506)
* Revert "Disable CAS until it work (#3504)"

This reverts commit 435241bccf3f3c678161b50cfefd4b036f1daba9.

* Revert exception that are not forwarded

* enable for add-ons

* Apply suggestions from code review

Co-authored-by: Mike Degatano <michael.degatano@gmail.com>

* fix black

Co-authored-by: Mike Degatano <michael.degatano@gmail.com>
2022-03-31 10:59:34 -04:00
Pascal Vizeli
435241bccf
Disable CAS until it work (#3504)
* Disable CAS until it work

* fix tests
2022-03-10 13:18:52 +01:00
Mike Degatano
c931a4c3e5
Error response handling & tests (#3473) 2022-03-01 21:52:50 +01:00
Pascal Vizeli
9104b287e5
New Backup format for core (#3451)
* New Backup format for core

* cleanup

* next round

* partial resotre encrypted function of metadata

* Using securetar as modul

* fix securetar imports

* simplify

* more typing

* adjust handling

* Handling replace better

* increase the order

* more logic cleanup

* create new core backup

* handle restore

* fix tests

* better checks

* Fix attribute issues

* Update supervisor/backups/manager.py

Co-authored-by: Joakim Sørensen <joasoe@gmail.com>

* Address comments

* Fix tests

* Update supervisor/exceptions.py

Co-authored-by: Joakim Sørensen <joasoe@gmail.com>

Co-authored-by: Joakim Sørensen <joasoe@gmail.com>
2022-02-23 16:08:02 +01:00
Pascal Vizeli
597a27ba33
Fix source-mod check (#3439)
* Fix source-mod check

* Fix stderr

* make handling robust
2022-02-10 13:23:05 +01:00
Stefan Agner
7a6663ba80
Use Python dbus-next D-Bus library (#3234)
* Use the correct interface name to get properties of systemd

It seems that gdbus (or systemd) automatically pick the correct
interface and return the properties. However, dbussy requires the
correct interface name to get all properties.

* Don't expect array from Strength property

The property returns a type "y" which equates to "guchar":
https://developer-old.gnome.org/NetworkManager/stable/gdbus-org.freedesktop.NetworkManager.AccessPoint.html#gdbus-property-org-freedesktop-NetworkManager-AccessPoint.Strength

It seems that the old D-Bus implementation returned an array. With
dbus-next a integer is returned, so no list indexing required.

* Support signals and remove no longer used tests and code

* Pass rauc update file path as string

That is what the interface is expecting, otherwise the new lib chocks on
the Pathlib type.

* Support Network configuration with dbus-next

Assemble Python native objects and pass them to dbus-next. Use dbus-next
specific Variant class where necessary.

* Use org.freedesktop.NetworkManager.Connection.Active.StateChanged

org.freedesktop.NetworkManager.Connection.Active.PropertyChanged is
depricated. Also it seems that StateChanged leads to fewer and more
accurate signals.

* Pass correct data type to RequestScan.

RequestScan expects an option dictionary. Pass an empty option
dictionary to it.

* Update unit tests

Replace gdbus specific fixtures with json files representing the return
values. Those can be easily converted into native Python objects.

* Rename D-Bus utils module gdbus to dbus
2021-10-18 23:06:44 +02:00
Pascal Vizeli
f93f5d0e71
Add CodeNotary / for version file (#2731)
* Add CodeNotary / for version file

* Apply suggestions from code review

Co-authored-by: Joakim Sørensen <joasoe@gmail.com>

* Address comment

Co-authored-by: Joakim Sørensen <joasoe@gmail.com>
2021-03-19 08:40:35 +01:00
Pascal Vizeli
09203f67b2
Fix defaults for builds (#2675) 2021-03-04 11:48:25 +01:00
Joakim Sørensen
bee55d08fb
Create FileConfiguration baseclass (#2651) 2021-03-01 12:26:43 +01:00
Joakim Sørensen
3760967f59
Allow use YAML for addon and repository config (#2645)
* Allow use YAML for addon and repository config

* pylint
2021-02-28 20:00:02 +01:00
Pascal Vizeli
f6bf44de1c
Add tests for remove function (#2309) 2020-11-29 12:34:33 +01:00
Pascal Vizeli
c4847ad10d
Handle dbus single byte (#2237) 2020-11-11 13:09:13 +01:00
Pascal Vizeli
35b626a1c5
Fix gvariant bytes array (#2162) 2020-10-22 15:56:32 +02:00
Joakim Sørensen
17559bfc8e
Extract exception message from chain for API errors (#2100)
* Get message from excepiton chain

* cleanup
2020-10-06 15:53:58 +02:00
Joakim Sørensen
9dc2f43ffb
Handle both cases of bind failures (#2099) 2020-10-06 11:28:09 +02:00
Joakim Sørensen
f35b6d0b00
Set permissions on JSON files (#2093)
* Set 600 premissions on json files

* Add test

* Fix local tar tests

* Fix tar test in action

* Use pytest fixture for tmp_path in tests

* remove not needed things
2020-10-05 15:14:09 +02:00
Pascal Vizeli
0f54824cdb add one test more 2020-09-17 10:32:41 +00:00
Pascal Vizeli
15a8f40f6f add more test 2020-09-17 10:07:05 +00:00
Pascal Vizeli
78f63380f2
Handle exception chain on addon boot (#2063)
* Handle exception chain on addon boot

* fix import

* Add tests
2020-09-17 12:01:48 +02:00
Joakim Sørensen
c9db42583b
Capture typeerror exception (#2052) 2020-09-14 13:40:11 +02:00
Pascal Vizeli
7be508214a
gvariant: parse byte string (#2041)
* Parse byte string

* fix lint
2020-09-11 18:36:19 +02:00
Pascal Vizeli
2321890dde
Full support of bytes arrays on gvariant (#2024)
* Full support of bytes arrays on gvariant

* Fix overlay ssid

* cleanup ssid

* fix test
2020-09-07 16:34:20 +02:00
Joakim Sørensen
f32d17d924
Format API error messages (#1997) 2020-08-30 17:53:10 +02:00
Pascal Vizeli
a2821a98ad
Fix gvar for dbus if binary is not a string (#1984) 2020-08-27 17:04:14 +02:00
Pascal Vizeli
6db4c60f47
Fix byte parser (#1981) 2020-08-27 14:31:34 +02:00
Pascal Vizeli
1a3b369dd7
Update black 20.8b1 (#1976) 2020-08-26 23:01:29 +02:00
Pascal Vizeli
bb64dca6e6
Fix symlink snapshot (#1833)
* Fix symbolic link issue with snapshot

* Add tests

* fix symlink

* add encrypted test

* Modify x
2020-07-14 15:39:03 +02:00
Franck Nijhof
9eab92513a
Add QA/CI to Supervisor (#1814) 2020-07-05 20:20:29 +02:00