|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Syslog Hostname Filtering |
| 3 | +
|
| 4 | +Verify hostname-filter feature: filtering incoming syslog messages based |
| 5 | +on the hostname. Tests log sink scenario with multiple remote clients. |
| 6 | +
|
| 7 | +""" |
| 8 | + |
| 9 | +import infamy |
| 10 | +import time |
| 11 | + |
| 12 | +TEST_MESSAGES = [ |
| 13 | + ("router1", "Message from router1"), |
| 14 | + ("router1", "Another from router1"), |
| 15 | + ("router2", "Message from router2"), |
| 16 | + ("other", "Message from a different host"), |
| 17 | +] |
| 18 | + |
| 19 | +with infamy.Test() as test: |
| 20 | + with test.step("Set up topology and attach to DUTs"): |
| 21 | + env = infamy.Env() |
| 22 | + client = env.attach("client", "mgmt") |
| 23 | + server = env.attach("server", "mgmt") |
| 24 | + clientssh = env.attach("client", "mgmt", "ssh") |
| 25 | + serverssh = env.attach("server", "mgmt", "ssh") |
| 26 | + |
| 27 | + with test.step("Clean up old log files on server"): |
| 28 | + serverssh.runsh("sudo rm -f /var/log/{router1,router2,all-hosts}") |
| 29 | + |
| 30 | + with test.step("Configure server as syslog sink with hostname filtering"): |
| 31 | + _, server_link = env.ltop.xlate("server", "link") |
| 32 | + |
| 33 | + server.put_config_dicts({ |
| 34 | + "ietf-interfaces": { |
| 35 | + "interfaces": { |
| 36 | + "interface": [{ |
| 37 | + "name": server_link, |
| 38 | + "enabled": True, |
| 39 | + "ipv4": { |
| 40 | + "address": [{ |
| 41 | + "ip": "10.0.0.1", |
| 42 | + "prefix-length": 24, |
| 43 | + }] |
| 44 | + } |
| 45 | + }] |
| 46 | + } |
| 47 | + }, |
| 48 | + "ietf-syslog": { |
| 49 | + "syslog": { |
| 50 | + "actions": { |
| 51 | + "file": { |
| 52 | + "log-file": [{ |
| 53 | + "name": "file:router1", |
| 54 | + "infix-syslog:hostname-filter": ["router1"], |
| 55 | + "facility-filter": { |
| 56 | + "facility-list": [{ |
| 57 | + "facility": "all", |
| 58 | + "severity": "info" |
| 59 | + }] |
| 60 | + } |
| 61 | + }, { |
| 62 | + "name": "file:router2", |
| 63 | + "infix-syslog:hostname-filter": ["router2"], |
| 64 | + "facility-filter": { |
| 65 | + "facility-list": [{ |
| 66 | + "facility": "all", |
| 67 | + "severity": "info" |
| 68 | + }] |
| 69 | + } |
| 70 | + }, { |
| 71 | + "name": "file:all-hosts", |
| 72 | + "facility-filter": { |
| 73 | + "facility-list": [{ |
| 74 | + "facility": "all", |
| 75 | + "severity": "info" |
| 76 | + }] |
| 77 | + } |
| 78 | + }] |
| 79 | + } |
| 80 | + }, |
| 81 | + "infix-syslog:server": { |
| 82 | + "enabled": True, |
| 83 | + "listen": { |
| 84 | + "udp": [{ |
| 85 | + "port": 514, |
| 86 | + "address": "10.0.0.1" |
| 87 | + }] |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + }) |
| 93 | + |
| 94 | + with test.step("Configure client to forward logs to server"): |
| 95 | + _, client_link = env.ltop.xlate("client", "link") |
| 96 | + |
| 97 | + client.put_config_dicts({ |
| 98 | + "ietf-interfaces": { |
| 99 | + "interfaces": { |
| 100 | + "interface": [{ |
| 101 | + "name": client_link, |
| 102 | + "enabled": True, |
| 103 | + "ipv4": { |
| 104 | + "address": [{ |
| 105 | + "ip": "10.0.0.2", |
| 106 | + "prefix-length": 24, |
| 107 | + }] |
| 108 | + } |
| 109 | + }] |
| 110 | + } |
| 111 | + }, |
| 112 | + "ietf-syslog": { |
| 113 | + "syslog": { |
| 114 | + "actions": { |
| 115 | + "remote": { |
| 116 | + "destination": [{ |
| 117 | + "name": "server", |
| 118 | + "udp": { |
| 119 | + "address": "10.0.0.1" |
| 120 | + }, |
| 121 | + "facility-filter": { |
| 122 | + "facility-list": [{ |
| 123 | + "facility": "all", |
| 124 | + "severity": "info" |
| 125 | + }] |
| 126 | + } |
| 127 | + }] |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + }) |
| 133 | + |
| 134 | + time.sleep(2) |
| 135 | + |
| 136 | + with test.step("Send log messages with different hostnames"): |
| 137 | + for hostname, message in TEST_MESSAGES: |
| 138 | + clientssh.runsh(f"logger -t test -p daemon.info -H {hostname} -h 10.0.0.1 '{message}'") |
| 139 | + time.sleep(2) |
| 140 | + |
| 141 | + with test.step("Verify router1 log contains only router1 messages"): |
| 142 | + rc = serverssh.runsh("cat /var/log/router1 2>/dev/null") |
| 143 | + log_content = rc.stdout if rc.returncode == 0 else "" |
| 144 | + |
| 145 | + router1_messages = [msg for host, msg in TEST_MESSAGES if host == "router1"] |
| 146 | + missing = [msg for msg in router1_messages if msg not in log_content] |
| 147 | + if missing: |
| 148 | + test.fail(f"Missing router1 messages in /var/log/router1: {missing}") |
| 149 | + |
| 150 | + unwanted_messages = [msg for host, msg in TEST_MESSAGES if host != "router1"] |
| 151 | + found = [msg for msg in unwanted_messages if msg in log_content] |
| 152 | + if found: |
| 153 | + test.fail(f"Found unwanted messages in /var/log/router1: {found}") |
| 154 | + |
| 155 | + with test.step("Verify router2 log contains only router2 messages"): |
| 156 | + rc = serverssh.runsh("cat /var/log/router2 2>/dev/null") |
| 157 | + log_content = rc.stdout if rc.returncode == 0 else "" |
| 158 | + |
| 159 | + router2_messages = [msg for host, msg in TEST_MESSAGES if host == "router2"] |
| 160 | + missing = [msg for msg in router2_messages if msg not in log_content] |
| 161 | + if missing: |
| 162 | + test.fail(f"Missing router2 messages in /var/log/router2: {missing}") |
| 163 | + |
| 164 | + unwanted_messages = [msg for host, msg in TEST_MESSAGES if host != "router2"] |
| 165 | + found = [msg for msg in unwanted_messages if msg in log_content] |
| 166 | + if found: |
| 167 | + test.fail(f"Found unwanted messages in /var/log/router2: {found}") |
| 168 | + |
| 169 | + with test.step("Verify all-hosts log contains all messages"): |
| 170 | + rc = serverssh.runsh("cat /var/log/all-hosts 2>/dev/null") |
| 171 | + log_content = rc.stdout if rc.returncode == 0 else "" |
| 172 | + |
| 173 | + expected_messages = [msg for _, msg in TEST_MESSAGES] |
| 174 | + missing = [msg for msg in expected_messages if msg not in log_content] |
| 175 | + |
| 176 | + if missing: |
| 177 | + test.fail(f"Missing messages in /var/log/all-hosts: {missing}") |
| 178 | + |
| 179 | + test.succeed() |
0 commit comments