44 "context"
55 "fmt"
66 "os"
7- "path"
87 "time"
98
109 "github.com/spf13/cobra"
@@ -13,7 +12,6 @@ import (
1312 "github.com/uselagoon/machinery/api/lagoon"
1413 lclient "github.com/uselagoon/machinery/api/lagoon/client"
1514 "golang.org/x/crypto/ssh"
16- "golang.org/x/crypto/ssh/knownhosts"
1715)
1816
1917var (
@@ -58,12 +56,13 @@ func generateLogsCommand(service, container string, lines uint,
5856 return argv , nil
5957}
6058
61- func getSSHHostPort (environmentName string , debug bool ) (string , string , error ) {
59+ func getSSHHostPort (environmentName string , debug bool ) (string , string , bool , error ) {
6260 current := lagoonCLIConfig .Current
6361 // set the default ssh host and port to the core ssh endpoint
6462 sshHost := lagoonCLIConfig .Lagoons [current ].HostName
6563 sshPort := lagoonCLIConfig .Lagoons [current ].Port
6664 token := lagoonCLIConfig .Lagoons [current ].Token
65+ portal := false
6766
6867 // get SSH Portal endpoint if required
6968 lc := lclient .New (
@@ -76,7 +75,7 @@ func getSSHHostPort(environmentName string, debug bool) (string, string, error)
7675 defer cancel ()
7776 project , err := lagoon .GetSSHEndpointsByProject (ctx , cmdProjectName , lc )
7877 if err != nil {
79- return "" , "" , fmt .Errorf ("couldn't get SSH endpoint by project: %v" , err )
78+ return "" , "" , portal , fmt .Errorf ("couldn't get SSH endpoint by project: %v" , err )
8079 }
8180 // check all the environments for this project
8281 for _ , env := range project .Environments {
@@ -86,13 +85,14 @@ func getSSHHostPort(environmentName string, debug bool) (string, string, error)
8685 if env .DeployTarget .SSHHost != "" && env .DeployTarget .SSHPort != "" {
8786 sshHost = env .DeployTarget .SSHHost
8887 sshPort = env .DeployTarget .SSHPort
88+ portal = true
8989 }
9090 }
9191 }
92- return sshHost , sshPort , nil
92+ return sshHost , sshPort , portal , nil
9393}
9494
95- func getSSHClientConfig (environmentName string ) (* ssh.ClientConfig ,
95+ func getSSHClientConfig (environmentName , host string , ignoreHostKey , acceptNewHostKey bool ) (* ssh.ClientConfig ,
9696 func () error , error ) {
9797 skipAgent := false
9898 privateKey := fmt .Sprintf ("%s/.ssh/id_rsa" , userPath )
@@ -107,17 +107,19 @@ func getSSHClientConfig(environmentName string) (*ssh.ClientConfig,
107107 skipAgent = true
108108 }
109109 // parse known_hosts
110- kh , err := knownhosts . New ( path . Join ( userPath , ".ssh/known_hosts" ) )
110+ hkcb , hkalgo , err := lagoonssh . InteractiveKnownHosts ( userPath , host , ignoreHostKey , acceptNewHostKey )
111111 if err != nil {
112112 return nil , nil , fmt .Errorf ("couldn't get ~/.ssh/known_hosts: %v" , err )
113113 }
114+
114115 // configure an SSH client session
115116 authMethod , closeSSHAgent := publicKey (privateKey , cmdPubkeyIdentity , lagoonCLIConfig .Lagoons [lagoonCLIConfig .Current ].PublicKeyIdentities , skipAgent )
116117 return & ssh.ClientConfig {
117- User : cmdProjectName + "-" + environmentName ,
118- Auth : []ssh.AuthMethod {authMethod },
119- HostKeyCallback : kh ,
120- Timeout : connTimeout ,
118+ User : cmdProjectName + "-" + environmentName ,
119+ Auth : []ssh.AuthMethod {authMethod },
120+ HostKeyCallback : hkcb ,
121+ HostKeyAlgorithms : hkalgo ,
122+ Timeout : connTimeout ,
121123 }, closeSSHAgent , nil
122124}
123125
@@ -136,6 +138,7 @@ var logsCmd = &cobra.Command{
136138 if err != nil {
137139 return fmt .Errorf ("couldn't get debug value: %v" , err )
138140 }
141+ ignoreHostKey , acceptNewHostKey := lagoonssh .CheckStrictHostKey (strictHostKeyCheck )
139142 argv , err := generateLogsCommand (logsService , logsContainer , logsTailLines ,
140143 logsFollow )
141144 if err != nil {
@@ -145,12 +148,12 @@ var logsCmd = &cobra.Command{
145148 environmentName := makeSafe (
146149 shortenEnvironment (cmdProjectName , cmdProjectEnvironment ))
147150 // query the Lagoon API for the environment's SSH endpoint
148- sshHost , sshPort , err := getSSHHostPort (environmentName , debug )
151+ sshHost , sshPort , _ , err := getSSHHostPort (environmentName , debug )
149152 if err != nil {
150153 return fmt .Errorf ("couldn't get SSH endpoint: %v" , err )
151154 }
152155 // configure SSH client session
153- sshConfig , closeSSHAgent , err := getSSHClientConfig (environmentName )
156+ sshConfig , closeSSHAgent , err := getSSHClientConfig (environmentName , fmt . Sprintf ( "%s:%s" , sshHost , sshPort ), ignoreHostKey , acceptNewHostKey )
154157 if err != nil {
155158 return fmt .Errorf ("couldn't get SSH client config: %v" , err )
156159 }
0 commit comments