From cef1164b55379be26acc531a0cef6aae80d6ef5c Mon Sep 17 00:00:00 2001 From: shreddedbacon Date: Thu, 7 Sep 2023 08:04:09 +1000 Subject: [PATCH] refactor: enable ssh-portal support by default --- cmd/ssh.go | 53 ++++++++++++++++++++++++++++--------------------- cmd/ssh_test.go | 20 +++++++++++++++++-- 2 files changed, 48 insertions(+), 25 deletions(-) diff --git a/cmd/ssh.go b/cmd/ssh.go index 61896674..eadcd3d8 100644 --- a/cmd/ssh.go +++ b/cmd/ssh.go @@ -41,28 +41,28 @@ var sshEnvCmd = &cobra.Command{ // set the default ssh host and port to the core ssh endpoint sshHost := lagoonCLIConfig.Lagoons[current].HostName sshPort := lagoonCLIConfig.Lagoons[current].Port + isPortal := false // if the config for this lagoon is set to use ssh portal support, handle that here - if lagoonCLIConfig.Lagoons[current].SSHPortal { - lc := client.New( - lagoonCLIConfig.Lagoons[current].GraphQL, - lagoonCLIConfig.Lagoons[current].Token, - lagoonCLIConfig.Lagoons[current].Version, - lagoonCLIVersion, - debug) - project, err := lagoon.GetSSHEndpointsByProject(context.TODO(), cmdProjectName, lc) - if err != nil { - return err - } - // check all the environments for this project - for _, env := range project.Environments { - // if the env name matches the requested environment then check if the deploytarget supports regional ssh endpoints - if env.Name == environmentName { - // if the deploytarget supports regional endpoints, then set these as the host and port for ssh - if env.DeployTarget.SSHHost != "" && env.DeployTarget.SSHPort != "" { - sshHost = env.DeployTarget.SSHHost - sshPort = env.DeployTarget.SSHPort - } + lc := client.New( + lagoonCLIConfig.Lagoons[current].GraphQL, + lagoonCLIConfig.Lagoons[current].Token, + lagoonCLIConfig.Lagoons[current].Version, + lagoonCLIVersion, + debug) + project, err := lagoon.GetSSHEndpointsByProject(context.TODO(), cmdProjectName, lc) + if err != nil { + return err + } + // check all the environments for this project + for _, env := range project.Environments { + // if the env name matches the requested environment then check if the deploytarget supports regional ssh endpoints + if env.Name == environmentName { + // if the deploytarget supports regional endpoints, then set these as the host and port for ssh + if env.DeployTarget.SSHHost != "" && env.DeployTarget.SSHPort != "" { + sshHost = env.DeployTarget.SSHHost + sshPort = env.DeployTarget.SSHPort + isPortal = true } } } @@ -88,7 +88,7 @@ var sshEnvCmd = &cobra.Command{ "sshkey": privateKey, } if sshConnString { - fmt.Println(generateSSHConnectionString(sshConfig, sshService, sshContainer)) + fmt.Println(generateSSHConnectionString(sshConfig, sshService, sshContainer, isPortal)) } else { // start an interactive ssh session @@ -127,8 +127,15 @@ func init() { } // generateSSHConnectionString . -func generateSSHConnectionString(lagoon map[string]string, service string, container string) string { - connString := fmt.Sprintf("ssh -t %s-o \"UserKnownHostsFile=/dev/null\" -o \"StrictHostKeyChecking=no\" -p %v %s@%s", lagoon["sshKey"], lagoon["port"], lagoon["username"], lagoon["hostname"]) +func generateSSHConnectionString(lagoon map[string]string, service string, container string, isPortal bool) string { + connString := fmt.Sprintf("ssh -t") + if lagoon["sshKey"] != "" { + connString = fmt.Sprintf("%s %s", connString, lagoon["sshKey"]) + } + if !isPortal { + connString = fmt.Sprintf("%s -o \"UserKnownHostsFile=/dev/null\" -o \"StrictHostKeyChecking=no\"", connString) + } + connString = fmt.Sprintf("%s -p %v %s@%s", connString, lagoon["port"], lagoon["username"], lagoon["hostname"]) if service != "" { connString = fmt.Sprintf("%s service=%s", connString, service) } diff --git a/cmd/ssh_test.go b/cmd/ssh_test.go index e062f5df..3e30f6c8 100644 --- a/cmd/ssh_test.go +++ b/cmd/ssh_test.go @@ -11,6 +11,7 @@ func Test_generateSSHConnectionString(t *testing.T) { lagoon map[string]string service string container string + isPortal bool } tests := []struct { name string @@ -65,7 +66,22 @@ func Test_generateSSHConnectionString(t *testing.T) { service: "cli", container: "cli", }, - want: `ssh -t /home/user/.ssh/my-key-o "UserKnownHostsFile=/dev/null" -o "StrictHostKeyChecking=no" -p 22 example-com-main@lagoon.example.com service=cli container=cli`, + want: `ssh -t /home/user/.ssh/my-key -o "UserKnownHostsFile=/dev/null" -o "StrictHostKeyChecking=no" -p 22 example-com-main@lagoon.example.com service=cli container=cli`, + }, + { + name: "test5 - sshportal", + args: args{ + lagoon: map[string]string{ + "hostname": "lagoon.example.com", + "port": "22", + "username": "example-com-main", + "sshKey": "/home/user/.ssh/my-key", + }, + isPortal: true, + service: "cli", + container: "cli", + }, + want: `ssh -t /home/user/.ssh/my-key -p 22 example-com-main@lagoon.example.com service=cli container=cli`, }, } for _, tt := range tests { @@ -73,7 +89,7 @@ func Test_generateSSHConnectionString(t *testing.T) { cmdProjectName = tt.args.project cmdProjectEnvironment = tt.args.environment - if got := generateSSHConnectionString(tt.args.lagoon, tt.args.service, tt.args.container); got != tt.want { + if got := generateSSHConnectionString(tt.args.lagoon, tt.args.service, tt.args.container, tt.args.isPortal); got != tt.want { t.Errorf("generateSSHConnectionString() = %v, want %v", got, tt.want) } })