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

Commit 042061e

Browse files
committed
Rename kssh config file
1 parent 90ceb94 commit 042061e

6 files changed

Lines changed: 25 additions & 15 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ client config file is how kssh determines which teams are using kssh and the nee
168168
channel name, the name of the bot, etc). When keybaseca stops, it deletes all of the client config files.
169169

170170
kssh reads the client config file in order to determine how to interact with a bot. kssh does not have any user controlled
171-
configuration. It does have one local config file stored in `~/.ssh/kssh.config` that is used to store a few settings
171+
configuration. It does have one local config file stored in `~/.ssh/kssh-config.json` that is used to store a few settings
172172
for kssh. By default, this config file is not used. It is only created and meant to be interacted with via the
173173
`--set-default-bot`, `--clear-default-bot`, `--set-default-user`, `--clear-default-user` flags.
174174

src/cmd/kssh/kssh.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,11 +283,20 @@ func provisionNewKey(config kssh.ConfigFile, keyPath string) error {
283283
// Run SSH with the given key. Calls os.Exit and does not return.
284284
func runSSHWithKey(keyPath string, remainingArgs []string) {
285285
// Create a config file for the default user if necessary
286-
useConfig, err := kssh.CreateDefaultUserConfigFile()
286+
useConfig := false
287+
user, err := kssh.GetDefaultSSHUser()
287288
if err != nil {
288-
fmt.Printf("Failed to set default user: %v\n", err)
289+
fmt.Printf("Failed to retrieve default SSH user: %v\n", err)
289290
os.Exit(1)
290291
}
292+
if user != "" {
293+
useConfig = true
294+
err = kssh.CreateDefaultUserConfigFile()
295+
if err != nil {
296+
fmt.Printf("Failed to set default user: %v\n", err)
297+
os.Exit(1)
298+
}
299+
}
291300

292301
// Add the key to the ssh-agent in case we are doing multiple connections (eg via the `-J` flag)
293302
err = kssh.AddKeyToSSHAgent(keyPath)

src/cmd/kssh/kssh_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func TestIsValidCert(t *testing.T) {
4646
}
4747

4848
func BenchmarkLoadConfigs(b *testing.B) {
49-
os.Remove("~/.ssh/kssh.config")
49+
os.Remove("~/.ssh/kssh-config.json")
5050
b.ResetTimer()
5151

5252
for n := 0; n < b.N; n++ {

src/kssh/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ type LocalConfigFile struct {
113113
}
114114

115115
// Where to store the local config file. Just stash it in ~/.ssh
116-
var localConfigFileLocation = shared.ExpandPathWithTilde("~/.ssh/kssh.config")
116+
var localConfigFileLocation = shared.ExpandPathWithTilde("~/.ssh/kssh-config.json")
117117

118118
func GetDefaultSSHUser() (string, error) {
119119
lcf, err := getCurrentConfig()

src/kssh/ssh.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,44 +20,45 @@ func AddKeyToSSHAgent(keyPath string) error {
2020

2121
var AlternateSSHConfigFile = shared.ExpandPathWithTilde("~/.ssh/kssh-config")
2222

23-
func CreateDefaultUserConfigFile() (bool, error) {
23+
// Create an SSH config file that inherits from the default SSH config file but sets a default SSH user
24+
func CreateDefaultUserConfigFile() error {
2425
user, err := GetDefaultSSHUser()
2526
if err != nil {
26-
return false, err
27+
return err
2728
}
2829
if user == "" {
29-
return false, nil
30+
return nil
3031
}
3132

3233
err = MakeDotSSH()
3334
if err != nil {
34-
return false, err
35+
return err
3536
}
3637

3738
if _, err := os.Stat(shared.ExpandPathWithTilde("~/.ssh/config")); os.IsNotExist(err) {
3839
f, err := os.OpenFile(shared.ExpandPathWithTilde("~/.ssh/config"), os.O_RDONLY|os.O_CREATE, 0644)
3940
if err != nil {
40-
return false, fmt.Errorf("failed to touch ~/.ssh/config: %v", err)
41+
return fmt.Errorf("failed to touch ~/.ssh/config: %v", err)
4142
}
4243
f.Close()
4344
}
4445

45-
config := fmt.Sprintf("# kssh config file\n"+
46+
config := fmt.Sprintf("# kssh config file to set a default SSH user\n"+
4647
"Include config\n"+
4748
"Host *\n"+
4849
" User %s\n", user)
4950

5051
f, err := os.OpenFile(AlternateSSHConfigFile, os.O_CREATE|os.O_WRONLY, 0644)
5152
if err != nil {
52-
return false, err
53+
return err
5354
}
5455
defer f.Close()
5556
_, err = f.WriteString(config)
5657
if err != nil {
57-
return false, err
58+
return err
5859
}
5960
fmt.Printf("Using default ssh user %s\n", user)
60-
return true, nil
61+
return nil
6162
}
6263

6364
func MakeDotSSH() error {

tests/tests/lib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def clear_keys():
9191
def clear_local_config():
9292
# Clear kssh's local config file
9393
try:
94-
run_command("rm -rf ~/.ssh/kssh.config")
94+
run_command("rm -rf ~/.ssh/kssh-config.json")
9595
except subprocess.CalledProcessError:
9696
pass
9797

0 commit comments

Comments
 (0)