@@ -18,7 +18,7 @@ import (
1818)
1919
2020func main () {
21- team , remainingArgs , action , err := handleArgs (os .Args [1 :])
21+ team , remainingArgs , action , runtimeConfig , err := handleArgs (os .Args [1 :])
2222 if err != nil {
2323 fmt .Printf ("Failed to parse arguments: %v\n " , err )
2424 os .Exit (1 )
@@ -29,31 +29,32 @@ func main() {
2929 os .Exit (1 )
3030 }
3131 if isValidCert (keyPath ) {
32- doAction (action , keyPath , remainingArgs )
32+ kssh .DebugLog (runtimeConfig , "Reusing unexpired certificate" )
33+ doAction (runtimeConfig , action , keyPath , remainingArgs )
3334 }
3435 config , err := getConfig (team )
3536 if err != nil {
3637 fmt .Printf ("%v\n " , err )
3738 os .Exit (1 )
3839 }
39- err = provisionNewKey (config , keyPath )
40+ err = provisionNewKey (runtimeConfig , config , keyPath )
4041 if err != nil {
4142 fmt .Printf ("%v\n " , err )
4243 os .Exit (1 )
4344 }
44- doAction (action , keyPath , remainingArgs )
45+ doAction (runtimeConfig , action , keyPath , remainingArgs )
4546}
4647
47- func doAction (action Action , keyPath string , remainingArgs []string ) {
48+ func doAction (runtimeConfig kssh. RuntimeConfig , action Action , keyPath string , remainingArgs []string ) {
4849 if action == SSH {
49- runSSHWithKey (keyPath , remainingArgs )
50+ runSSHWithKey (runtimeConfig , keyPath , remainingArgs )
5051 } else if action == Provision {
5152 err := kssh .AddKeyToSSHAgent (keyPath )
5253 if err != nil {
5354 fmt .Printf ("%v\n " , err )
5455 os .Exit (1 )
5556 }
56- fmt . Printf ( "Provisioned new SSH key at %s\n " , keyPath )
57+ kssh . DebugLog ( runtimeConfig , "Provisioned new SSH key at %s\n " , keyPath )
5758 }
5859}
5960
@@ -80,6 +81,7 @@ var cliArguments = []kssh.CLIArgument{
8081 {Name : "--set-default-user" , HasArgument : true },
8182 {Name : "--clear-default-user" , HasArgument : false },
8283 {Name : "--help" , HasArgument : false },
84+ {Name : "-v" , HasArgument : false , Preserve : true },
8385}
8486
8587func generateHelpPage () string {
@@ -93,7 +95,8 @@ VERSION:
9395 0.0.1
9496
9597GLOBAL OPTIONS:
96- --help, Show help
98+ --help Show help
99+ -v Enable kssh and ssh debug logs
97100 --provision Provision a new SSH key and add it to the ssh-agent. Useful if you need to run another
98101 program that uses SSH auth (eg scp, rsync, etc)
99102 --set-default-bot Set the default bot to be used for kssh. Not necessary if you are only in one team that
@@ -113,14 +116,15 @@ const (
113116 SSH
114117)
115118
116- // Returns botname, remaining arguments, action, error
119+ // Returns botname, remaining arguments, action, runtimeConfig, error
117120// If the argument requires exiting after processing, it will call os.Exit
118- func handleArgs (args []string ) (string , []string , Action , error ) {
121+ func handleArgs (args []string ) (string , []string , Action , kssh. RuntimeConfig , error ) {
119122 remaining , found , err := kssh .ParseArgs (args , cliArguments )
120123 if err != nil {
121- return "" , nil , 0 , fmt .Errorf ("Failed to parse provided arguments: %v" , err )
124+ return "" , nil , 0 , kssh. RuntimeConfig {}, fmt .Errorf ("Failed to parse provided arguments: %v" , err )
122125 }
123126
127+ debug := false
124128 team := ""
125129 action := SSH
126130 for _ , arg := range found {
@@ -171,8 +175,11 @@ func handleArgs(args []string) (string, []string, Action, error) {
171175 fmt .Println (generateHelpPage ())
172176 os .Exit (0 )
173177 }
178+ if arg .Argument .Name == "-v" {
179+ debug = true
180+ }
174181 }
175- return team , remaining , action , nil
182+ return team , remaining , action , kssh. RuntimeConfig { Debug : debug }, nil
176183}
177184
178185// Get the kssh.ConfigFile. botname is the team specified via --bot if one was specified, otherwise the empty string
@@ -248,8 +255,8 @@ func isValidCert(keyPath string) bool {
248255}
249256
250257// Provision a new signed SSH key with the given config
251- func provisionNewKey (config kssh.ConfigFile , keyPath string ) error {
252- fmt . Println ( "Generating a new SSH key..." )
258+ func provisionNewKey (runtimeConfig kssh. RuntimeConfig , config kssh.ConfigFile , keyPath string ) error {
259+ kssh . DebugLog ( runtimeConfig , "Generating a new SSH key..." )
253260 err := sshutils .GenerateNewSSHKey (keyPath , true , false )
254261 if err != nil {
255262 return fmt .Errorf ("Failed to generate a new SSH key: %v" , err )
@@ -264,13 +271,15 @@ func provisionNewKey(config kssh.ConfigFile, keyPath string) error {
264271 return fmt .Errorf ("Failed to generate a new UUID for the SignatureRequest: %v" , err )
265272 }
266273
274+ kssh .DebugLog (runtimeConfig , "Requesting signature from the CA...." )
267275 resp , err := kssh .GetSignedKey (config , shared.SignatureRequest {
268276 UUID : randomUUID .String (),
269277 SSHPublicKey : string (pubKey ),
270278 })
271279 if err != nil {
272280 return fmt .Errorf ("Failed to get a signed key from the CA: %v" , err )
273281 }
282+ kssh .DebugLog (runtimeConfig , "Received signature from the CA!" )
274283
275284 err = ioutil .WriteFile (shared .KeyPathToCert (keyPath ), []byte (resp .SignedKey ), 0600 )
276285 if err != nil {
@@ -281,7 +290,7 @@ func provisionNewKey(config kssh.ConfigFile, keyPath string) error {
281290}
282291
283292// Run SSH with the given key. Calls os.Exit and does not return.
284- func runSSHWithKey (keyPath string , remainingArgs []string ) {
293+ func runSSHWithKey (runtimeConfig kssh. RuntimeConfig , keyPath string , remainingArgs []string ) {
285294 // Determine whether a default SSH user has been specified and configure it if so
286295 useConfig := false
287296 user , err := kssh .GetDefaultSSHUser ()
@@ -305,13 +314,11 @@ func runSSHWithKey(keyPath string, remainingArgs []string) {
305314 os .Exit (1 )
306315 }
307316
308- // A new line to separate kssh output from ssh output
309- fmt .Printf ("\n " )
310-
311317 argumentList := []string {"-i" , keyPath , "-o" , "IdentitiesOnly=yes" }
312318 checkAndWarnOnUnspecifiedBehavior (useConfig , remainingArgs )
313319 if useConfig {
314320 argumentList = append (argumentList , "-F" , kssh .AlternateSSHConfigFile )
321+ kssh .DebugLog (runtimeConfig , "Using default ssh user %s" , user )
315322 }
316323
317324 argumentList = append (argumentList , remainingArgs ... )
0 commit comments