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

Commit b7f4180

Browse files
authored
Merge pull request #49 from keybase/david/keybaseca-debug-mode
keybaseca debug mode
2 parents d60ba2e + bfa7038 commit b7f4180

File tree

4 files changed

+62
-8
lines changed

4 files changed

+62
-8
lines changed

src/cmd/keybaseca/keybaseca.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/keybase/bot-sshca/src/kssh"
2424
"github.com/keybase/bot-sshca/src/shared"
2525

26+
"github.com/sirupsen/logrus"
2627
"github.com/urfave/cli"
2728
)
2829

@@ -34,6 +35,10 @@ func main() {
3435
app.Usage = "An SSH CA built on top of Keybase"
3536
app.Version = VersionNumber
3637
app.Flags = []cli.Flag{
38+
cli.BoolFlag{
39+
Name: "debug",
40+
Usage: "Log debug information",
41+
},
3742
cli.BoolFlag{
3843
Name: "wipe-all-configs",
3944
Hidden: true,
@@ -50,6 +55,7 @@ func main() {
5055
Name: "backup",
5156
Usage: "Print the current CA private key to stdout for backup purposes",
5257
Action: backupAction,
58+
Before: beforeAction,
5359
},
5460
{
5561
Name: "generate",
@@ -60,11 +66,13 @@ func main() {
6066
},
6167
},
6268
Action: generateAction,
69+
Before: beforeAction,
6370
},
6471
{
6572
Name: "service",
6673
Usage: "Start the CA service in the foreground",
6774
Action: serviceAction,
75+
Before: beforeAction,
6876
},
6977
{
7078
Name: "sign",
@@ -81,6 +89,7 @@ func main() {
8189
},
8290
},
8391
Action: signAction,
92+
Before: beforeAction,
8493
},
8594
}
8695
app.Action = mainAction
@@ -190,6 +199,14 @@ func signAction(c *cli.Context) error {
190199
return nil
191200
}
192201

202+
// A global before action that handles the --debug flag by setting the logrus logging level
203+
func beforeAction(c *cli.Context) error {
204+
if c.GlobalBool("debug") {
205+
logrus.SetLevel(logrus.DebugLevel)
206+
}
207+
return nil
208+
}
209+
193210
// The action for the `keybaseca` command. Only used for hidden and unlisted flags.
194211
func mainAction(c *cli.Context) error {
195212
switch {
@@ -280,6 +297,8 @@ func writeClientConfig(conf config.Config) error {
280297
}
281298
}
282299

300+
logrus.Debugf("Wrote kssh client config files for the teams: %v", teams)
301+
283302
return nil
284303
}
285304

@@ -299,6 +318,9 @@ func deleteClientConfig(conf config.Config) error {
299318
return err
300319
}
301320
}
321+
322+
logrus.Debugf("Deleted kssh client config files for the teams: %v", teams)
323+
302324
return nil
303325
}
304326

src/keybaseca/bot/bot.go

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ import (
77

88
"github.com/keybase/bot-sshca/src/keybaseca/botwrapper"
99

10-
"github.com/keybase/bot-sshca/src/keybaseca/log"
10+
auditlog "github.com/keybase/bot-sshca/src/keybaseca/log"
1111

1212
"github.com/keybase/bot-sshca/src/keybaseca/sshutils"
1313

1414
"github.com/keybase/bot-sshca/src/keybaseca/config"
1515
"github.com/keybase/bot-sshca/src/shared"
1616
"github.com/keybase/go-keybase-chat-bot/kbchat"
17+
18+
log "github.com/sirupsen/logrus"
1719
)
1820

1921
// Get a running instance of the keybase chat API. Will use the configured credentials if necessary.
@@ -53,27 +55,41 @@ func StartBot(conf config.Config) error {
5355
return fmt.Errorf("failed to read message: %v", err)
5456
}
5557

56-
if msg.Message.Content.Type != "text" || msg.Message.Sender.Username == kbc.GetUsername() {
58+
if msg.Message.Content.Type != "text" {
59+
continue
60+
}
61+
62+
messageBody := msg.Message.Content.Text.Body
63+
64+
log.Debugf("Received message in %s#%s: %s", msg.Message.Channel.Name, msg.Message.Channel.TopicName, messageBody)
65+
66+
if msg.Message.Sender.Username == kbc.GetUsername() {
67+
log.Debug("Skipping message since it comes from the bot user")
68+
if strings.Contains(messageBody, shared.AckRequestPrefix) || strings.Contains(messageBody, shared.SignatureRequestPreamble) {
69+
log.Warn("Ignoring AckRequest/SignatureRequest coming from the bot user! Are you trying to run the bot " +
70+
"and kssh as the same user?")
71+
}
5772
continue
5873
}
5974

6075
// Note that this line is one of the main security barriers around the SSH bot. If this line were removed
6176
// or had a bug, it would cause the SSH bot to respond to any SignatureRequest messages in any channels. This
6277
// would allow an attacker to provision SSH keys even though they are not in the listed channels.
6378
if !isConfiguredTeam(conf, msg.Message.Channel.Name, msg.Message.Channel.TopicName) {
79+
log.Debug("Skipping message since it is not in a configured team")
6480
continue
6581
}
6682

67-
messageBody := msg.Message.Content.Text.Body
68-
6983
if shared.IsAckRequest(messageBody) {
84+
log.Debug("Responding to AckMessage")
7085
// Ack any AckRequests so that kssh can determine whether it has fully connected
7186
_, err = kbc.SendMessageByConvID(msg.Message.ConversationID, shared.GenerateAckResponse(messageBody))
7287
if err != nil {
7388
LogError(conf, kbc, msg, err)
7489
continue
7590
}
7691
} else if strings.HasPrefix(messageBody, shared.SignatureRequestPreamble) {
92+
log.Debug("Responding to SignatureRequest")
7793
signatureRequest, err := shared.ParseSignatureRequest(messageBody)
7894
if err != nil {
7995
LogError(conf, kbc, msg, err)
@@ -97,6 +113,8 @@ func StartBot(conf config.Config) error {
97113
LogError(conf, kbc, msg, err)
98114
continue
99115
}
116+
} else {
117+
log.Debug("Ignoring unparsed message")
100118
}
101119
}
102120
}
@@ -105,10 +123,10 @@ func StartBot(conf config.Config) error {
105123
// due to an error caused by a malformed message.
106124
func LogError(conf config.Config, kbc *kbchat.API, msg kbchat.SubscriptionMessage, err error) {
107125
message := fmt.Sprintf("Encountered error while processing message from %s (messageID:%d): %v", msg.Message.Sender.Username, msg.Message.MsgID, err)
108-
log.Log(conf, message)
126+
auditlog.Log(conf, message)
109127
_, e := kbc.SendMessageByConvID(msg.Message.ConversationID, message)
110128
if e != nil {
111-
log.Log(conf, fmt.Sprintf("failed to log an error to chat (something is probably very wrong): %v", err))
129+
auditlog.Log(conf, fmt.Sprintf("failed to log an error to chat (something is probably very wrong): %v", err))
112130
}
113131
}
114132

src/keybaseca/config/config.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"github.com/keybase/bot-sshca/src/keybaseca/botwrapper"
1212

1313
"github.com/keybase/bot-sshca/src/shared"
14+
15+
log "github.com/sirupsen/logrus"
1416
)
1517

1618
// Represents a loaded and validated config for keybaseca
@@ -25,6 +27,7 @@ type Config interface {
2527
GetChannelName() string
2628
GetLogLocation() string
2729
GetStrictLogging() bool
30+
DebugString() string
2831
}
2932

3033
// Validate the given config file. If offline, do so without connecting to keybase (used in code that is meant
@@ -58,6 +61,7 @@ func ValidateConfig(conf EnvConfig, offline bool) error {
5861
return fmt.Errorf("STRICT_LOGGING must be either 'true' or 'false', '%s' is not valid", conf.getStrictLogging())
5962
}
6063
}
64+
log.Debugf("Validated config: %s", conf.DebugString())
6165
return nil
6266
}
6367

@@ -217,6 +221,14 @@ func (ef *EnvConfig) GetChannelName() string {
217221
return channel
218222
}
219223

224+
// Dump this EnvConfig to a string for debugging purposes
225+
func (ef *EnvConfig) DebugString() string {
226+
return fmt.Sprintf("CAKeyLocation='%s'; KeybaseHomeDir='%s'; KeybasePaperKey='%s'; KeybaseUsername='%s'; "+
227+
"KeyExpiration='%s'; Teams='%s'; ChatTeam='%s'; ChannelName='%s'; LogLocation='%s'; StrictLogging='%s'",
228+
ef.GetCAKeyLocation(), ef.GetKeybaseHomeDir(), ef.GetKeybasePaperKey(), ef.GetKeybaseUsername(),
229+
ef.GetKeyExpiration(), ef.GetTeams(), ef.GetChatTeam(), ef.GetChannelName(), ef.GetLogLocation(), ef.getStrictLogging())
230+
}
231+
220232
// Split a teamChannel of the form team.foo.bar#chan into "team.foo.bar", "chan"
221233
func splitTeamChannel(teamChannel string) (string, string, error) {
222234
split := strings.Split(teamChannel, "#")

src/shared/chat_types.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,11 @@ func ParseSignatureResponse(body string) (SignatureResponse, error) {
5959
return sr, err
6060
}
6161

62+
const AckRequestPrefix = "AckRequest--"
63+
6264
// Generate an AckRequest for the given username
6365
func GenerateAckRequest(username string) string {
64-
return "AckRequest--" + username
66+
return AckRequestPrefix + username
6567
}
6668

6769
// Generate an AckResponse in response to the given ack request
@@ -71,7 +73,7 @@ func GenerateAckResponse(ackRequest string) string {
7173

7274
// Returns whether the given message is an ack request
7375
func IsAckRequest(msg string) bool {
74-
return strings.HasPrefix(msg, "AckRequest--")
76+
return strings.HasPrefix(msg, AckRequestPrefix)
7577
}
7678

7779
// Returns whether the given message is an ack response

0 commit comments

Comments
 (0)