From 305e8164d698a5fc4b3475ce4ebac89f278ff47b Mon Sep 17 00:00:00 2001 From: Boshi Lian Date: Thu, 30 May 2024 11:25:35 -0700 Subject: [PATCH] feat: Add test case for publickey_simple_withmultiple_keyfile (#396) This commit adds a new test case to cover the scenario where multiple key files are used for authentication with the publickey_simple user. It generates a temporary key file and attempts to SSH to the piper server using both the correct and incorrect key files. The test verifies that the correct key file allows the connection and writes a random text to a shared file. --- e2e/yaml_test.go | 51 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/e2e/yaml_test.go b/e2e/yaml_test.go index d3304e20..439eb1b9 100644 --- a/e2e/yaml_test.go +++ b/e2e/yaml_test.go @@ -289,4 +289,55 @@ func TestYaml(t *testing.T) { checkSharedFileContent(t, targetfie, randtext) }) + t.Run("publickey_simple_withmultiple_keyfile", func(t *testing.T) { + randtext := uuid.New().String() + targetfie := uuid.New().String() + + wrongkeydir, err := os.MkdirTemp("", "") + if err != nil { + t.Errorf("failed to create temp key file: %v", err) + } + + wrongkeyfile := path.Join(wrongkeydir, "key") + + if err := runCmdAndWait( + "ssh-keygen", + "-N", + "", + "-f", + wrongkeyfile, + ); err != nil { + t.Errorf("failed to generate key: %v", err) + } + + c, _, _, err := runCmd( + "ssh", + "-v", + "-o", + "StrictHostKeyChecking=no", + "-o", + "UserKnownHostsFile=/dev/null", + "-p", + piperport, + "-l", + "publickey_simple", + "-i", + wrongkeyfile, + "-i", + path.Join(yamldir, "id_rsa_simple"), + "127.0.0.1", + fmt.Sprintf(`sh -c "echo -n %v > /shared/%v"`, randtext, targetfie), + ) + + if err != nil { + t.Errorf("failed to ssh to piper, %v", err) + } + + defer killCmd(c) + + time.Sleep(time.Second) // wait for file flush + + checkSharedFileContent(t, targetfie, randtext) + }) + }