Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit f9c0930

Browse files
committed
Fetch and write private ssh key
1 parent 50f9ab7 commit f9c0930

File tree

2 files changed

+45
-6
lines changed

2 files changed

+45
-6
lines changed

cmd/coder/config_ssh.go

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"context"
45
"fmt"
56
"io/ioutil"
67
"os"
@@ -19,6 +20,7 @@ type configSSHCmd struct {
1920
remove bool
2021

2122
startToken, startMessage, endToken string
23+
privateKeyFilepath string
2224
}
2325

2426
func (cmd *configSSHCmd) Spec() cli.CommandSpec {
@@ -31,7 +33,8 @@ func (cmd *configSSHCmd) Spec() cli.CommandSpec {
3133

3234
func (cmd *configSSHCmd) RegisterFlags(fl *pflag.FlagSet) {
3335
fl.BoolVar(&cmd.remove, "remove", false, "remove the auto-generated Coder Enterprise ssh config")
34-
defaultPath := filepath.Join(os.Getenv("HOME"), ".ssh", "config")
36+
home := os.Getenv("HOME")
37+
defaultPath := filepath.Join(home, ".ssh", "config")
3538
fl.StringVar(&cmd.filepath, "config-path", defaultPath, "overide the default path of your ssh config file")
3639

3740
cmd.startToken = "# ------------START-CODER-ENTERPRISE-----------"
@@ -44,9 +47,13 @@ func (cmd *configSSHCmd) RegisterFlags(fl *pflag.FlagSet) {
4447
#
4548
# You should not hand-edit this section, unless you are deleting it.`
4649
cmd.endToken = "# ------------END-CODER-ENTERPRISE------------"
50+
cmd.privateKeyFilepath = filepath.Join(home, ".ssh", "coder_enterprise")
4751
}
4852

4953
func (cmd *configSSHCmd) Run(fl *pflag.FlagSet) {
54+
ctx, cancel := context.WithCancel(context.Background())
55+
defer cancel()
56+
5057
currentConfig, err := readStr(cmd.filepath)
5158
if err != nil {
5259
flog.Fatal("failed to read ssh config file %q: %v", cmd.filepath, err)
@@ -91,30 +98,48 @@ func (cmd *configSSHCmd) Run(fl *pflag.FlagSet) {
9198
if err != nil {
9299
flog.Fatal("failed to write new configurations to ssh config file %q: %v", cmd.filepath, err)
93100
}
101+
err = cmd.writeSSHKey(ctx, entClient)
102+
if err != nil {
103+
flog.Fatal("failed to fetch and write ssh key: %v", err)
104+
}
105+
94106
fmt.Printf("An auto-generated ssh config was written to %q\n", cmd.filepath)
107+
fmt.Printf("Your private ssh key was written to %q\n", cmd.privateKeyFilepath)
95108
fmt.Println("You should now be able to ssh into your environment")
96109
fmt.Printf("For example, try running\n\n\t$ ssh coder:%s\n\n", envs[0].Name)
97110
}
98111

112+
func (cmd *configSSHCmd) writeSSHKey(ctx context.Context, client *entclient.Client) error {
113+
key, err := client.SSHKey()
114+
if err != nil {
115+
return err
116+
}
117+
err = ioutil.WriteFile(cmd.privateKeyFilepath, []byte(key.PrivateKey), 400)
118+
if err != nil {
119+
return err
120+
}
121+
return nil
122+
}
123+
99124
func (cmd *configSSHCmd) makeNewConfigs(userName string, envs []entclient.Environment) string {
100125
newConfig := fmt.Sprintf("\n%s\n%s\n\n", cmd.startToken, cmd.startMessage)
101126
for _, env := range envs {
102-
newConfig += makeConfig(userName, env.Name)
127+
newConfig += cmd.makeConfig(userName, env.Name)
103128
}
104129
newConfig += fmt.Sprintf("\n%s\n", cmd.endToken)
105130

106131
return newConfig
107132
}
108133

109-
func makeConfig(userName, envName string) string {
134+
func (cmd *configSSHCmd) makeConfig(userName, envName string) string {
110135
return fmt.Sprintf(
111136
`Host coder:%s
112137
HostName %s
113138
User %s-%s
114-
Port 2222
115-
KeepAlive=yes
139+
StrictHostKeyChecking no
116140
ConnectTimeout=0
117-
`, envName, "MOCK-SSHPROXY-IP", userName, envName) // TODO: get real ssh proxy ip address
141+
IdentityFile=%s
142+
`, envName, "MOCK-SSHPROXY-IP", userName, envName, cmd.privateKeyFilepath) // TODO: get real ssh proxy ip address
118143
}
119144

120145
func writeStr(filename, data string) error {

internal/entclient/me.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,17 @@ func (c Client) Me() (*User, error) {
1414
}
1515
return &u, nil
1616
}
17+
18+
type SSHKey struct {
19+
PublicKey string `json:"public_key"`
20+
PrivateKey string `json:"private_key"`
21+
}
22+
23+
func (c Client) SSHKey() (*SSHKey, error) {
24+
var key SSHKey
25+
err := c.requestBody("GET", "/api/users/me/sshkey", nil, &key)
26+
if err != nil {
27+
return nil, err
28+
}
29+
return &key, nil
30+
}

0 commit comments

Comments
 (0)