| 1 | import * as fs from "node:fs"; |
| 2 | import * as os from "node:os"; |
| 3 | import * as path from "node:path"; |
| 4 | import { afterEach, beforeEach, describe, expect, test } from "vitest"; |
| 5 | import { CertsCache, getCertificateContent } from "./certs.js"; |
| 6 | import { getAgentOptions } from "./getAgentOptions.js"; |
| 7 | |
| 8 | // Store original env |
| 9 | const originalEnv = process.env; |
| 10 | |
| 11 | // Temporary directory for test certificate files |
| 12 | let tempDir: string; |
| 13 | |
| 14 | beforeEach(() => { |
| 15 | // Create a temporary directory for test files |
| 16 | tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "cert-troubleshoot-test-")); |
| 17 | process.env = { ...originalEnv }; |
| 18 | }); |
| 19 | |
| 20 | afterEach(() => { |
| 21 | process.env = originalEnv; |
| 22 | CertsCache.getInstance().clear(); |
| 23 | |
| 24 | // Clean up temporary directory |
| 25 | if (tempDir && fs.existsSync(tempDir)) { |
| 26 | fs.rmSync(tempDir, { recursive: true, force: true }); |
| 27 | } |
| 28 | }); |
| 29 | |
| 30 | describe("SSL certificate handling", () => { |
| 31 | describe("CA certificate handling", () => { |
| 32 | test("should handle custom CA bundle from file path", async () => { |
| 33 | const customCaCert = `-----BEGIN CERTIFICATE----- |
| 34 | MIIDXTCCAkWgAwIBAgIJAKoK/heBjcOuMA0GCSqGSIb3DQEBBQUAMEUxCzAJBgNV |
| 35 | BAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRlcm5ldCBX |
| 36 | aWRnaXRzIFB0eSBMdGQwHhcNMTcwODI4MTkzNDA5WhcNMTgwODI4MTkzNDA5WjBF |
| 37 | -----END CERTIFICATE-----`; |
| 38 | |
| 39 | const caCertPath = path.join(tempDir, "ca-bundle.pem"); |
| 40 | fs.writeFileSync(caCertPath, customCaCert); |
| 41 | |
| 42 | const options = await getAgentOptions({ caBundlePath: caCertPath }); |
| 43 | |
| 44 | expect(options.ca).toBeDefined(); |
| 45 | expect(Array.isArray(options.ca)).toBe(true); |
| 46 | expect(options.ca).toContain(customCaCert); |
| 47 | }); |
| 48 | |
| 49 | test("should handle NODE_EXTRA_CA_CERTS environment variable", async () => { |
| 50 | const extraCaCert = `-----BEGIN CERTIFICATE----- |
| 51 | MIIDUzCCAjugAwIBAgIJALvxFjX5V+/vMA0GCSqGSIb3DQEBCwUAMDgxCzAJBgNV |
| 52 | BAYTAlVTMRAwDgYDVQQIDAdDb21wYW55MRcwFQYDVQQKDA5Db21wYW55IENvcnAw |
| 53 | -----END CERTIFICATE-----`; |
| 54 | |
| 55 | const extraCaPath = path.join(tempDir, "extra-ca.crt"); |
| 56 | fs.writeFileSync(extraCaPath, extraCaCert); |
| 57 | |
| 58 | process.env.NODE_EXTRA_CA_CERTS = extraCaPath; |
| 59 | |
| 60 | const options = await getAgentOptions(); |
| 61 | |
| 62 | expect(options.ca).toBeDefined(); |
| 63 | expect(Array.isArray(options.ca)).toBe(true); |
| 64 | expect(options.ca).toContain(extraCaCert); |
| 65 | }); |
| 66 | |
| 67 | test("should combine NODE_EXTRA_CA_CERTS with custom CA bundle", async () => { |
| 68 | const extraCaCert = `-----BEGIN CERTIFICATE----- |
| 69 | MIIDExtra1234567890abcdefghijklmnopqrstuvwxyz |
| 70 | -----END CERTIFICATE-----`; |
| 71 | |
| 72 | const customCaCert = `-----BEGIN CERTIFICATE----- |
| 73 | MIIDCustom1234567890abcdefghijklmnopqrstuvwxyz |
| 74 | -----END CERTIFICATE-----`; |
| 75 | |
| 76 | const extraCaPath = path.join(tempDir, "extra-ca.crt"); |
| 77 | const customCaPath = path.join(tempDir, "custom-ca.pem"); |
| 78 | |
| 79 | fs.writeFileSync(extraCaPath, extraCaCert); |
| 80 | fs.writeFileSync(customCaPath, customCaCert); |
| 81 | |
| 82 | process.env.NODE_EXTRA_CA_CERTS = extraCaPath; |
| 83 | |
| 84 | const options = await getAgentOptions({ caBundlePath: customCaPath }); |
| 85 | |
| 86 | expect(options.ca).toBeDefined(); |
| 87 | expect(Array.isArray(options.ca)).toBe(true); |
| 88 | expect(options.ca).toContain(extraCaCert); |
| 89 | expect(options.ca).toContain(customCaCert); |
| 90 | }); |
| 91 | }); |
| 92 | |
| 93 | describe("Data URI certificate handling", () => { |
| 94 | test("should handle base64 encoded data URI certificates", () => { |
| 95 | const originalCert = `-----BEGIN CERTIFICATE----- |
| 96 | MIIDUzCCAjugAwIBAgIJALvxFjX5V+/vMA0GCSqGSIb3DQEB |
| 97 | -----END CERTIFICATE-----`; |
| 98 | |
| 99 | const base64Data = Buffer.from(originalCert).toString("base64"); |
| 100 | const dataUri = `data:application/x-pem-file;base64,${base64Data}`; |
| 101 | |
| 102 | const result = getCertificateContent(dataUri); |
| 103 | |
| 104 | expect(result).toBe(originalCert); |
| 105 | }); |
| 106 | |
| 107 | test("should handle URL-encoded data URI certificates", () => { |
| 108 | const originalCert = |
| 109 | "certificate with spaces and special chars: !@#$%^&*()"; |
| 110 | const encodedData = encodeURIComponent(originalCert); |
| 111 | const dataUri = `data:text/plain,${encodedData}`; |
| 112 | |
| 113 | const result = getCertificateContent(dataUri); |
| 114 | |
| 115 | expect(result).toBe(originalCert); |
| 116 | }); |
| 117 | }); |
| 118 | |
| 119 | describe("SSL/TLS configuration", () => { |
| 120 | test("should configure SSL verification correctly", async () => { |
| 121 | // Test with SSL verification enabled |
| 122 | let options = await getAgentOptions({ verifySsl: true }); |
| 123 | expect(options.rejectUnauthorized).toBe(true); |
| 124 | |
| 125 | // Test with SSL verification disabled |
| 126 | options = await getAgentOptions({ verifySsl: false }); |
| 127 | expect(options.rejectUnauthorized).toBe(false); |
| 128 | }); |
| 129 | |
| 130 | test("should configure timeout correctly", async () => { |
| 131 | const customTimeout = 60; // 1 minute in seconds |
| 132 | const options = await getAgentOptions({ timeout: customTimeout }); |
| 133 | |
| 134 | expect(options.timeout).toBe(customTimeout * 1000); // Should be converted to milliseconds |
| 135 | }); |
| 136 | |
| 137 | test("should handle client certificate authentication", async () => { |
| 138 | const clientCert = `-----BEGIN CERTIFICATE----- |
| 139 | MIIDClientCert1234567890abcdefghijklmnopqrstuvwxyz |
| 140 | -----END CERTIFICATE-----`; |
| 141 | |
| 142 | const clientKey = `-----BEGIN PRIVATE KEY----- |
| 143 | MIIEClientKey567890abcdefghijklmnopqrstuvwxyz |
| 144 | -----END PRIVATE KEY-----`; |
| 145 | |
| 146 | const clientCertPath = path.join(tempDir, "client.crt"); |
| 147 | const clientKeyPath = path.join(tempDir, "client.key"); |
| 148 | |
| 149 | fs.writeFileSync(clientCertPath, clientCert); |
| 150 | fs.writeFileSync(clientKeyPath, clientKey); |
| 151 | |
| 152 | const options = await getAgentOptions({ |
| 153 | clientCertificate: { |
| 154 | cert: clientCertPath, |
| 155 | key: clientKeyPath, |
| 156 | passphrase: "test-passphrase", |
| 157 | }, |
| 158 | }); |
| 159 | |
| 160 | expect(options.cert).toBe(clientCert); |
| 161 | expect(options.key).toBe(clientKey); |
| 162 | expect(options.passphrase).toBe("test-passphrase"); |
| 163 | }); |
| 164 | }); |
| 165 | |
| 166 | describe("Certificate caching behavior", () => { |
| 167 | test("should cache certificate content to avoid repeated file reads", async () => { |
| 168 | const certsCache = CertsCache.getInstance(); |
| 169 | const certPath = path.join(tempDir, "cached-cert.pem"); |
| 170 | const certContent = `-----BEGIN CERTIFICATE----- |
| 171 | MIIDCachedCert1234567890abcdefghijklmnopqrstuvwxyz |
| 172 | -----END CERTIFICATE-----`; |
| 173 | |
| 174 | fs.writeFileSync(certPath, certContent); |
| 175 | |
| 176 | // First call should read from file |
| 177 | const result1 = await certsCache.getCachedCustomCert(certPath); |
| 178 | expect(result1).toBe(certContent); |
| 179 | |
| 180 | // Modify the file on disk |
| 181 | const modifiedContent = certContent.replace("CachedCert", "ModifiedCert"); |
| 182 | fs.writeFileSync(certPath, modifiedContent); |
| 183 | |
| 184 | // Second call should return cached content, not the modified content |
| 185 | const result2 = await certsCache.getCachedCustomCert(certPath); |
| 186 | expect(result2).toBe(certContent); // Should still be the original cached content |
| 187 | }); |
| 188 | |
| 189 | test("should handle cache clearing", async () => { |
| 190 | const certsCache = CertsCache.getInstance(); |
| 191 | const certPath = path.join(tempDir, "clear-cache-cert.pem"); |
| 192 | const originalContent = `-----BEGIN CERTIFICATE----- |
| 193 | MIIDOriginalCert1234567890abcdefghijklmnopqrstuvwxyz |
| 194 | -----END CERTIFICATE-----`; |
| 195 | |
| 196 | fs.writeFileSync(certPath, originalContent); |
| 197 | |
| 198 | // Load into cache |
| 199 | await certsCache.getCachedCustomCert(certPath); |
| 200 | |
| 201 | // Clear cache |
| 202 | certsCache.clear(); |
| 203 | |
| 204 | // Modify file |
| 205 | const newContent = originalContent.replace("OriginalCert", "NewCert"); |
| 206 | fs.writeFileSync(certPath, newContent); |
| 207 | |
| 208 | // Should read new content after cache clear |
| 209 | const result2 = await certsCache.getCachedCustomCert(certPath); |
| 210 | expect(result2).toBe(newContent); |
| 211 | }); |
| 212 | }); |
| 213 | |
| 214 | describe("Error handling scenarios", () => { |
| 215 | test("should handle non-existent certificate files gracefully", async () => { |
| 216 | const nonExistentPath = path.join(tempDir, "does-not-exist.pem"); |
| 217 | |
| 218 | // Should not throw but should continue with system certificates only |
| 219 | const options = await getAgentOptions({ caBundlePath: nonExistentPath }); |
| 220 | |
| 221 | expect(options.ca).toBeDefined(); |
| 222 | expect(Array.isArray(options.ca)).toBe(true); |
| 223 | // Should only contain system certificates, not the missing custom one |
| 224 | expect(options.ca.length).toBeGreaterThan(0); |
| 225 | }); |
| 226 | |
| 227 | test("should handle empty certificate files", () => { |
| 228 | const emptyCertPath = path.join(tempDir, "empty-cert.pem"); |
| 229 | fs.writeFileSync(emptyCertPath, ""); |
| 230 | |
| 231 | const result = getCertificateContent(emptyCertPath); |
| 232 | expect(result).toBe(""); |
| 233 | }); |
| 234 | |
| 235 | test("should handle malformed data URIs", () => { |
| 236 | const validMalformedDataUris = ["data:invalid-format", "data:"]; |
| 237 | |
| 238 | validMalformedDataUris.forEach((uri) => { |
| 239 | // These should not throw but may return unexpected content |
| 240 | expect(() => getCertificateContent(uri)).not.toThrow(); |
| 241 | const result = getCertificateContent(uri); |
| 242 | expect(typeof result).toBe("string"); |
| 243 | }); |
| 244 | |
| 245 | // This one will be treated as a file path and should throw |
| 246 | expect(() => getCertificateContent("not-a-data-uri-at-all")).toThrow( |
| 247 | "ENOENT", |
| 248 | ); |
| 249 | }); |
| 250 | }); |
| 251 | |
| 252 | describe("Real-world scenarios", () => { |
| 253 | test("should handle certificate bundle with multiple certificates", () => { |
| 254 | const bundleContent = `-----BEGIN CERTIFICATE----- |
| 255 | MIIDRootCA1234567890abcdefghijklmnopqrstuvwxyz |
| 256 | -----END CERTIFICATE----- |
| 257 | -----BEGIN CERTIFICATE----- |
| 258 | MIIDIntermediateCA1234567890abcdefghijklmnopqrstuvwxyz |
| 259 | -----END CERTIFICATE-----`; |
| 260 | |
| 261 | const bundlePath = path.join(tempDir, "ca-bundle.pem"); |
| 262 | fs.writeFileSync(bundlePath, bundleContent); |
| 263 | |
| 264 | const result = getCertificateContent(bundlePath); |
| 265 | expect(result).toBe(bundleContent); |
| 266 | expect(result).toContain("RootCA"); |
| 267 | expect(result).toContain("IntermediateCA"); |
| 268 | }); |
| 269 | |
| 270 | test("should work with common certificate file extensions", () => { |
| 271 | const certExtensions = [".pem", ".crt", ".cer"]; |
| 272 | const testCert = `-----BEGIN CERTIFICATE----- |
| 273 | MIIDTestCert1234567890abcdefghijklmnopqrstuvwxyz |
| 274 | -----END CERTIFICATE-----`; |
| 275 | |
| 276 | for (const ext of certExtensions) { |
| 277 | const certPath = path.join(tempDir, `test-cert${ext}`); |
| 278 | fs.writeFileSync(certPath, testCert); |
| 279 | |
| 280 | const result = getCertificateContent(certPath); |
| 281 | expect(result).toBe(testCert); |
| 282 | } |
| 283 | }); |
| 284 | |
| 285 | test("should handle proxy scenarios with custom certificates", async () => { |
| 286 | // Test that certificates work properly when proxy might be involved |
| 287 | const proxyCaCert = `-----BEGIN CERTIFICATE----- |
| 288 | MIIDProxyCA1234567890abcdefghijklmnopqrstuvwxyz |
| 289 | -----END CERTIFICATE-----`; |
| 290 | |
| 291 | const proxyCaPath = path.join(tempDir, "proxy-ca.pem"); |
| 292 | fs.writeFileSync(proxyCaPath, proxyCaCert); |
| 293 | |
| 294 | const options = await getAgentOptions({ caBundlePath: proxyCaPath }); |
| 295 | |
| 296 | expect(options.ca).toBeDefined(); |
| 297 | expect(options.ca).toContain(proxyCaCert); |
| 298 | expect(options.keepAlive).toBe(true); // Should work with keep-alive for proxy scenarios |
| 299 | }); |
| 300 | }); |
| 301 | }); |