]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
journalctl: allow to dump generated key in json format 35507/head
authorYu Watanabe <watanabe.yu+github@gmail.com>
Sun, 8 Dec 2024 20:22:05 +0000 (05:22 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 11 Dec 2024 02:18:06 +0000 (11:18 +0900)
Closes #35503.

man/journalctl.xml
src/journal/journalctl-authenticate.c
test/units/TEST-04-JOURNAL.fss.sh

index 0cc2b72acc5149a5f7b9034ffb47565936df8715..1f9ca364f30034f47b3d665276e54159408caf69 100644 (file)
         with <option>--setup-keys</option>. Shorter intervals increase CPU consumption but shorten the time
         range of undetectable journal alterations. Defaults to 15min.</para>
 
+        <para>Note, <option>--output=json-sse</option> and <option>--output=json-seq</option> are silently
+        migrated to <option>--output=json</option>.</para>
+
         <xi:include href="version-info.xml" xpointer="v189"/></listitem>
       </varlistentry>
 
index 87374816b480ad58b1f24a440380d392a8e2c2a5..7aaa340cd3cffd8935c83ad8050a1a72ee5a4deb 100644 (file)
@@ -1,5 +1,7 @@
 /* SPDX-License-Identifier: LGPL-2.1-or-later */
 
+#include "sd-json.h"
+
 #include "ansi-color.h"
 #include "chattr-util.h"
 #include "errno-util.h"
@@ -158,7 +160,7 @@ int action_setup_keys(void) {
         if (r < 0)
                 return r;
 
-        if (!on_tty() || arg_quiet) {
+        if ((!on_tty() || arg_quiet) && !sd_json_format_enabled(arg_json_format_flags)) {
                 /* If we are not on a TTY, show only the key. */
                 puts(key);
                 return 0;
@@ -169,6 +171,32 @@ int action_setup_keys(void) {
         if (hn)
                 hostname_cleanup(hn);
 
+        if (sd_json_format_enabled(arg_json_format_flags)) {
+                _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
+
+                if (arg_json_format_flags & (SD_JSON_FORMAT_SSE | SD_JSON_FORMAT_SEQ)) {
+                        log_debug("Specified --output=%s with --setup-keys, migrating to --output=json.",
+                                  FLAGS_SET(arg_json_format_flags, SD_JSON_FORMAT_SSE) ? "json-sse" : "json-seq");
+                        arg_json_format_flags &= ~(SD_JSON_FORMAT_SSE | SD_JSON_FORMAT_SEQ);
+                        arg_json_format_flags |= SD_JSON_FORMAT_NEWLINE;
+                }
+
+                r = sd_json_buildo(
+                                &v,
+                                SD_JSON_BUILD_PAIR_ID128("machine", machine),
+                                SD_JSON_BUILD_PAIR_STRING("hostname", hn),
+                                SD_JSON_BUILD_PAIR_STRING("path", path),
+                                SD_JSON_BUILD_PAIR_STRING("key", key));
+                if (r < 0)
+                        return log_error_errno(r, "Failed to build json object: %m");
+
+                r = sd_json_variant_dump(v, arg_json_format_flags, /* f = */ NULL, /* prefix = */ NULL);
+                if (r < 0)
+                        return log_error_errno(r, "Failed to dump json object: %m");
+
+                return 0;
+        }
+
         fprintf(stderr,
                 "\nNew keys have been generated for host %s%s" SD_ID128_FORMAT_STR ".\n"
                 "\n"
index 140bd9fd67dacb92d31b6310ef3e67c641639df5..7edcbf95b09033c95e3294a7ae2f5378a9e8e400 100755 (executable)
@@ -10,6 +10,11 @@ if ! journalctl --version | grep -qF +GCRYPT; then
     exit 0
 fi
 
+# output key and related info in json format
+for mode in json json-pretty json-seq json-sse; do
+    journalctl --force --setup-keys --interval=2 --output="$mode" | jq . >/dev/null
+done
+
 # without --quiet, should be effectively equivalent to the below, as we are not on tty
 journalctl --force --setup-keys --interval=2