@@ -13,12 +13,14 @@ import (
1313 "github.com/google/uuid"
1414 "github.com/keybase/bot-ssh-ca/src/kssh"
1515 "github.com/keybase/bot-ssh-ca/src/shared"
16+ log "github.com/sirupsen/logrus"
1617
1718 "golang.org/x/crypto/ssh"
1819)
1920
2021func main () {
21- team , remainingArgs , action , runtimeConfig , err := handleArgs (os .Args [1 :])
22+ kssh .InitLogging ()
23+ team , remainingArgs , action , err := handleArgs (os .Args [1 :])
2224 if err != nil {
2325 fmt .Printf ("Failed to parse arguments: %v\n " , err )
2426 os .Exit (1 )
@@ -29,32 +31,33 @@ func main() {
2931 os .Exit (1 )
3032 }
3133 if isValidCert (keyPath ) {
32- kssh .DebugLog (runtimeConfig , "Reusing unexpired certificate" )
33- doAction (runtimeConfig , action , keyPath , remainingArgs )
34+ log .WithField ("keyPath" , keyPath ).Debug ("Reusing unexpired certificate" )
35+ doAction (action , keyPath , remainingArgs )
36+ os .Exit (0 )
3437 }
3538 config , err := getConfig (team )
3639 if err != nil {
3740 fmt .Printf ("%v\n " , err )
3841 os .Exit (1 )
3942 }
40- err = provisionNewKey (runtimeConfig , config , keyPath )
43+ err = provisionNewKey (config , keyPath )
4144 if err != nil {
4245 fmt .Printf ("%v\n " , err )
4346 os .Exit (1 )
4447 }
45- doAction (runtimeConfig , action , keyPath , remainingArgs )
48+ doAction (action , keyPath , remainingArgs )
4649}
4750
48- func doAction (runtimeConfig kssh. RuntimeConfig , action Action , keyPath string , remainingArgs []string ) {
51+ func doAction (action Action , keyPath string , remainingArgs []string ) {
4952 if action == SSH {
50- runSSHWithKey (runtimeConfig , keyPath , remainingArgs )
53+ runSSHWithKey (keyPath , remainingArgs )
5154 } else if action == Provision {
5255 err := kssh .AddKeyToSSHAgent (keyPath )
5356 if err != nil {
5457 fmt .Printf ("%v\n " , err )
5558 os .Exit (1 )
5659 }
57- kssh . DebugLog ( runtimeConfig , "Provisioned new SSH key at %s\n " , keyPath )
60+ fmt . Printf ( "Provisioned new SSH key at %s\n " , keyPath )
5861 }
5962}
6063
@@ -116,15 +119,14 @@ const (
116119 SSH
117120)
118121
119- // Returns botname, remaining arguments, action, runtimeConfig, error
122+ // Returns botname, remaining arguments, action, error
120123// If the argument requires exiting after processing, it will call os.Exit
121- func handleArgs (args []string ) (string , []string , Action , kssh. RuntimeConfig , error ) {
124+ func handleArgs (args []string ) (string , []string , Action , error ) {
122125 remaining , found , err := kssh .ParseArgs (args , cliArguments )
123126 if err != nil {
124- return "" , nil , 0 , kssh. RuntimeConfig {}, fmt .Errorf ("Failed to parse provided arguments: %v" , err )
127+ return "" , nil , 0 , fmt .Errorf ("Failed to parse provided arguments: %v" , err )
125128 }
126129
127- debug := false
128130 team := ""
129131 action := SSH
130132 for _ , arg := range found {
@@ -176,10 +178,10 @@ func handleArgs(args []string) (string, []string, Action, kssh.RuntimeConfig, er
176178 os .Exit (0 )
177179 }
178180 if arg .Argument .Name == "-v" {
179- debug = true
181+ log . SetLevel ( log . DebugLevel )
180182 }
181183 }
182- return team , remaining , action , kssh. RuntimeConfig { Debug : debug }, nil
184+ return team , remaining , action , nil
183185}
184186
185187// Get the kssh.ConfigFile. botname is the team specified via --bot if one was specified, otherwise the empty string
@@ -255,8 +257,8 @@ func isValidCert(keyPath string) bool {
255257}
256258
257259// Provision a new signed SSH key with the given config
258- func provisionNewKey (runtimeConfig kssh. RuntimeConfig , config kssh.ConfigFile , keyPath string ) error {
259- kssh . DebugLog ( runtimeConfig , "Generating a new SSH key..." )
260+ func provisionNewKey (config kssh.ConfigFile , keyPath string ) error {
261+ log . Debug ( "Generating a new SSH key..." )
260262 err := sshutils .GenerateNewSSHKey (keyPath , true , false )
261263 if err != nil {
262264 return fmt .Errorf ("Failed to generate a new SSH key: %v" , err )
@@ -271,15 +273,15 @@ func provisionNewKey(runtimeConfig kssh.RuntimeConfig, config kssh.ConfigFile, k
271273 return fmt .Errorf ("Failed to generate a new UUID for the SignatureRequest: %v" , err )
272274 }
273275
274- kssh . DebugLog ( runtimeConfig , "Requesting signature from the CA...." )
276+ log . Debug ( "Requesting signature from the CA...." )
275277 resp , err := kssh .GetSignedKey (config , shared.SignatureRequest {
276278 UUID : randomUUID .String (),
277279 SSHPublicKey : string (pubKey ),
278280 })
279281 if err != nil {
280282 return fmt .Errorf ("Failed to get a signed key from the CA: %v" , err )
281283 }
282- kssh . DebugLog ( runtimeConfig , "Received signature from the CA!" )
284+ log . Debug ( "Received signature from the CA!" )
283285
284286 err = ioutil .WriteFile (shared .KeyPathToCert (keyPath ), []byte (resp .SignedKey ), 0600 )
285287 if err != nil {
@@ -290,7 +292,7 @@ func provisionNewKey(runtimeConfig kssh.RuntimeConfig, config kssh.ConfigFile, k
290292}
291293
292294// Run SSH with the given key. Calls os.Exit and does not return.
293- func runSSHWithKey (runtimeConfig kssh. RuntimeConfig , keyPath string , remainingArgs []string ) {
295+ func runSSHWithKey (keyPath string , remainingArgs []string ) {
294296 // Determine whether a default SSH user has been specified and configure it if so
295297 useConfig := false
296298 user , err := kssh .GetDefaultSSHUser ()
@@ -318,7 +320,7 @@ func runSSHWithKey(runtimeConfig kssh.RuntimeConfig, keyPath string, remainingAr
318320 checkAndWarnOnUnspecifiedBehavior (useConfig , remainingArgs )
319321 if useConfig {
320322 argumentList = append (argumentList , "-F" , kssh .AlternateSSHConfigFile )
321- kssh . DebugLog ( runtimeConfig , "Using default ssh user %s" , user )
323+ log . WithField ( "user" , user ). Debug ( "Using default ssh user" )
322324 }
323325
324326 argumentList = append (argumentList , remainingArgs ... )
@@ -340,7 +342,7 @@ func checkAndWarnOnUnspecifiedBehavior(useConfig bool, arguments []string) {
340342 if useConfig {
341343 for _ , arg := range arguments {
342344 if arg == "-F" {
343- fmt . Println ("Warning: You passed a -F flag, but kssh also uses this argument in " +
345+ log . Warn ("Warning: You passed a -F flag, but kssh also uses this argument in " +
344346 "order to implement support for a default SSH username, which you're also using. " +
345347 "Either do not use the -F flag or run `kssh --clear-default-user` to reset the " +
346348 "default SSH user and delegate this to the running CA bot." )
0 commit comments