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

Use site config to use tunnel on workspaces in built-in wsp and site … #343

Merged
merged 1 commit into from
May 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions coder-sdk/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,21 @@ func (c *DefaultClient) SiteConfigExtensionMarketplace(ctx context.Context) (*Co
func (c *DefaultClient) PutSiteConfigExtensionMarketplace(ctx context.Context, req ConfigExtensionMarketplace) error {
return c.requestBody(ctx, http.MethodPut, "/api/private/extensions/config", req, nil)
}

// ConfigWorkspaces is the site configuration for workspace attributes.
type ConfigWorkspaces struct {
GPUVendor string `json:"gpu_vendor,omitempty" valid:"in(nvidia|amd)"`
EnableContainerVMs bool `json:"enable_container_vms,omitempty"`
EnableWorkspacesAsCode bool `json:"enable_workspaces_as_code,omitempty"`
EnableP2P bool `json:"enable_p2p,omitempty"`
}

// SiteConfigWorkspaces fetches the workspace configuration.
func (c *DefaultClient) SiteConfigWorkspaces(ctx context.Context) (*ConfigWorkspaces, error) {
var conf ConfigWorkspaces
// TODO: use the `/api/v0/workspaces/config route once we migrate from using general config
if err := c.requestBody(ctx, http.MethodGet, "/api/private/config", nil, &conf); err != nil {
return nil, err
}
return &conf, nil
}
3 changes: 3 additions & 0 deletions coder-sdk/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ type Client interface {
// PutSiteConfigExtensionMarketplace sets the extension marketplace configuration.
PutSiteConfigExtensionMarketplace(ctx context.Context, req ConfigExtensionMarketplace) error

// SiteConfigWorkspaces fetches the workspace configuration.
SiteConfigWorkspaces(ctx context.Context) (*ConfigWorkspaces, error)

// DeleteDevURL deletes the specified devurl.
DeleteDevURL(ctx context.Context, envID, urlID string) error

Expand Down
1 change: 0 additions & 1 deletion docs/coder_config-ssh.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ coder config-ssh [flags]
```
--filepath string override the default path of your ssh config file (default "~/.ssh/config")
-h, --help help for config-ssh
--p2p (experimental) uses coder tunnel to proxy ssh connection
--remove remove the auto-generated Coder ssh config
```

Expand Down
21 changes: 13 additions & 8 deletions internal/cmd/configssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,21 @@ func configSSHCmd() *cobra.Command {
var (
configpath string
remove = false
p2p = false
)

cmd := &cobra.Command{
Use: "config-ssh",
Short: "Configure SSH to access Coder environments",
Long: "Inject the proper OpenSSH configuration into your local SSH config file.",
RunE: configSSH(&configpath, &remove, &p2p),
RunE: configSSH(&configpath, &remove),
}
cmd.Flags().StringVar(&configpath, "filepath", filepath.Join("~", ".ssh", "config"), "override the default path of your ssh config file")
cmd.Flags().BoolVar(&remove, "remove", false, "remove the auto-generated Coder ssh config")
cmd.Flags().BoolVar(&p2p, "p2p", false, "(experimental) uses coder tunnel to proxy ssh connection")

return cmd
}

func configSSH(configpath *string, remove *bool, p2p *bool) func(cmd *cobra.Command, _ []string) error {
func configSSH(configpath *string, remove *bool) func(cmd *cobra.Command, _ []string) error {
return func(cmd *cobra.Command, _ []string) error {
ctx := cmd.Context()
usr, err := user.Current()
Expand Down Expand Up @@ -115,7 +113,12 @@ func configSSH(configpath *string, remove *bool, p2p *bool) func(cmd *cobra.Comm
return xerrors.New("SSH is disabled or not available for any environments in your Coder deployment.")
}

newConfig := makeNewConfigs(user.Username, envsWithProviders, privateKeyFilepath, *p2p)
wconf, err := client.SiteConfigWorkspaces(ctx)
if err != nil {
return xerrors.Errorf("getting site workspace config: %w", err)
}

newConfig := makeNewConfigs(user.Username, envsWithProviders, privateKeyFilepath, wconf.EnableP2P)

err = os.MkdirAll(filepath.Dir(*configpath), os.ModePerm)
if err != nil {
Expand Down Expand Up @@ -194,15 +197,17 @@ func makeNewConfigs(userName string, envs []coderutil.EnvWithWorkspaceProvider,
clog.LogWarn("invalid access url", clog.Causef("malformed url: %q", env.WorkspaceProvider.EnvproxyAccessURL))
continue
}
newConfig += makeSSHConfig(u.Host, userName, env.Env.Name, privateKeyFilepath, p2p)

useTunnel := env.WorkspaceProvider.BuiltIn && p2p
newConfig += makeSSHConfig(u.Host, userName, env.Env.Name, privateKeyFilepath, useTunnel)
}
newConfig += fmt.Sprintf("\n%s\n", sshEndToken)

return newConfig
}

func makeSSHConfig(host, userName, envName, privateKeyFilepath string, p2p bool) string {
if p2p {
func makeSSHConfig(host, userName, envName, privateKeyFilepath string, tunnel bool) string {
if tunnel {
return fmt.Sprintf(
`Host coder.%s
HostName coder.%s
Expand Down