|
2 | 2 | import axios, { AxiosResponse } from "axios"
|
3 | 3 | import { getAuthenticatedUser } from "coder/site/src/api/api"
|
4 | 4 | import { readFileSync } from "fs"
|
| 5 | +import * as https from "https" |
5 | 6 | import * as module from "module"
|
6 | 7 | import * as os from "os"
|
7 | 8 | import path from "path"
|
8 | 9 | import * as vscode from "vscode"
|
9 | 10 | import { Commands } from "./commands"
|
| 11 | +import { SelfSignedCertificateError } from "./error" |
10 | 12 | import { Remote } from "./remote"
|
11 | 13 | import { Storage } from "./storage"
|
12 | 14 | import { WorkspaceQuery, WorkspaceProvider } from "./workspacesProvider"
|
13 | 15 |
|
14 | 16 | export async function activate(ctx: vscode.ExtensionContext): Promise<void> {
|
| 17 | + // The Remote SSH extension's proposed APIs are used to override |
| 18 | + // the SSH host name in VS Code itself. It's visually unappealing |
| 19 | + // having a lengthy name! |
| 20 | + // |
| 21 | + // This is janky, but that's alright since it provides such minimal |
| 22 | + // functionality to the extension. |
| 23 | + const remoteSSHExtension = vscode.extensions.getExtension("ms-vscode-remote.remote-ssh") |
| 24 | + if (!remoteSSHExtension) { |
| 25 | + throw new Error("Remote SSH extension not found") |
| 26 | + } |
| 27 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 28 | + const vscodeProposed: typeof vscode = (module as any)._load( |
| 29 | + "vscode", |
| 30 | + { |
| 31 | + filename: remoteSSHExtension?.extensionPath, |
| 32 | + }, |
| 33 | + false, |
| 34 | + ) |
| 35 | + |
| 36 | + // updateInsecure is called on extension activation and when the insecure |
| 37 | + // setting is changed. It updates the https agent to allow self-signed |
| 38 | + // certificates if the insecure setting is true. |
| 39 | + const applyInsecure = () => { |
| 40 | + const insecure = Boolean(vscode.workspace.getConfiguration().get("coder.insecure")) |
| 41 | + |
| 42 | + axios.defaults.httpsAgent = new https.Agent({ |
| 43 | + // rejectUnauthorized defaults to true, so we need to explicitly set it to false |
| 44 | + // if we want to allow self-signed certificates. |
| 45 | + rejectUnauthorized: !insecure, |
| 46 | + }) |
| 47 | + } |
| 48 | + |
| 49 | + axios.interceptors.response.use( |
| 50 | + (r) => r, |
| 51 | + (err) => { |
| 52 | + if (err) { |
| 53 | + const msg = err.toString() as string |
| 54 | + if (msg.indexOf("unable to verify the first certificate") !== -1) { |
| 55 | + throw new SelfSignedCertificateError(msg) |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + throw err |
| 60 | + }, |
| 61 | + ) |
| 62 | + |
| 63 | + vscode.workspace.onDidChangeConfiguration((e) => { |
| 64 | + e.affectsConfiguration("coder.insecure") && applyInsecure() |
| 65 | + }) |
| 66 | + applyInsecure() |
| 67 | + |
15 | 68 | const output = vscode.window.createOutputChannel("Coder")
|
16 | 69 | const storage = new Storage(output, ctx.globalState, ctx.secrets, ctx.globalStorageUri, ctx.logUri)
|
17 | 70 | await storage.init()
|
@@ -67,25 +120,6 @@ export async function activate(ctx: vscode.ExtensionContext): Promise<void> {
|
67 | 120 | },
|
68 | 121 | })
|
69 | 122 |
|
70 |
| - // The Remote SSH extension's proposed APIs are used to override |
71 |
| - // the SSH host name in VS Code itself. It's visually unappealing |
72 |
| - // having a lengthy name! |
73 |
| - // |
74 |
| - // This is janky, but that's alright since it provides such minimal |
75 |
| - // functionality to the extension. |
76 |
| - const remoteSSHExtension = vscode.extensions.getExtension("ms-vscode-remote.remote-ssh") |
77 |
| - if (!remoteSSHExtension) { |
78 |
| - throw new Error("Remote SSH extension not found") |
79 |
| - } |
80 |
| - // eslint-disable-next-line @typescript-eslint/no-explicit-any |
81 |
| - const vscodeProposed: typeof vscode = (module as any)._load( |
82 |
| - "vscode", |
83 |
| - { |
84 |
| - filename: remoteSSHExtension?.extensionPath, |
85 |
| - }, |
86 |
| - false, |
87 |
| - ) |
88 |
| - |
89 | 123 | const commands = new Commands(vscodeProposed, storage)
|
90 | 124 |
|
91 | 125 | vscode.commands.registerCommand("coder.login", commands.login.bind(commands))
|
@@ -114,6 +148,27 @@ export async function activate(ctx: vscode.ExtensionContext): Promise<void> {
|
114 | 148 | try {
|
115 | 149 | await remote.setup(vscodeProposed.env.remoteAuthority)
|
116 | 150 | } catch (ex) {
|
| 151 | + if (ex instanceof SelfSignedCertificateError) { |
| 152 | + const prompt = await vscodeProposed.window.showErrorMessage( |
| 153 | + "Failed to open workspace", |
| 154 | + { |
| 155 | + detail: SelfSignedCertificateError.Notification, |
| 156 | + modal: true, |
| 157 | + useCustom: true, |
| 158 | + }, |
| 159 | + SelfSignedCertificateError.ActionAllowInsecure, |
| 160 | + SelfSignedCertificateError.ActionViewMoreDetails, |
| 161 | + ) |
| 162 | + if (prompt === SelfSignedCertificateError.ActionAllowInsecure) { |
| 163 | + await ex.allowInsecure(storage) |
| 164 | + await remote.reloadWindow() |
| 165 | + return |
| 166 | + } |
| 167 | + if (prompt === SelfSignedCertificateError.ActionViewMoreDetails) { |
| 168 | + await ex.viewMoreDetails() |
| 169 | + return |
| 170 | + } |
| 171 | + } |
117 | 172 | await vscodeProposed.window.showErrorMessage("Failed to open workspace", {
|
118 | 173 | detail: (ex as string).toString(),
|
119 | 174 | modal: true,
|
|
0 commit comments