Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: enable ssh-portal support by default #290

Merged
merged 1 commit into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 -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)
}
Expand Down
34 changes: 32 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,44 @@ 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 {
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