hostnamed: expose parsed TAGS in Describe

The DBus API already exposes the parsed machine-tags list via the Tags
property but varlink clients needed to parse the raw TAGS= line from
MachineInformationData (which also required implementing tag name
validation if the tags would later be used with SetTags, to avoid
spurious errors).

So, provide the same information that DBus gets (a list of pre-parsed
tags with bad tags gracefully removed) as a new MachineTags field
returned by Describe -- this does mean that the DBus API now exposes the
same data in two ways but making Describe() consistent is more important
and mirrors the OperatingSystem* class of properties.
This commit is contained in:
Aleksa Sarai
2026-08-23 14:10:22 +01:00
committed by Luca Boccassi
parent ded83144eb
commit 44cc69769d
4 changed files with 34 additions and 0 deletions
+7
View File
@@ -77,6 +77,13 @@ CHANGES WITH 262:
should be provisioned as far as possible without ever blocking on a
prompt.
Changes in systemd-hostnamed:
* The Describe() method (for both D-Bus and Varlink) now exposes the
parsed machine tag list as a new MachineTags field, so that Varlink
clients no longer need to parse the raw TAGS= line out of
MachineInformationData themselves.
Changes in systemd-networkd:
* A new IPv4ProxyARPAddress= setting has been added to the [Network]
+7
View File
@@ -2013,6 +2013,12 @@ static int build_describe_response(Context *c, bool privileged, sd_json_variant
(void) load_os_release_pairs(/* root= */ NULL, &os_release_pairs);
(void) load_env_file_pairs(/* f= */ NULL, etc_machine_info(), &machine_info_pairs);
/* Silently drop any invalid tags that might have been written into the file by hand */
_cleanup_strv_free_ char **tags = NULL;
r = machine_tags_from_string(c->data[PROP_TAGS], /* graceful= */ true, &tags);
if (r < 0)
log_warning_errno(r, "Failed to parse machine tags '%s', ignoring: %m", strnull(c->data[PROP_TAGS]));
r = sd_json_buildo(
&v,
SD_JSON_BUILD_PAIR_STRING("Hostname", hn ?: dhn),
@@ -2037,6 +2043,7 @@ static int build_describe_response(Context *c, bool privileged, sd_json_variant
SD_JSON_BUILD_PAIR_STRING("OperatingSystemImageID", c->data[PROP_OS_IMAGE_ID]),
SD_JSON_BUILD_PAIR_STRING("OperatingSystemImageVersion", c->data[PROP_OS_IMAGE_VERSION]),
SD_JSON_BUILD_PAIR("MachineInformationData", JSON_BUILD_STRV_ENV_PAIR(machine_info_pairs)),
SD_JSON_BUILD_PAIR_STRV("MachineTags", tags),
SD_JSON_BUILD_PAIR_STRING("HardwareVendor", vendor ?: c->data[PROP_HARDWARE_VENDOR]),
SD_JSON_BUILD_PAIR_STRING("HardwareModel", model ?: c->data[PROP_HARDWARE_MODEL]),
SD_JSON_BUILD_PAIR_STRING("HardwareSerial", serial),
+2
View File
@@ -50,6 +50,8 @@ static SD_VARLINK_DEFINE_METHOD(
SD_VARLINK_DEFINE_OUTPUT(OperatingSystemImageVersion, SD_VARLINK_STRING, SD_VARLINK_NULLABLE),
SD_VARLINK_FIELD_COMMENT("The full contents of /etc/machine-info, as an array of 'KEY=VALUE' strings."),
SD_VARLINK_DEFINE_OUTPUT(MachineInformationData, SD_VARLINK_STRING, SD_VARLINK_NULLABLE|SD_VARLINK_ARRAY),
SD_VARLINK_FIELD_COMMENT("The machine tags, from the TAGS= field of /etc/machine-info with any invalid tags filtered out. Empty if none are configured."),
SD_VARLINK_DEFINE_OUTPUT(MachineTags, SD_VARLINK_STRING, SD_VARLINK_NULLABLE|SD_VARLINK_ARRAY),
SD_VARLINK_FIELD_COMMENT("The hardware vendor of this system. Null if not known."),
SD_VARLINK_DEFINE_OUTPUT(HardwareVendor, SD_VARLINK_STRING, SD_VARLINK_NULLABLE),
SD_VARLINK_FIELD_COMMENT("The hardware model of this system. Null if not known."),
+18
View File
@@ -488,6 +488,24 @@ testcase_tags() {
# Invalid tags are refused.
(! varlinkctl call /run/systemd/io.systemd.Hostname io.systemd.Hostname.SetTags '{"add":["in valid"]}')
# Describe() exposes the parsed tag list as MachineTags, on both transports.
varlinkctl call /run/systemd/io.systemd.Hostname io.systemd.Hostname.SetTags '{"set":["foo","bar"]}'
assert_eq "$(varlinkctl call /run/systemd/io.systemd.Hostname io.systemd.Hostname.Describe '{}' | jq --compact-output .MachineTags)" '["bar","foo"]'
assert_eq "$(busctl call --json=short org.freedesktop.hostname1 /org/freedesktop/hostname1 org.freedesktop.hostname1 Describe | jq --compact-output '.data[0] | fromjson | .MachineTags')" '["bar","foo"]'
assert_eq "$(busctl get-property org.freedesktop.hostname1 /org/freedesktop/hostname1 org.freedesktop.hostname1 Tags)" 'as 2 "bar" "foo"'
# The field is an empty list if no tags are configured (as with the D-Bus Tags property).
varlinkctl call /run/systemd/io.systemd.Hostname io.systemd.Hostname.SetTags '{"set":[]}'
assert_eq "$(varlinkctl call /run/systemd/io.systemd.Hostname io.systemd.Hostname.Describe '{}' | jq --compact-output .MachineTags)" "[]"
assert_eq "$(busctl call --json=short org.freedesktop.hostname1 /org/freedesktop/hostname1 org.freedesktop.hostname1 Describe | jq --compact-output '.data[0] | fromjson | .MachineTags')" "[]"
assert_eq "$(busctl get-property org.freedesktop.hostname1 /org/freedesktop/hostname1 org.freedesktop.hostname1 Tags)" 'as 0'
# Invalid tags edited into /etc/machine-info by hand are gracefully dropped from MachineTags
# and D-Bus's Tags property, while MachineInformationData still carries the raw TAGS= line.
echo 'TAGS=good:-invalid' >/etc/machine-info
assert_eq "$(varlinkctl call /run/systemd/io.systemd.Hostname io.systemd.Hostname.Describe '{}' | jq --compact-output .MachineTags)" '["good"]'
assert_eq "$(busctl call --json=short org.freedesktop.hostname1 /org/freedesktop/hostname1 org.freedesktop.hostname1 Describe | jq --compact-output '.data[0] | fromjson | .MachineTags')" '["good"]'
assert_eq "$(busctl get-property org.freedesktop.hostname1 /org/freedesktop/hostname1 org.freedesktop.hostname1 Tags)" 'as 1 "good"'
assert_eq "$(varlinkctl call /run/systemd/io.systemd.Hostname io.systemd.Hostname.Describe '{}' | jq --raw-output '.MachineInformationData[] | select(startswith("TAGS="))')" "TAGS=good:-invalid"
hostnamectl tags ""
}