Skip to content

Commit

Permalink
[Banner] Add new feature including config, yang and tests (#16957)
Browse files Browse the repository at this point in the history
- Why I did it
Added Banner feature related services according to HLD: sonic-net/SONiC#1361

- How I did it
Added banner-config systemd service, YANG model for new ConfDB table and YANG model tests

- How to verify it
Manual test

Co-authored-by: Sviatoslav Boichuk <[email protected]>
  • Loading branch information
SviatoslavBoichuk and Sviatoslav Boichuk authored Apr 16, 2024
1 parent b0f5398 commit 0b8b503
Show file tree
Hide file tree
Showing 11 changed files with 186 additions and 0 deletions.
8 changes: 8 additions & 0 deletions files/build_templates/init_cfg.json.j2
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,14 @@
"special_class": "true"
}
},
"BANNER_MESSAGE": {
"global": {
"state": "disabled",
"login": "Debian GNU/Linux 11",
"motd": "You are on\n ____ ___ _ _ _ ____\n / ___| / _ \\| \\ | (_)/ ___|\n \\___ \\| | | | \\| | | |\n ___) | |_| | |\\ | | |___\n |____/ \\___/|_| \\_|_|\\____|\n\n-- Software for Open Networking in the Cloud --\n\nUnauthorized access and/or use are prohibited.\nAll access and/or use are subject to monitoring.\n\nHelp: https://sonic-net.github.io/SONiC/\n\n",
"logout": ""
}
},
"SYSTEM_DEFAULTS" : {
{%- if include_mux == "y" %}
"mux_tunnel_egress_acl": {
Expand Down
7 changes: 7 additions & 0 deletions files/build_templates/sonic_debian_extension.j2
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ sudo LANG=C DEBIAN_FRONTEND=noninteractive chroot $FILESYSTEM_ROOT apt-get -y in
# Apply environtment configuration files
sudo cp $IMAGE_CONFIGS/environment/environment $FILESYSTEM_ROOT/etc/
sudo cp $IMAGE_CONFIGS/environment/motd $FILESYSTEM_ROOT/etc/
sudo cp $IMAGE_CONFIGS/environment/logout_message $FILESYSTEM_ROOT/etc/

# Create all needed directories
sudo mkdir -p $FILESYSTEM_ROOT/etc/sonic/
Expand Down Expand Up @@ -228,6 +229,7 @@ sudo dpkg --root=$FILESYSTEM_ROOT -i $debs_path/bash_*.deb || \
# sonic-utilities-data installs bash-completion as a dependency. However, it is disabled by default
# in bash.bashrc, so we copy a version of the file with it enabled here.
sudo cp -f $IMAGE_CONFIGS/bash/bash.bashrc $FILESYSTEM_ROOT/etc/
sudo cp -f $IMAGE_CONFIGS/bash/bash.bash_logout $FILESYSTEM_ROOT/etc/

# Install readline's initialization file
sudo cp -f $IMAGE_CONFIGS/readline/inputrc $FILESYSTEM_ROOT/etc/
Expand Down Expand Up @@ -605,6 +607,11 @@ sudo cp $IMAGE_CONFIGS/hostname/hostname-config.service $FILESYSTEM_ROOT_USR_LIB
echo "hostname-config.service" | sudo tee -a $GENERATED_SERVICE_FILE
sudo cp $IMAGE_CONFIGS/hostname/hostname-config.sh $FILESYSTEM_ROOT/usr/bin/

# Copy banner configuration scripts
sudo cp $IMAGE_CONFIGS/bannerconfig/banner-config.service $FILESYSTEM_ROOT_USR_LIB_SYSTEMD_SYSTEM
echo "banner-config.service" | sudo tee -a $GENERATED_SERVICE_FILE
sudo cp $IMAGE_CONFIGS/bannerconfig/banner-config.sh $FILESYSTEM_ROOT/usr/bin/

# Copy miscellaneous scripts
sudo cp $IMAGE_CONFIGS/misc/docker-wait-any $FILESYSTEM_ROOT/usr/bin/

Expand Down
17 changes: 17 additions & 0 deletions files/image_config/bannerconfig/banner-config.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[Unit]
Description=Update banner config based on configdb
Requires=config-setup.service
After=config-setup.service
Before=systemd-logind.service sshd.service
BindsTo=database.service
BindsTo=sonic.target

[Service]
Type=oneshot
RemainAfterExit=no
ExecStart=/usr/bin/banner-config.sh

[Install]
WantedBy=sonic.target


17 changes: 17 additions & 0 deletions files/image_config/bannerconfig/banner-config.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash -e

STATE=$(sonic-cfggen -d -v 'BANNER_MESSAGE["global"]["state"]')
LOGIN=
MOTD=
LOGOUT=

if [[ $STATE == "enabled" ]]; then
LOGIN=$(sonic-cfggen -d -v 'BANNER_MESSAGE["global"]["login"]')
MOTD=$(sonic-cfggen -d -v 'BANNER_MESSAGE["global"]["motd"]')
LOGOUT=$(sonic-cfggen -d -v 'BANNER_MESSAGE["global"]["logout"]')

echo -e "$LOGIN" > /etc/issue.net
echo -e "$LOGIN" > /etc/issue
echo -e "$MOTD" > /etc/motd
echo -e "$LOGOUT" > /etc/logout_message
fi
12 changes: 12 additions & 0 deletions files/image_config/bash/bash.bash_logout
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
LOGOUT_MESSAGE_PATH="/etc/logout_message"
LOGOUT_MESSAGE=

if [[ -f "$LOGOUT_MESSAGE_PATH" ]]; then
LOGOUT_MESSAGE=$(cat $LOGOUT_MESSAGE_PATH)
fi

if [[ -n "$LOGOUT_MESSAGE" ]]; then
# Print logout message
echo $LOGOUT_MESSAGE
fi

Empty file.
2 changes: 2 additions & 0 deletions src/sonic-yang-models/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ def run(self):
('yang-models', ['./yang-models/sonic-acl.yang',
'./yang-models/sonic-auto_techsupport.yang',
'./yang-models/sonic-bgp-bbr.yang',
'./yang-models/sonic-banner.yang',
'./yang-models/sonic-bgp-common.yang',
'./yang-models/sonic-bgp-device-global.yang',
'./yang-models/sonic-bgp-global.yang',
Expand Down Expand Up @@ -201,6 +202,7 @@ def run(self):
'./yang-models/sonic-bgp-sentinel.yang',
'./yang-models/sonic-smart-switch.yang',]),
('cvlyang-models', ['./cvlyang-models/sonic-acl.yang',
'./cvlyang-models/sonic-banner.yang',
'./cvlyang-models/sonic-bgp-common.yang',
'./cvlyang-models/sonic-bgp-global.yang',
'./cvlyang-models/sonic-bgp-monitor.yang',
Expand Down
8 changes: 8 additions & 0 deletions src/sonic-yang-models/tests/files/sample_config_db.json
Original file line number Diff line number Diff line change
Expand Up @@ -2636,6 +2636,14 @@
"dpu1": {
"midplane_interface": "dpu1"
}
},
"BANNER_MESSAGE": {
"global": {
"state": "enabled",
"login": "Some login message",
"motd": "Some message of the day",
"logout": "Some logout message"
}
}
},
"SAMPLE_CONFIG_DB_UNKNOWN": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"BANNER_MESSAGE_TEST_STATE": {
"desc": "Configure Banner feature state."
},
"BANNER_MESSAGE_TEST_LOGIN" : {
"desc": "Banner login messages configuration in BANNER_MESSAGE table."
},
"BANNER_MESSAGE_TEST_MOTD" : {
"desc": "Banner MOTD messages configuration in BANNER_MESSAGE table."
},
"BANNER_MESSAGE_TEST_LOGOUT" : {
"desc": "Banner logout messages configuration in BANNER_MESSAGE table."
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"BANNER_MESSAGE_TEST_STATE": {
"sonic-banner:sonic-banner": {
"sonic-banner:BANNER_MESSAGE": {
"global": {
"state": "enabled",
"login": "Some login message",
"motd": "Some message of the day",
"logout": "Some logout message"
}
}
}
},
"BANNER_MESSAGE_TEST_LOGIN": {
"sonic-banner:sonic-banner": {
"sonic-banner:BANNER_MESSAGE": {
"global": {
"state": "enabled",
"login": "Some login message",
"motd": "Some message of the day",
"logout": "Some logout message"
}
}
}
},
"BANNER_MESSAGE_TEST_MOTD": {
"sonic-banner:sonic-banner": {
"sonic-banner:BANNER_MESSAGE": {
"global": {
"state": "enabled",
"login": "Some login message",
"motd": "Some message of the day",
"logout": "Some logout message"
}
}
}
},
"BANNER_MESSAGE_TEST_LOGOUT": {
"sonic-banner:sonic-banner": {
"sonic-banner:BANNER_MESSAGE": {
"global": {
"state": "enabled",
"login": "Some login message",
"motd": "Some message of the day",
"logout": "Some logout message"
}
}
}
}
}
51 changes: 51 additions & 0 deletions src/sonic-yang-models/yang-models/sonic-banner.yang
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
module sonic-banner {
yang-version 1.1;
namespace "http://github.com/sonic-net/sonic-banner";
prefix banner_message;

import sonic-types {
prefix stypes;
}

description "BANNER_MESSAGE YANG Module for SONiC-based OS";
revision 2023-05-18 {
description "First Revision";
}
container sonic-banner {
container BANNER_MESSAGE {
description "BANNER_MESSAGE part of config_db.json";
container global {
leaf state {
type stypes:admin_mode;
description "Banner feature state";
default disabled;
}
leaf login {
type string;
description "Banner message displayed to user before login prompt";
default "Debian GNU/Linux 11";
}
leaf motd {
type string;
description "Banner message displayed to user after login prompt";
default "You are on
____ ___ _ _ _ ____
/ ___| / _ \\| \\ | (_)/ ___|
\\___ \\| | | | \\| | | |
___) | |_| | |\\ | | |___
|____/ \\___/|_| \\_|_|\\____|
-- Software for Open Networking in the Cloud --
Unauthorized access and/or use are prohibited.
All access and/or use are subject to monitoring.
Help: https://sonic-net.github.io/SONiC/
";
}
leaf logout {
type string;
description "Banner message dispalyed to the users on logout";
default "";
}
} /* end of container MESSAGE */
} /* end of container BANNER_MESSAGE */
} /* end of top level container */
}

0 comments on commit 0b8b503

Please sign in to comment.