Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Agent add support for Windows and Linux self signed certs. #3752

Merged
merged 5 commits into from
Apr 10, 2024
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
9 changes: 7 additions & 2 deletions agent/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@
"assets": "dist/*.{wasm,map}"
},
"bin": "dist/index.js",
"files": ["dist/index.js", "dist/index.js.map", "dist/*.wasm"],
"files": [
"dist/index.js",
"dist/index.js.map",
"dist/*.wasm"
],
"dependencies": {
"@pollyjs/core": "^6.0.6",
"@pollyjs/persister": "^6.0.6",
Expand All @@ -52,7 +56,8 @@
"minimatch": "^9.0.3",
"pretty-ms": "^8.0.0",
"uuid": "^9.0.0",
"vscode-uri": "^3.0.7"
"vscode-uri": "^3.0.7",
"win-ca": "^3.5.1"
},
"devDependencies": {
"@types/dedent": "^0.7.0",
Expand Down
69 changes: 69 additions & 0 deletions agent/src/certs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import fspromises from 'node:fs/promises'
import { globalAgent } from 'node:https'

/**
* Registers local root certificates onto the global HTTPS agent.
*
* On macOS, this adds macOS root certs.
* On Windows, this adds Windows root certs.
* On Linux, this adds Linux root certs.
*
* This allows HTTPS requests made via the global agent to trust these root certs.
*/
export function registerLocalCertificates() {
// Deduplicates and installs mac root certs onto the global agent
// This is a no op for non-mac environments
require('mac-ca').addToGlobalAgent({ excludeBundled: false })

// Installs windows root certs onto the global agent
// This is a no op for non-windows environments
require('win-ca/fallback').inject('+')

// Installs linux root certs onto the global agent
// This is a no op for non-linux environments
try {
addLinuxCerts()
} catch (e) {
console.warn('Error installing linux certs', e)
}
}

const linuxPossibleCertPaths = ['/etc/ssl/certs/ca-certificates.crt', '/etc/ssl/certs/ca-bundle.crt']

function addLinuxCerts() {
if (process.platform !== 'linux') {
return
}
const originalCA = globalAgent.options.ca
let cas: (string | Buffer)[]
if (!Array.isArray(originalCA)) {
cas = typeof originalCA !== 'undefined' ? [originalCA] : []
} else {
cas = Array.from(originalCA)
}

loadLinuxCerts()
.then(certs => cas.push(...certs))
.catch(err => console.warn('Error loading linux certs', err))
globalAgent.options.ca = cas
}

async function loadLinuxCerts(): Promise<Array<string>> {
const certs = new Set<string>()

for (const path of linuxPossibleCertPaths) {
try {
const content: string = await fspromises.readFile(path, { encoding: 'utf8' })
content
.split(/(?=-----BEGIN CERTIFICATE-----)/g)
.filter(pem => !!pem.length)
.map(pem => certs.add(pem))
} catch (err: any) {
// this is the error code for "no such file"
if (err?.code !== 'ENOENT') {
console.warn(err)
}
}
}
return Array.from(certs)
}
5 changes: 2 additions & 3 deletions agent/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env node

import type { Command } from 'commander'
import { registerLocalCertificates } from './certs'

console.log = console.error

Expand All @@ -22,9 +23,7 @@ process.on('uncaughtException', e => {
console.error('Uncaught exception:', e)
})

// Deduplicates and installs mac root certs onto the global agent
// This is a no op for non-mac environments
require('mac-ca').addToGlobalAgent({ excludeBundled: false })
registerLocalCertificates()

const args = process.argv.slice(2)
const { operands } = rootCommand.parseOptions(args)
Expand Down
39 changes: 39 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading