smartproxy/test/test.certificate-provisioning.ts

141 lines
3.6 KiB
TypeScript
Raw Permalink Normal View History

2025-05-10 15:09:58 +00:00
import { SmartProxy } from '../ts/proxies/smart-proxy/index.js';
2025-05-19 12:04:26 +00:00
import { expect, tap } from '@git.zone/tstest/tapbundle';
2025-05-10 15:09:58 +00:00
2025-05-18 15:38:07 +00:00
const testProxy = new SmartProxy({
routes: [{
name: 'test-route',
match: { ports: 443, domains: 'test.example.com' },
action: {
type: 'forward',
target: { host: 'localhost', port: 8080 },
tls: {
2025-05-18 15:38:07 +00:00
mode: 'terminate',
certificate: 'auto',
acme: {
email: 'test@example.com',
useProduction: false
}
2025-05-10 15:09:58 +00:00
}
2025-05-18 15:38:07 +00:00
}
}]
});
2025-05-10 15:09:58 +00:00
2025-05-18 15:38:07 +00:00
tap.test('should provision certificate automatically', async () => {
await testProxy.start();
2025-05-10 15:09:58 +00:00
2025-05-18 15:38:07 +00:00
// Wait for certificate provisioning
await new Promise(resolve => setTimeout(resolve, 5000));
2025-05-10 15:09:58 +00:00
2025-05-18 15:38:07 +00:00
const status = testProxy.getCertificateStatus('test-route');
expect(status).toBeDefined();
expect(status.status).toEqual('valid');
expect(status.source).toEqual('acme');
await testProxy.stop();
2025-05-10 15:09:58 +00:00
});
2025-05-18 15:38:07 +00:00
tap.test('should handle static certificates', async () => {
const proxy = new SmartProxy({
routes: [{
name: 'static-route',
match: { ports: 443, domains: 'static.example.com' },
action: {
type: 'forward',
target: { host: 'localhost', port: 8080 },
tls: {
mode: 'terminate',
certificate: {
cert: '-----BEGIN CERTIFICATE-----\nMIIC...\n-----END CERTIFICATE-----',
key: '-----BEGIN PRIVATE KEY-----\nMIIE...\n-----END PRIVATE KEY-----'
2025-05-18 15:38:07 +00:00
}
}
}
}]
});
2025-05-10 15:09:58 +00:00
2025-05-18 15:38:07 +00:00
await proxy.start();
2025-05-10 15:09:58 +00:00
2025-05-18 15:38:07 +00:00
const status = proxy.getCertificateStatus('static-route');
expect(status).toBeDefined();
expect(status.status).toEqual('valid');
expect(status.source).toEqual('static');
2025-05-10 15:09:58 +00:00
2025-05-18 15:38:07 +00:00
await proxy.stop();
2025-05-10 15:09:58 +00:00
});
2025-05-18 15:38:07 +00:00
tap.test('should handle ACME challenge routes', async () => {
const proxy = new SmartProxy({
routes: [{
name: 'auto-cert-route',
match: { ports: 443, domains: 'acme.example.com' },
action: {
type: 'forward',
target: { host: 'localhost', port: 8080 },
tls: {
mode: 'terminate',
certificate: 'auto',
acme: {
email: 'acme@example.com',
useProduction: false,
challengePort: 80
}
}
}
}, {
name: 'port-80-route',
match: { ports: 80, domains: 'acme.example.com' },
action: {
type: 'forward',
target: { host: 'localhost', port: 8080 }
}
}]
2025-05-10 15:09:58 +00:00
});
2025-05-18 15:38:07 +00:00
await proxy.start();
2025-05-10 15:09:58 +00:00
2025-05-18 15:38:07 +00:00
// The SmartCertManager should automatically add challenge routes
// Let's verify the route manager sees them
const routes = proxy.routeManager.getAllRoutes();
const challengeRoute = routes.find(r => r.name === 'acme-challenge');
expect(challengeRoute).toBeDefined();
expect(challengeRoute?.match.path).toEqual('/.well-known/acme-challenge/*');
expect(challengeRoute?.priority).toEqual(1000);
await proxy.stop();
2025-05-10 15:09:58 +00:00
});
2025-05-18 15:38:07 +00:00
tap.test('should renew certificates', async () => {
const proxy = new SmartProxy({
routes: [{
name: 'renew-route',
match: { ports: 443, domains: 'renew.example.com' },
action: {
type: 'forward',
target: { host: 'localhost', port: 8080 },
tls: {
mode: 'terminate',
certificate: 'auto',
acme: {
email: 'renew@example.com',
useProduction: false,
renewBeforeDays: 30
}
}
}
}]
});
2025-05-10 15:09:58 +00:00
2025-05-18 15:38:07 +00:00
await proxy.start();
2025-05-10 15:09:58 +00:00
2025-05-18 15:38:07 +00:00
// Force renewal
await proxy.renewCertificate('renew-route');
2025-05-10 15:09:58 +00:00
2025-05-18 15:38:07 +00:00
const status = proxy.getCertificateStatus('renew-route');
expect(status).toBeDefined();
expect(status.status).toEqual('valid');
2025-05-10 15:09:58 +00:00
2025-05-18 15:38:07 +00:00
await proxy.stop();
2025-05-10 15:09:58 +00:00
});
2025-05-18 15:38:07 +00:00
tap.start();