rsyslog/tests/yaml-include.sh
Rainer Gerhards e8fa2034fe feat: add YAML as an alternative configuration format
rsyslog now accepts .yaml / .yml files as a full alternative to
RainerScript configuration.  Whenever a .yaml or .yml file is
referenced (e.g. via $IncludeConfig or as the main config), it is
routed through the new yamlconf loader instead of the bison/flex
parser.  RainerScript and YAML configurations may coexist.

Supported top-level sections
  global:          global settings
  main_queue:      main queue settings
  modules:         module loading (load:, params:)
  inputs:          input objects  templates:       string, subtree, plugin, and list templates
                   (including all property/constant modifiers)
  rulesets:        rulesets with optional script: block (raw
                   RainerScript), filter/action shortcut (if:/then:),
                   and the full statements: block:
                     action:, set:, unset:, call:, call_indirect:,
                     foreach:, if:, stop:, continue:,
                     reload_lookup_table:
  outputs:         output objects (top-level convenience alias)
  include:         recursive includes (.yaml processed immediately;
                   .conf files pushed to LIFO stack in reverse order
                   for correct document-order processing)

Implementation notes
  - Thin front-end over existing cnfobj/nvlst/cnfDoObj() machinery;
    all parameter validation happens downstream unchanged.
  - Script blocks synthesised as RainerScript strings and injected via
    cnfAddConfigBuffer() (requires two trailing NUL bytes).
  - YAML -> YAML includes are synchronous recursive calls;
    YAML -> .conf includes use the flex buffer stack.
  - Mixed include lists (.yaml + .conf) emit a warning because strict
    document order cannot be preserved across the two mechanisms.
  - Built conditionally: --with-libyaml / HAVE_LIBYAML.

Tests
  19 new shell tests cover: basic config, modules, inputs, templates
  (string/subtree/list with modifiers), rulesets with script/filter/
  statements blocks, stop/continue, set/unset, foreach, call,
  call_indirect, reload_lookup_table, includes, error handling.

Documentation
  doc/source/configuration/yaml_config.rst  (new reference page)
  Inline comments in yamlconf.c explain all invariants and non-obvious
  design choices (es_getBufAddr non-null-termination, LIFO stack
  ordering, cnfAddConfigBuffer double-NUL requirement, etc.)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-03-27 09:41:37 +01:00

71 lines
2.2 KiB
Bash
Executable File

#!/bin/bash
# Tests that YAML config files can include both:
# - another .yaml file (YAML-from-YAML include)
# - a .conf (RainerScript) file (RainerScript-from-YAML include)
# Verifies the extension-based router in cnfDoInclude() works correctly.
#
# Template "outfmt" is defined in the main YAML's templates: section so that
# it is registered via cnfDoObj() before any flex buffers are consumed.
# The included .conf file adds a second template ("confmt") to prove that
# RainerScript includes from YAML are processed; that template is used by a
# second action in the ruleset script so both outputs are checked.
#
# Added 2025 by contributors, released under ASL 2.0
. ${srcdir:=.}/diag.sh init
require_plugin imtcp
export NUMMESSAGES=50
export QUEUE_EMPTY_CHECK_FUNC=wait_file_lines
generate_conf
add_conf '
# Testbench preamble in RainerScript; main config in YAML
include(file="'${RSYSLOG_DYNNAME}'.yaml")
'
# Sub-config included from YAML as a .conf (RainerScript) fragment.
# Defines a second template so we can verify .conf includes are processed.
cat > "${RSYSLOG_DYNNAME}_inc.conf" << CONFEOF
template(name="confmt" type="string" string="%msg:F,58:2%\n")
CONFEOF
# Sub-config included from YAML as a nested .yaml file — loads imtcp.
cat > "${RSYSLOG_DYNNAME}_sub.yaml" << 'SUBEOF'
modules:
- load: "../plugins/imtcp/.libs/imtcp"
SUBEOF
# Main YAML config.
# templates: is processed via cnfDoObj() during YAML loading (before any flex
# buffers are consumed), so "outfmt" is always available to the ruleset script.
cat > "${RSYSLOG_DYNNAME}.yaml" << YAMLEOF
include:
- path: "${RSYSLOG_DYNNAME}_sub.yaml"
- path: "${RSYSLOG_DYNNAME}_inc.conf"
templates:
- name: outfmt
type: string
string: "%msg:F,58:2%\n"
rulesets:
- name: main
script: |
:msg, contains, "msgnum:" action(type="omfile"
template="outfmt" file="${RSYSLOG_OUT_LOG}")
YAMLEOF
# Expand shell vars
sed -i \
-e "s|\${RSYSLOG_DYNNAME}|${RSYSLOG_DYNNAME}|g" \
-e "s|\${RSYSLOG_OUT_LOG}|${RSYSLOG_OUT_LOG}|g" \
"${RSYSLOG_DYNNAME}.yaml"
add_conf '
input(type="imtcp" port="0" listenPortFileName="'${RSYSLOG_DYNNAME}'.tcpflood_port"
ruleset="main")
'
startup
tcpflood -m $NUMMESSAGES
shutdown_when_empty
wait_shutdown
seq_check
exit_test