Skip to content

Commit 8f2ee5f

Browse files
wangxinmssonicbld
authored andcommitted
Support alternate password for PTF container (#16457)
What is the motivation for this PR? The PTF container is always using default password. If the PTF container is on same bridge with the host server's management IP, then it is easily accessible from other host servers. This is not secure enough. We need to support alternate password for the PTF container and password rotation. How did you do it? This change improved the ansible related code to support accessing the PTF containers using the multi_ssh_pass ansible plugin. Then we can specify alternate passwords for the PTF container. When alternate passwords are specified, the default password of PTF container is updated after PTF creation. How did you verify/test it? Tested remove-topo/add-topo/restart-ptf on KVM and physical testbed.
1 parent cad42f8 commit 8f2ee5f

12 files changed

Lines changed: 160 additions & 91 deletions

File tree

ansible/group_vars/ptf/secrets.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
ansible_connection: multi_passwd_ssh
2+
3+
ansible_user: root
4+
ansible_ssh_pass: root
5+
# ansible_altpasswords:
6+
# - fakepassword1
7+
# - fakepassword2

ansible/group_vars/ptf_host/secrets.yml

Lines changed: 0 additions & 4 deletions
This file was deleted.

ansible/roles/vm_set/tasks/add_topo.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@
7979
memory_swap: 8G
8080
become: yes
8181

82+
- name: Update ptf password
83+
include_tasks: update_ptf_password.yml
84+
8285
- name: Bind ptf_ip to keysight_api_server
8386
vm_topology:
8487
cmd: "bind_keysight_api_server_ip"
@@ -170,6 +173,9 @@
170173
memory_swap: 32G
171174
become: yes
172175

176+
- name: Update ptf password
177+
include_tasks: update_ptf_password.yml
178+
173179
- name: Enable ipv6 for docker container ptf_{{ vm_set_name }}
174180
command: docker exec -i ptf_{{ vm_set_name }} sysctl -w net.ipv6.conf.all.disable_ipv6=0
175181
become: yes

ansible/roles/vm_set/tasks/announce_routes.yml

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,14 @@
11
---
2-
- name: Include variables for PTF containers
3-
include_vars:
4-
dir: "{{ playbook_dir }}/group_vars/ptf_host/"
5-
62
- name: Set ptf host
73
set_fact:
8-
ptf_host: "ptf_{{ vm_set_name }}"
4+
ptf_host: "{{ ptf_ip.split('/')[0] }}"
95
ptf_host_ip: "{{ ptf_ip.split('/')[0] }}"
106

117
- name: Add ptf host
128
add_host:
13-
hostname: "{{ ptf_host }}"
14-
ansible_user: "{{ ptf_host_user }}"
15-
ansible_ssh_host: "{{ ptf_host_ip }}"
16-
ansible_ssh_pass: "{{ ptf_host_pass }}"
17-
ansible_python_interpreter: "/usr/bin/python"
9+
name: "{{ ptf_host }}"
1810
groups:
19-
- ptf_host
11+
- ptf
2012

2113
- name: Set default exabgp_action
2214
set_fact:

ansible/roles/vm_set/tasks/ptf_change_mac.yml

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,14 @@
11
---
2-
- name: Include variables for PTF containers
3-
include_vars:
4-
dir: "{{ playbook_dir }}/group_vars/ptf_host/"
5-
62
- name: Set ptf host
73
set_fact:
8-
ptf_host: "ptf_{{ vm_set_name }}"
4+
ptf_host: "{{ ptf_ip.split('/')[0] }}"
95
ptf_host_ip: "{{ ptf_ip.split('/')[0] }}"
106

117
- name: Add ptf host
128
add_host:
13-
hostname: "{{ ptf_host }}"
14-
ansible_user: "{{ ptf_host_user }}"
15-
ansible_ssh_host: "{{ ptf_host_ip }}"
16-
ansible_ssh_pass: "{{ ptf_host_pass }}"
17-
ansible_python_interpreter: "/usr/bin/python"
9+
name: "{{ ptf_host }}"
1810
groups:
19-
- ptf_host
11+
- ptf
2012

2113
- name: wait until ptf is reachable
2214
wait_for:

ansible/roles/vm_set/tasks/ptf_portchannel.yml

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,14 @@
11
---
2-
- name: Include variables for PTF containers
3-
include_vars:
4-
dir: "{{ playbook_dir }}/group_vars/ptf_host/"
5-
62
- name: Set ptf host
73
set_fact:
8-
ptf_host: "ptf_{{ vm_set_name }}"
4+
ptf_host: "{{ ptf_ip.split('/')[0] }}"
95
ptf_host_ip: "{{ ptf_ip.split('/')[0] }}"
106

117
- name: Add ptf host
128
add_host:
13-
hostname: "{{ ptf_host }}"
14-
ansible_user: "{{ ptf_host_user }}"
15-
ansible_ssh_host: "{{ ptf_host_ip }}"
16-
ansible_ssh_pass: "{{ ptf_host_pass }}"
17-
ansible_python_interpreter: "/usr/bin/python"
9+
name: "{{ ptf_host }}"
1810
groups:
19-
- ptf_host
11+
- ptf
2012

2113
- name: find downlink portchannel configuration
2214
set_fact:

ansible/roles/vm_set/tasks/renumber_topo.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@
122122
memory_swap: 32G
123123
become: yes
124124

125+
- name: Update ptf password
126+
include_tasks: update_ptf_password.yml
127+
125128
- name: Enable ipv6 for docker container ptf_{{ vm_set_name }}
126129
command: docker exec -i ptf_{{ vm_set_name }} sysctl -w net.ipv6.conf.all.disable_ipv6=0
127130
become: yes
Lines changed: 36 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,43 @@
11
---
2-
- name: Include variables for PTF containers
3-
include_vars:
4-
dir: "{{ playbook_dir }}/group_vars/ptf_host/"
2+
- name: Set ptf host
3+
set_fact:
4+
ptf_host: "{{ ptf_ip.split('/')[0] }}"
5+
6+
- name: Add ptf host
7+
add_host:
8+
name: "{{ ptf_host }}"
9+
groups:
10+
- ptf
11+
12+
- name: Check if ptf_tgen exists
13+
supervisorctl:
14+
name: ptf_tgen
15+
state: present
16+
become: True
17+
delegate_to: "{{ ptf_host }}"
18+
ignore_errors: True
19+
register: ptf_tgen_state
520

621
- block:
7-
- name: Set ptf host
8-
set_fact:
9-
ptf_host: "ptf_{{ vm_set_name }}"
10-
ptf_host_ip: "{{ ptf_ip.split('/')[0] }}"
22+
- name: Copy scapy scripts to ptf host
23+
copy:
24+
src: "{{ item }}"
25+
dest: "/ptf_tgen/"
26+
with_fileglob:
27+
- "{{ playbook_dir }}/../spytest/spytest/tgen/scapy/*"
28+
- "{{ playbook_dir }}/../spytest/spytest/dicts.py"
1129

12-
- name: Add ptf host
13-
add_host:
14-
hostname: "{{ ptf_host }}"
15-
ansible_user: "{{ ptf_host_user }}"
16-
ansible_ssh_host: "{{ ptf_host_ip }}"
17-
ansible_ssh_pass: "{{ ptf_host_pass }}"
18-
groups:
19-
- ptf_host
30+
- name: Create ptf_tgen service
31+
copy:
32+
src: "/ptf_tgen/service.sh"
33+
dest: "/ptf_tgen/ptf_tgen.sh"
34+
mode: "0755"
35+
remote_src: yes
2036

21-
- name: Check if ptf_tgen exists
37+
- name: Start ptf_tgen
2238
supervisorctl:
2339
name: ptf_tgen
24-
state: present
25-
become: True
26-
delegate_to: "{{ ptf_host }}"
27-
ignore_errors: True
28-
register: ptf_tgen_state
29-
30-
- block:
31-
- name: Copy scapy scripts to ptf host
32-
copy:
33-
src: "{{ item }}"
34-
dest: "/ptf_tgen/"
35-
with_fileglob:
36-
- "{{ playbook_dir }}/../spytest/spytest/tgen/scapy/*"
37-
- "{{ playbook_dir }}/../spytest/spytest/dicts.py"
38-
39-
- name: Create ptf_tgen service
40-
copy:
41-
src: "/ptf_tgen/service.sh"
42-
dest: "/ptf_tgen/ptf_tgen.sh"
43-
mode: "0755"
44-
remote_src: yes
45-
46-
- name: Start ptf_tgen
47-
supervisorctl:
48-
name: ptf_tgen
49-
state: restarted
50-
become: True
51-
delegate_to: "{{ ptf_host }}"
52-
when: ptf_tgen_state is not failed
53-
when:
54-
- ptf_host_user is defined
55-
- ptf_host_pass is defined
40+
state: restarted
41+
become: True
42+
delegate_to: "{{ ptf_host }}"
43+
when: ptf_tgen_state is not failed

ansible/roles/vm_set/tasks/start_tacacs_daily_daemon.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,14 @@
4343

4444
- name: Set ptf host
4545
set_fact:
46-
ptf_host: "ptf_{{ vm_set_name }}"
46+
ptf_host: "{{ ptf_ip.split('/')[0] }}"
47+
ptf_host_ip: "{{ ptf_ip.split('/')[0] }}"
48+
49+
- name: Add ptf host
50+
add_host:
51+
name: "{{ ptf_host }}"
52+
groups:
53+
- ptf
4754

4855
- debug: msg="ptf_host {{ ptf_host }}"
4956

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
- include_vars:
2+
file: "{{ playbook_dir }}/group_vars/ptf/secrets.yml"
3+
name: raw_ptf_secrets
4+
no_log: true
5+
6+
- name: Render ptf secrets
7+
set_fact:
8+
ptf_secrets: >-
9+
{{
10+
dict(
11+
raw_ptf_secrets.keys() | zip(raw_ptf_secrets.values()
12+
)
13+
)
14+
}}
15+
no_log: true
16+
17+
- block:
18+
19+
- name: Init default ptf_username
20+
set_fact:
21+
ptf_username: "root"
22+
when: ptf_username is not defined
23+
24+
- name: Init default ptf_password
25+
set_fact:
26+
ptf_password: "root"
27+
when: ptf_password is not defined
28+
no_log: true
29+
30+
- name: Override default ptf_username
31+
set_fact:
32+
ptf_username: "{{ ptf_secrets['ansible_user'] }}"
33+
when: "'ansible_user' in ptf_secrets"
34+
35+
- name: Override default ptf_password
36+
set_fact:
37+
ptf_password: "{{ ptf_secrets['ansible_ssh_pass'] }}"
38+
when: "'ansible_ssh_pass' in ptf_secrets"
39+
40+
- name: Get ptf_alt_passwords from ptf_secrets
41+
set_fact:
42+
ptf_alt_passwords: "{{ ptf_secrets['ansible_altpasswords'] }}"
43+
no_log: true
44+
45+
- name: If ptf_alt_passwords is a list, set ptf_password to its first value
46+
set_fact:
47+
ptf_password: "{{ ptf_alt_passwords[0] }}"
48+
when: ptf_alt_passwords | type_debug == "list" and ptf_alt_passwords | length > 0
49+
no_log: true
50+
51+
- name: If ptf_alt_passwords is not a list, log a debug message
52+
debug:
53+
msg: >-
54+
The 'ansible_altpasswords' field in group_vars/ptf/secrets.yml is not a list.
55+
Falling back to use the 'ansible_ssh_pass' field."
56+
when: ptf_alt_passwords | type_debug != "list"
57+
58+
- name: Update ptf username and password
59+
command: docker exec -t ptf_{{ vm_set_name }} sh -c 'echo "{{ ptf_username }}:{{ ptf_password }}" | chpasswd'
60+
become: yes
61+
no_log: true
62+
63+
when: ptf_secrets is defined and 'ansible_altpasswords' in ptf_secrets

0 commit comments

Comments
 (0)