Skip to content

Commit f440f5e

Browse files
authored
feat: add options to support client tls (#128)
1 parent 1fe34ab commit f440f5e

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

package.json

+15
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,21 @@
6262
"markdownDescription": "An external command that outputs additional HTTP headers added to all requests. The command must output each header as `key=value` on its own line. The following environment variables will be available to the process: `CODER_URL`.",
6363
"type": "string",
6464
"default": ""
65+
},
66+
"coder.tlsCertFile": {
67+
"markdownDescription": "Path to file for TLS client cert",
68+
"type": "string",
69+
"default": ""
70+
},
71+
"coder.tlsKeyFile": {
72+
"markdownDescription": "Path to file for TLS client key",
73+
"type": "string",
74+
"default": ""
75+
},
76+
"coder.tlsCaFile": {
77+
"markdownDescription": "Path to file for TLS certificate authority",
78+
"type": "string",
79+
"default": ""
6580
}
6681
}
6782
},

src/extension.ts

+23-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"use strict"
22
import axios from "axios"
33
import { getAuthenticatedUser } from "coder/site/src/api/api"
4+
import fs from "fs"
45
import * as https from "https"
56
import * as module from "module"
67
import * as vscode from "vscode"
@@ -30,13 +31,21 @@ export async function activate(ctx: vscode.ExtensionContext): Promise<void> {
3031
false,
3132
)
3233

33-
// updateInsecure is called on extension activation and when the insecure
34-
// setting is changed. It updates the https agent to allow self-signed
35-
// certificates if the insecure setting is true.
36-
const applyInsecure = () => {
37-
const insecure = Boolean(vscode.workspace.getConfiguration().get("coder.insecure"))
34+
// applyHttpProperties is called on extension activation and when the
35+
// insecure or TLS setting are changed. It updates the https agent to allow
36+
// self-signed certificates if the insecure setting is true, as well as
37+
// adding cert/key/ca properties for TLS.
38+
const applyHttpProperties = () => {
39+
const cfg = vscode.workspace.getConfiguration()
40+
const insecure = Boolean(cfg.get("coder.insecure"))
41+
const certFile = String(cfg.get("coder.tlsCertFile"))
42+
const keyFile = String(cfg.get("coder.tlsKeyFile"))
43+
const caFile = String(cfg.get("coder.tlsCaFile"))
3844

3945
axios.defaults.httpsAgent = new https.Agent({
46+
cert: certFile === "" ? undefined : fs.readFileSync(certFile),
47+
key: keyFile === "" ? undefined : fs.readFileSync(keyFile),
48+
ca: caFile === "" ? undefined : fs.readFileSync(caFile),
4049
// rejectUnauthorized defaults to true, so we need to explicitly set it to false
4150
// if we want to allow self-signed certificates.
4251
rejectUnauthorized: !insecure,
@@ -51,9 +60,16 @@ export async function activate(ctx: vscode.ExtensionContext): Promise<void> {
5160
)
5261

5362
vscode.workspace.onDidChangeConfiguration((e) => {
54-
e.affectsConfiguration("coder.insecure") && applyInsecure()
63+
if (
64+
e.affectsConfiguration("coder.insecure") ||
65+
e.affectsConfiguration("coder.tlsCertFile") ||
66+
e.affectsConfiguration("coder.tlsKeyFile") ||
67+
e.affectsConfiguration("coder.tlsCaFile")
68+
) {
69+
applyHttpProperties()
70+
}
5571
})
56-
applyInsecure()
72+
applyHttpProperties()
5773

5874
const output = vscode.window.createOutputChannel("Coder")
5975
const storage = new Storage(output, ctx.globalState, ctx.secrets, ctx.globalStorageUri, ctx.logUri)

0 commit comments

Comments
 (0)