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

Commit f14f8ea

Browse files
committed
Use env.ssh_available field in config-ssh
1 parent db06ecc commit f14f8ea

File tree

2 files changed

+52
-18
lines changed

2 files changed

+52
-18
lines changed

coder-sdk/env.go

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ type Environment struct {
3232
LastOpenedAt time.Time `json:"last_opened_at" table:"-"`
3333
LastConnectionAt time.Time `json:"last_connection_at" table:"-"`
3434
AutoOffThreshold Duration `json:"auto_off_threshold" table:"-"`
35+
SSHAvailable bool `json:"ssh_available" table:"-"`
3536
}
3637

3738
// RebuildMessage defines the message shown when an Environment requires a rebuild for it can be accessed.

internal/cmd/configssh.go

+51-18
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,6 @@ func configSSH(configpath *string, remove *bool) func(cmd *cobra.Command, _ []st
9393
return err
9494
}
9595

96-
if !isSSHAvailable(ctx) {
97-
return xerrors.New("SSH is disabled or not available for your Coder Enterprise deployment.")
98-
}
99-
10096
user, err := client.Me(ctx)
10197
if err != nil {
10298
return xerrors.Errorf("fetch username: %w", err)
@@ -109,6 +105,16 @@ func configSSH(configpath *string, remove *bool) func(cmd *cobra.Command, _ []st
109105
if len(envs) < 1 {
110106
return xerrors.New("no environments found")
111107
}
108+
109+
if !sshAvailable(envs) {
110+
return xerrors.New("SSH is disabled or not available any environments in your Coder Enterprise deployment.")
111+
}
112+
113+
err = canConnectSSH(ctx)
114+
if err != nil {
115+
return xerrors.Errorf("check if SSH is available: unable to connect to SSH endpoint: %w", err)
116+
}
117+
112118
newConfig, err := makeNewConfigs(user.Username, envs, startToken, startMessage, endToken, privateKeyFilepath)
113119
if err != nil {
114120
return xerrors.Errorf("make new ssh configurations: %w", err)
@@ -145,6 +151,43 @@ func configSSH(configpath *string, remove *bool) func(cmd *cobra.Command, _ []st
145151
}
146152
}
147153

154+
// sshAvailable returns true if SSH is available for at least one environment.
155+
func sshAvailable(envs []coder.Environment) bool {
156+
for _, env := range envs {
157+
if env.SSHAvailable {
158+
return true
159+
}
160+
}
161+
162+
return false
163+
}
164+
165+
// canConnectSSH returns an error if we cannot dial the SSH port.
166+
func canConnectSSH(ctx context.Context) error {
167+
ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
168+
defer cancel()
169+
170+
host, err := configuredHostname()
171+
if err != nil {
172+
return xerrors.Errorf("get configured manager hostname: %w", err)
173+
}
174+
175+
var (
176+
dialer net.Dialer
177+
hostPort = net.JoinHostPort(host, "22")
178+
)
179+
conn, err := dialer.DialContext(ctx, "tcp", hostPort)
180+
if err != nil {
181+
if err == context.DeadlineExceeded {
182+
err = xerrors.New("timed out after 3 seconds")
183+
}
184+
return xerrors.Errorf("dial tcp://%v: %w", hostPort, err)
185+
}
186+
conn.Close()
187+
188+
return nil
189+
}
190+
148191
func writeSSHKey(ctx context.Context, client *coder.Client, privateKeyPath string) error {
149192
key, err := client.SSHKey(ctx)
150193
if err != nil {
@@ -161,6 +204,10 @@ func makeNewConfigs(userName string, envs []coder.Environment, startToken, start
161204

162205
newConfig := fmt.Sprintf("\n%s\n%s\n\n", startToken, startMsg)
163206
for _, env := range envs {
207+
if !env.SSHAvailable {
208+
continue
209+
}
210+
164211
newConfig += makeSSHConfig(hostname, userName, env.Name, privateKeyFilepath)
165212
}
166213
newConfig += fmt.Sprintf("\n%s\n", endToken)
@@ -181,20 +228,6 @@ func makeSSHConfig(host, userName, envName, privateKeyFilepath string) string {
181228
`, envName, host, userName, envName, privateKeyFilepath)
182229
}
183230

184-
func isSSHAvailable(ctx context.Context) bool {
185-
ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
186-
defer cancel()
187-
188-
host, err := configuredHostname()
189-
if err != nil {
190-
return false
191-
}
192-
193-
var dialer net.Dialer
194-
_, err = dialer.DialContext(ctx, "tcp", net.JoinHostPort(host, "22"))
195-
return err == nil
196-
}
197-
198231
func configuredHostname() (string, error) {
199232
u, err := config.URL.Read()
200233
if err != nil {

0 commit comments

Comments
 (0)