Skip to content

Commit a289f4a

Browse files
authored
Use Vagrant private key instead of username for FRR (#1963)
This is a proof-of-concept for the 'use Vagrant private key' VM authentication. FRR is used as an example because it already uses Debian Bookworm; other devices that could use the same approach are Cumulus (4/5), VyOS, and Linux. We're adding private-key-based-authentication functionality to netlab while retaining the password-based authentication that might be needed for network management tools. The Vagrant provisioning script adds the commands to set the password for the 'vagrant' user (unless it's not defined) and change the Debian default SSH authentication policy. The Debian-specific provisioning steps are stored in a separate template as we'll need them when migrating Linux node to Debian.
1 parent 583a55f commit a289f4a

File tree

5 files changed

+61
-14
lines changed

5 files changed

+61
-14
lines changed

netsim/cli/connect.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class LogLevel(IntEnum):
2323

2424
from . import load_snapshot, parser_add_verbose
2525
from ..outputs import common as outputs_common
26-
from ..utils import strings, log
26+
from ..utils import strings, log, templates
2727

2828
#
2929
# CLI parser for 'netlab initial' command
@@ -99,6 +99,10 @@ def ssh_connect(
9999
if data.ansible_ssh_pass:
100100
c_args = ['sshpass','-p',data.ansible_ssh_pass ] + c_args
101101

102+
if data.ansible_ssh_private_key_file:
103+
data.inventory_hostname = data.host
104+
c_args.extend(['-i', templates.render_template(data,j2_text=data.ansible_ssh_private_key_file)])
105+
102106
if data.ansible_port:
103107
c_args.extend(['-p',str(data.ansible_port)])
104108

netsim/devices/frr.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,24 @@ clab:
2929
config_templates:
3030
daemons: /etc/frr/daemons
3131
hosts: /etc/hosts
32+
3233
libvirt:
33-
image: debian/bookworm64 #generic/ubuntu2004
34+
image: debian/bookworm64
3435
group_vars:
3536
ansible_connection: paramiko
3637
ansible_user: vagrant
3738
ansible_ssh_pass: vagrant
39+
ansible_ssh_private_key_file: .vagrant/machines/{{ inventory_hostname }}/libvirt/private_key
3840
netlab_show_command: [ sudo, vtysh, -c, 'show $@' ]
3941

4042
virtualbox:
41-
image: debian/bookworm64 #generic/ubuntu2004
43+
image: generic/ubuntu2004
4244
group_vars:
4345
ansible_connection: paramiko
4446
ansible_user: vagrant
4547
ansible_ssh_pass: vagrant
4648
netlab_show_command: [ sudo, vtysh, -c, 'show $@' ]
49+
4750
external:
4851
image: none
4952
features:
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
echo "Setting password for user Vagrant"
2+
echo vagrant:{{ n.ansible_ssh_pass|default }} | chpasswd
3+
4+
echo "Enabling SSH password authentication"
5+
sed -i -e "s#PasswordAuthentication no#PasswordAuthentication yes#" /etc/ssh/sshd_config
6+
service sshd restart

netsim/templates/provider/libvirt/frr-domain.j2

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
11
{% if _vagrant_scripts.frr is not defined %}
22
{% set _vagrant_scripts.frr = True %}
33

4-
$frr_script = <<-SCRIPT
5-
echo "Setting password for user Vagrant"
6-
echo vagrant:{{ n.ansible_ssh_pass }} | chpasswd
7-
8-
echo "Enabling SSH password authentication"
9-
sed -i -e "s#PasswordAuthentication no#PasswordAuthentication yes#" /etc/ssh/sshd_config
10-
service sshd restart
11-
SCRIPT
12-
134
$frr_install_script = <<-SCRIPT
145
set -e
156
if which /usr/lib/frr/frrinit.sh; then
@@ -36,7 +27,15 @@ fi
3627
domain.memory = 1024
3728
end
3829

39-
# Run debian-specific provisioning script
30+
# Run debian-specific provisioning script.
4031
#
41-
{{ name }}.vm.provision :shell, :inline => $frr_script{%
32+
# Password change has to be inline as the password might be different for each VM
33+
#
34+
$frr_password = <<-SCRIPT
35+
{% if n.ansible_ssh_pass|default(False) %}
36+
{% include '_debian_vagrant_password.j2' +%}
37+
{% endif +%}
38+
SCRIPT
39+
40+
{{ name }}.vm.provision :shell, :inline => $frr_password{%
4241
if not (n.netlab_quick_start|default(False)) %} + $frr_install_script {% endif +%}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
message: |
2+
The test checks different combinations of Vagrant user authentication:
3+
4+
* SSH password + SSH private key (default)
5+
* SSH password only
6+
* SSH private key only
7+
8+
Ansible access is checked during the configuration phase, netlab access
9+
during the validation phase
10+
11+
provider: libvirt
12+
defaults.device: frr
13+
module: [ ospf ]
14+
ospf.timers.hello: 1
15+
16+
nodes:
17+
r1: # password + private key
18+
r2:
19+
ansible_ssh_private_key_file: # No private key file
20+
r3:
21+
ansible_ssh_pass: # No password
22+
r4:
23+
ansible_ssh_pass:
24+
netlab_quick_start: True # Combination of no password + no install
25+
26+
links: [ r1-r2, r2-r3, r2-r4 ]
27+
28+
validate:
29+
adj_r2:
30+
plugin: ospf_neighbor(nodes.r2.ospf.router_id)
31+
wait: 15
32+
nodes: [ r1, r3, r4 ]
33+
adj_r1:
34+
plugin: ospf_neighbor(nodes.r1.ospf.router_id)
35+
nodes: [ r2 ]

0 commit comments

Comments
 (0)