From 38cfffe66fb23c4f5199133567738598d4c56c19 Mon Sep 17 00:00:00 2001 From: Ben Jackson Date: Thu, 7 Sep 2023 12:56:52 +1000 Subject: [PATCH 1/4] refactor: enable ssh-portal support by default (#290) --- cmd/ssh.go | 53 ++++++++++++++++++++++++++++--------------------- cmd/ssh_test.go | 34 +++++++++++++++++++++++++++++-- 2 files changed, 62 insertions(+), 25 deletions(-) diff --git a/cmd/ssh.go b/cmd/ssh.go index 61896674..37b97350 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 -i %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..fb2adfa2 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,36 @@ 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 -i /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 -i /home/user/.ssh/my-key -p 22 example-com-main@lagoon.example.com service=cli container=cli`, + }, + { + name: "test6 - sshportal", + args: args{ + lagoon: map[string]string{ + "hostname": "lagoon.example.com", + "port": "22", + "username": "example-com-main", + }, + isPortal: true, + service: "cli", + container: "cli", + }, + want: `ssh -t -p 22 example-com-main@lagoon.example.com service=cli container=cli`, }, } for _, tt := range tests { @@ -73,7 +103,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) } }) From 31c24bccad2c8f314e30417161f663362e563ffb Mon Sep 17 00:00:00 2001 From: Chris Goodwin <40746380+CGoodwin90@users.noreply.github.com> Date: Thu, 7 Sep 2023 13:02:30 +1000 Subject: [PATCH 2/4] Included type validation in update environment command (#284) Co-authored-by: Ben Jackson --- cmd/environment.go | 8 +++++++- go.mod | 4 ++-- go.sum | 8 ++------ 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/cmd/environment.go b/cmd/environment.go index ecd113a3..f7dddd4b 100644 --- a/cmd/environment.go +++ b/cmd/environment.go @@ -3,6 +3,7 @@ package cmd import ( "context" "fmt" + s "github.com/uselagoon/machinery/api/schema" "os" "strings" @@ -13,7 +14,6 @@ import ( "github.com/uselagoon/lagoon-cli/pkg/output" l "github.com/uselagoon/machinery/api/lagoon" lclient "github.com/uselagoon/machinery/api/lagoon/client" - s "github.com/uselagoon/machinery/api/schema" ) // @TODO re-enable this at some point if more environment based commands are made available @@ -131,10 +131,16 @@ var updateEnvironmentCmd = &cobra.Command{ } if environmentType != "" { envType := s.EnvType(strings.ToUpper(environmentType)) + if validationErr := s.ValidateType(envType); validationErr != nil { + handleError(validationErr) + } environmentFlags.EnvironmentType = &envType } if deployT != "" { deployType := s.DeployType(strings.ToUpper(deployT)) + if validationErr := s.ValidateType(deployType); validationErr != nil { + handleError(validationErr) + } environmentFlags.DeployType = &deployType } diff --git a/go.mod b/go.mod index 07b84f04..4ec1069e 100644 --- a/go.mod +++ b/go.mod @@ -30,7 +30,7 @@ require ( github.com/guregu/null v4.0.0+incompatible // workaround for https://github.com/manifoldco/promptui/issues/98 github.com/nicksnyder/go-i18n v1.10.1 // indirect - github.com/uselagoon/machinery v0.0.10 + github.com/uselagoon/machinery v0.0.11 golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3 // indirect golang.org/x/sys v0.0.0-20220412211240-33da011f77ad // indirect gopkg.in/alecthomas/kingpin.v3-unstable v3.0.0-20191105091915-95d230a53780 // indirect @@ -41,4 +41,4 @@ replace github.com/olekukonko/tablewriter => github.com/shreddedbacon/tablewrite // replace github.com/machinebox/graphql => ../../shreddedbacon/graphql -// replace github.com/olekukonko/tablewriter => ../../shreddedbacon/tablewriter +// replace github.com/olekukonko/tablewriter => ../../shreddedbacon/tablewriter \ No newline at end of file diff --git a/go.sum b/go.sum index 448aa306..fe18d60a 100644 --- a/go.sum +++ b/go.sum @@ -105,12 +105,8 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf github.com/tsenart/deadcode v0.0.0-20160724212837-210d2dc333e9 h1:vY5WqiEon0ZSTGM3ayVVi+twaHKHDFUVloaQ/wug9/c= github.com/tsenart/deadcode v0.0.0-20160724212837-210d2dc333e9/go.mod h1:q+QjxYvZ+fpjMXqs+XEriussHjSYqeXVnAdSV1tkMYk= github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0= -github.com/uselagoon/machinery v0.0.8 h1:PDPjBOoQGcmDDFsdibVN/iNkvWo3LJd1Yj+jOuw2bBg= -github.com/uselagoon/machinery v0.0.8/go.mod h1:dGAvkxWQkRu/eLQeWLrTl5HvNoFsiXlXInCptHUxzUw= -github.com/uselagoon/machinery v0.0.9-0.20230710124508-ef420b365811 h1:OUujMdzHe71+7l/GlS4VRyvehatO/jTT9+EIfbr+gwc= -github.com/uselagoon/machinery v0.0.9-0.20230710124508-ef420b365811/go.mod h1:IXLxlkahEAEgpCmu9Xa/Wmjo6ja4Aoq7tf8G7VrileE= -github.com/uselagoon/machinery v0.0.10 h1:pR+2iwXod0XnnyeZsJMlmkSU5BV8cYI65JNoaLwUVbQ= -github.com/uselagoon/machinery v0.0.10/go.mod h1:IXLxlkahEAEgpCmu9Xa/Wmjo6ja4Aoq7tf8G7VrileE= +github.com/uselagoon/machinery v0.0.11 h1:s6EhyU/pj1+C4FdS0EqmR6C0dLsoeCd9n+5xHL1YDag= +github.com/uselagoon/machinery v0.0.11/go.mod h1:IXLxlkahEAEgpCmu9Xa/Wmjo6ja4Aoq7tf8G7VrileE= github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= From f5b467e262806e3c257da0b1150651ae28cf2c5e Mon Sep 17 00:00:00 2001 From: Chris Goodwin <40746380+CGoodwin90@users.noreply.github.com> Date: Mon, 30 Oct 2023 13:36:14 +1100 Subject: [PATCH 3/4] Change: Updates the output from an `Error` to `Stderr` when listing variables where none are set (#298) --- cmd/list.go | 14 +++++++------- pkg/output/main.go | 4 ++++ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/cmd/list.go b/cmd/list.go index efbcbbad..f71c71b3 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -290,13 +290,6 @@ var listVariablesCmd = &cobra.Command{ } data = append(data, env) } - if len(data) == 0 { - if cmdProjectEnvironment != "" { - return fmt.Errorf("There are no variables for environment '%s' in project '%s'", cmdProjectEnvironment, cmdProjectName) - } else { - return fmt.Errorf("There are no variables for project '%s'", cmdProjectName) - } - } header := []string{ "ID", "Project", @@ -309,6 +302,13 @@ var listVariablesCmd = &cobra.Command{ if reveal { header = append(header, "Value") } + if len(data) == 0 { + if cmdProjectEnvironment != "" { + outputOptions.Error = fmt.Sprintf("There are no variables for environment '%s' in project '%s'", cmdProjectEnvironment, cmdProjectName) + } else { + outputOptions.Error = fmt.Sprintf("There are no variables for project '%s'\n", cmdProjectName) + } + } output.RenderOutput(output.Table{ Header: header, Data: data, diff --git a/pkg/output/main.go b/pkg/output/main.go index 756bd69c..05ff549e 100644 --- a/pkg/output/main.go +++ b/pkg/output/main.go @@ -26,6 +26,7 @@ type Options struct { JSON bool Pretty bool Debug bool + Error string } // Result . @@ -125,6 +126,9 @@ func RenderOutput(data Table, opts Options) { RenderJSON(returnedData, opts) } else { // otherwise render a table + if opts.Error != "" { + os.Stderr.WriteString(opts.Error) + } table := tablewriter.NewWriter(os.Stdout) opts.Header = !opts.Header if opts.Header { From 5ce8a2a223b31f3be37832e6aa254ecfd68641ea Mon Sep 17 00:00:00 2001 From: Ben Jackson Date: Mon, 30 Oct 2023 13:49:19 +1100 Subject: [PATCH 4/4] fix: if value of variable is empty, dont replace it with - (#299) --- cmd/list.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/list.go b/cmd/list.go index f71c71b3..6c2a23ef 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -286,7 +286,7 @@ var listVariablesCmd = &cobra.Command{ env = append(env, returnNonEmptyString(fmt.Sprintf("%v", envvar.Scope))) env = append(env, returnNonEmptyString(fmt.Sprintf("%v", envvar.Name))) if reveal { - env = append(env, returnNonEmptyString(fmt.Sprintf("%v", envvar.Value))) + env = append(env, fmt.Sprintf("%v", envvar.Value)) } data = append(data, env) }