Skip to content
This repository was archived by the owner on Jan 22, 2025. It is now read-only.

Commit e5bea29

Browse files
committed
Switch to using logrus for logging with levels
1 parent 4594faa commit e5bea29

7 files changed

Lines changed: 50 additions & 42 deletions

File tree

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.12
55
require (
66
github.com/google/uuid v1.1.1
77
github.com/keybase/go-keybase-chat-bot v0.0.0-20190812134859-bc54fd9cf83b
8+
github.com/sirupsen/logrus v1.4.2
89
github.com/stretchr/testify v1.3.0
910
github.com/urfave/cli v1.21.0
1011
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4

go.sum

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
22
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
33
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
5+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
46
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
57
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
68
github.com/keybase/go-keybase-chat-bot v0.0.0-20190812134859-bc54fd9cf83b h1:7Te2f9LQ/rd6XSzpntz6BaCBgglZ0uiCdv3/GdhX9VA=
79
github.com/keybase/go-keybase-chat-bot v0.0.0-20190812134859-bc54fd9cf83b/go.mod h1:vNc28YFzigVJod0j5EbuTtRIe7swx8vodh2yA4jZ2s8=
10+
github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=
11+
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
812
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
913
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
14+
github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=
15+
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
1016
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
17+
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
18+
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
1119
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
1220
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
1321
github.com/urfave/cli v1.21.0 h1:wYSSj06510qPIzGSua9ZqsncMmWE3Zr55KBERygyrxE=
@@ -19,6 +27,7 @@ golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn
1927
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
2028
golang.org/x/sys v0.0.0-20190412213103-97732733099d h1:+R4KGOnez64A81RvjARKc4UT5/tI9ujCIVX+P5KiHuI=
2129
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
30+
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
2231
golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e h1:D5TXcfTk7xF7hvieo4QErS3qqCB4teTffacDWr7CI+0=
2332
golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
2433
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

src/cmd/kssh/kssh.go

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -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

2021
func 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.")

src/cmd/kssh/kssh_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ func copyKeyFromTestFixture(t *testing.T, name, destination string) {
2323
require.NoError(t, err)
2424
err = ioutil.WriteFile(shared.KeyPathToCert(destination), cert, 0600)
2525
require.NoError(t, err)
26-
2726
}
2827

2928
func TestIsValidCert(t *testing.T) {

src/kssh/config.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,3 @@ func GetTeamFromBot(botname string) (string, error) {
227227
}
228228
return "", fmt.Errorf("did not find a client config file matching botname=%s (is the CA bot running and are you in the correct teams?)", botname)
229229
}
230-
231-
type RuntimeConfig struct {
232-
Debug bool
233-
}

src/kssh/debug.go

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/kssh/log.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package kssh
2+
3+
import log "github.com/sirupsen/logrus"
4+
5+
// Prefix formatter adds a prefix of "kssh: " to log messages before delegating to the default text formatter
6+
type prefixFormatter struct{}
7+
8+
func (pf *prefixFormatter) Format(entry *log.Entry) ([]byte, error) {
9+
entry.Message = "kssh: " + entry.Message
10+
textFormatter := &log.TextFormatter{}
11+
return textFormatter.Format(entry)
12+
}
13+
14+
func InitLogging() {
15+
log.SetLevel(log.WarnLevel)
16+
log.SetFormatter(&prefixFormatter{})
17+
}

0 commit comments

Comments
 (0)