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

Commit fcd16c6

Browse files
committed
Use real hostname
1 parent 02c209a commit fcd16c6

File tree

1 file changed

+28
-12
lines changed

1 file changed

+28
-12
lines changed

cmd/coder/config_ssh.go

+28-12
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,28 @@ import (
44
"context"
55
"fmt"
66
"io/ioutil"
7+
"net/url"
78
"os"
89
"path/filepath"
910
"strings"
1011

12+
"cdr.dev/coder-cli/internal/config"
1113
"cdr.dev/coder-cli/internal/entclient"
1214
"github.com/spf13/pflag"
1315

1416
"go.coder.com/cli"
1517
"go.coder.com/flog"
1618
)
1719

20+
var (
21+
privateKeyFilepath = filepath.Join(os.Getenv("HOME"), ".ssh", "coder_enterprise")
22+
)
23+
1824
type configSSHCmd struct {
1925
filepath string
2026
remove bool
2127

2228
startToken, startMessage, endToken string
23-
privateKeyFilepath string
2429
}
2530

2631
func (cmd *configSSHCmd) Spec() cli.CommandSpec {
@@ -47,7 +52,6 @@ func (cmd *configSSHCmd) RegisterFlags(fl *pflag.FlagSet) {
4752
#
4853
# You should not hand-edit this section, unless you are deleting it.`
4954
cmd.endToken = "# ------------END-CODER-ENTERPRISE------------"
50-
cmd.privateKeyFilepath = filepath.Join(home, ".ssh", "coder_enterprise")
5155
}
5256

5357
func (cmd *configSSHCmd) Run(fl *pflag.FlagSet) {
@@ -87,7 +91,10 @@ func (cmd *configSSHCmd) Run(fl *pflag.FlagSet) {
8791
if len(envs) < 1 {
8892
flog.Fatal("no environments found")
8993
}
90-
newConfig := cmd.makeNewConfigs(me.Username, envs)
94+
newConfig, err := cmd.makeNewConfigs(me.Username, envs)
95+
if err != nil {
96+
flog.Fatal("failed to make new ssh configurations: %v", err)
97+
}
9198

9299
// if we find the old config, remove those chars from the string
93100
if startIndex != -1 && endIndex != -1 {
@@ -98,48 +105,57 @@ func (cmd *configSSHCmd) Run(fl *pflag.FlagSet) {
98105
if err != nil {
99106
flog.Fatal("failed to write new configurations to ssh config file %q: %v", cmd.filepath, err)
100107
}
101-
err = cmd.writeSSHKey(ctx, entClient)
108+
err = writeSSHKey(ctx, entClient)
102109
if err != nil {
103110
flog.Fatal("failed to fetch and write ssh key: %v", err)
104111
}
105112

106113
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)
114+
fmt.Printf("Your private ssh key was written to %q\n", privateKeyFilepath)
108115
fmt.Println("You should now be able to ssh into your environment")
109116
fmt.Printf("For example, try running\n\n\t$ ssh coder.%s\n\n", envs[0].Name)
110117
}
111118

112-
func (cmd *configSSHCmd) writeSSHKey(ctx context.Context, client *entclient.Client) error {
119+
func writeSSHKey(ctx context.Context, client *entclient.Client) error {
113120
key, err := client.SSHKey()
114121
if err != nil {
115122
return err
116123
}
117-
err = ioutil.WriteFile(cmd.privateKeyFilepath, []byte(key.PrivateKey), 400)
124+
err = ioutil.WriteFile(privateKeyFilepath, []byte(key.PrivateKey), 400)
118125
if err != nil {
119126
return err
120127
}
121128
return nil
122129
}
123130

124-
func (cmd *configSSHCmd) makeNewConfigs(userName string, envs []entclient.Environment) string {
131+
func (cmd *configSSHCmd) makeNewConfigs(userName string, envs []entclient.Environment) (string, error) {
132+
u, err := config.URL.Read()
133+
if err != nil {
134+
return "", err
135+
}
136+
url, err := url.Parse(u)
137+
if err != nil {
138+
return "", err
139+
}
140+
125141
newConfig := fmt.Sprintf("\n%s\n%s\n\n", cmd.startToken, cmd.startMessage)
126142
for _, env := range envs {
127-
newConfig += cmd.makeConfig(userName, env.Name)
143+
newConfig += cmd.makeConfig(url.Hostname(), userName, env.Name)
128144
}
129145
newConfig += fmt.Sprintf("\n%s\n", cmd.endToken)
130146

131-
return newConfig
147+
return newConfig, nil
132148
}
133149

134-
func (cmd *configSSHCmd) makeConfig(userName, envName string) string {
150+
func (cmd *configSSHCmd) makeConfig(host, userName, envName string) string {
135151
return fmt.Sprintf(
136152
`Host coder.%s
137153
HostName %s
138154
User %s-%s
139155
StrictHostKeyChecking no
140156
ConnectTimeout=0
141157
IdentityFile=%s
142-
`, envName, "MOCK-SSHPROXY-IP", userName, envName, cmd.privateKeyFilepath) // TODO: get real ssh proxy ip address
158+
`, envName, host, userName, envName, privateKeyFilepath)
143159
}
144160

145161
func writeStr(filename, data string) error {

0 commit comments

Comments
 (0)