Skip to content

Commit

Permalink
test: re-use admin ssh key setup across machines
Browse files Browse the repository at this point in the history
This was copy'd twice in Cockpit tests and will also come in useful in
the cockpit-machines multi machine tests.
  • Loading branch information
jelly authored and martinpitt committed Nov 28, 2023
1 parent 4748180 commit e82aa59
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 28 deletions.
17 changes: 17 additions & 0 deletions test/common/testlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -2160,6 +2160,23 @@ def setup_provisioned_hosts(self, disable_preload: bool = False):
if disable_preload:
self.disable_preload("packagekit", "playground", "systemd", machine=m)

def authorize_pubkey(self, machine, account, pubkey):
machine.execute(f"a={account} d=/home/$a/.ssh; mkdir -p $d; chown $a:$a $d; chmod 700 $d")
machine.write(f"/home/{account}/.ssh/authorized_keys", pubkey)
machine.execute(f"a={account}; chown $a:$a /home/$a/.ssh/authorized_keys")

def get_pubkey(self, machine, account):
return machine.execute(f"cat /home/{account}/.ssh/id_rsa.pub")

def setup_ssh_auth(self):
self.machine.execute("d=/home/admin/.ssh; mkdir -p $d; chown admin:admin $d; chmod 700 $d")
self.machine.execute("test -f /home/admin/.ssh/id_rsa || ssh-keygen -f /home/admin/.ssh/id_rsa -t rsa -N ''")
self.machine.execute("chown admin:admin /home/admin/.ssh/id_rsa*")
pubkey = self.get_pubkey(self.machine, "admin")

for m in self.machines:
self.authorize_pubkey(self.machines[m], "admin", pubkey)


###########################
# Global helper functions
Expand Down
17 changes: 0 additions & 17 deletions test/verify/check-shell-host-switching
Original file line number Diff line number Diff line change
Expand Up @@ -82,23 +82,6 @@ class HostSwitcherHelpers:
b.click("a[href='/@localhost']")
b.click("#hosts-sel button")

def get_pubkey(self, machine, account):
return machine.execute(f"cat /home/{account}/.ssh/id_rsa.pub")

def authorize_pubkey(self, machine, account, pubkey):
machine.execute(f"a={account} d=/home/$a/.ssh; mkdir -p $d; chown $a:$a $d; chmod 700 $d")
machine.write(f"/home/{account}/.ssh/authorized_keys", pubkey)
machine.execute(f"a={account}; chown $a:$a /home/$a/.ssh/authorized_keys")

def setup_ssh_auth(self):
self.machine.execute("d=/home/admin/.ssh; mkdir -p $d; chown admin:admin $d; chmod 700 $d")
self.machine.execute("test -f /home/admin/.ssh/id_rsa || ssh-keygen -f /home/admin/.ssh/id_rsa -t rsa -N ''")
self.machine.execute("chown admin:admin /home/admin/.ssh/id_rsa*")
pubkey = self.get_pubkey(self.machine, "admin")

for m in self.machines:
self.authorize_pubkey(self.machines[m], "admin", pubkey)


@testlib.skipDistroPackage()
@testlib.todoPybridgeRHEL8()
Expand Down
11 changes: 0 additions & 11 deletions test/verify/check-shell-multi-machine
Original file line number Diff line number Diff line change
Expand Up @@ -146,17 +146,6 @@ class TestMultiMachineAdd(testlib.MachineCase):
"machine3": {"address": "10.111.113.3/20", "memory_mb": 660},
}

def setup_ssh_auth(self):
self.machine.execute("d=/home/admin/.ssh; mkdir -p $d; chown admin:admin $d; chmod 700 $d")
self.machine.execute("test -f /home/admin/.ssh/id_rsa || ssh-keygen -f /home/admin/.ssh/id_rsa -t rsa -N ''")
self.machine.execute("chown admin:admin /home/admin/.ssh/id_rsa*")
pubkey = self.machine.execute("cat /home/admin/.ssh/id_rsa.pub")

for m in self.machines:
self.machines[m].execute("d=/home/admin/.ssh; mkdir -p $d; chown admin:admin $d; chmod 700 $d")
self.machines[m].write("/home/admin/.ssh/authorized_keys", pubkey)
self.machines[m].execute("chown admin:admin /home/admin/.ssh/authorized_keys")

def setUp(self):
super().setUp()
self.machine2 = self.machines['machine2']
Expand Down

0 comments on commit e82aa59

Please sign in to comment.