From d6f6fb609ee15448c20ca2918b1d002d81b6b36a Mon Sep 17 00:00:00 2001 From: Garrett Date: Tue, 11 May 2021 04:13:27 +0000 Subject: [PATCH] Use site config to use tunnel on workspaces in built-in wsp and site p2p enabled --- coder-sdk/config.go | 18 ++++++++++++++++++ coder-sdk/interface.go | 3 +++ docs/coder_config-ssh.md | 1 - internal/cmd/configssh.go | 21 +++++++++++++-------- 4 files changed, 34 insertions(+), 9 deletions(-) diff --git a/coder-sdk/config.go b/coder-sdk/config.go index 7012f90d..c43ddf2c 100644 --- a/coder-sdk/config.go +++ b/coder-sdk/config.go @@ -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 +} diff --git a/coder-sdk/interface.go b/coder-sdk/interface.go index ef9995c1..42c34d2c 100644 --- a/coder-sdk/interface.go +++ b/coder-sdk/interface.go @@ -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 diff --git a/docs/coder_config-ssh.md b/docs/coder_config-ssh.md index 311387aa..8ac849cf 100644 --- a/docs/coder_config-ssh.md +++ b/docs/coder_config-ssh.md @@ -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 ``` diff --git a/internal/cmd/configssh.go b/internal/cmd/configssh.go index 7abe2c2b..8314b5a9 100644 --- a/internal/cmd/configssh.go +++ b/internal/cmd/configssh.go @@ -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() @@ -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 { @@ -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