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

Commit 1f151a3

Browse files
committed
Add hidden/undocumented/untested option to configure keybase binary for kssh
1 parent 3092a35 commit 1f151a3

7 files changed

Lines changed: 63 additions & 28 deletions

File tree

src/cmd/keybaseca/keybaseca.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import (
1212
"sync"
1313
"syscall"
1414

15+
"github.com/keybase/bot-ssh-ca/src/keybaseca/constants"
16+
1517
"github.com/google/uuid"
1618

1719
"github.com/keybase/bot-ssh-ca/src/keybaseca/bot"
@@ -192,7 +194,7 @@ func signAction(c *cli.Context) error {
192194
func mainAction(c *cli.Context) error {
193195
switch {
194196
case c.Bool("wipe-all-configs"):
195-
teams, err := shared.KBFSList("/keybase/team/")
197+
teams, err := constants.GetDefaultKBFSOperationsStruct().KBFSList("/keybase/team/")
196198
if err != nil {
197199
return err
198200
}
@@ -206,9 +208,9 @@ func mainAction(c *cli.Context) error {
206208
boundChan <- 0
207209

208210
filename := fmt.Sprintf("/keybase/team/%s/%s", team, shared.ConfigFilename)
209-
exists, _ := shared.KBFSFileExists(filename)
211+
exists, _ := constants.GetDefaultKBFSOperationsStruct().KBFSFileExists(filename)
210212
if exists {
211-
err = shared.KBFSDelete(filename)
213+
err = constants.GetDefaultKBFSOperationsStruct().KBFSDelete(filename)
212214
if err != nil {
213215
fmt.Printf("%v\n", err)
214216
}
@@ -227,7 +229,7 @@ func mainAction(c *cli.Context) error {
227229
}
228230
logLocation := conf.GetLogLocation()
229231
if strings.HasPrefix(logLocation, "/keybase/") {
230-
err = shared.KBFSDelete(logLocation)
232+
err = constants.GetDefaultKBFSOperationsStruct().KBFSDelete(logLocation)
231233
if err != nil {
232234
return fmt.Errorf("Failed to delete log file at %s: %v", logLocation, err)
233235
}
@@ -272,7 +274,7 @@ func writeClientConfig(conf config.Config) error {
272274
return err
273275
}
274276

275-
err = shared.KBFSWrite(filename, string(content), false)
277+
err = constants.GetDefaultKBFSOperationsStruct().KBFSWrite(filename, string(content), false)
276278
if err != nil {
277279
return err
278280
}
@@ -292,7 +294,7 @@ func deleteClientConfig(conf config.Config) error {
292294

293295
for _, team := range teams {
294296
filename := filepath.Join("/keybase/team/", team, shared.ConfigFilename)
295-
err := shared.KBFSDelete(filename)
297+
err := constants.GetDefaultKBFSOperationsStruct().KBFSDelete(filename)
296298
if err != nil {
297299
return err
298300
}

src/keybaseca/config/config.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"os"
77
"strings"
88

9+
"github.com/keybase/bot-ssh-ca/src/keybaseca/constants"
10+
911
"github.com/keybase/bot-ssh-ca/src/keybaseca/botwrapper"
1012

1113
"github.com/keybase/bot-ssh-ca/src/shared"
@@ -89,17 +91,17 @@ func validateChannel(conf Config, teamName string, channelName string) error {
8991
func validatePath(path string) error {
9092
if strings.HasPrefix(path, "/keybase/") {
9193
// If it exists it is valid
92-
exists, _ := shared.KBFSFileExists(path)
94+
exists, _ := constants.GetDefaultKBFSOperationsStruct().KBFSFileExists(path)
9395
if exists {
9496
return nil
9597
}
9698

9799
// Otherwise try to write to it
98-
err := shared.KBFSWrite(path, "", false)
100+
err := constants.GetDefaultKBFSOperationsStruct().KBFSWrite(path, "", false)
99101
if err != nil {
100102
return fmt.Errorf("path is not writable: %v", err)
101103
}
102-
shared.KBFSDelete(path)
104+
constants.GetDefaultKBFSOperationsStruct().KBFSDelete(path)
103105
return nil
104106
}
105107
_, err := os.Stat(path)

src/keybaseca/constants/kbfs.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package constants
2+
3+
import "github.com/keybase/bot-ssh-ca/src/shared"
4+
5+
// Get the default KBFSOperation struct used for KBFS operations. Currently keybaseca does not support running with a
6+
// custom keybase binary path
7+
func GetDefaultKBFSOperationsStruct() *shared.KBFSOperation {
8+
return &shared.KBFSOperation{KeybaseBinaryPath: "keybase"}
9+
}

src/keybaseca/log/log.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ import (
66
"strings"
77
"time"
88

9+
"github.com/keybase/bot-ssh-ca/src/keybaseca/constants"
10+
911
"github.com/keybase/bot-ssh-ca/src/keybaseca/config"
10-
"github.com/keybase/bot-ssh-ca/src/shared"
1112
)
1213

1314
// Log attempts to log the given string to a file. If conf.GetStrictLogging() it will panic if it fails
@@ -33,7 +34,7 @@ func Log(conf config.Config, str string) {
3334
// the local filesystem
3435
func appendToFile(filename string, str string) error {
3536
if strings.HasPrefix(filename, "/keybase/") {
36-
return shared.KBFSWrite(filename, str, true)
37+
return constants.GetDefaultKBFSOperationsStruct().KBFSWrite(filename, str, true)
3738
}
3839
f, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
3940
if err != nil {

src/kssh/bot.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
func GetSignedKey(config ConfigFile, request shared.SignatureRequest) (shared.SignatureResponse, error) {
1515
empty := shared.SignatureResponse{}
1616

17-
runOptions := kbchat.RunOptions{KeybaseLocation: "keybase"}
17+
runOptions := kbchat.RunOptions{KeybaseLocation: GetKeybaseBinaryPath()}
1818
kbc, err := kbchat.Start(runOptions)
1919
if err != nil {
2020
return empty, fmt.Errorf("error starting Keybase chat: %v", err)

src/kssh/config.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type ConfigFile struct {
2323
// Both lists are deduplicated based on ConfigFile.BotName. Runs the KBFS operations in parallel
2424
// to speed up loading configs.
2525
func LoadConfigs() ([]ConfigFile, []string, error) {
26-
allTeamsFromKBFS, err := shared.KBFSList("/keybase/team/")
26+
allTeamsFromKBFS, err := GetKBFSOperationsStruct().KBFSList("/keybase/team/")
2727
if err != nil {
2828
return nil, nil, fmt.Errorf("failed to load config file(s): %v", err)
2929
}
@@ -41,7 +41,7 @@ func LoadConfigs() ([]ConfigFile, []string, error) {
4141
boundChan <- 0
4242

4343
filename := fmt.Sprintf("/keybase/team/%s/%s", team, shared.ConfigFilename)
44-
exists, err := shared.KBFSFileExists(filename)
44+
exists, err := GetKBFSOperationsStruct().KBFSFileExists(filename)
4545
if err != nil {
4646
// Treat an error as it not existing and just skip that team while searching for config files
4747
exists = false
@@ -89,7 +89,7 @@ func LoadConfig(kbfsFilename string) (ConfigFile, error) {
8989
if !strings.HasPrefix(kbfsFilename, "/keybase/") {
9090
return cf, fmt.Errorf("cannot load a kssh config from outside of KBFS")
9191
}
92-
bytes, err := shared.KBFSRead(kbfsFilename)
92+
bytes, err := GetKBFSOperationsStruct().KBFSRead(kbfsFilename)
9393
if err != nil {
9494
return cf, fmt.Errorf("found a config file at %s that could not be read: %v", kbfsFilename, err)
9595
}
@@ -117,6 +117,23 @@ type LocalConfigFile struct {
117117
DefaultBotName string `json:"default_bot"`
118118
DefaultBotTeam string `json:"default_team"`
119119
DefaultSSHUser string `json:"default_ssh_user"`
120+
KeybaseBinPath string `json:"keybase_binary"`
121+
}
122+
123+
func GetKeybaseBinaryPath() string {
124+
lcf, err := getCurrentConfig()
125+
if err != nil {
126+
return "keybase"
127+
}
128+
129+
if lcf.KeybaseBinPath != "" {
130+
return lcf.KeybaseBinPath
131+
}
132+
return "keybase"
133+
}
134+
135+
func GetKBFSOperationsStruct() *shared.KBFSOperation {
136+
return &shared.KBFSOperation{KeybaseBinaryPath: GetKeybaseBinaryPath()}
120137
}
121138

122139
// Where to store the local config file. Just stash it in ~/.ssh rather than making a ~/.kssh folder

src/shared/kbfs.go

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,12 @@ func supportsFuse() bool {
2222
return err1 == nil && err2 == nil && err3 == nil && err4 == nil
2323
}
2424

25+
type KBFSOperation struct {
26+
KeybaseBinaryPath string
27+
}
28+
2529
// Returns whether the given KBFS file exists
26-
func KBFSFileExists(kbfsFilename string) (bool, error) {
30+
func (ko *KBFSOperation) KBFSFileExists(kbfsFilename string) (bool, error) {
2731
if supportsFuse() {
2832
// Note that this code is not tested via integration tests since fuse does not run in docker. Handle with care.
2933
_, err := os.Stat(kbfsFilename)
@@ -36,7 +40,7 @@ func KBFSFileExists(kbfsFilename string) (bool, error) {
3640
return false, err
3741
}
3842

39-
cmd := exec.Command("keybase", "fs", "stat", kbfsFilename)
43+
cmd := exec.Command(ko.KeybaseBinaryPath, "fs", "stat", kbfsFilename)
4044
bytes, err := cmd.CombinedOutput()
4145
if err == nil {
4246
return true, nil
@@ -48,12 +52,12 @@ func KBFSFileExists(kbfsFilename string) (bool, error) {
4852
}
4953

5054
// Reads the specified KBFS file into a byte array
51-
func KBFSRead(kbfsFilename string) ([]byte, error) {
55+
func (ko *KBFSOperation) KBFSRead(kbfsFilename string) ([]byte, error) {
5256
if supportsFuse() {
5357
// Note that this code is not tested via integration tests since fuse does not run in docker. Handle with care.
5458
return ioutil.ReadFile(kbfsFilename)
5559
}
56-
cmd := exec.Command("keybase", "fs", "read", kbfsFilename)
60+
cmd := exec.Command(ko.KeybaseBinaryPath, "fs", "read", kbfsFilename)
5761
bytes, err := cmd.CombinedOutput()
5862
if err != nil {
5963
return nil, fmt.Errorf("failed to read %s: %s (%v)", kbfsFilename, strings.TrimSpace(string(bytes)), err)
@@ -62,8 +66,8 @@ func KBFSRead(kbfsFilename string) ([]byte, error) {
6266
}
6367

6468
// Delete the specified KBFS file
65-
func KBFSDelete(filename string) error {
66-
cmd := exec.Command("keybase", "fs", "rm", filename)
69+
func (ko *KBFSOperation) KBFSDelete(filename string) error {
70+
cmd := exec.Command(ko.KeybaseBinaryPath, "fs", "rm", filename)
6771
bytes, err := cmd.CombinedOutput()
6872
if err != nil {
6973
return fmt.Errorf("failed to delete the file at %s: %s (%v)", filename, strings.TrimSpace(string(bytes)), err)
@@ -73,20 +77,20 @@ func KBFSDelete(filename string) error {
7377

7478
// Write contents to the specified KBFS file. If appendToFile, appends onto the end of the file. Otherwise, overwrites
7579
// and truncates the file.
76-
func KBFSWrite(filename string, contents string, appendToFile bool) error {
80+
func (ko *KBFSOperation) KBFSWrite(filename string, contents string, appendToFile bool) error {
7781
var cmd *exec.Cmd
7882
if appendToFile {
7983
// `keybase fs write --append` only works if the file already exists so create it if it does not exist
80-
exists, err := KBFSFileExists(filename)
84+
exists, err := ko.KBFSFileExists(filename)
8185
if !exists || err != nil {
82-
err = KBFSWrite(filename, "", false)
86+
err = ko.KBFSWrite(filename, "", false)
8387
if err != nil {
8488
return err
8589
}
8690
}
87-
cmd = exec.Command("keybase", "fs", "write", "--append", filename)
91+
cmd = exec.Command(ko.KeybaseBinaryPath, "fs", "write", "--append", filename)
8892
} else {
89-
cmd = exec.Command("keybase", "fs", "write", filename)
93+
cmd = exec.Command(ko.KeybaseBinaryPath, "fs", "write", filename)
9094
}
9195

9296
cmd.Stdin = strings.NewReader(string(contents))
@@ -98,8 +102,8 @@ func KBFSWrite(filename string, contents string, appendToFile bool) error {
98102
}
99103

100104
// List KBFS files in the given KBFS path
101-
func KBFSList(path string) ([]string, error) {
102-
cmd := exec.Command("keybase", "fs", "ls", "-1", "--nocolor", path)
105+
func (ko *KBFSOperation) KBFSList(path string) ([]string, error) {
106+
cmd := exec.Command(ko.KeybaseBinaryPath, "fs", "ls", "-1", "--nocolor", path)
103107
output, err := cmd.CombinedOutput()
104108
if err != nil {
105109
return nil, fmt.Errorf("failed to list files in /keybase/team/: %s (%v)", strings.TrimSpace(string(output)), err)

0 commit comments

Comments
 (0)