mirror of
https://github.com/Studio-Saelix/sencho.git
synced 2026-08-11 11:16:55 +00:00
fix(pilot): let SENCHO_PUBLIC_URL override the request Host in enrollment (#1122)
The enrollment minter inferred SENCHO_PRIMARY_URL from the request Host header, which baked loopback or LAN addresses into the compose YAML when the admin opened Add Node on the central's own machine. Pilots on a different network (a public cloud VPS, for example) cannot dial that. SENCHO_PUBLIC_URL on the primary now wins when set and well-formed (http(s)://, no loopback). Trailing slashes are stripped. Falls back to the request Host when unset or invalid.
This commit is contained in:
@@ -134,6 +134,71 @@ describe('POST /api/nodes (pilot_agent mode)', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('SENCHO_PUBLIC_URL override in mintPilotEnrollment', () => {
|
||||
const ORIGINAL = process.env.SENCHO_PUBLIC_URL;
|
||||
afterAll(() => {
|
||||
if (ORIGINAL === undefined) delete process.env.SENCHO_PUBLIC_URL;
|
||||
else process.env.SENCHO_PUBLIC_URL = ORIGINAL;
|
||||
});
|
||||
|
||||
it('bakes the env-var URL into composeYaml when set and valid', async () => {
|
||||
process.env.SENCHO_PUBLIC_URL = 'https://sencho.example.com';
|
||||
const res = await request(app)
|
||||
.post('/api/nodes')
|
||||
.set('Cookie', adminCookie)
|
||||
.send({ name: 'pilot-public-url-set', type: 'remote', mode: 'pilot_agent' });
|
||||
|
||||
const parsed = parseYaml(res.body.enrollment.composeYaml) as ComposeFile;
|
||||
expect(parsed.services.agent.environment.SENCHO_PRIMARY_URL).toBe('https://sencho.example.com');
|
||||
});
|
||||
|
||||
it('strips a trailing slash from the env-var URL', async () => {
|
||||
process.env.SENCHO_PUBLIC_URL = 'https://sencho.example.com/';
|
||||
const res = await request(app)
|
||||
.post('/api/nodes')
|
||||
.set('Cookie', adminCookie)
|
||||
.send({ name: 'pilot-public-url-trailing', type: 'remote', mode: 'pilot_agent' });
|
||||
|
||||
const parsed = parseYaml(res.body.enrollment.composeYaml) as ComposeFile;
|
||||
expect(parsed.services.agent.environment.SENCHO_PRIMARY_URL).toBe('https://sencho.example.com');
|
||||
});
|
||||
|
||||
it('falls back to request host when env var is unset', async () => {
|
||||
delete process.env.SENCHO_PUBLIC_URL;
|
||||
const res = await request(app)
|
||||
.post('/api/nodes')
|
||||
.set('Cookie', adminCookie)
|
||||
.send({ name: 'pilot-public-url-unset', type: 'remote', mode: 'pilot_agent' });
|
||||
|
||||
const parsed = parseYaml(res.body.enrollment.composeYaml) as ComposeFile;
|
||||
// Supertest sends requests with host = `127.0.0.1:<ephemeral-port>`,
|
||||
// so the fallback URL must reflect that, not the env-var override.
|
||||
expect(parsed.services.agent.environment.SENCHO_PRIMARY_URL).toMatch(/^http:\/\/127\.0\.0\.1:\d+$/);
|
||||
});
|
||||
|
||||
it('falls back to request host when env var is malformed', async () => {
|
||||
process.env.SENCHO_PUBLIC_URL = 'not a url';
|
||||
const res = await request(app)
|
||||
.post('/api/nodes')
|
||||
.set('Cookie', adminCookie)
|
||||
.send({ name: 'pilot-public-url-bad', type: 'remote', mode: 'pilot_agent' });
|
||||
|
||||
const parsed = parseYaml(res.body.enrollment.composeYaml) as ComposeFile;
|
||||
expect(parsed.services.agent.environment.SENCHO_PRIMARY_URL).toMatch(/^http:\/\/127\.0\.0\.1:\d+$/);
|
||||
});
|
||||
|
||||
it('rejects a loopback URL and falls back to the request host', async () => {
|
||||
process.env.SENCHO_PUBLIC_URL = 'http://127.0.0.1:1852';
|
||||
const res = await request(app)
|
||||
.post('/api/nodes')
|
||||
.set('Cookie', adminCookie)
|
||||
.send({ name: 'pilot-public-url-loopback', type: 'remote', mode: 'pilot_agent' });
|
||||
|
||||
const parsed = parseYaml(res.body.enrollment.composeYaml) as ComposeFile;
|
||||
expect(parsed.services.agent.environment.SENCHO_PRIMARY_URL).toMatch(/^http:\/\/127\.0\.0\.1:\d+$/);
|
||||
});
|
||||
});
|
||||
|
||||
describe('POST /api/nodes/:id/pilot/enroll', () => {
|
||||
it('regenerates the enrollment for an existing pilot node', async () => {
|
||||
const create = await request(app)
|
||||
|
||||
@@ -22,6 +22,27 @@ import { getErrorMessage } from '../utils/errors';
|
||||
const NODE_SCOPE_MESSAGE = 'API tokens cannot manage nodes.';
|
||||
const REMOTE_META_CACHE_TTL = 3 * 60 * 1000;
|
||||
|
||||
/**
|
||||
* Pick the URL the pilot agent should dial. SENCHO_PUBLIC_URL wins when set
|
||||
* and well-formed, because the request Host header is only reachable from
|
||||
* the network the operator opened the dialog from. Pilots on a public cloud
|
||||
* cannot dial a LAN or loopback address, so an explicit public URL is the
|
||||
* only thing that lets the enrolled YAML work unmodified.
|
||||
*/
|
||||
function resolvePrimaryUrl(req: Request): string {
|
||||
const override = process.env.SENCHO_PUBLIC_URL?.trim();
|
||||
if (override) {
|
||||
const check = isValidRemoteUrl(override);
|
||||
if (check.valid) return override.replace(/\/$/, '');
|
||||
console.warn(`[Enrollment] SENCHO_PUBLIC_URL is set but invalid (${check.reason}); falling back to request host.`);
|
||||
}
|
||||
const forwardedProto = req.headers['x-forwarded-proto'];
|
||||
const protoHeader = Array.isArray(forwardedProto) ? forwardedProto[0] : forwardedProto;
|
||||
const protocol = protoHeader || req.protocol || 'http';
|
||||
const host = req.get('host') || 'localhost:1852';
|
||||
return `${protocol}://${host}`;
|
||||
}
|
||||
|
||||
function mintPilotEnrollment(nodeId: number, req: Request): { token: string; expiresAt: number; composeYaml: string } {
|
||||
const db = DatabaseService.getInstance();
|
||||
const jwtSecret = db.getGlobalSettings().auth_jwt_secret;
|
||||
@@ -38,11 +59,7 @@ function mintPilotEnrollment(nodeId: number, req: Request): { token: string; exp
|
||||
const tokenHash = crypto.createHash('sha256').update(token).digest('hex');
|
||||
db.createPilotEnrollment(nodeId, tokenHash, expiresAt);
|
||||
|
||||
const forwardedProto = req.headers['x-forwarded-proto'];
|
||||
const protoHeader = Array.isArray(forwardedProto) ? forwardedProto[0] : forwardedProto;
|
||||
const protocol = protoHeader || req.protocol || 'http';
|
||||
const host = req.get('host') || 'localhost:1852';
|
||||
const primaryUrl = `${protocol}://${host}`;
|
||||
const primaryUrl = resolvePrimaryUrl(req);
|
||||
|
||||
// Top-level `name` plus `container_name` make the agent container's HOSTNAME
|
||||
// equal to `sencho-agent`, which is how SelfUpdateService locates its own
|
||||
|
||||
Reference in New Issue
Block a user