Skip to content

Commit

Permalink
refactor: enable ssh-portal support by default
Browse files Browse the repository at this point in the history
  • Loading branch information
shreddedbacon committed Sep 6, 2023
1 parent e1bdcb5 commit cef1164
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 25 deletions.
53 changes: 30 additions & 23 deletions cmd/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
Expand All @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down
20 changes: 18 additions & 2 deletions cmd/ssh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ func Test_generateSSHConnectionString(t *testing.T) {
lagoon map[string]string
service string
container string
isPortal bool
}
tests := []struct {
name string
Expand Down Expand Up @@ -65,15 +66,30 @@ 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 {
t.Run(tt.name, func(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)
}
})
Expand Down

0 comments on commit cef1164

Please sign in to comment.