@@ -11,15 +11,17 @@ import (
1111 "github.com/keybase/bot-ssh-ca/src/shared"
1212)
1313
14- // A ConfigFile that is provided by the keybaseca server process and lives in kbfs
14+ // A ConfigFile that is provided by the keybaseca server process and lives in kbfs. It is used to share configuration
15+ // information about how kssh should communicate with the keybaseca chatbot.
1516type ConfigFile struct {
1617 TeamName string `json:"teamname"`
1718 ChannelName string `json:"channelname"`
1819 BotName string `json:"botname"`
1920}
2021
2122// LoadConfigs loads client configs from KBFS. Returns a (listOfConfigFiles, listOfBotNames, err)
22- // Both lists are deduplicated based on ConfigFile.BotName
23+ // Both lists are deduplicated based on ConfigFile.BotName. Runs the KBFS operations in parallel
24+ // to speed up loading configs.
2325func LoadConfigs () ([]ConfigFile , []string , error ) {
2426 allTeamsFromKBFS , err := shared .KBFSList ("/keybase/team/" )
2527 if err != nil {
@@ -81,6 +83,7 @@ func LoadConfigs() ([]ConfigFile, []string, error) {
8183 return configs , botnames , nil
8284}
8385
86+ // Load a kssh client config file from the given filename
8487func LoadConfig (kbfsFilename string ) (ConfigFile , error ) {
8588 var cf ConfigFile
8689 if ! strings .HasPrefix (kbfsFilename , "/keybase/" ) {
@@ -100,21 +103,26 @@ func LoadConfig(kbfsFilename string) (ConfigFile, error) {
100103 return cf , err
101104}
102105
103- // A LocalConfigFile is a file that lives on the FS of the computer running kssh. It is only used if the user is
104- // in multiple teams that are running the CA bot. Note that we store the team in here (even though it wasn't specified
105- // by the user) so that we can avoid doing a call to `LoadConfigs` if a default is set (since `LoadConfigs can be very
106- // slow if the user is in a large number of teams).
106+ // A LocalConfigFile is a file that lives on the FS of the computer running kssh. By default (and for most users), this
107+ // file is not used.
107108//
108- // Controlled via `kssh --set-default-bot foo`.
109+ // If a user of kssh is in in multiple teams that are running the CA bot they can configure a default bot to communicate
110+ // with. Note that we store the team in here (even though it wasn't specified by the user) so that we can avoid doing
111+ // a call to `LoadConfigs` if a default is set (since `LoadConfigs can be very slow if the user is in a large number of teams).
112+ // This is controlled via `kssh --set-default-bot foo`.
113+ //
114+ // If a user of kssh wishes to configure a default ssh user to use (see README.md for a description of why this may
115+ // be useful) this is also stored in the local config file. This is controlled via `kssh --set-default-user foo`.
109116type LocalConfigFile struct {
110117 DefaultBotName string `json:"default_bot"`
111118 DefaultBotTeam string `json:"default_team"`
112119 DefaultSSHUser string `json:"default_ssh_user"`
113120}
114121
115- // Where to store the local config file. Just stash it in ~/.ssh
122+ // Where to store the local config file. Just stash it in ~/.ssh rather than making a ~/.kssh folder
116123var localConfigFileLocation = shared .ExpandPathWithTilde ("~/.ssh/kssh-config.json" )
117124
125+ // Get the default SSH user to use for kssh connections. Empty if no user is configured.
118126func GetDefaultSSHUser () (string , error ) {
119127 lcf , err := getCurrentConfig ()
120128 if err != nil {
@@ -124,6 +132,7 @@ func GetDefaultSSHUser() (string, error) {
124132 return lcf .DefaultSSHUser , nil
125133}
126134
135+ // Set the default SSH user to use for kssh connections.
127136func SetDefaultSSHUser (username string ) error {
128137 if strings .ContainsAny (username , " \t \n \r '\" " ) {
129138 return fmt .Errorf ("invalid username: %s" , username )
@@ -138,6 +147,7 @@ func SetDefaultSSHUser(username string) error {
138147 return writeConfig (lcf )
139148}
140149
150+ // Write the given config file to disk
141151func writeConfig (lcf LocalConfigFile ) error {
142152 bytes , err := json .Marshal (& lcf )
143153 if err != nil {
@@ -157,6 +167,7 @@ func writeConfig(lcf LocalConfigFile) error {
157167 return nil
158168}
159169
170+ // Get the current kssh config file
160171func getCurrentConfig () (lcf LocalConfigFile , err error ) {
161172 if _ , err := os .Stat (localConfigFileLocation ); os .IsNotExist (err ) {
162173 return lcf , nil
@@ -172,10 +183,12 @@ func getCurrentConfig() (lcf LocalConfigFile, err error) {
172183 return lcf , nil
173184}
174185
186+ // Set the default keybaseca bot to communicate with.
175187func SetDefaultBot (botname string ) error {
176188 teamname := ""
177189 var err error
178190 if botname != "" {
191+ // Get the team associated with it and cache that too in order to avoid looking it up everytime
179192 teamname , err = GetTeamFromBot (botname )
180193 if err != nil {
181194 return err
@@ -192,6 +205,7 @@ func SetDefaultBot(botname string) error {
192205 return writeConfig (lcf )
193206}
194207
208+ // Get the default bot and team for kssh
195209func GetDefaultBotAndTeam () (string , string , error ) {
196210 lcf , err := getCurrentConfig ()
197211 if err != nil {
@@ -200,6 +214,7 @@ func GetDefaultBotAndTeam() (string, string, error) {
200214 return lcf .DefaultBotName , lcf .DefaultBotTeam , nil
201215}
202216
217+ // Get the teamname associated with the given botname
203218func GetTeamFromBot (botname string ) (string , error ) {
204219 configs , _ , err := LoadConfigs ()
205220 if err != nil {
0 commit comments