@celilo/cli 0.24.1 → 0.25.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CELILO_CORE_MODULES.md +2 -2
- package/CELILO_SUBSYSTEMS.md +3 -1
- package/package.json +2 -2
- package/schemas/system_config.json +10 -5
- package/src/cli/commands/system-apply-config-equivalence.test.ts +46 -8
- package/src/cli/commands/system-apply-config.test.ts +57 -1
- package/src/cli/commands/system-apply-config.ts +43 -0
- package/src/cli/commands/system-discover-network.ts +35 -0
- package/src/cli/completion.ts +1 -0
- package/src/cli/index.ts +9 -0
- package/src/cli/stdout-is-undecorated.test.ts +7 -1
- package/src/db/client.test.ts +63 -0
- package/src/db/client.ts +9 -2
- package/src/hooks/capability-loader.ts +33 -17
- package/src/manifest/network-requirement-schema.test.ts +141 -0
- package/src/manifest/schema.ts +124 -0
- package/src/services/module-deploy.ts +31 -3
- package/src/services/network-discovery.test.ts +198 -0
- package/src/services/network-discovery.ts +164 -0
- package/src/services/network-ensure.test.ts +324 -0
- package/src/services/network-ensure.ts +260 -0
- package/src/services/system-init.ts +14 -3
- package/src/templates/dns-ingress-ip.test.ts +123 -0
- package/src/templates/generator.ts +66 -43
- package/src/test-utils/bus-responder.ts +14 -1
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `requires.networks` — a module NAMES the networks it depends on and never
|
|
3
|
+
* carries their values
|
|
4
|
+
* (openspec/changes/networks-are-declared-not-written/specs/network-declaration/spec.md).
|
|
5
|
+
*
|
|
6
|
+
* The rejection below is the schema-level half of "a module has no path to
|
|
7
|
+
* create a network definition". The other half is the write path
|
|
8
|
+
* ([[cli/commands/system-apply-config.ts]]).
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { describe, expect, test } from 'bun:test';
|
|
12
|
+
import { NetworkRequirementSchema, getRequiredNetworkNames } from './schema';
|
|
13
|
+
import type { ModuleManifest } from './schema';
|
|
14
|
+
|
|
15
|
+
describe('NetworkRequirementSchema', () => {
|
|
16
|
+
test('a requirement is just a name', () => {
|
|
17
|
+
expect(NetworkRequirementSchema.safeParse({ name: 'control-plane-vpn' }).success).toBe(true);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
test('names are kebab-case, like every other user-facing identifier', () => {
|
|
21
|
+
expect(NetworkRequirementSchema.safeParse({ name: 'control_plane_vpn' }).success).toBe(false);
|
|
22
|
+
expect(NetworkRequirementSchema.safeParse({ name: 'DMZ' }).success).toBe(false);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
test('a requirement carrying a subnet is rejected, and the message says why', () => {
|
|
26
|
+
const result = NetworkRequirementSchema.safeParse({
|
|
27
|
+
name: 'control-plane-vpn',
|
|
28
|
+
subnet: '10.255.255.0/24',
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
expect(result.success).toBe(false);
|
|
32
|
+
if (result.success) return;
|
|
33
|
+
const message = result.error.errors.map((e) => e.message).join(' ');
|
|
34
|
+
expect(message).toContain('celilo owns');
|
|
35
|
+
expect(message).toContain('$system:network.<name>.subnet');
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
test('a gateway or vlan on the requirement is rejected too', () => {
|
|
39
|
+
expect(NetworkRequirementSchema.safeParse({ name: 'dmz', gateway: '10.0.10.1' }).success).toBe(
|
|
40
|
+
false,
|
|
41
|
+
);
|
|
42
|
+
expect(NetworkRequirementSchema.safeParse({ name: 'dmz', vlan: 10 }).success).toBe(false);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* `from:` is the second form, for a module whose required set is decided per
|
|
47
|
+
* install rather than at authoring time — a firewall requires the networks it
|
|
48
|
+
* has legs on, and which legs it has is a property of the box it lands on.
|
|
49
|
+
* It still names networks and still carries no values.
|
|
50
|
+
*/
|
|
51
|
+
test('a requirement may name a config array instead of a literal', () => {
|
|
52
|
+
expect(NetworkRequirementSchema.safeParse({ from: '$self:zones' }).success).toBe(true);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
test("from must reference this module's own config, not another source", () => {
|
|
56
|
+
// `$machine:` / `$capability:` would make some other thing the author of the
|
|
57
|
+
// required set, which is the authority question this change exists to settle.
|
|
58
|
+
expect(NetworkRequirementSchema.safeParse({ from: '$machine:zones' }).success).toBe(false);
|
|
59
|
+
expect(NetworkRequirementSchema.safeParse({ from: '$capability:firewall.zones' }).success).toBe(
|
|
60
|
+
false,
|
|
61
|
+
);
|
|
62
|
+
expect(NetworkRequirementSchema.safeParse({ from: 'zones' }).success).toBe(false);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
test('exactly one of name or from — neither both nor neither', () => {
|
|
66
|
+
expect(NetworkRequirementSchema.safeParse({ name: 'dmz', from: '$self:zones' }).success).toBe(
|
|
67
|
+
false,
|
|
68
|
+
);
|
|
69
|
+
expect(NetworkRequirementSchema.safeParse({}).success).toBe(false);
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
describe('getRequiredNetworkNames', () => {
|
|
74
|
+
function manifestWith(
|
|
75
|
+
networks: Array<{ name?: string; from?: string }> | undefined,
|
|
76
|
+
): ModuleManifest {
|
|
77
|
+
return {
|
|
78
|
+
requires: { capabilities: [], networks },
|
|
79
|
+
} as unknown as ModuleManifest;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
test('is empty for a module that requires no network', () => {
|
|
83
|
+
expect(getRequiredNetworkNames(manifestWith(undefined))).toEqual([]);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
test('keeps declaration order and drops duplicates', () => {
|
|
87
|
+
expect(
|
|
88
|
+
getRequiredNetworkNames(manifestWith([{ name: 'dmz' }, { name: 'app' }, { name: 'dmz' }])),
|
|
89
|
+
).toEqual(['dmz', 'app']);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
test("resolves a from: requirement against the module's own config", () => {
|
|
93
|
+
expect(
|
|
94
|
+
getRequiredNetworkNames(manifestWith([{ from: '$self:zones' }]), {
|
|
95
|
+
zones: ['dmz', 'app', 'secure'],
|
|
96
|
+
}),
|
|
97
|
+
).toEqual(['dmz', 'app', 'secure']);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
test('an array stored as JSON resolves too', () => {
|
|
101
|
+
// Array config arrives as either shape depending on how it was written, and
|
|
102
|
+
// a requirement that silently resolved to nothing would leave a firewall leg
|
|
103
|
+
// undeclared — which is how an interface ends up classified alien.
|
|
104
|
+
expect(
|
|
105
|
+
getRequiredNetworkNames(manifestWith([{ from: '$self:zones' }]), {
|
|
106
|
+
zones: '["dmz","secure-mgmt"]',
|
|
107
|
+
}),
|
|
108
|
+
).toEqual(['dmz', 'secure-mgmt']);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* `external` is the RESIDUAL: an interface is external because it is publicly
|
|
113
|
+
* routable and matched no declared zone, never because it is contained in a
|
|
114
|
+
* subnet. Giving it one would reintroduce the overload that
|
|
115
|
+
* `readDeclaredNetworks` and `deployFirewall` both already exclude.
|
|
116
|
+
*/
|
|
117
|
+
test('external is dropped, however it is named', () => {
|
|
118
|
+
expect(
|
|
119
|
+
getRequiredNetworkNames(manifestWith([{ from: '$self:zones' }]), {
|
|
120
|
+
zones: ['dmz', 'external'],
|
|
121
|
+
}),
|
|
122
|
+
).toEqual(['dmz']);
|
|
123
|
+
expect(getRequiredNetworkNames(manifestWith([{ name: 'external' }]))).toEqual([]);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
test('an unanswered config array requires nothing rather than throwing', () => {
|
|
127
|
+
// The firewall's zones are answered by the config interview, which runs
|
|
128
|
+
// before this. An empty result here means the interview has not happened
|
|
129
|
+
// yet, not that the module needs no networks.
|
|
130
|
+
expect(getRequiredNetworkNames(manifestWith([{ from: '$self:zones' }]), {})).toEqual([]);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
test('literal and dynamic requirements combine', () => {
|
|
134
|
+
expect(
|
|
135
|
+
getRequiredNetworkNames(
|
|
136
|
+
manifestWith([{ name: 'control-plane-vpn' }, { from: '$self:zones' }]),
|
|
137
|
+
{ zones: ['dmz'] },
|
|
138
|
+
),
|
|
139
|
+
).toEqual(['control-plane-vpn', 'dmz']);
|
|
140
|
+
});
|
|
141
|
+
});
|
package/src/manifest/schema.ts
CHANGED
|
@@ -354,6 +354,68 @@ export const SystemResourceSchema = z.object({
|
|
|
354
354
|
zone: z.enum(NETWORK_ZONES).describe('Required security zone for this module'),
|
|
355
355
|
});
|
|
356
356
|
|
|
357
|
+
/**
|
|
358
|
+
* A network a module depends on, declared under `requires.networks`
|
|
359
|
+
* (openspec/changes/networks-are-declared-not-written/specs/network-declaration/spec.md).
|
|
360
|
+
*
|
|
361
|
+
* It names the network and stops there. celilo owns the network namespace: the
|
|
362
|
+
* range belongs to the operator's topology, not to whichever module happens to
|
|
363
|
+
* arrive first and need it. A module that carried the value here would be
|
|
364
|
+
* defining the network by another route, which is the authority this change
|
|
365
|
+
* reverses — so `.strict()` is load-bearing rather than tidiness, and its
|
|
366
|
+
* message says why.
|
|
367
|
+
*
|
|
368
|
+
* When the named network has no `network.<name>.subnet` in system config, the
|
|
369
|
+
* deploy asks for one BEFORE anything is generated or any hook runs
|
|
370
|
+
* ([[services/network-ensure.ts]]).
|
|
371
|
+
*
|
|
372
|
+
* ── Two forms, because some requirements are not knowable at authoring time ──
|
|
373
|
+
*
|
|
374
|
+
* `name:` is a literal — `wireguard` always needs `control-plane-vpn`, and says
|
|
375
|
+
* so once. `from:` names a config array whose VALUES are the network names, for
|
|
376
|
+
* a module whose set is decided per install: a firewall requires the networks it
|
|
377
|
+
* has legs on, and which legs it has is a property of the box it lands on.
|
|
378
|
+
*
|
|
379
|
+
* `from:` is not a loophole in "a requirement carries no value". It still names
|
|
380
|
+
* networks and still carries none — it just names them indirectly. What it fixes
|
|
381
|
+
* is the split that `firewall-interface-classification` §3 already diagnosed from
|
|
382
|
+
* the harness side: the legs a firewall has and the networks it declares were two
|
|
383
|
+
* hand-maintained lists, and every one of the ~20 call sites that had to keep
|
|
384
|
+
* them in step got it wrong. A leg whose network has no declared subnet
|
|
385
|
+
* classifies `alien`, so under-declaring is not cosmetic — it is how an interface
|
|
386
|
+
* gets isolated. One list makes that structurally impossible.
|
|
387
|
+
*/
|
|
388
|
+
export const NetworkRequirementSchema = z
|
|
389
|
+
.object({
|
|
390
|
+
name: z
|
|
391
|
+
.string()
|
|
392
|
+
.regex(/^[a-z0-9]+(-[a-z0-9]+)*$/, 'network name must be kebab-case')
|
|
393
|
+
.describe('Name of a network this module needs defined before it deploys')
|
|
394
|
+
.optional(),
|
|
395
|
+
from: z
|
|
396
|
+
.string()
|
|
397
|
+
.regex(
|
|
398
|
+
/^\$self:[a-zA-Z_][a-zA-Z0-9_]*$/,
|
|
399
|
+
'from must reference one of this module\'s own config values, e.g. "$self:zones"',
|
|
400
|
+
)
|
|
401
|
+
.describe('A config array whose values are network names, e.g. "$self:zones"')
|
|
402
|
+
.optional(),
|
|
403
|
+
})
|
|
404
|
+
.strict(
|
|
405
|
+
'A network requirement NAMES networks; it never carries their values. celilo owns ' +
|
|
406
|
+
'the network namespace — the range is supplied before the deploy runs — so a ' +
|
|
407
|
+
'requirement takes `name:` (a literal) or `from:` (a $self: config array of names) ' +
|
|
408
|
+
'and nothing else. Read the range with `source: system` / ' +
|
|
409
|
+
'`derive_from: "$system:network.<name>.subnet"`.',
|
|
410
|
+
)
|
|
411
|
+
.refine((requirement) => Boolean(requirement.name) !== Boolean(requirement.from), {
|
|
412
|
+
message:
|
|
413
|
+
'A network requirement declares exactly one of `name:` (a literal network) or ' +
|
|
414
|
+
'`from:` (a $self: config array whose values are network names)',
|
|
415
|
+
});
|
|
416
|
+
|
|
417
|
+
export type NetworkRequirement = z.infer<typeof NetworkRequirementSchema>;
|
|
418
|
+
|
|
357
419
|
/**
|
|
358
420
|
* One system a module deploys (openspec/specs/module-systems-addressing/spec.md). A module
|
|
359
421
|
* declares 0..N of these under `requires.systems`. `name` is the stable
|
|
@@ -505,6 +567,11 @@ export const ModuleManifestSchema = z
|
|
|
505
567
|
* `systems` gets one host per entry, each addressable as `$infra:<name>`.
|
|
506
568
|
*/
|
|
507
569
|
systems: z.array(SystemDeclarationSchema).optional(),
|
|
570
|
+
/**
|
|
571
|
+
* Networks that must be DEFINED in celilo's own config before this
|
|
572
|
+
* module deploys. Names only — see `NetworkRequirementSchema`.
|
|
573
|
+
*/
|
|
574
|
+
networks: z.array(NetworkRequirementSchema).optional(),
|
|
508
575
|
/**
|
|
509
576
|
* Singular form — sugar for a single unnamed system. Normalized to one
|
|
510
577
|
* `systems` entry (name `main`) by `getDeclaredSystems`. Optional because
|
|
@@ -857,3 +924,60 @@ export function getDeclaredSystems(manifest: ModuleManifest): SystemDeclaration[
|
|
|
857
924
|
export function getSingularSystemSpec(manifest: ModuleManifest): SystemResource | undefined {
|
|
858
925
|
return manifest.requires?.system;
|
|
859
926
|
}
|
|
927
|
+
|
|
928
|
+
/**
|
|
929
|
+
* The network names a module requires
|
|
930
|
+
* (openspec/changes/networks-are-declared-not-written/specs/network-declaration/spec.md).
|
|
931
|
+
* Deduplicated and in declaration order. Empty for the modules that depend on no
|
|
932
|
+
* network, which is most of them.
|
|
933
|
+
*
|
|
934
|
+
* `from:` entries are resolved against `config` — the module's own values, as
|
|
935
|
+
* answered by the config interview. A module whose required set is decided per
|
|
936
|
+
* install (a firewall requires the networks it has legs on) therefore needs its
|
|
937
|
+
* config populated before this is meaningful, which is why the ensure runs after
|
|
938
|
+
* the config interview and before generation.
|
|
939
|
+
*
|
|
940
|
+
* `external` is dropped wherever it appears. It is the RESIDUAL — an interface is
|
|
941
|
+
* `external` because it is publicly routable and matched no declared zone, never
|
|
942
|
+
* because it is contained in a subnet — so giving it one would reintroduce the
|
|
943
|
+
* overload that `readDeclaredNetworks` and `deployFirewall` both already exclude.
|
|
944
|
+
*/
|
|
945
|
+
export function getRequiredNetworkNames(
|
|
946
|
+
manifest: ModuleManifest,
|
|
947
|
+
config: Record<string, unknown> = {},
|
|
948
|
+
): string[] {
|
|
949
|
+
const names: string[] = [];
|
|
950
|
+
for (const requirement of manifest.requires?.networks ?? []) {
|
|
951
|
+
if (requirement.name) {
|
|
952
|
+
names.push(requirement.name);
|
|
953
|
+
continue;
|
|
954
|
+
}
|
|
955
|
+
if (!requirement.from) continue;
|
|
956
|
+
const key = requirement.from.slice('$self:'.length);
|
|
957
|
+
names.push(...asNetworkNameArray(config[key]));
|
|
958
|
+
}
|
|
959
|
+
return [...new Set(names)].filter((name) => name !== 'external');
|
|
960
|
+
}
|
|
961
|
+
|
|
962
|
+
/**
|
|
963
|
+
* Narrow a `from:`-referenced config value to network names.
|
|
964
|
+
*
|
|
965
|
+
* Tolerates the JSON-string form as well as a real array: an array config value
|
|
966
|
+
* arrives as either depending on how it was written, and a requirement that
|
|
967
|
+
* silently resolved to nothing would leave a leg undeclared — which is the
|
|
968
|
+
* failure this whole mechanism exists to prevent.
|
|
969
|
+
*/
|
|
970
|
+
function asNetworkNameArray(raw: unknown): string[] {
|
|
971
|
+
const value =
|
|
972
|
+
typeof raw === 'string' && raw.trim().startsWith('[')
|
|
973
|
+
? (() => {
|
|
974
|
+
try {
|
|
975
|
+
return JSON.parse(raw) as unknown;
|
|
976
|
+
} catch {
|
|
977
|
+
return undefined;
|
|
978
|
+
}
|
|
979
|
+
})()
|
|
980
|
+
: raw;
|
|
981
|
+
if (!Array.isArray(value)) return [];
|
|
982
|
+
return value.filter((entry): entry is string => typeof entry === 'string' && entry.length > 0);
|
|
983
|
+
}
|
|
@@ -38,6 +38,7 @@ import { getModuleSystems } from './deployed-systems';
|
|
|
38
38
|
import { E2E_CONFLICT_MESSAGE, runningE2eContainers } from './e2e-guard';
|
|
39
39
|
import { resolveInfrastructureVariables } from './infrastructure-variable-resolver';
|
|
40
40
|
import { findMachineForModule } from './machine-pool';
|
|
41
|
+
import { ensureRequiredNetworks } from './network-ensure';
|
|
41
42
|
import { checkProxmoxReachable, formatProxmoxUnreachableError } from './proxmox-preflight';
|
|
42
43
|
import { republishStaticWebConsumers } from './public-web-republish';
|
|
43
44
|
import { LOCAL_MACHINE_IP, deleteTemporarySshKey, writeTemporarySshKey } from './ssh-key-manager';
|
|
@@ -367,6 +368,32 @@ async function deployModuleImpl(
|
|
|
367
368
|
return { success: false, error: E2E_CONFLICT_MESSAGE, phases };
|
|
368
369
|
}
|
|
369
370
|
|
|
371
|
+
// Every network this module REQUIRES must be defined before ANYTHING else
|
|
372
|
+
// (openspec/changes/networks-are-declared-not-written/specs/network-declaration/spec.md).
|
|
373
|
+
//
|
|
374
|
+
// First, not merely "before the hooks". `validateAndPrepareDeployment`
|
|
375
|
+
// generates templates itself when no variable is missing, and generation is
|
|
376
|
+
// where `$system:` derivations resolve and get persisted. A module reading
|
|
377
|
+
// `network.<name>.subnet` would derive it BEFORE the network existed, get
|
|
378
|
+
// nothing, and — because generation does not run twice — never get it. The
|
|
379
|
+
// hook would then fail on an unset value the deploy had just been told to
|
|
380
|
+
// supply. Ensuring the network up front is what makes "the value is
|
|
381
|
+
// available before any hook runs" true rather than nearly true.
|
|
382
|
+
const declaringModule = await db.select().from(modules).where(eq(modules.id, moduleId)).get();
|
|
383
|
+
if (declaringModule?.manifestData) {
|
|
384
|
+
const networksEnsured = await ensureRequiredNetworks(
|
|
385
|
+
moduleId,
|
|
386
|
+
declaringModule.manifestData as ModuleManifest,
|
|
387
|
+
db,
|
|
388
|
+
);
|
|
389
|
+
if (!networksEnsured.success) {
|
|
390
|
+
return { success: false, error: networksEnsured.error, phases };
|
|
391
|
+
}
|
|
392
|
+
for (const line of networksEnsured.applied) {
|
|
393
|
+
log.success(`network defined: ${line}`);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
|
|
370
397
|
const validation = await validateAndPrepareDeployment(moduleId, db);
|
|
371
398
|
phases.validation = validation.success;
|
|
372
399
|
phases.autoGenerated = validation.autoGenerated;
|
|
@@ -517,9 +544,10 @@ async function deployModuleImpl(
|
|
|
517
544
|
// The bus-mediated interview has fired and any responders have
|
|
518
545
|
// answered; the operator can inspect the encrypted store and
|
|
519
546
|
// module config to confirm what landed without spinning up
|
|
520
|
-
// terraform / ansible / actual hooks.
|
|
521
|
-
//
|
|
522
|
-
//
|
|
547
|
+
// terraform / ansible / actual hooks. The required-network
|
|
548
|
+
// interview above HAS run by this point; the cross-module
|
|
549
|
+
// `ensure` events fire later from hook execution, so those are
|
|
550
|
+
// NOT exercised here — that requires a real run.
|
|
523
551
|
if (options.stopAfterInterview) {
|
|
524
552
|
log.info('--stop-after-interview: exiting before infrastructure phase.');
|
|
525
553
|
return {
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* celilo discovering the network of the box it is installed on.
|
|
3
|
+
*
|
|
4
|
+
* These tests moved here with the code, from
|
|
5
|
+
* `modules/celilo-mgmt/scripts/discovery.test.ts`. They were always claims about
|
|
6
|
+
* CELILO — that it reflects whatever network it finds and bakes in no range of
|
|
7
|
+
* its own — being made in a module's test file because that is where the code
|
|
8
|
+
* happened to live
|
|
9
|
+
* (openspec/changes/networks-are-declared-not-written/specs/network-declaration/spec.md).
|
|
10
|
+
*
|
|
11
|
+
* The headline test feeds a RANDOM RFC1918 /24 and asserts celilo uses exactly
|
|
12
|
+
* that: the product-level guarantee that no specific address range is baked in.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { beforeEach, describe, expect, test } from 'bun:test';
|
|
16
|
+
import { eq } from 'drizzle-orm';
|
|
17
|
+
import type { DbClient } from '../db/client';
|
|
18
|
+
import { systemConfig } from '../db/schema';
|
|
19
|
+
import { setupTestDatabase } from '../test-utils/database';
|
|
20
|
+
import {
|
|
21
|
+
discoverAndRecordNetwork,
|
|
22
|
+
discoveredNetworkKeys,
|
|
23
|
+
parseInternalNetwork,
|
|
24
|
+
} from './network-discovery';
|
|
25
|
+
|
|
26
|
+
/** Build an `ip route` output for a given /24 base, gateway, and host IP. */
|
|
27
|
+
function ipRouteFor(base: string, gateway: string, hostIp: string, dev = 'eth0'): string {
|
|
28
|
+
return [
|
|
29
|
+
`default via ${gateway} dev ${dev} proto dhcp src ${hostIp} metric 100`,
|
|
30
|
+
`${base}/24 dev ${dev} proto kernel scope link src ${hostIp} metric 100`,
|
|
31
|
+
'',
|
|
32
|
+
].join('\n');
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Pick a random RFC1918 /24 base + a host IP + gateway within it. */
|
|
36
|
+
function randomRfc1918Slash24(): { base: string; gateway: string; hostIp: string } {
|
|
37
|
+
const r = Math.random();
|
|
38
|
+
let a: number;
|
|
39
|
+
let b: number;
|
|
40
|
+
if (r < 0.34) {
|
|
41
|
+
a = 10;
|
|
42
|
+
b = Math.floor(Math.random() * 256);
|
|
43
|
+
} else if (r < 0.67) {
|
|
44
|
+
a = 172;
|
|
45
|
+
b = 16 + Math.floor(Math.random() * 16); // 172.16–172.31
|
|
46
|
+
} else {
|
|
47
|
+
a = 192;
|
|
48
|
+
b = 168;
|
|
49
|
+
}
|
|
50
|
+
const c = Math.floor(Math.random() * 256);
|
|
51
|
+
return {
|
|
52
|
+
base: `${a}.${b}.${c}.0`,
|
|
53
|
+
gateway: `${a}.${b}.${c}.1`,
|
|
54
|
+
hostIp: `${a}.${b}.${c}.${10 + Math.floor(Math.random() * 200)}`,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
describe('parseInternalNetwork', () => {
|
|
59
|
+
test('uses whatever /24 the host is actually on — no hard-coded range', () => {
|
|
60
|
+
for (let i = 0; i < 50; i++) {
|
|
61
|
+
const { base, gateway, hostIp } = randomRfc1918Slash24();
|
|
62
|
+
const result = parseInternalNetwork(ipRouteFor(base, gateway, hostIp));
|
|
63
|
+
expect(result).toEqual({ subnet: `${base}/24`, gateway });
|
|
64
|
+
// Guard against any latent assumption of the old default.
|
|
65
|
+
if (base !== '192.168.0.0') {
|
|
66
|
+
expect(result?.subnet).not.toBe('192.168.0.0/24');
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
test('parses a typical dhcp default route + connected subnet', () => {
|
|
72
|
+
const out = ipRouteFor('10.37.42.0', '10.37.42.1', '10.37.42.50');
|
|
73
|
+
expect(parseInternalNetwork(out)).toEqual({ subnet: '10.37.42.0/24', gateway: '10.37.42.1' });
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
test('honors a non-/24 prefix length', () => {
|
|
77
|
+
const out = [
|
|
78
|
+
'default via 172.20.0.1 dev ens3',
|
|
79
|
+
'172.20.0.0/16 dev ens3 proto kernel scope link src 172.20.5.9',
|
|
80
|
+
].join('\n');
|
|
81
|
+
expect(parseInternalNetwork(out)).toEqual({ subnet: '172.20.0.0/16', gateway: '172.20.0.1' });
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
test('returns null when there is no default route', () => {
|
|
85
|
+
expect(parseInternalNetwork('10.0.0.0/24 dev eth0 proto kernel scope link src 10.0.0.5')).toBe(
|
|
86
|
+
null,
|
|
87
|
+
);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
test('returns null when the connected subnet route is missing', () => {
|
|
91
|
+
expect(parseInternalNetwork('default via 10.0.0.1 dev eth0')).toBe(null);
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
describe('discoveredNetworkKeys (issue #300 redeploy idempotency)', () => {
|
|
96
|
+
const internal = { subnet: '192.168.1.0/24', gateway: '192.168.1.1' };
|
|
97
|
+
|
|
98
|
+
test('writes discovered subnet+gateway on first install (unset)', () => {
|
|
99
|
+
expect(discoveredNetworkKeys(internal, false)).toEqual({
|
|
100
|
+
'network.internal.subnet': '192.168.1.0/24',
|
|
101
|
+
'network.internal.gateway': '192.168.1.1',
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
test('preserves an already-set subnet on redeploy (no override)', () => {
|
|
106
|
+
// The regression: a redeploy on a box off the internal network discovers the
|
|
107
|
+
// WRONG subnet — it must not clobber the operator's value.
|
|
108
|
+
expect(discoveredNetworkKeys(internal, true)).toEqual({});
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
test('box IS on the configured internal network → nothing to record', () => {
|
|
112
|
+
expect(discoveredNetworkKeys(internal, true, { internalSubnet: '192.168.1.0/24' })).toEqual({});
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
test('box is OFF the internal network → records it as secure-mgmt, not discarded', () => {
|
|
116
|
+
// #300 discarded this value to protect `internal`. Correct, but it left celilo
|
|
117
|
+
// blind to its own network: untrusted by the firewall, and absent from the
|
|
118
|
+
// resolver's split-horizon views (NOERROR/0-records → public DNS → hairpin).
|
|
119
|
+
const mgmtBox = { subnet: '10.0.120.0/24', gateway: '10.0.120.1' };
|
|
120
|
+
expect(discoveredNetworkKeys(mgmtBox, true, { internalSubnet: '192.168.0.0/24' })).toEqual({
|
|
121
|
+
'network.secure-mgmt.subnet': '10.0.120.0/24',
|
|
122
|
+
'network.secure-mgmt.gateway': '10.0.120.1',
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
test('never clobbers an already-set secure-mgmt subnet', () => {
|
|
127
|
+
const mgmtBox = { subnet: '10.0.120.0/24', gateway: '10.0.120.1' };
|
|
128
|
+
expect(
|
|
129
|
+
discoveredNetworkKeys(mgmtBox, true, {
|
|
130
|
+
internalSubnet: '192.168.0.0/24',
|
|
131
|
+
secureMgmtAlreadySet: true,
|
|
132
|
+
}),
|
|
133
|
+
).toEqual({});
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
test('internal set but its value unreadable → stays conservative, writes nothing', () => {
|
|
137
|
+
// Without knowing the configured subnet we cannot tell whether this box is on
|
|
138
|
+
// it; guessing wrong would mislabel the LAN. Preserve #300's behaviour.
|
|
139
|
+
expect(discoveredNetworkKeys(internal, true, {})).toEqual({});
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
test('writes nothing when discovery failed and nothing is set', () => {
|
|
143
|
+
expect(discoveredNetworkKeys(null, false)).toEqual({});
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
describe('discoverAndRecordNetwork', () => {
|
|
148
|
+
let db: DbClient;
|
|
149
|
+
|
|
150
|
+
beforeEach(async () => {
|
|
151
|
+
db = await setupTestDatabase();
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
function read(key: string): string | undefined {
|
|
155
|
+
return db.select().from(systemConfig).where(eq(systemConfig.key, key)).get()?.value;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
test('records the discovered network on a box that has none', () => {
|
|
159
|
+
const result = discoverAndRecordNetwork(db, () =>
|
|
160
|
+
ipRouteFor('10.31.7.0', '10.31.7.1', '10.31.7.20'),
|
|
161
|
+
);
|
|
162
|
+
|
|
163
|
+
expect(result.applied).toHaveLength(2);
|
|
164
|
+
expect(read('network.internal.subnet')).toBe('10.31.7.0/24');
|
|
165
|
+
expect(read('network.internal.gateway')).toBe('10.31.7.1');
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
test('a redeploy on the same box changes nothing and says so', () => {
|
|
169
|
+
const routes = () => ipRouteFor('10.31.7.0', '10.31.7.1', '10.31.7.20');
|
|
170
|
+
discoverAndRecordNetwork(db, routes);
|
|
171
|
+
const second = discoverAndRecordNetwork(db, routes);
|
|
172
|
+
|
|
173
|
+
expect(second.applied).toEqual([]);
|
|
174
|
+
expect(second.skipped).toContain('already accounts for');
|
|
175
|
+
expect(read('network.internal.subnet')).toBe('10.31.7.0/24');
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
test('a box off the internal LAN records its own network as secure-mgmt', () => {
|
|
179
|
+
db.insert(systemConfig)
|
|
180
|
+
.values({ key: 'network.internal.subnet', value: '192.168.0.0/24' })
|
|
181
|
+
.run();
|
|
182
|
+
|
|
183
|
+
discoverAndRecordNetwork(db, () => ipRouteFor('10.0.120.0', '10.0.120.1', '10.0.120.10'));
|
|
184
|
+
|
|
185
|
+
// The operator's internal value is untouched, and celilo is no longer blind
|
|
186
|
+
// to the network its own control plane occupies.
|
|
187
|
+
expect(read('network.internal.subnet')).toBe('192.168.0.0/24');
|
|
188
|
+
expect(read('network.secure-mgmt.subnet')).toBe('10.0.120.0/24');
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
test('an unreadable routing table is reported, not guessed at', () => {
|
|
192
|
+
const result = discoverAndRecordNetwork(db, () => null);
|
|
193
|
+
|
|
194
|
+
expect(result.applied).toEqual([]);
|
|
195
|
+
expect(result.skipped).toContain('ip route');
|
|
196
|
+
expect(read('network.internal.subnet')).toBeUndefined();
|
|
197
|
+
});
|
|
198
|
+
});
|