@namzu/sandbox 14.0.0 → 15.0.0
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/CHANGELOG.md +838 -0
- package/README.md +310 -14
- package/dist/backends/aci-standby-pool/index.d.ts.map +1 -1
- package/dist/backends/aci-standby-pool/index.js +13 -1
- package/dist/backends/aci-standby-pool/index.js.map +1 -1
- package/dist/backends/docker/index.d.ts.map +1 -1
- package/dist/backends/docker/index.js +19 -1
- package/dist/backends/docker/index.js.map +1 -1
- package/dist/backends/firecracker/index.d.ts.map +1 -1
- package/dist/backends/firecracker/index.js +12 -2
- package/dist/backends/firecracker/index.js.map +1 -1
- package/dist/backends/firecracker/protocol.d.ts +459 -8
- package/dist/backends/firecracker/protocol.d.ts.map +1 -1
- package/dist/backends/firecracker/protocol.js +136 -0
- package/dist/backends/firecracker/protocol.js.map +1 -1
- package/dist/backends/firecracker/transport.d.ts +539 -6
- package/dist/backends/firecracker/transport.d.ts.map +1 -1
- package/dist/backends/firecracker/transport.js +1171 -24
- package/dist/backends/firecracker/transport.js.map +1 -1
- package/dist/backends/kubernetes/egress-policy.d.ts +1088 -11
- package/dist/backends/kubernetes/egress-policy.d.ts.map +1 -1
- package/dist/backends/kubernetes/egress-policy.js +2173 -29
- package/dist/backends/kubernetes/egress-policy.js.map +1 -1
- package/dist/backends/kubernetes/identity.d.ts +193 -0
- package/dist/backends/kubernetes/identity.d.ts.map +1 -0
- package/dist/backends/kubernetes/identity.js +147 -0
- package/dist/backends/kubernetes/identity.js.map +1 -0
- package/dist/backends/kubernetes/index.d.ts +678 -33
- package/dist/backends/kubernetes/index.d.ts.map +1 -1
- package/dist/backends/kubernetes/index.js +1180 -95
- package/dist/backends/kubernetes/index.js.map +1 -1
- package/dist/backends/kubernetes/ingress-policy.d.ts +375 -0
- package/dist/backends/kubernetes/ingress-policy.d.ts.map +1 -0
- package/dist/backends/kubernetes/ingress-policy.js +1050 -0
- package/dist/backends/kubernetes/ingress-policy.js.map +1 -0
- package/dist/backends/kubernetes/k8s-client.d.ts +213 -4
- package/dist/backends/kubernetes/k8s-client.d.ts.map +1 -1
- package/dist/backends/kubernetes/k8s-client.js +359 -52
- package/dist/backends/kubernetes/k8s-client.js.map +1 -1
- package/dist/backends/kubernetes/lease.d.ts +40 -14
- package/dist/backends/kubernetes/lease.d.ts.map +1 -1
- package/dist/backends/kubernetes/lease.js +68 -18
- package/dist/backends/kubernetes/lease.js.map +1 -1
- package/dist/backends/kubernetes/objects.d.ts +423 -3
- package/dist/backends/kubernetes/objects.d.ts.map +1 -1
- package/dist/backends/kubernetes/objects.js +364 -2
- package/dist/backends/kubernetes/objects.js.map +1 -1
- package/dist/backends/kubernetes/per-sandbox-policy.d.ts +219 -0
- package/dist/backends/kubernetes/per-sandbox-policy.d.ts.map +1 -0
- package/dist/backends/kubernetes/per-sandbox-policy.js +407 -0
- package/dist/backends/kubernetes/per-sandbox-policy.js.map +1 -0
- package/dist/backends/kubernetes/rbac.d.ts +153 -0
- package/dist/backends/kubernetes/rbac.d.ts.map +1 -0
- package/dist/backends/kubernetes/rbac.js +177 -0
- package/dist/backends/kubernetes/rbac.js.map +1 -0
- package/dist/backends/kubernetes/sandbox.d.ts +81 -14
- package/dist/backends/kubernetes/sandbox.d.ts.map +1 -1
- package/dist/backends/kubernetes/sandbox.js +149 -15
- package/dist/backends/kubernetes/sandbox.js.map +1 -1
- package/dist/backends/kubernetes/transport.d.ts +935 -9
- package/dist/backends/kubernetes/transport.d.ts.map +1 -1
- package/dist/backends/kubernetes/transport.js +1958 -62
- package/dist/backends/kubernetes/transport.js.map +1 -1
- package/dist/backends/kubernetes/workspace.d.ts +1149 -18
- package/dist/backends/kubernetes/workspace.d.ts.map +1 -1
- package/dist/backends/kubernetes/workspace.js +2825 -186
- package/dist/backends/kubernetes/workspace.js.map +1 -1
- package/dist/backends/remote-execution-controller.d.ts +14 -0
- package/dist/backends/remote-execution-controller.d.ts.map +1 -1
- package/dist/backends/remote-execution-controller.js.map +1 -1
- package/dist/index.d.ts +231 -13
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +247 -5
- package/dist/index.js.map +1 -1
- package/dist/testing/sandbox-conformance.d.ts +39 -5
- package/dist/testing/sandbox-conformance.d.ts.map +1 -1
- package/dist/testing/sandbox-conformance.js +436 -5
- package/dist/testing/sandbox-conformance.js.map +1 -1
- package/package.json +3 -3
- package/src/backends/aci-standby-pool/index.ts +16 -1
- package/src/backends/docker/index.ts +22 -1
- package/src/backends/firecracker/index.ts +14 -2
- package/src/backends/firecracker/protocol.ts +514 -6
- package/src/backends/firecracker/transport.ts +1492 -40
- package/src/backends/kubernetes/egress-policy.ts +3064 -53
- package/src/backends/kubernetes/identity.ts +261 -0
- package/src/backends/kubernetes/index.ts +1785 -127
- package/src/backends/kubernetes/ingress-policy.ts +1344 -0
- package/src/backends/kubernetes/k8s-client.ts +444 -54
- package/src/backends/kubernetes/lease.ts +75 -19
- package/src/backends/kubernetes/objects.ts +626 -6
- package/src/backends/kubernetes/per-sandbox-policy.ts +542 -0
- package/src/backends/kubernetes/rbac.ts +192 -0
- package/src/backends/kubernetes/sandbox.ts +218 -20
- package/src/backends/kubernetes/transport.ts +2733 -124
- package/src/backends/kubernetes/workspace.ts +4476 -222
- package/src/backends/remote-execution-controller.ts +14 -0
- package/src/index.ts +595 -14
- package/src/testing/sandbox-conformance.ts +540 -5
|
@@ -76,11 +76,470 @@
|
|
|
76
76
|
* the kernel, not by the workload's cooperation.
|
|
77
77
|
*/
|
|
78
78
|
import { isDeepStrictEqual } from 'node:util';
|
|
79
|
+
// The policy-enumeration and shape-reading primitives, shared with the
|
|
80
|
+
// ingress direction. There is one enumeration of the policies selecting a pod
|
|
81
|
+
// in this package and one reading of what a peer is; see that module's
|
|
82
|
+
// "Shared with the egress direction".
|
|
83
|
+
import { UnreadPolicyCollection, ciliumIdentityLabels, ciliumSelectorKey, corePeerIsWideOpen, formatLabels, isRecord, listPolicies, matchesLabelSelector, policyName, readList, selectorIsReadable, } from './ingress-policy.js';
|
|
79
84
|
import { KubernetesAlreadyGoneError } from './k8s-client.js';
|
|
80
|
-
import { CILIUM_NETWORK_POLICY_API_GROUP, CILIUM_NETWORK_POLICY_API_VERSION, CORE_NETWORK_POLICY_API_GROUP, CORE_NETWORK_POLICY_API_VERSION, ciliumNetworkPolicyPath, networkPolicyPath, sandboxTemplateLabel, } from './objects.js';
|
|
81
|
-
/**
|
|
82
|
-
|
|
83
|
-
|
|
85
|
+
import { CILIUM_NETWORK_POLICY_API_GROUP, CILIUM_NETWORK_POLICY_API_VERSION, CORE_NETWORK_POLICY_API_GROUP, CORE_NETWORK_POLICY_API_VERSION, SANDBOX_TEMPLATE_LABEL_KEY, ciliumNetworkPolicyCollectionPath, ciliumNetworkPolicyPath, networkPolicyCollectionPath, networkPolicyPath, sandboxTemplateLabel, } from './objects.js';
|
|
86
|
+
/**
|
|
87
|
+
* Default {@link KubernetesEgressConfig.profileLabelKey} — this backend's
|
|
88
|
+
* own label domain, matching `SANDBOX_TEMPLATE_LABEL_KEY`'s prefix so
|
|
89
|
+
* every label a namzu host puts on a sandbox pod reads as one family.
|
|
90
|
+
*
|
|
91
|
+
* It is deliberately NOT upstream's `sandbox.users.io`: a key in someone
|
|
92
|
+
* else's domain is a key someone else may define differently. The cost is
|
|
93
|
+
* the ConfigMap edit named on {@link KubernetesEgressConfig.profile}, and
|
|
94
|
+
* the controller's refusal spells that edit out itself.
|
|
95
|
+
*/
|
|
96
|
+
export const DEFAULT_EGRESS_PROFILE_LABEL_KEY = 'sandbox.namzu.ai/egress-profile';
|
|
97
|
+
/**
|
|
98
|
+
* Kubernetes' own label-value grammar, narrowed to DNS-1123: lowercase
|
|
99
|
+
* alphanumerics and `-`, starting and ending alphanumeric, at most 63
|
|
100
|
+
* characters.
|
|
101
|
+
*
|
|
102
|
+
* Narrower than what a label value may legally hold (`_` and `.` are legal
|
|
103
|
+
* there, and uppercase is too) because the profile is also a NAME: it goes
|
|
104
|
+
* into `${template}-${profile}-egress`, which has to be a legal object name,
|
|
105
|
+
* and a value that is legal as a label but not as a name would produce a
|
|
106
|
+
* policy an operator cannot apply.
|
|
107
|
+
*/
|
|
108
|
+
const DNS_1123_LABEL = /^[a-z0-9]([-a-z0-9]{0,61}[a-z0-9])?$/;
|
|
109
|
+
/**
|
|
110
|
+
* A label KEY: an optional DNS-subdomain prefix, a `/`, then a name segment
|
|
111
|
+
* of at most 63 characters. Exactly what the API server enforces, checked
|
|
112
|
+
* here so a typo is refused during host wiring rather than as a claim the
|
|
113
|
+
* controller rejects one round trip later.
|
|
114
|
+
*/
|
|
115
|
+
const LABEL_KEY_NAME = /^[A-Za-z0-9]([-A-Za-z0-9_.]{0,61}[A-Za-z0-9])?$/;
|
|
116
|
+
const LABEL_KEY_PREFIX = /^[a-z0-9]([-a-z0-9.]{0,251}[a-z0-9])?$/;
|
|
117
|
+
/**
|
|
118
|
+
* Named refusal for an egress PROFILE this backend can read but not use.
|
|
119
|
+
* Sibling of {@link KubernetesEgressPolicyConfigError} rather than a reuse of
|
|
120
|
+
* it: that one names a field under `config.egress.policy` and this one names
|
|
121
|
+
* a field beside it, and an operator reading either should not have to work
|
|
122
|
+
* out which level of the config the path belongs to.
|
|
123
|
+
*
|
|
124
|
+
* Thrown SYNCHRONOUSLY from `buildKubernetesBackend` and
|
|
125
|
+
* `createKubernetesWorkspace`, so a misconfigured profile surfaces during
|
|
126
|
+
* host wiring rather than on the first `create()`.
|
|
127
|
+
*/
|
|
128
|
+
export class KubernetesEgressProfileConfigError extends Error {
|
|
129
|
+
field;
|
|
130
|
+
value;
|
|
131
|
+
name = 'KubernetesEgressProfileConfigError';
|
|
132
|
+
constructor(field, value, reason) {
|
|
133
|
+
super(`kubernetes: config.egress.${field} is unusable: ${JSON.stringify(value)} ${reason}. The profile travels onto a SandboxClaim's additionalPodMetadata.labels, onto a directly created Sandbox's pod template and into the translated policy's own selector, so a value the API server would reject leaves either a claim nothing binds or a policy nobody can apply. Refusing here rather than emitting it.`);
|
|
134
|
+
this.field = field;
|
|
135
|
+
this.value = value;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* The profile label this config asks for, or nothing at all — the ONE place
|
|
140
|
+
* the key/value pair is derived, so the claim body, the Sandbox pod
|
|
141
|
+
* template, the policy selector and the policy name cannot disagree about
|
|
142
|
+
* what the profile is.
|
|
143
|
+
*
|
|
144
|
+
* Validates as it resolves: the value has to be a DNS-1123 label and the key
|
|
145
|
+
* a legal label key, both refused with {@link KubernetesEgressProfileConfigError}.
|
|
146
|
+
*/
|
|
147
|
+
export function egressProfileLabel(egress) {
|
|
148
|
+
// The KEY is validated whenever it is present, profile or no profile. A
|
|
149
|
+
// key set without a value is a half-finished configuration — the value is
|
|
150
|
+
// usually the next line someone writes — and reporting the typo only once
|
|
151
|
+
// the profile arrives is reporting it at the second edit rather than the
|
|
152
|
+
// first.
|
|
153
|
+
const configured = egress?.profileLabelKey;
|
|
154
|
+
if (configured !== undefined)
|
|
155
|
+
assertUsableProfileLabelKey(configured);
|
|
156
|
+
const value = egress?.profile;
|
|
157
|
+
if (value === undefined)
|
|
158
|
+
return undefined;
|
|
159
|
+
if (!DNS_1123_LABEL.test(value)) {
|
|
160
|
+
throw new KubernetesEgressProfileConfigError('profile', value, 'is not a DNS-1123 label (lowercase letters, digits and dashes, starting and ending alphanumeric, at most 63 characters)');
|
|
161
|
+
}
|
|
162
|
+
const key = configured ?? DEFAULT_EGRESS_PROFILE_LABEL_KEY;
|
|
163
|
+
return { key, value };
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Why a label KEY is unusable, or `undefined` when it is fine — the grammar
|
|
167
|
+
* half of the two checks below, shared because the API server's rule is the
|
|
168
|
+
* same whichever of this backend's label keys is being configured and two
|
|
169
|
+
* spellings of it would be two rules.
|
|
170
|
+
*/
|
|
171
|
+
function labelKeyProblem(key) {
|
|
172
|
+
const slash = key.indexOf('/');
|
|
173
|
+
const name = slash === -1 ? key : key.slice(slash + 1);
|
|
174
|
+
const prefix = slash === -1 ? undefined : key.slice(0, slash);
|
|
175
|
+
if (!LABEL_KEY_NAME.test(name) ||
|
|
176
|
+
(prefix !== undefined && !LABEL_KEY_PREFIX.test(prefix)) ||
|
|
177
|
+
key.indexOf('/', slash + 1) !== -1) {
|
|
178
|
+
return 'is not a Kubernetes label key (an optional DNS-subdomain prefix, a single slash, then a name of at most 63 characters)';
|
|
179
|
+
}
|
|
180
|
+
return undefined;
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Refuse a `profileLabelKey` the API server would not take, or that this
|
|
184
|
+
* backend already uses for something else.
|
|
185
|
+
*/
|
|
186
|
+
function assertUsableProfileLabelKey(key) {
|
|
187
|
+
const problem = labelKeyProblem(key);
|
|
188
|
+
if (problem !== undefined) {
|
|
189
|
+
throw new KubernetesEgressProfileConfigError('profileLabelKey', key, problem);
|
|
190
|
+
}
|
|
191
|
+
// The one legal key that must not be used: it is the key this backend
|
|
192
|
+
// stamps the SandboxTemplate name under, and the profile label is applied
|
|
193
|
+
// LAST (see `sandboxPodLabels`), so this key would overwrite the template
|
|
194
|
+
// label on every pod this backend creates — and the translated policy's
|
|
195
|
+
// selector, built from the same resolution, would agree with it. Both
|
|
196
|
+
// halves would be wrong together, which is exactly the shape nothing else
|
|
197
|
+
// would catch.
|
|
198
|
+
if (key === SANDBOX_TEMPLATE_LABEL_KEY) {
|
|
199
|
+
throw new KubernetesEgressProfileConfigError('profileLabelKey', key, 'is the key this backend writes the SandboxTemplate name under, and a profile label is applied last — a pod would carry the profile value where its template label belongs, and the policy selector built from the same resolution would match it anyway');
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Validate the profile without needing its value — the wiring-time hook, so
|
|
204
|
+
* `buildKubernetesBackend` and `createKubernetesWorkspace` refuse a bad
|
|
205
|
+
* profile the same moment they refuse an unenforceable policy.
|
|
206
|
+
*/
|
|
207
|
+
export function assertEgressProfileIsUsable(egress) {
|
|
208
|
+
egressProfileLabel(egress);
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Default {@link KubernetesPerSandboxEgressConfig.labelKey} — the key the
|
|
212
|
+
* per-sandbox policy's `endpointSelector` matches on.
|
|
213
|
+
*
|
|
214
|
+
* In this backend's own label domain, for the same reason the profile key is
|
|
215
|
+
* (see {@link DEFAULT_EGRESS_PROFILE_LABEL_KEY}), and carrying the same
|
|
216
|
+
* operator prerequisite: the controller's `allowed-label-domains` allowlist
|
|
217
|
+
* has to admit `sandbox.namzu.ai`, or every claim carrying it is refused with
|
|
218
|
+
* {@link KubernetesPodLabelsRejectedError} as the cause. A deployment that
|
|
219
|
+
* would rather not edit that ConfigMap sets this to a key under
|
|
220
|
+
* `sandbox.users.io`, which is upstream's own default domain.
|
|
221
|
+
*/
|
|
222
|
+
export const DEFAULT_PER_SANDBOX_EGRESS_LABEL_KEY = 'sandbox.namzu.ai/per-sandbox-egress';
|
|
223
|
+
/**
|
|
224
|
+
* Named refusal for a `config.egress.perSandbox` this backend can read but
|
|
225
|
+
* not honour. Thrown SYNCHRONOUSLY from `buildKubernetesBackend`, beside the
|
|
226
|
+
* policy and profile refusals, so a host that has mis-declared the capability
|
|
227
|
+
* learns it during wiring rather than from the first `setNetworkPolicy` call
|
|
228
|
+
* — which may be an hour into a run, after work that cannot be redone.
|
|
229
|
+
*
|
|
230
|
+
* Its own class rather than a reuse of {@link KubernetesEgressProfileConfigError}
|
|
231
|
+
* for the reason that one is not a reuse of
|
|
232
|
+
* {@link KubernetesEgressPolicyConfigError}: an operator reading a refusal
|
|
233
|
+
* should not have to work out which level of `config.egress` the named field
|
|
234
|
+
* belongs to.
|
|
235
|
+
*/
|
|
236
|
+
export class KubernetesPerSandboxEgressConfigError extends Error {
|
|
237
|
+
field;
|
|
238
|
+
value;
|
|
239
|
+
name = 'KubernetesPerSandboxEgressConfigError';
|
|
240
|
+
constructor(field, value, reason) {
|
|
241
|
+
super(`kubernetes: config.egress.perSandbox.${field} is unusable: ${JSON.stringify(value)} ${reason}. Per-sandbox egress writes one CiliumNetworkPolicy per live sandbox, selected by a per-sandbox pod label and fenced by an operator-applied ValidatingAdmissionPolicy, so a value this backend cannot honour would leave either a policy that selects nothing or a write the fence refuses. Refusing during host wiring rather than at the first setNetworkPolicy call.`);
|
|
242
|
+
this.field = field;
|
|
243
|
+
this.value = value;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* The per-sandbox selector label KEY this config asks for, or nothing at all
|
|
248
|
+
* when `perSandbox` is unset — the one place the default is applied, so the
|
|
249
|
+
* pod label, the policy selector and the admission policy's own expectation
|
|
250
|
+
* cannot disagree.
|
|
251
|
+
*
|
|
252
|
+
* Validates as it resolves, exactly as {@link egressProfileLabel} does.
|
|
253
|
+
*/
|
|
254
|
+
export function perSandboxEgressLabelKey(egress) {
|
|
255
|
+
const perSandbox = egress?.perSandbox;
|
|
256
|
+
if (perSandbox === undefined)
|
|
257
|
+
return undefined;
|
|
258
|
+
const key = perSandbox.labelKey ?? DEFAULT_PER_SANDBOX_EGRESS_LABEL_KEY;
|
|
259
|
+
const problem = labelKeyProblem(key);
|
|
260
|
+
if (problem !== undefined) {
|
|
261
|
+
throw new KubernetesPerSandboxEgressConfigError('labelKey', key, problem);
|
|
262
|
+
}
|
|
263
|
+
if (key === SANDBOX_TEMPLATE_LABEL_KEY) {
|
|
264
|
+
throw new KubernetesPerSandboxEgressConfigError('labelKey', key, 'is the key this backend writes the SandboxTemplate name under, so a pod would carry a sandbox name where its template label belongs and every policy selecting the template would stop selecting it');
|
|
265
|
+
}
|
|
266
|
+
// The profile's key is refused from the other direction too — see
|
|
267
|
+
// `composeAdditionalPodLabels` — but naming it HERE names the field a
|
|
268
|
+
// reader has to change, which the generic collision message cannot.
|
|
269
|
+
const profile = egressProfileLabel(egress);
|
|
270
|
+
if (profile !== undefined && key === profile.key) {
|
|
271
|
+
throw new KubernetesPerSandboxEgressConfigError('labelKey', key, 'is also config.egress.profileLabelKey, and both are written into the SAME pod-label map — one value would silently replace the other, and whichever lost would be a label a policy selector still expects');
|
|
272
|
+
}
|
|
273
|
+
return key;
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* Validate `config.egress.perSandbox` in full — the wiring-time hook, so a
|
|
277
|
+
* mis-declared capability is refused the same moment an unenforceable policy
|
|
278
|
+
* or an unusable profile is.
|
|
279
|
+
*
|
|
280
|
+
* `engine: 'core'` is the refusal the SDK's contract cares about: core
|
|
281
|
+
* `NetworkPolicy` cannot express a hostname at all, so a host that configured
|
|
282
|
+
* it would be told "policy applied" about an object that could never carry
|
|
283
|
+
* the allowlist. It is refused here rather than from the method, which means
|
|
284
|
+
* a `'core'` deployment never gets a handle carrying the method in the first
|
|
285
|
+
* place.
|
|
286
|
+
*/
|
|
287
|
+
export function assertPerSandboxEgressIsUsable(egress) {
|
|
288
|
+
const perSandbox = egress?.perSandbox;
|
|
289
|
+
if (perSandbox === undefined)
|
|
290
|
+
return;
|
|
291
|
+
if (perSandbox.engine !== 'cilium') {
|
|
292
|
+
throw new KubernetesPerSandboxEgressConfigError('engine', perSandbox.engine, "is not 'cilium'; a per-sandbox allowlist is a list of HOSTNAMES, and core NetworkPolicy has only ipBlock, podSelector and namespaceSelector — it has no hostname concept to translate one into");
|
|
293
|
+
}
|
|
294
|
+
// `admissionPolicyName` is REQUIRED by the type; the cast is for the
|
|
295
|
+
// caller reaching here from JavaScript, or through a cast of its own.
|
|
296
|
+
// There is no default and there must not be one: the fence is what makes
|
|
297
|
+
// the policy-write RBAC safe to grant, and a host that could skip naming
|
|
298
|
+
// it would hold create/patch/delete on every CiliumNetworkPolicy in the
|
|
299
|
+
// namespace with nothing bounding what it writes.
|
|
300
|
+
const names = [
|
|
301
|
+
['admissionPolicyName', perSandbox.admissionPolicyName, true],
|
|
302
|
+
['admissionPolicyBindingName', perSandbox.admissionPolicyBindingName, false],
|
|
303
|
+
];
|
|
304
|
+
for (const [field, value, required] of names) {
|
|
305
|
+
if (value === undefined) {
|
|
306
|
+
if (!required)
|
|
307
|
+
continue;
|
|
308
|
+
throw new KubernetesPerSandboxEgressConfigError(field, '', 'is required, and has no default: an unnamed fence is an unchecked one');
|
|
309
|
+
}
|
|
310
|
+
if (value === '' || value.trim() !== value) {
|
|
311
|
+
throw new KubernetesPerSandboxEgressConfigError(field, value, 'is not an object name (an empty or space-padded name matches nothing, so the fence check would refuse every write)');
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
if (perSandbox.narrowing !== undefined) {
|
|
315
|
+
assertCiliumNarrowingIsUsable(perSandbox.narrowing, 'perSandbox.narrowing');
|
|
316
|
+
}
|
|
317
|
+
perSandboxEgressLabelKey(egress);
|
|
318
|
+
}
|
|
319
|
+
/**
|
|
320
|
+
* Named refusal for `config.egress.perSandbox` reaching an entry point that
|
|
321
|
+
* can never carry the capability it configures: `createKubernetesWorkspace`.
|
|
322
|
+
*
|
|
323
|
+
* `perSandbox` exists to make `Sandbox.setNetworkPolicy` PRESENT on a TASK
|
|
324
|
+
* handle, where the acquire creates the object each policy is named after and
|
|
325
|
+
* owned by and stamps the per-sandbox pod label its selector matches, and
|
|
326
|
+
* where the RBAC and the admission fence are the ones that bound the write.
|
|
327
|
+
* A workspace's create path does none of that — it composes no per-sandbox
|
|
328
|
+
* pod label and tracks no owner uid for one — so on that path the option
|
|
329
|
+
* would be accepted and mean nothing at all: exactly the "declared and
|
|
330
|
+
* silently unused" configuration this module refuses everywhere else. The
|
|
331
|
+
* alternative, omitting the method and saying nothing, is how a host comes to
|
|
332
|
+
* believe it narrowed a workspace's egress.
|
|
333
|
+
*
|
|
334
|
+
* NOT a variant of {@link KubernetesPerSandboxEgressConfigError}, which is
|
|
335
|
+
* about a `perSandbox` value this backend cannot honour ANYWHERE (an engine
|
|
336
|
+
* with no hostname concept, an unnamed fence). This one is about a `perSandbox`
|
|
337
|
+
* value it honours perfectly well on the other entry point, so a caller
|
|
338
|
+
* catching the sibling for a typo'd value does not also catch a correct
|
|
339
|
+
* configuration aimed at the wrong path.
|
|
340
|
+
*/
|
|
341
|
+
export class KubernetesWorkspacePerSandboxEgressConfigError extends Error {
|
|
342
|
+
name = 'KubernetesWorkspacePerSandboxEgressConfigError';
|
|
343
|
+
constructor() {
|
|
344
|
+
super(`kubernetes: config.egress.perSandbox is set, but a KubernetesWorkspace cannot carry the capability it configures: Sandbox.setNetworkPolicy is implemented on a TASK handle, whose acquire creates the object each per-sandbox policy is named after and owned by and stamps the pod label its selector matches, while a workspace's create path composes no per-sandbox pod label and tracks no owner uid for one. Accepting the option here would declare a capability this path never serves — the silent downgrade this backend refuses on every other entry point — so it is refused before anything is sent. Drop egress.perSandbox from the configuration workspaces are created from (a backend serving task sandboxes can keep it), or create a task sandbox with it, where the method is present.`);
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
/**
|
|
348
|
+
* Refuse `config.egress.perSandbox` on the entry point that cannot carry it —
|
|
349
|
+
* `createKubernetesWorkspace`. See
|
|
350
|
+
* {@link KubernetesWorkspacePerSandboxEgressConfigError}.
|
|
351
|
+
*
|
|
352
|
+
* Deliberately not {@link assertPerSandboxEgressIsUsable}, which validates the
|
|
353
|
+
* same config for the path that DOES honour it: a workspace has nothing to
|
|
354
|
+
* validate the option for, whatever its `engine` or `admissionPolicyName` say,
|
|
355
|
+
* because it serves no `setNetworkPolicy` at all. Calling that validator here
|
|
356
|
+
* instead would be worse than saying nothing — it would report a
|
|
357
|
+
* `perSandbox` this deployment can use as if it were in use.
|
|
358
|
+
*/
|
|
359
|
+
export function assertWorkspaceCarriesNoPerSandboxEgress(egress) {
|
|
360
|
+
if (egress?.perSandbox === undefined)
|
|
361
|
+
return;
|
|
362
|
+
throw new KubernetesWorkspacePerSandboxEgressConfigError();
|
|
363
|
+
}
|
|
364
|
+
/**
|
|
365
|
+
* The ONE composer for a sandbox pod's `additionalPodMetadata.labels`.
|
|
366
|
+
*
|
|
367
|
+
* Every label this backend asks the controller to put on a POD is built
|
|
368
|
+
* here: the egress profile today, and whatever a later capability
|
|
369
|
+
* contributes through `extra` (a per-sandbox policy selector, for one). Two
|
|
370
|
+
* independent constructions would be two answers to "what labels is this pod
|
|
371
|
+
* selected by", and the policy selector is built from the same resolution —
|
|
372
|
+
* so a second builder would be a pod bound under a policy nobody checked.
|
|
373
|
+
*
|
|
374
|
+
* Deliberately NOT where `KubernetesBackendInternalConfig.claimLabels`
|
|
375
|
+
* goes. Those are a host's own bookkeeping on the CLAIM object's
|
|
376
|
+
* `metadata.labels`; putting them on the pod would change what selectors
|
|
377
|
+
* match a running sandbox, which is a different question on a different
|
|
378
|
+
* object.
|
|
379
|
+
*
|
|
380
|
+
* Returns an empty object when nothing applies, which every caller reads as
|
|
381
|
+
* "emit nothing at all" — that is what keeps an unprofiled body byte-identical.
|
|
382
|
+
*
|
|
383
|
+
* A key in `extra` that is ALSO the profile's is refused rather than merged
|
|
384
|
+
* either way round. Whichever won, the loser would be a label the translated
|
|
385
|
+
* policy's selector still expects: the profile's selector is built from this
|
|
386
|
+
* same resolution, so a pod carrying the other value is selected by no
|
|
387
|
+
* per-profile policy while `create()` reported the boundary verified. It is
|
|
388
|
+
* the same failure {@link egressProfileLabel} refuses the template key for,
|
|
389
|
+
* reached from the other direction.
|
|
390
|
+
*/
|
|
391
|
+
export function composeAdditionalPodLabels(egress, extra) {
|
|
392
|
+
const profile = egressProfileLabel(egress);
|
|
393
|
+
if (profile !== undefined && extra !== undefined && profile.key in extra) {
|
|
394
|
+
throw new KubernetesEgressProfileConfigError('profileLabelKey', profile.key, `is also the key another capability contributes to the same pod-label map (as ${JSON.stringify(extra[profile.key])}), and one of the two values would silently replace the other`);
|
|
395
|
+
}
|
|
396
|
+
return {
|
|
397
|
+
...(profile !== undefined ? { [profile.key]: profile.value } : {}),
|
|
398
|
+
...extra,
|
|
399
|
+
};
|
|
400
|
+
}
|
|
401
|
+
/**
|
|
402
|
+
* Why a claim the controller refused with `InvalidMetadata` was refused, in
|
|
403
|
+
* this backend's own terms — in practice a pod label whose domain is not in
|
|
404
|
+
* the controller's `allowed-label-domains` allowlist, which today means the
|
|
405
|
+
* egress profile's.
|
|
406
|
+
*
|
|
407
|
+
* NOT what an acquire throws. A refused claim comes out of `create()` as
|
|
408
|
+
* `KubernetesAcquireError { reason: 'claim-rejected' }` whether or not a
|
|
409
|
+
* profile is configured — one condition, one taxonomy, one `catch` — and this
|
|
410
|
+
* rides as that error's `cause`. Two classes for one controller condition
|
|
411
|
+
* would make a host's error handling correct or incorrect depending on
|
|
412
|
+
* whether `config.egress.profile` happened to be set.
|
|
413
|
+
*
|
|
414
|
+
* What it adds to the acquire error is what the controller cannot know: the
|
|
415
|
+
* map this backend actually sent, and the `config.egress.profileLabelKey`
|
|
416
|
+
* that moves the offending key to an allowed domain. It carries the whole map
|
|
417
|
+
* rather than the profile alone, and `profile` is optional, because the map is
|
|
418
|
+
* {@link composeAdditionalPodLabels}'s — the profile is the only thing in it
|
|
419
|
+
* today, and a later capability adding a second key would otherwise get an
|
|
420
|
+
* explanation that named a label it did not send.
|
|
421
|
+
*
|
|
422
|
+
* It carries the controller's OWN `reason` and `message` rather than a
|
|
423
|
+
* translation of them: the message agent-sandbox v1.0.2 writes names the
|
|
424
|
+
* offending key, the domain, the ConfigMap key to edit and its default, and
|
|
425
|
+
* no paraphrase of it would be as useful. Measured verbatim against a kind
|
|
426
|
+
* cluster running that controller:
|
|
427
|
+
*
|
|
428
|
+
* > invalid additionalPodMetadata: failed to validate label
|
|
429
|
+
* > "sandbox.namzu.ai/egress-profile": label domain "sandbox.namzu.ai" is
|
|
430
|
+
* > not in the allowlist (configure the allowed-label-domains key of the
|
|
431
|
+
* > agent-sandbox-config ConfigMap in the controller namespace; default:
|
|
432
|
+
* > sandbox.users.io)
|
|
433
|
+
*
|
|
434
|
+
* The refusal is raised as soon as that condition is read rather than after
|
|
435
|
+
* the readiness budget, because `InvalidMetadata` is one of
|
|
436
|
+
* `TERMINAL_CLAIM_REASONS` — nothing about it becomes true by waiting — and
|
|
437
|
+
* the claim is deleted on the way out, so a misconfigured profile costs one
|
|
438
|
+
* round trip rather than a minute of polling.
|
|
439
|
+
*/
|
|
440
|
+
export class KubernetesPodLabelsRejectedError extends Error {
|
|
441
|
+
requestedPodLabels;
|
|
442
|
+
claimName;
|
|
443
|
+
namespace;
|
|
444
|
+
controllerReason;
|
|
445
|
+
controllerMessage;
|
|
446
|
+
profile;
|
|
447
|
+
name = 'KubernetesPodLabelsRejectedError';
|
|
448
|
+
constructor(
|
|
449
|
+
/** Every label this backend put on the claim's `additionalPodMetadata`. */
|
|
450
|
+
requestedPodLabels, claimName, namespace,
|
|
451
|
+
/** The controller's own condition `reason`, e.g. `InvalidMetadata`. */
|
|
452
|
+
controllerReason,
|
|
453
|
+
/** The controller's own condition `message`, verbatim. */
|
|
454
|
+
controllerMessage,
|
|
455
|
+
/** The egress profile among those labels, when one is configured. */
|
|
456
|
+
profile) {
|
|
457
|
+
super(`kubernetes: the controller refused SandboxClaim ${claimName} in namespace ${namespace} carrying the pod labels ${formatLabels(requestedPodLabels)} — ${controllerReason}: ${controllerMessage}. Those labels are written onto the claim's additionalPodMetadata.labels, so each key's domain has to appear in the controller's allowed-label-domains allowlist; add it there, or move the offending key to a domain that is already allowed${profile === undefined ? '' : ` (config.egress.profileLabelKey, for the egress profile ${profile.key}=${profile.value})`}. The claim has been deleted.`);
|
|
458
|
+
this.requestedPodLabels = requestedPodLabels;
|
|
459
|
+
this.claimName = claimName;
|
|
460
|
+
this.namespace = namespace;
|
|
461
|
+
this.controllerReason = controllerReason;
|
|
462
|
+
this.controllerMessage = controllerMessage;
|
|
463
|
+
this.profile = profile;
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
/**
|
|
467
|
+
* Named refusal for a bound pod that never carried a label this backend asked
|
|
468
|
+
* the controller to put on it — the egress profile's, today the only one
|
|
469
|
+
* {@link composeAdditionalPodLabels} produces, which is why the class is named
|
|
470
|
+
* for the LABEL rather than for the profile.
|
|
471
|
+
*
|
|
472
|
+
* This is the one failure this capability must not have quietly. An
|
|
473
|
+
* unlabelled pod handed back is a sandbox running under the DEFAULT policy
|
|
474
|
+
* while the host believes it is on a narrower profile — the translated
|
|
475
|
+
* policy's selector includes the profile label, so a pod without it is
|
|
476
|
+
* selected by neither this profile's policy nor, necessarily, anything else.
|
|
477
|
+
* Refusing is correct and waiting is correct; proceeding is not, so the
|
|
478
|
+
* acquire releases what it claimed and raises this instead.
|
|
479
|
+
*
|
|
480
|
+
* `missingLabel` is the pair that never arrived rather than "the profile", so
|
|
481
|
+
* the refusal stays true for whatever a later capability contributes to that
|
|
482
|
+
* same map: the wait in `readAddressedPod` already covers every entry of it,
|
|
483
|
+
* and this refusal covers exactly the same set.
|
|
484
|
+
*/
|
|
485
|
+
export class KubernetesPodLabelNotObservedError extends Error {
|
|
486
|
+
missingLabel;
|
|
487
|
+
subject;
|
|
488
|
+
observedLabels;
|
|
489
|
+
name = 'KubernetesPodLabelNotObservedError';
|
|
490
|
+
constructor(
|
|
491
|
+
/** The label this backend requested and never saw on the bound pod. */
|
|
492
|
+
missingLabel, subject, observedLabels) {
|
|
493
|
+
super(`kubernetes: refusing ${subject} — its pod never carried the label ${missingLabel.key}=${missingLabel.value} this backend asked the controller to put on it, within the readiness budget; the labels it did carry are ${formatLabels(observedLabels)}. The translated policy's selector includes that label, so admitting this pod would run it under whatever policy DOES select it rather than under the configured profile. The controller patches a claim's additionalPodMetadata.labels onto the pod it binds — a pod that never got them means the claim's metadata was not applied. Nothing was handed back and the claim was released.`);
|
|
494
|
+
this.missingLabel = missingLabel;
|
|
495
|
+
this.subject = subject;
|
|
496
|
+
this.observedLabels = observedLabels;
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
/** `true` unless the deployment asked for the single-object check by name. */
|
|
500
|
+
export function egressUnionVerificationEnabled(egress) {
|
|
501
|
+
return egress !== undefined && egress.verify !== 'named-object-only';
|
|
502
|
+
}
|
|
503
|
+
/**
|
|
504
|
+
* What a translated policy's `podSelector`/`endpointSelector` matches: the
|
|
505
|
+
* template label, plus the profile label when one is configured. One
|
|
506
|
+
* function so the two manifest builders below and every reader of a
|
|
507
|
+
* translation agree on the selector down to the key order.
|
|
508
|
+
*/
|
|
509
|
+
export function egressPolicySelectorLabels(target) {
|
|
510
|
+
return {
|
|
511
|
+
...sandboxTemplateLabel(target.sandboxTemplateName),
|
|
512
|
+
...(target.profile !== undefined ? { [target.profile.key]: target.profile.value } : {}),
|
|
513
|
+
};
|
|
514
|
+
}
|
|
515
|
+
/**
|
|
516
|
+
* The longest name a Kubernetes object may carry. A `NetworkPolicy` name is a
|
|
517
|
+
* DNS subdomain, so 253 characters, and the API server refuses anything
|
|
518
|
+
* longer.
|
|
519
|
+
*/
|
|
520
|
+
const MAX_OBJECT_NAME_LENGTH = 253;
|
|
521
|
+
/**
|
|
522
|
+
* `${sandboxTemplateName}-egress`, the name
|
|
523
|
+
* {@link KubernetesEgressConfig.networkPolicyName} defaults to — or
|
|
524
|
+
* `${sandboxTemplateName}-${profile}-egress` when a profile is configured,
|
|
525
|
+
* because one template under two profiles needs two policy objects and a
|
|
526
|
+
* single default name would have the second silently verify against the
|
|
527
|
+
* first's manifest.
|
|
528
|
+
*
|
|
529
|
+
* The profile is bounded at 63 characters on its own, but the CONCATENATION
|
|
530
|
+
* is what has to be a legal object name, and only this function knows both
|
|
531
|
+
* halves. Refused here, during host wiring, rather than as an API-server
|
|
532
|
+
* rejection on the first policy GET of the first `create()`: `networkPolicyName`
|
|
533
|
+
* is the way out and it is a config field, so this is a config error.
|
|
534
|
+
*/
|
|
535
|
+
export function defaultEgressPolicyName(sandboxTemplateName, profile) {
|
|
536
|
+
if (profile === undefined)
|
|
537
|
+
return `${sandboxTemplateName}-egress`;
|
|
538
|
+
const name = `${sandboxTemplateName}-${profile}-egress`;
|
|
539
|
+
if (name.length > MAX_OBJECT_NAME_LENGTH) {
|
|
540
|
+
throw new KubernetesEgressProfileConfigError('profile', profile, `makes the default egress policy name ${JSON.stringify(name)} ${name.length} characters long, past the ${MAX_OBJECT_NAME_LENGTH} an object name may carry — shorten the profile or the SandboxTemplate name, or set config.egress.networkPolicyName yourself`);
|
|
541
|
+
}
|
|
542
|
+
return name;
|
|
84
543
|
}
|
|
85
544
|
/**
|
|
86
545
|
* Named refusal for a hostname allowlist with no FQDN-capable engine
|
|
@@ -97,18 +556,195 @@ export class KubernetesUnenforceableEgressPolicyError extends Error {
|
|
|
97
556
|
this.policyKind = policyKind;
|
|
98
557
|
}
|
|
99
558
|
}
|
|
559
|
+
/**
|
|
560
|
+
* Named refusal for a config value this backend can read but not use — today
|
|
561
|
+
* only a `public-internet` `exceptCidrs` entry that is not a CIDR. Thrown
|
|
562
|
+
* SYNCHRONOUSLY from `buildKubernetesBackend`, beside
|
|
563
|
+
* {@link KubernetesUnenforceableEgressPolicyError}, so a typo surfaces
|
|
564
|
+
* during host wiring rather than as a policy the API server rejects when an
|
|
565
|
+
* operator applies it.
|
|
566
|
+
*/
|
|
567
|
+
export class KubernetesEgressPolicyConfigError extends Error {
|
|
568
|
+
field;
|
|
569
|
+
name = 'KubernetesEgressPolicyConfigError';
|
|
570
|
+
constructor(field, reason) {
|
|
571
|
+
// `field` is relative to `config.egress`, not to `config.egress.policy`:
|
|
572
|
+
// the fields that land here live at BOTH levels — `policy.exceptCidrs`
|
|
573
|
+
// on the policy, `ciliumNarrowing` and `perSandbox.narrowing` beside
|
|
574
|
+
// it — and a prefix that assumed one of them sent a reader to a key
|
|
575
|
+
// that does not exist.
|
|
576
|
+
super(`kubernetes: config.egress.${field} is unusable: ${reason}`);
|
|
577
|
+
this.field = field;
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
/**
|
|
581
|
+
* Named refusal for `config.egress.ciliumNarrowing` set on a policy/engine
|
|
582
|
+
* combination it does not apply to. Thrown SYNCHRONOUSLY from
|
|
583
|
+
* {@link assertEgressPolicyIsEnforceable}, beside
|
|
584
|
+
* {@link KubernetesUnenforceableEgressPolicyError}, so a narrowing option
|
|
585
|
+
* that would silently do nothing is refused at host-wiring time rather than
|
|
586
|
+
* accepted and ignored.
|
|
587
|
+
*/
|
|
588
|
+
export class KubernetesEgressNarrowingUnsupportedError extends Error {
|
|
589
|
+
policyKind;
|
|
590
|
+
engine;
|
|
591
|
+
name = 'KubernetesEgressNarrowingUnsupportedError';
|
|
592
|
+
constructor(policyKind, engine) {
|
|
593
|
+
super(`kubernetes: config.egress.ciliumNarrowing is set, but config.egress.policy is '${policyKind}' under engine ${JSON.stringify(engine)}. Port, DNS-name and TLS-server-name narrowing only apply to a 'static' or 'resolver' hostname allowlist under engine: 'cilium' — set engine: 'cilium' with one of those policy kinds, or remove ciliumNarrowing. Refusing rather than silently ignoring an option that would never be applied.`);
|
|
594
|
+
this.policyKind = policyKind;
|
|
595
|
+
this.engine = engine;
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
/**
|
|
599
|
+
* The grammar a host refusal normally closes with: what an `allowedHosts`
|
|
600
|
+
* entry is. Stated only where the translation actually implements it — see
|
|
601
|
+
* `stateHostnameGrammar` on {@link KubernetesNetworkPolicyHostError}.
|
|
602
|
+
*/
|
|
603
|
+
const HOSTNAME_GRAMMAR_SENTENCE = "Entries are hostnames: 'api.example.com' for one host, '.example.com' for that domain and its subdomains.";
|
|
604
|
+
/**
|
|
605
|
+
* Raised for an `allowedHosts` entry this backend will not translate.
|
|
606
|
+
*
|
|
607
|
+
* Named for the ENTRY rather than for a config field, because entries arrive
|
|
608
|
+
* from two directions: a host's `Sandbox.setNetworkPolicy` call, and the
|
|
609
|
+
* `allowedHosts` of a `static`/`resolver` `config.egress.policy`.
|
|
610
|
+
* `SandboxNetworkPolicy` names HOSTS: `api.example.com` for one host,
|
|
611
|
+
* `.example.com` for a domain and its subdomains. A glob, a URL, a port
|
|
612
|
+
* suffix or an address is refused rather than emitted, because a
|
|
613
|
+
* `CiliumNetworkPolicy` carrying one is an object the API server rejects on
|
|
614
|
+
* apply — or, worse, accepts as a name that resolves to nothing, which reads
|
|
615
|
+
* from the outside exactly like a policy that is working.
|
|
616
|
+
*
|
|
617
|
+
* `stateHostnameGrammar` is the one part of the message a caller can turn
|
|
618
|
+
* off, because NOT every refusal can make that claim. It is false for the
|
|
619
|
+
* config-level refusal of a leading-dot entry under `tlsServerNames`, whose
|
|
620
|
+
* body has just said that the entry reaches the object as a literal
|
|
621
|
+
* `matchName` admitting nothing with the option on or off: closing that
|
|
622
|
+
* message with "'.example.com' for that domain and its subdomains" would
|
|
623
|
+
* re-assert, as implemented, the very grammar whose absence is why the entry
|
|
624
|
+
* is refused. Every other refusal keeps the sentence, the expanding branch of
|
|
625
|
+
* the same one included.
|
|
626
|
+
*/
|
|
627
|
+
export class KubernetesNetworkPolicyHostError extends Error {
|
|
628
|
+
host;
|
|
629
|
+
name = 'KubernetesNetworkPolicyHostError';
|
|
630
|
+
constructor(host, reason, options = {}) {
|
|
631
|
+
const grammar = options.stateHostnameGrammar === false ? '' : ` ${HOSTNAME_GRAMMAR_SENTENCE}`;
|
|
632
|
+
super(`kubernetes: the allowedHosts entry ${JSON.stringify(host)} is refused: it ${reason}.${grammar} Nothing was written.`);
|
|
633
|
+
this.host = host;
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
/**
|
|
637
|
+
* Every entry in `narrowing.ports`, `narrowing.hostPorts` and
|
|
638
|
+
* `narrowing.tlsPorts` is a port the API server will actually accept, none of
|
|
639
|
+
* those three is an explicitly empty array, and every DNS suffix
|
|
640
|
+
* `narrowing.dnsNames` names is a non-empty string — checked here,
|
|
641
|
+
* synchronously, for the same reason {@link assertEgressPolicyIsEnforceable}
|
|
642
|
+
* checks `exceptCidrs`: a value the API server rejects on apply would leave a
|
|
643
|
+
* policy that never verifies.
|
|
644
|
+
*/
|
|
645
|
+
/**
|
|
646
|
+
* Refuse a narrowing the API server would reject on apply, naming the field
|
|
647
|
+
* that has to change.
|
|
648
|
+
*
|
|
649
|
+
* `fieldPath` is how the refusal spells the option's location, because the
|
|
650
|
+
* same shape is configurable in two places now: `ciliumNarrowing` narrows the
|
|
651
|
+
* CONFIG-level translated policy, and `perSandbox.narrowing` narrows the
|
|
652
|
+
* per-sandbox policies a host writes through `setNetworkPolicy`. One
|
|
653
|
+
* validator, because they are one shape and two would disagree the first time
|
|
654
|
+
* either grew a field.
|
|
655
|
+
*/
|
|
656
|
+
export function assertCiliumNarrowingIsUsable(narrowing, fieldPath = 'ciliumNarrowing') {
|
|
657
|
+
const assertValidPort = (port, field) => {
|
|
658
|
+
if (typeof port !== 'number' || !Number.isInteger(port) || port < 1 || port > 65535) {
|
|
659
|
+
throw new KubernetesEgressPolicyConfigError(field, `${JSON.stringify(port)} is not a TCP port a NetworkPolicy/CiliumNetworkPolicy can carry (an integer 1-65535)`);
|
|
660
|
+
}
|
|
661
|
+
};
|
|
662
|
+
// `undefined` means "no restriction on this list" and is fine; `[]` is
|
|
663
|
+
// neither that nor a usable restriction — `narrowedHostFqdnRule` would
|
|
664
|
+
// emit `toPorts: [{ ports: [] }]` for it, a shape the API server rejects
|
|
665
|
+
// on apply. Refuse it here rather than let a typo (`ports: []` where
|
|
666
|
+
// `ports` was meant to be omitted) reach the cluster as a policy that
|
|
667
|
+
// never verifies.
|
|
668
|
+
const assertNotEmptyPortList = (ports, field) => {
|
|
669
|
+
if (ports !== undefined && ports.length === 0) {
|
|
670
|
+
throw new KubernetesEgressPolicyConfigError(field, 'must not be an empty array — omit the field entirely for no port restriction, or list at least one port');
|
|
671
|
+
}
|
|
672
|
+
};
|
|
673
|
+
assertNotEmptyPortList(narrowing.ports, `${fieldPath}.ports`);
|
|
674
|
+
for (const port of narrowing.ports ?? [])
|
|
675
|
+
assertValidPort(port, `${fieldPath}.ports`);
|
|
676
|
+
for (const [host, ports] of Object.entries(narrowing.hostPorts ?? {})) {
|
|
677
|
+
assertNotEmptyPortList(ports, `${fieldPath}.hostPorts[${JSON.stringify(host)}]`);
|
|
678
|
+
for (const port of ports) {
|
|
679
|
+
assertValidPort(port, `${fieldPath}.hostPorts[${JSON.stringify(host)}]`);
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
assertNotEmptyPortList(narrowing.tlsPorts, `${fieldPath}.tlsPorts`);
|
|
683
|
+
for (const port of narrowing.tlsPorts ?? [])
|
|
684
|
+
assertValidPort(port, `${fieldPath}.tlsPorts`);
|
|
685
|
+
if (typeof narrowing.dnsNames === 'object') {
|
|
686
|
+
const { clusterDomain, searchSuffixes } = narrowing.dnsNames;
|
|
687
|
+
if (clusterDomain !== undefined && clusterDomain.trim() === '') {
|
|
688
|
+
throw new KubernetesEgressPolicyConfigError(`${fieldPath}.dnsNames.clusterDomain`, 'must not be an empty string');
|
|
689
|
+
}
|
|
690
|
+
for (const suffix of searchSuffixes ?? []) {
|
|
691
|
+
if (typeof suffix !== 'string' || suffix.trim() === '') {
|
|
692
|
+
throw new KubernetesEgressPolicyConfigError(`${fieldPath}.dnsNames.searchSuffixes`, `${JSON.stringify(suffix)} is not a usable DNS suffix`);
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
/** Does `narrowing` actually ask for anything, or is every field off/absent. */
|
|
698
|
+
function ciliumNarrowingIsActive(narrowing) {
|
|
699
|
+
if (narrowing === undefined)
|
|
700
|
+
return false;
|
|
701
|
+
if (narrowing.ports !== undefined)
|
|
702
|
+
return true;
|
|
703
|
+
if (narrowing.hostPorts !== undefined && Object.keys(narrowing.hostPorts).length > 0)
|
|
704
|
+
return true;
|
|
705
|
+
if (dnsNarrowingIsActive(narrowing.dnsNames))
|
|
706
|
+
return true;
|
|
707
|
+
if (narrowing.tlsServerNames === true)
|
|
708
|
+
return true;
|
|
709
|
+
return false;
|
|
710
|
+
}
|
|
711
|
+
function dnsNarrowingIsActive(dnsNames) {
|
|
712
|
+
return activeDnsNarrowing(dnsNames) !== undefined;
|
|
713
|
+
}
|
|
714
|
+
/** `dnsNames` reduced to the config it names, or `undefined` when it is off. `true` reduces to every default. */
|
|
715
|
+
function activeDnsNarrowing(dnsNames) {
|
|
716
|
+
if (dnsNames === true)
|
|
717
|
+
return {};
|
|
718
|
+
if (typeof dnsNames === 'object' && dnsNames !== null)
|
|
719
|
+
return dnsNames;
|
|
720
|
+
return undefined;
|
|
721
|
+
}
|
|
100
722
|
/**
|
|
101
723
|
* Synchronous, no-I/O precondition: can `policy.kind` be enforced under
|
|
102
|
-
* `engine` at all
|
|
103
|
-
* policy
|
|
104
|
-
*
|
|
105
|
-
*
|
|
106
|
-
*
|
|
724
|
+
* `engine` at all, and — if `narrowing` is set — does it apply to this
|
|
725
|
+
* policy/engine combination. Deliberately decided from the KIND alone — a
|
|
726
|
+
* `resolver` policy's `resolve()` is never invoked here, both because calling
|
|
727
|
+
* it just to prove a refusal would be wasted work (and possibly a side effect
|
|
728
|
+
* the host did not expect yet) and because this has to stay callable
|
|
729
|
+
* synchronously from `buildKubernetesBackend`, which contacts nothing.
|
|
107
730
|
*/
|
|
108
|
-
export function assertEgressPolicyIsEnforceable(policy, engine) {
|
|
731
|
+
export function assertEgressPolicyIsEnforceable(policy, engine, narrowing) {
|
|
109
732
|
if ((policy.kind === 'static' || policy.kind === 'resolver') && engine !== 'cilium') {
|
|
110
733
|
throw new KubernetesUnenforceableEgressPolicyError(policy.kind);
|
|
111
734
|
}
|
|
735
|
+
if (policy.kind === 'public-internet') {
|
|
736
|
+
for (const cidr of policy.exceptCidrs ?? []) {
|
|
737
|
+
if (parseCidr(cidr) === undefined) {
|
|
738
|
+
throw new KubernetesEgressPolicyConfigError('policy.exceptCidrs', `${JSON.stringify(cidr)} is not an IPv4 or IPv6 CIDR this backend can read. A NetworkPolicy ipBlock.except entry has to be a CIDR inside the block it carves out of, and an entry the API server rejects on apply would leave a policy that never verifies.`);
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
}
|
|
742
|
+
if (ciliumNarrowingIsActive(narrowing)) {
|
|
743
|
+
if (engine !== 'cilium' || (policy.kind !== 'static' && policy.kind !== 'resolver')) {
|
|
744
|
+
throw new KubernetesEgressNarrowingUnsupportedError(policy.kind, engine);
|
|
745
|
+
}
|
|
746
|
+
assertCiliumNarrowingIsUsable(narrowing);
|
|
747
|
+
}
|
|
112
748
|
}
|
|
113
749
|
/**
|
|
114
750
|
* The cluster DNS egress rule every translated `NetworkPolicy` carries,
|
|
@@ -116,12 +752,125 @@ export function assertEgressPolicyIsEnforceable(policy, engine) {
|
|
|
116
752
|
* allows the cluster's own DNS" section.
|
|
117
753
|
*/
|
|
118
754
|
const CLUSTER_DNS_EGRESS_RULE = {
|
|
119
|
-
to: [
|
|
755
|
+
to: [
|
|
756
|
+
{
|
|
757
|
+
namespaceSelector: {
|
|
758
|
+
matchLabels: { 'kubernetes.io/metadata.name': 'kube-system' },
|
|
759
|
+
},
|
|
760
|
+
},
|
|
761
|
+
],
|
|
762
|
+
ports: [
|
|
763
|
+
{ protocol: 'UDP', port: 53 },
|
|
764
|
+
{ protocol: 'TCP', port: 53 },
|
|
765
|
+
],
|
|
766
|
+
};
|
|
767
|
+
/**
|
|
768
|
+
* DNS to the cluster resolver's own pods, which is narrower than
|
|
769
|
+
* {@link CLUSTER_DNS_EGRESS_RULE}'s whole-namespace rule and is what
|
|
770
|
+
* `'public-internet'` emits: that kind exists to name a destination set
|
|
771
|
+
* precisely, so it names the resolver precisely too. `kube-system` +
|
|
772
|
+
* `k8s-app: kube-dns` is the pairing CoreDNS ships under on every cluster
|
|
773
|
+
* this was checked against, and the same pairing the repo's own
|
|
774
|
+
* `k8s/manifests/networkpolicy.yaml` baseline already uses.
|
|
775
|
+
*
|
|
776
|
+
* `'deny-all'` keeps {@link CLUSTER_DNS_EGRESS_RULE} instead. It is not
|
|
777
|
+
* changed to this one: its emitted manifest is pinned byte-for-byte, because
|
|
778
|
+
* verification of the named object is an exact match and any change to the
|
|
779
|
+
* translation fails every `create()` on every deployment that already
|
|
780
|
+
* applied a policy.
|
|
781
|
+
*/
|
|
782
|
+
const KUBE_DNS_EGRESS_RULE = {
|
|
783
|
+
to: [
|
|
784
|
+
{
|
|
785
|
+
namespaceSelector: {
|
|
786
|
+
matchLabels: { 'kubernetes.io/metadata.name': 'kube-system' },
|
|
787
|
+
},
|
|
788
|
+
podSelector: { matchLabels: { 'k8s-app': 'kube-dns' } },
|
|
789
|
+
},
|
|
790
|
+
],
|
|
120
791
|
ports: [
|
|
121
792
|
{ protocol: 'UDP', port: 53 },
|
|
122
793
|
{ protocol: 'TCP', port: 53 },
|
|
123
794
|
],
|
|
124
795
|
};
|
|
796
|
+
/**
|
|
797
|
+
* What `'public-internet'` carves out of `0.0.0.0/0`, and why each entry is
|
|
798
|
+
* here. Order is part of the emitted manifest and therefore part of what
|
|
799
|
+
* verification compares, so it is fixed rather than sorted at build time.
|
|
800
|
+
*
|
|
801
|
+
* - `10.0.0.0/8`, `172.16.0.0/12`, `192.168.0.0/16` — RFC 1918. The cluster
|
|
802
|
+
* network, the node network and every other sandbox pod live in one of
|
|
803
|
+
* them on every deployment this was checked against.
|
|
804
|
+
* - `100.64.0.0/10` — RFC 6598 carrier-grade NAT, which several managed
|
|
805
|
+
* Kubernetes offerings hand to pods or nodes. Missing from the policy the
|
|
806
|
+
* agent-sandbox controller writes when a template declares no
|
|
807
|
+
* `networkPolicy`, which is exactly why this check compares except lists
|
|
808
|
+
* rather than trusting that a policy "looks restrictive".
|
|
809
|
+
* - `169.254.0.0/16` — link-local, and with it `169.254.169.254`, the
|
|
810
|
+
* instance-metadata address whose credentials are the reason a sandbox
|
|
811
|
+
* reaching "only the internet" still must not reach sideways.
|
|
812
|
+
* - `127.0.0.0/8` — loopback. Not routable off the pod, but a policy that
|
|
813
|
+
* says "the public internet" should not say it admits loopback either.
|
|
814
|
+
* - `168.63.129.16/32` — one cloud's platform endpoint, a single address
|
|
815
|
+
* outside every range above that answers DNS and instance services.
|
|
816
|
+
*/
|
|
817
|
+
const PUBLIC_INTERNET_EXCLUDED_IPV4_CIDRS = [
|
|
818
|
+
'10.0.0.0/8',
|
|
819
|
+
'172.16.0.0/12',
|
|
820
|
+
'192.168.0.0/16',
|
|
821
|
+
'100.64.0.0/10',
|
|
822
|
+
'169.254.0.0/16',
|
|
823
|
+
'127.0.0.0/8',
|
|
824
|
+
'168.63.129.16/32',
|
|
825
|
+
];
|
|
826
|
+
/**
|
|
827
|
+
* The IPv6 equivalents: unique-local (`fc00::/7`), link-local
|
|
828
|
+
* (`fe80::/10`, which carries the v6 spelling of instance metadata) and
|
|
829
|
+
* loopback (`::1/128`). A cluster with no IPv6 at all is unaffected by the
|
|
830
|
+
* rule's presence — it names destinations nothing routes to.
|
|
831
|
+
*/
|
|
832
|
+
const PUBLIC_INTERNET_EXCLUDED_IPV6_CIDRS = ['fc00::/7', 'fe80::/10', '::1/128'];
|
|
833
|
+
/**
|
|
834
|
+
* The one rule `'public-internet'` adds beside DNS: everything, minus the
|
|
835
|
+
* lists above, minus whatever the deployment added. No `ports`, because the
|
|
836
|
+
* kind is a statement about DESTINATIONS — a deployment that also wants to
|
|
837
|
+
* bound ports states that in its own applied policy, which this check then
|
|
838
|
+
* accepts as narrower.
|
|
839
|
+
*
|
|
840
|
+
* A deployment's own `exceptCidrs` are routed to the block of their own
|
|
841
|
+
* address family: a `NetworkPolicy` requires every `except` entry to sit
|
|
842
|
+
* inside the `cidr` it carves out of, so an IPv6 entry under the IPv4 block
|
|
843
|
+
* is rejected on apply.
|
|
844
|
+
*/
|
|
845
|
+
function publicInternetEgressRule(exceptCidrs) {
|
|
846
|
+
const extraV4 = [];
|
|
847
|
+
const extraV6 = [];
|
|
848
|
+
for (const cidr of exceptCidrs ?? []) {
|
|
849
|
+
const parsed = parseCidr(cidr);
|
|
850
|
+
// Unparseable entries are refused by `assertEgressPolicyIsEnforceable`
|
|
851
|
+
// at construction; reaching here with one would mean this function was
|
|
852
|
+
// called around it, so it is dropped rather than emitted.
|
|
853
|
+
if (parsed === undefined)
|
|
854
|
+
continue;
|
|
855
|
+
(parsed.version === 4 ? extraV4 : extraV6).push(cidr);
|
|
856
|
+
}
|
|
857
|
+
return {
|
|
858
|
+
to: [
|
|
859
|
+
{
|
|
860
|
+
ipBlock: {
|
|
861
|
+
cidr: '0.0.0.0/0',
|
|
862
|
+
except: [...PUBLIC_INTERNET_EXCLUDED_IPV4_CIDRS, ...extraV4],
|
|
863
|
+
},
|
|
864
|
+
},
|
|
865
|
+
{
|
|
866
|
+
ipBlock: {
|
|
867
|
+
cidr: '::/0',
|
|
868
|
+
except: [...PUBLIC_INTERNET_EXCLUDED_IPV6_CIDRS, ...extraV6],
|
|
869
|
+
},
|
|
870
|
+
},
|
|
871
|
+
],
|
|
872
|
+
};
|
|
873
|
+
}
|
|
125
874
|
/**
|
|
126
875
|
* Cilium requires DNS lookups to be explicitly allowed AND made visible to
|
|
127
876
|
* the agent before `toFQDNs` enforcement can match anything a name resolves
|
|
@@ -156,9 +905,10 @@ const CILIUM_DNS_VISIBILITY_RULE = {
|
|
|
156
905
|
},
|
|
157
906
|
],
|
|
158
907
|
};
|
|
159
|
-
function buildCoreNetworkPolicy(target, egress) {
|
|
908
|
+
function buildCoreNetworkPolicy(target, policyKind, egress) {
|
|
160
909
|
return {
|
|
161
910
|
kind: 'NetworkPolicy',
|
|
911
|
+
policyKind,
|
|
162
912
|
namespace: target.namespace,
|
|
163
913
|
name: target.name,
|
|
164
914
|
manifest: {
|
|
@@ -166,32 +916,303 @@ function buildCoreNetworkPolicy(target, egress) {
|
|
|
166
916
|
kind: 'NetworkPolicy',
|
|
167
917
|
metadata: { name: target.name, namespace: target.namespace },
|
|
168
918
|
spec: {
|
|
169
|
-
podSelector: {
|
|
919
|
+
podSelector: {
|
|
920
|
+
matchLabels: egressPolicySelectorLabels(target),
|
|
921
|
+
},
|
|
170
922
|
policyTypes: ['Egress'],
|
|
171
923
|
egress,
|
|
172
924
|
},
|
|
173
925
|
},
|
|
174
926
|
};
|
|
175
927
|
}
|
|
176
|
-
|
|
928
|
+
/** `[443]` — the TLS-port default for both `tlsServerNames` and `tlsPorts`. */
|
|
929
|
+
const DEFAULT_TLS_PORTS = [443];
|
|
930
|
+
/** `{ port: '443', protocol: 'TCP' }` — Cilium's port shape, ports spelled as strings. */
|
|
931
|
+
function ciliumPortEntry(port) {
|
|
932
|
+
return { port: String(port), protocol: 'TCP' };
|
|
933
|
+
}
|
|
934
|
+
/**
|
|
935
|
+
* The refusal context of the PER-SANDBOX translation — the one that expands,
|
|
936
|
+
* and so the one `expandDomains: true` builds.
|
|
937
|
+
*
|
|
938
|
+
* Shared deliberately: the per-sandbox writer refuses a host by this context
|
|
939
|
+
* before it reads the fence, and {@link buildCiliumEgressManifest} refuses the
|
|
940
|
+
* same host by the same context on the way through the translation, so the two
|
|
941
|
+
* cannot name different fields for one entry however the earlier check is
|
|
942
|
+
* reached or skipped.
|
|
943
|
+
*/
|
|
944
|
+
export const PER_SANDBOX_NARROWING_REFUSAL = {
|
|
945
|
+
fieldPath: 'config.egress.perSandbox.narrowing',
|
|
946
|
+
expandsDottedEntries: true,
|
|
947
|
+
};
|
|
948
|
+
/**
|
|
949
|
+
* The refusal context of the config-level translation — `config.egress.policy`
|
|
950
|
+
* and its `config.egress.ciliumNarrowing`, which expands nothing.
|
|
951
|
+
*/
|
|
952
|
+
export const CONFIG_LEVEL_NARROWING_REFUSAL = {
|
|
953
|
+
fieldPath: 'config.egress.ciliumNarrowing',
|
|
954
|
+
expandsDottedEntries: false,
|
|
955
|
+
};
|
|
956
|
+
/**
|
|
957
|
+
* Refuse the one allowlist entry a narrowing option cannot express.
|
|
958
|
+
*
|
|
959
|
+
* `tlsServerNames` puts the entry on the rule as a TLS server name — one
|
|
960
|
+
* exact SNI value a handshake presents — and a `.domain` entry is a set of
|
|
961
|
+
* names no single SNI value means. Either way the emitted object would be
|
|
962
|
+
* admitted by the shipped fence, read back deep-equal to what was sent, and
|
|
963
|
+
* deny what the caller asked to allow — the failure every other refusal in
|
|
964
|
+
* this module exists to prevent. Refused rather than translated another way,
|
|
965
|
+
* because both other ways are guesses: dropping the subdomains silently
|
|
966
|
+
* narrows what the caller asked for, and a wildcard SNI value is not
|
|
967
|
+
* something the Cilium versions these manifests are written against are
|
|
968
|
+
* known here to match — an SNI that matches nothing denies just as
|
|
969
|
+
* completely, and more quietly.
|
|
970
|
+
*
|
|
971
|
+
* The entry is refused for a DIFFERENT reason on each of the two paths, and
|
|
972
|
+
* the message says which one the caller is on, because the same sentence
|
|
973
|
+
* cannot be true of both:
|
|
974
|
+
*
|
|
975
|
+
* - `expandDomains` — the per-sandbox writer's translation, which turns
|
|
976
|
+
* `.domain` into `matchName: domain` PLUS `matchPattern: '*.domain'`. No
|
|
977
|
+
* single SNI value means that pair: `domain` alone denies every subdomain
|
|
978
|
+
* the pattern admits, and the entry as written is not a name any handshake
|
|
979
|
+
* presents at all.
|
|
980
|
+
* - unexpanded — the config-level translation, which expands nothing. There
|
|
981
|
+
* the entry reaches the object as the literal `matchName: '.domain'`,
|
|
982
|
+
* which no DNS answer carries, so it already admits nothing; `serverNames`
|
|
983
|
+
* on top of it is a second, independent denial. Telling this caller to
|
|
984
|
+
* "leave tlsServerNames off for a domain list" would name a repair that is
|
|
985
|
+
* not one — the config-level `.domain` entry denies the domain and every
|
|
986
|
+
* subdomain with or without the option (a pre-existing defect of this
|
|
987
|
+
* translation, deferred to its own change) — so it is not offered here.
|
|
988
|
+
*
|
|
989
|
+
* `context` is WHICH translation the refusal is being spelled for, as one
|
|
990
|
+
* value — see {@link HostsFitNarrowingContext}. A reader has to be sent to
|
|
991
|
+
* the field they actually set, and the sentence they are sent by has to be
|
|
992
|
+
* true of the translation they are on, and both follow from that one fact:
|
|
993
|
+
* passed separately, an expanding caller gets the per-sandbox remedy attached
|
|
994
|
+
* to the config-level field name, a message whose only repair is a field the
|
|
995
|
+
* entry never came from. Called from BOTH — the translation itself, so every
|
|
996
|
+
* caller is covered, and the per-sandbox writer, which refuses earlier still,
|
|
997
|
+
* before it reads the fence.
|
|
998
|
+
*/
|
|
999
|
+
export function assertHostsFitNarrowing(allowedHosts, narrowing, context) {
|
|
1000
|
+
if (narrowing?.tlsServerNames !== true)
|
|
1001
|
+
return;
|
|
1002
|
+
for (const host of allowedHosts) {
|
|
1003
|
+
if (!host.startsWith('.'))
|
|
1004
|
+
continue;
|
|
1005
|
+
throw new KubernetesNetworkPolicyHostError(host, context.expandsDottedEntries
|
|
1006
|
+
? `names a domain and its subdomains while ${context.fieldPath}.tlsServerNames is on; a TLS server name is one exact SNI value a handshake presents, and this entry becomes a name plus a '*.domain' pattern, which no single value means — list the exact hosts, or leave tlsServerNames off for a domain list`
|
|
1007
|
+
: `names a domain and its subdomains while ${context.fieldPath}.tlsServerNames is on; a TLS server name is one exact SNI value a handshake presents, and this translation does not expand a leading-dot entry — it reaches the object as the literal matchName ${JSON.stringify(host)}, which no DNS answer carries — so the entry admits nothing as written, and serverNames on top of it is a second, independent denial. List the exact hosts this policy should allow; leaving tlsServerNames off does not repair the entry here`,
|
|
1008
|
+
// The closing grammar rides the same input as the sentence, and for
|
|
1009
|
+
// the same reason: the unexpanded body has just said this entry
|
|
1010
|
+
// admits nothing as written, so a tail asserting that
|
|
1011
|
+
// `.example.com` means the domain and its subdomains would state,
|
|
1012
|
+
// as implemented, the grammar the refusal exists because this
|
|
1013
|
+
// translation does not apply to it.
|
|
1014
|
+
{ stateHostnameGrammar: context.expandsDottedEntries });
|
|
1015
|
+
}
|
|
1016
|
+
}
|
|
1017
|
+
/**
|
|
1018
|
+
* The port list a narrowed translation applies to `host`, before splitting
|
|
1019
|
+
* out the TLS subset — see {@link KubernetesCiliumEgressNarrowing.ports}.
|
|
1020
|
+
* `undefined` means no port restriction: the host's `toFQDNs` rule carries
|
|
1021
|
+
* no `toPorts` at all.
|
|
1022
|
+
*/
|
|
1023
|
+
function narrowedHostPorts(host, narrowing) {
|
|
1024
|
+
const explicit = narrowing.hostPorts?.[host] ?? narrowing.ports;
|
|
1025
|
+
if (explicit !== undefined)
|
|
1026
|
+
return explicit;
|
|
1027
|
+
// `serverNames` needs a port to attach to — leaving this host with no
|
|
1028
|
+
// port at all would mean either no `serverNames` rule (silently dropping
|
|
1029
|
+
// the option) or one with no `toPorts`, which Cilium rejects on apply.
|
|
1030
|
+
if (narrowing.tlsServerNames === true)
|
|
1031
|
+
return narrowing.tlsPorts ?? DEFAULT_TLS_PORTS;
|
|
1032
|
+
return undefined;
|
|
1033
|
+
}
|
|
1034
|
+
/**
|
|
1035
|
+
* One host's `toFQDNs` rule under narrowing: the allowlist entry's `toFQDNs`
|
|
1036
|
+
* entries, plus `toPorts` split into a `serverNames`-bearing entry for the
|
|
1037
|
+
* host's TLS ports and a plain entry for whatever is left, when
|
|
1038
|
+
* `tlsServerNames` is on.
|
|
1039
|
+
*
|
|
1040
|
+
* `serverNames` carries the allowlist ENTRY as written, which is the same
|
|
1041
|
+
* string as the `toFQDNs` entry only while nothing expanded it. A TLS server
|
|
1042
|
+
* name is one exact SNI value a handshake presents, and a `.example.com`
|
|
1043
|
+
* entry — whose `toFQDNs` half is `example.com` PLUS `*.example.com` — has no
|
|
1044
|
+
* single SNI value meaning that set: `example.com` would deny every subdomain
|
|
1045
|
+
* the pattern admits, and `.example.com` is not a name any handshake ever
|
|
1046
|
+
* presents. So {@link assertHostsFitNarrowing} refuses that one combination
|
|
1047
|
+
* before this rule is built, from `buildCiliumEgressManifest` for every
|
|
1048
|
+
* caller and from the per-sandbox writer before it reads the fence — which is
|
|
1049
|
+
* why this function can take `host` and the entry as one string.
|
|
1050
|
+
*/
|
|
1051
|
+
function narrowedHostFqdnRule(host, narrowing, fqdns = [{ matchName: host }]) {
|
|
1052
|
+
const ports = narrowedHostPorts(host, narrowing);
|
|
1053
|
+
if (ports === undefined)
|
|
1054
|
+
return { toFQDNs: fqdns };
|
|
1055
|
+
if (narrowing.tlsServerNames !== true) {
|
|
1056
|
+
return { toFQDNs: fqdns, toPorts: [{ ports: ports.map(ciliumPortEntry) }] };
|
|
1057
|
+
}
|
|
1058
|
+
const tlsPorts = narrowing.tlsPorts ?? DEFAULT_TLS_PORTS;
|
|
1059
|
+
const tlsSubset = ports.filter((port) => tlsPorts.includes(port));
|
|
1060
|
+
const rest = ports.filter((port) => !tlsPorts.includes(port));
|
|
1061
|
+
const toPorts = [];
|
|
1062
|
+
if (tlsSubset.length > 0) {
|
|
1063
|
+
toPorts.push({ ports: tlsSubset.map(ciliumPortEntry), serverNames: [host] });
|
|
1064
|
+
}
|
|
1065
|
+
if (rest.length > 0)
|
|
1066
|
+
toPorts.push({ ports: rest.map(ciliumPortEntry) });
|
|
1067
|
+
return {
|
|
1068
|
+
toFQDNs: fqdns,
|
|
1069
|
+
...(toPorts.length > 0 ? { toPorts } : {}),
|
|
1070
|
+
};
|
|
1071
|
+
}
|
|
1072
|
+
/**
|
|
1073
|
+
* One allowlist entry, expanded to the `toFQDNs` entries it means.
|
|
1074
|
+
*
|
|
1075
|
+
* `SandboxNetworkPolicy.allowedHosts`'s own grammar, which the SDK states and
|
|
1076
|
+
* the docker backend implements: `api.example.com` is that host, and
|
|
1077
|
+
* `.example.com` is the domain AND its subdomains. Cilium's `matchName` is an
|
|
1078
|
+
* exact name and does not match across a `.`, so the domain form needs the
|
|
1079
|
+
* name plus a `matchPattern` — `*.example.com` alone would admit
|
|
1080
|
+
* `a.example.com` and not `example.com` itself.
|
|
1081
|
+
*
|
|
1082
|
+
* Used by the PER-SANDBOX policy only. The config-level translation
|
|
1083
|
+
* (`config.egress.policy`) deliberately does not expand anything: what it
|
|
1084
|
+
* emits for a given config is pinned byte-for-byte, because verification of
|
|
1085
|
+
* the named object is an exact match and a changed translation fails every
|
|
1086
|
+
* `create()` on every deployment that already applied a policy.
|
|
1087
|
+
*/
|
|
1088
|
+
export function ciliumFqdnEntries(entry) {
|
|
1089
|
+
if (!entry.startsWith('.'))
|
|
1090
|
+
return [{ matchName: entry }];
|
|
1091
|
+
const domain = entry.slice(1);
|
|
1092
|
+
return [{ matchName: domain }, { matchPattern: `*.${domain}` }];
|
|
1093
|
+
}
|
|
1094
|
+
/**
|
|
1095
|
+
* The DNS-visibility rule narrowed to an exact `matchName` per allowed host
|
|
1096
|
+
* plus the host under every search suffix, replacing
|
|
1097
|
+
* {@link CILIUM_DNS_VISIBILITY_RULE}'s `matchPattern: '*'`. See
|
|
1098
|
+
* {@link KubernetesCiliumDnsNarrowing}.
|
|
1099
|
+
*/
|
|
1100
|
+
function narrowedDnsVisibilityRule(allowedHosts, fallbackNamespace, dnsNames, expandDomains = false) {
|
|
1101
|
+
const namespace = dnsNames.namespace ?? fallbackNamespace;
|
|
1102
|
+
const clusterDomain = dnsNames.clusterDomain ?? 'cluster.local';
|
|
1103
|
+
const suffixes = [
|
|
1104
|
+
`${namespace}.svc.${clusterDomain}`,
|
|
1105
|
+
`svc.${clusterDomain}`,
|
|
1106
|
+
clusterDomain,
|
|
1107
|
+
...(dnsNames.searchSuffixes ?? []),
|
|
1108
|
+
];
|
|
1109
|
+
const matchNames = [];
|
|
1110
|
+
for (const entry of allowedHosts) {
|
|
1111
|
+
// A `.domain` entry resolves through its subdomains as well, so the
|
|
1112
|
+
// DNS proxy has to be allowed to SEE those lookups or `toFQDNs` never
|
|
1113
|
+
// learns the addresses they resolve to. Off for the config-level
|
|
1114
|
+
// translation, whose emitted bytes are pinned: `expandDomains` is
|
|
1115
|
+
// false there and `host === entry`, so this loop is what it always
|
|
1116
|
+
// was.
|
|
1117
|
+
const wildcard = expandDomains && entry.startsWith('.');
|
|
1118
|
+
const host = wildcard ? entry.slice(1) : entry;
|
|
1119
|
+
matchNames.push({ matchName: host });
|
|
1120
|
+
if (wildcard)
|
|
1121
|
+
matchNames.push({ matchPattern: `*.${host}` });
|
|
1122
|
+
for (const suffix of suffixes) {
|
|
1123
|
+
matchNames.push({ matchName: `${host}.${suffix}` });
|
|
1124
|
+
// The suffixed names are here because a guest resolving a name
|
|
1125
|
+
// with fewer dots than the cluster's `ndots` tries the search
|
|
1126
|
+
// suffixes FIRST, and a lookup the DNS proxy refuses is not an
|
|
1127
|
+
// NXDOMAIN the resolver walks past — it can fail the whole
|
|
1128
|
+
// resolution. An EXPANDED entry admits subdomains, so its
|
|
1129
|
+
// subdomains need the same treatment, or `a.example.com` fails on
|
|
1130
|
+
// its first search-suffix attempt under a rule that allows it.
|
|
1131
|
+
if (wildcard)
|
|
1132
|
+
matchNames.push({ matchPattern: `*.${host}.${suffix}` });
|
|
1133
|
+
}
|
|
1134
|
+
}
|
|
1135
|
+
return {
|
|
1136
|
+
toEndpoints: CILIUM_DNS_VISIBILITY_RULE.toEndpoints,
|
|
1137
|
+
toPorts: [
|
|
1138
|
+
{
|
|
1139
|
+
ports: [{ port: '53', protocol: 'ANY' }],
|
|
1140
|
+
rules: { dns: matchNames },
|
|
1141
|
+
},
|
|
1142
|
+
],
|
|
1143
|
+
};
|
|
1144
|
+
}
|
|
1145
|
+
export function buildCiliumEgressManifest(options) {
|
|
1146
|
+
const { narrowing, allowedHosts, expandDomains = false } = options;
|
|
1147
|
+
// Before anything is emitted. A leading-dot entry under `tlsServerNames`
|
|
1148
|
+
// would become `serverNames: ['.domain']` — not a name any handshake
|
|
1149
|
+
// presents — and `['domain']` would deny every subdomain the `toFQDNs`
|
|
1150
|
+
// half of the same rule admits; the object is admitted by the shipped
|
|
1151
|
+
// fence and reads back deep-equal to what was sent, so nothing downstream
|
|
1152
|
+
// would ever report it.
|
|
1153
|
+
//
|
|
1154
|
+
// What this call guarantees, per caller shape: the per-sandbox writer
|
|
1155
|
+
// refuses the same hosts earlier still, by name and by the same context
|
|
1156
|
+
// (`per-sandbox-policy.ts`), and this call covers them again if that check
|
|
1157
|
+
// is ever reached later or skipped; the config-level translation has no
|
|
1158
|
+
// earlier check of its own and relies on this one entirely; and a direct
|
|
1159
|
+
// call to this function is covered here too, whatever its `expandDomains`.
|
|
1160
|
+
//
|
|
1161
|
+
// ONE input decides all of it. `expandDomains` is not a formatting flag —
|
|
1162
|
+
// it is which translation this is, and so which field a refused caller
|
|
1163
|
+
// actually set, which sentence is true of the entry on that path, and
|
|
1164
|
+
// whether the translation applies the hostname grammar at all. Deriving
|
|
1165
|
+
// the field path from anything else is how an expanding caller came to be
|
|
1166
|
+
// sent to the config-level field for a repair that only exists under
|
|
1167
|
+
// `perSandbox.narrowing`.
|
|
1168
|
+
assertHostsFitNarrowing(allowedHosts, narrowing, expandDomains ? PER_SANDBOX_NARROWING_REFUSAL : CONFIG_LEVEL_NARROWING_REFUSAL);
|
|
1169
|
+
// Unnarrowed is the exact shape every release before #490 emitted — kept
|
|
1170
|
+
// as its own branch, untouched, rather than folded into the narrowed one
|
|
1171
|
+
// with every option defaulted off, so the byte-identical guarantee does
|
|
1172
|
+
// not depend on the narrowed code path happening to reduce to it.
|
|
1173
|
+
const narrowed = ciliumNarrowingIsActive(narrowing);
|
|
1174
|
+
const activeDns = narrowed ? activeDnsNarrowing(narrowing.dnsNames) : undefined;
|
|
1175
|
+
const dnsRule = activeDns !== undefined
|
|
1176
|
+
? narrowedDnsVisibilityRule(allowedHosts, options.namespace, activeDns, expandDomains)
|
|
1177
|
+
: CILIUM_DNS_VISIBILITY_RULE;
|
|
1178
|
+
const entriesFor = (entry) => expandDomains ? ciliumFqdnEntries(entry) : [{ matchName: entry }];
|
|
1179
|
+
const hostRules = narrowed
|
|
1180
|
+
? allowedHosts.map((host) => narrowedHostFqdnRule(host, narrowing, entriesFor(host)))
|
|
1181
|
+
: [{ toFQDNs: allowedHosts.flatMap(entriesFor) }];
|
|
177
1182
|
return {
|
|
178
1183
|
kind: 'CiliumNetworkPolicy',
|
|
179
|
-
|
|
180
|
-
|
|
1184
|
+
policyKind: options.policyKind,
|
|
1185
|
+
namespace: options.namespace,
|
|
1186
|
+
name: options.name,
|
|
181
1187
|
manifest: {
|
|
182
1188
|
apiVersion: `${CILIUM_NETWORK_POLICY_API_GROUP}/${CILIUM_NETWORK_POLICY_API_VERSION}`,
|
|
183
1189
|
kind: 'CiliumNetworkPolicy',
|
|
184
|
-
metadata: {
|
|
1190
|
+
metadata: {
|
|
1191
|
+
name: options.name,
|
|
1192
|
+
namespace: options.namespace,
|
|
1193
|
+
...(options.ownerReferences !== undefined
|
|
1194
|
+
? { ownerReferences: options.ownerReferences }
|
|
1195
|
+
: {}),
|
|
1196
|
+
},
|
|
185
1197
|
spec: {
|
|
186
|
-
endpointSelector: {
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
],
|
|
1198
|
+
endpointSelector: {
|
|
1199
|
+
matchLabels: options.selectorLabels,
|
|
1200
|
+
},
|
|
1201
|
+
egress: [dnsRule, ...hostRules],
|
|
191
1202
|
},
|
|
192
1203
|
},
|
|
193
1204
|
};
|
|
194
1205
|
}
|
|
1206
|
+
function buildCiliumNetworkPolicy(target, policyKind, allowedHosts, narrowing) {
|
|
1207
|
+
return buildCiliumEgressManifest({
|
|
1208
|
+
namespace: target.namespace,
|
|
1209
|
+
name: target.name,
|
|
1210
|
+
selectorLabels: egressPolicySelectorLabels(target),
|
|
1211
|
+
allowedHosts,
|
|
1212
|
+
policyKind,
|
|
1213
|
+
...(narrowing !== undefined ? { narrowing } : {}),
|
|
1214
|
+
});
|
|
1215
|
+
}
|
|
195
1216
|
/**
|
|
196
1217
|
* The pure translation: an {@link EgressPolicy} plus the declared
|
|
197
1218
|
* {@link KubernetesEgressEngine} in, the concrete manifest this backend can
|
|
@@ -209,23 +1230,34 @@ function buildCiliumNetworkPolicy(target, allowedHosts) {
|
|
|
209
1230
|
* hosts against, and re-resolving on every `create()` would only produce a
|
|
210
1231
|
* value nothing downstream re-applies to the cluster.
|
|
211
1232
|
*/
|
|
212
|
-
export async function translateEgressPolicy(policy, engine, target) {
|
|
213
|
-
assertEgressPolicyIsEnforceable(policy, engine);
|
|
1233
|
+
export async function translateEgressPolicy(policy, engine, target, ciliumNarrowing) {
|
|
1234
|
+
assertEgressPolicyIsEnforceable(policy, engine, ciliumNarrowing);
|
|
214
1235
|
switch (policy.kind) {
|
|
215
1236
|
case 'deny-all':
|
|
216
|
-
return buildCoreNetworkPolicy(target, [CLUSTER_DNS_EGRESS_RULE]);
|
|
1237
|
+
return buildCoreNetworkPolicy(target, 'deny-all', [CLUSTER_DNS_EGRESS_RULE]);
|
|
1238
|
+
case 'no-network':
|
|
1239
|
+
// `policyTypes: ['Egress']` with an EMPTY rule list is the API's own
|
|
1240
|
+
// spelling of "this pod sends nothing": the pod is in egress
|
|
1241
|
+
// default-deny and no rule lets anything back out. Not even DNS —
|
|
1242
|
+
// see the kind's own doc.
|
|
1243
|
+
return buildCoreNetworkPolicy(target, 'no-network', []);
|
|
1244
|
+
case 'public-internet':
|
|
1245
|
+
return buildCoreNetworkPolicy(target, 'public-internet', [
|
|
1246
|
+
KUBE_DNS_EGRESS_RULE,
|
|
1247
|
+
publicInternetEgressRule(policy.exceptCidrs),
|
|
1248
|
+
]);
|
|
217
1249
|
case 'allow-all':
|
|
218
1250
|
// No `to`/`ports` on an egress rule matches every destination and
|
|
219
1251
|
// every port. `CLUSTER_DNS_EGRESS_RULE` is a strict subset of this,
|
|
220
1252
|
// so it is folded in rather than listed twice.
|
|
221
|
-
return buildCoreNetworkPolicy(target, [{}]);
|
|
1253
|
+
return buildCoreNetworkPolicy(target, 'allow-all', [{}]);
|
|
222
1254
|
case 'static':
|
|
223
1255
|
// `assertEgressPolicyIsEnforceable` already threw above unless
|
|
224
1256
|
// `engine === 'cilium'`, so reaching here means it did not.
|
|
225
|
-
return buildCiliumNetworkPolicy(target, policy.allowedHosts);
|
|
1257
|
+
return buildCiliumNetworkPolicy(target, 'static', policy.allowedHosts, ciliumNarrowing);
|
|
226
1258
|
case 'resolver': {
|
|
227
1259
|
const allowedHosts = await policy.resolve();
|
|
228
|
-
return buildCiliumNetworkPolicy(target, allowedHosts);
|
|
1260
|
+
return buildCiliumNetworkPolicy(target, 'resolver', allowedHosts, ciliumNarrowing);
|
|
229
1261
|
}
|
|
230
1262
|
default: {
|
|
231
1263
|
const exhaustive = policy;
|
|
@@ -277,7 +1309,13 @@ export class KubernetesEgressPolicyMismatchError extends Error {
|
|
|
277
1309
|
* - `NetworkPolicy` additionally declares `policyTypes` including
|
|
278
1310
|
* `'Egress'` — a `NetworkPolicy` with an `egress` array but no `'Egress'`
|
|
279
1311
|
* in `policyTypes` enforces nothing on egress at all;
|
|
280
|
-
* - the `egress` rule array matches the translation exactly
|
|
1312
|
+
* - the `egress` rule array matches the translation exactly;
|
|
1313
|
+
* - and, ONLY when the translation carries `metadata.ownerReferences` (the
|
|
1314
|
+
* per-sandbox policies of `per-sandbox-policy.ts`, never the operator's
|
|
1315
|
+
* own object), that the live object still carries each of them — an owner
|
|
1316
|
+
* reference dropped between the write and the read is a policy the
|
|
1317
|
+
* cluster will never collect with the sandbox it belongs to, which is the
|
|
1318
|
+
* leak the reference exists to prevent, and it is invisible in `spec`.
|
|
281
1319
|
*
|
|
282
1320
|
* Never mutates and never creates — a 404/410 is refused, not repaired.
|
|
283
1321
|
*/
|
|
@@ -310,5 +1348,1111 @@ export async function verifyEgressPolicyApplied(client, translated, signal) {
|
|
|
310
1348
|
if (!isDeepStrictEqual(actualSpec.egress, expectedSpec.egress)) {
|
|
311
1349
|
throw new KubernetesEgressPolicyMismatchError(translated.kind, path, `spec.egress is ${JSON.stringify(actualSpec.egress)}, expected ${JSON.stringify(expectedSpec.egress)}`);
|
|
312
1350
|
}
|
|
1351
|
+
// Last, and only for a translation that asked for one: an object written
|
|
1352
|
+
// with an owner reference and read back without it is a policy that
|
|
1353
|
+
// outlives its sandbox. The operator-applied objects this function's other
|
|
1354
|
+
// caller checks carry none, so they never reach this branch.
|
|
1355
|
+
const expectedOwners = translated.manifest.metadata?.ownerReferences;
|
|
1356
|
+
if (expectedOwners !== undefined) {
|
|
1357
|
+
const actualOwners = resource?.metadata
|
|
1358
|
+
?.ownerReferences;
|
|
1359
|
+
const held = Array.isArray(actualOwners) ? actualOwners : [];
|
|
1360
|
+
for (const owner of expectedOwners) {
|
|
1361
|
+
if (held.some((entry) => isDeepStrictEqual(entry, owner)))
|
|
1362
|
+
continue;
|
|
1363
|
+
throw new KubernetesEgressPolicyMismatchError(translated.kind, path, `metadata.ownerReferences is ${JSON.stringify(actualOwners)}, expected it to carry ${JSON.stringify(owner)} — without that entry the cluster never collects this policy with the object it belongs to, so it would outlive the sandbox it was written for`);
|
|
1364
|
+
}
|
|
1365
|
+
}
|
|
1366
|
+
}
|
|
1367
|
+
function parseIpv4(text) {
|
|
1368
|
+
const parts = text.split('.');
|
|
1369
|
+
if (parts.length !== 4)
|
|
1370
|
+
return undefined;
|
|
1371
|
+
let value = 0n;
|
|
1372
|
+
for (const part of parts) {
|
|
1373
|
+
if (!/^\d{1,3}$/.test(part))
|
|
1374
|
+
return undefined;
|
|
1375
|
+
const octet = Number(part);
|
|
1376
|
+
if (octet > 255)
|
|
1377
|
+
return undefined;
|
|
1378
|
+
value = (value << 8n) | BigInt(octet);
|
|
1379
|
+
}
|
|
1380
|
+
return value;
|
|
1381
|
+
}
|
|
1382
|
+
function parseIpv6(text) {
|
|
1383
|
+
// An embedded IPv4 tail (`::ffff:10.0.0.1`) is legal and is how a
|
|
1384
|
+
// dual-stack cluster spells a v4 address in a v6 field.
|
|
1385
|
+
let head = text;
|
|
1386
|
+
let tail = 0n;
|
|
1387
|
+
let tailGroups = 0;
|
|
1388
|
+
const lastColon = text.lastIndexOf(':');
|
|
1389
|
+
if (lastColon >= 0 && text.slice(lastColon + 1).includes('.')) {
|
|
1390
|
+
const embedded = parseIpv4(text.slice(lastColon + 1));
|
|
1391
|
+
if (embedded === undefined)
|
|
1392
|
+
return undefined;
|
|
1393
|
+
head = text.slice(0, lastColon + 1);
|
|
1394
|
+
tail = embedded;
|
|
1395
|
+
tailGroups = 2;
|
|
1396
|
+
}
|
|
1397
|
+
const halves = head.split('::');
|
|
1398
|
+
if (halves.length > 2)
|
|
1399
|
+
return undefined;
|
|
1400
|
+
const readGroups = (part) => {
|
|
1401
|
+
if (part === '')
|
|
1402
|
+
return [];
|
|
1403
|
+
const groups = [];
|
|
1404
|
+
for (const group of part.split(':')) {
|
|
1405
|
+
if (group === '')
|
|
1406
|
+
continue;
|
|
1407
|
+
if (!/^[0-9a-fA-F]{1,4}$/.test(group))
|
|
1408
|
+
return undefined;
|
|
1409
|
+
groups.push(BigInt(Number.parseInt(group, 16)));
|
|
1410
|
+
}
|
|
1411
|
+
return groups;
|
|
1412
|
+
};
|
|
1413
|
+
const left = readGroups(halves[0] ?? '');
|
|
1414
|
+
const right = readGroups(halves[1] ?? '');
|
|
1415
|
+
if (left === undefined || right === undefined)
|
|
1416
|
+
return undefined;
|
|
1417
|
+
const present = left.length + right.length + tailGroups;
|
|
1418
|
+
if (present > 8)
|
|
1419
|
+
return undefined;
|
|
1420
|
+
if (halves.length === 1 && present !== 8)
|
|
1421
|
+
return undefined;
|
|
1422
|
+
const zeros = 8 - present;
|
|
1423
|
+
const groups = [...left, ...Array.from({ length: zeros }, () => 0n), ...right];
|
|
1424
|
+
let value = 0n;
|
|
1425
|
+
for (const group of groups)
|
|
1426
|
+
value = (value << 16n) | group;
|
|
1427
|
+
if (tailGroups === 2)
|
|
1428
|
+
value = (value << 32n) | tail;
|
|
1429
|
+
return value;
|
|
1430
|
+
}
|
|
1431
|
+
/**
|
|
1432
|
+
* A CIDR, or `undefined` when it is not one this check can read. Host bits
|
|
1433
|
+
* are masked off rather than rejected: `10.0.0.1/8` and `10.0.0.0/8` name the
|
|
1434
|
+
* same block, and an operator who wrote the first meant the second.
|
|
1435
|
+
*/
|
|
1436
|
+
export function parseCidr(text) {
|
|
1437
|
+
if (typeof text !== 'string')
|
|
1438
|
+
return undefined;
|
|
1439
|
+
const slash = text.indexOf('/');
|
|
1440
|
+
if (slash < 0)
|
|
1441
|
+
return undefined;
|
|
1442
|
+
const address = text.slice(0, slash);
|
|
1443
|
+
const prefix = text.slice(slash + 1);
|
|
1444
|
+
if (!/^\d{1,3}$/.test(prefix))
|
|
1445
|
+
return undefined;
|
|
1446
|
+
const bits = Number(prefix);
|
|
1447
|
+
if (address.includes(':')) {
|
|
1448
|
+
const value = parseIpv6(address);
|
|
1449
|
+
if (value === undefined || bits > 128)
|
|
1450
|
+
return undefined;
|
|
1451
|
+
return { version: 6, base: maskAddress(value, bits, 128), bits };
|
|
1452
|
+
}
|
|
1453
|
+
const value = parseIpv4(address);
|
|
1454
|
+
if (value === undefined || bits > 32)
|
|
1455
|
+
return undefined;
|
|
1456
|
+
return { version: 4, base: maskAddress(value, bits, 32), bits };
|
|
1457
|
+
}
|
|
1458
|
+
function maskAddress(value, bits, width) {
|
|
1459
|
+
if (bits === 0)
|
|
1460
|
+
return 0n;
|
|
1461
|
+
const host = BigInt(width - bits);
|
|
1462
|
+
return (value >> host) << host;
|
|
1463
|
+
}
|
|
1464
|
+
/** Every address of `inner` is an address of `outer`. */
|
|
1465
|
+
function cidrContains(outer, inner) {
|
|
1466
|
+
if (outer.version !== inner.version)
|
|
1467
|
+
return false;
|
|
1468
|
+
if (outer.bits > inner.bits)
|
|
1469
|
+
return false;
|
|
1470
|
+
const width = outer.version === 4 ? 32 : 128;
|
|
1471
|
+
return maskAddress(inner.base, outer.bits, width) === outer.base;
|
|
1472
|
+
}
|
|
1473
|
+
/** They share at least one address — i.e. one contains the other. */
|
|
1474
|
+
function cidrsOverlap(a, b) {
|
|
1475
|
+
return cidrContains(a, b) || cidrContains(b, a);
|
|
1476
|
+
}
|
|
1477
|
+
/**
|
|
1478
|
+
* The cluster resolver's identity as a `PolicyPeer` — shared by
|
|
1479
|
+
* {@link egressAllowance} (reading a Cilium DNS-visibility rule into its
|
|
1480
|
+
* core-shaped equivalent) and {@link reachesResolverAtDnsPort} (deciding
|
|
1481
|
+
* whether some OTHER policy's rule reaches it), so there is one definition of
|
|
1482
|
+
* "this peer is the cluster resolver" rather than two that could drift apart.
|
|
1483
|
+
*/
|
|
1484
|
+
const RESOLVER_PEER = {
|
|
1485
|
+
kind: 'selector',
|
|
1486
|
+
namespaceSelector: {
|
|
1487
|
+
matchLabels: { 'kubernetes.io/metadata.name': 'kube-system' },
|
|
1488
|
+
},
|
|
1489
|
+
podSelector: { matchLabels: { 'k8s-app': 'kube-dns' } },
|
|
1490
|
+
text: 'the cluster resolver',
|
|
1491
|
+
};
|
|
1492
|
+
function readPortRanges(ports, defaultProtocol) {
|
|
1493
|
+
const entries = readList(ports);
|
|
1494
|
+
if (entries === 'unreadable')
|
|
1495
|
+
return 'unreadable';
|
|
1496
|
+
// Absent or empty `ports` on an egress rule means EVERY port.
|
|
1497
|
+
if (entries === undefined || entries.length === 0)
|
|
1498
|
+
return 'all';
|
|
1499
|
+
const ranges = [];
|
|
1500
|
+
for (const entry of entries) {
|
|
1501
|
+
if (!isRecord(entry))
|
|
1502
|
+
return 'unreadable';
|
|
1503
|
+
const rawProtocol = entry.protocol;
|
|
1504
|
+
if (rawProtocol !== undefined && typeof rawProtocol !== 'string')
|
|
1505
|
+
return 'unreadable';
|
|
1506
|
+
const protocol = rawProtocol === undefined
|
|
1507
|
+
? defaultProtocol
|
|
1508
|
+
: rawProtocol.toUpperCase() === 'ANY'
|
|
1509
|
+
? undefined
|
|
1510
|
+
: rawProtocol.toUpperCase();
|
|
1511
|
+
const endPort = entry.endPort;
|
|
1512
|
+
if (endPort !== undefined && typeof endPort !== 'number')
|
|
1513
|
+
return 'unreadable';
|
|
1514
|
+
const raw = entry.port;
|
|
1515
|
+
if (raw === undefined) {
|
|
1516
|
+
ranges.push(protocol === undefined ? {} : { protocol });
|
|
1517
|
+
continue;
|
|
1518
|
+
}
|
|
1519
|
+
const port = typeof raw === 'number' ? raw : typeof raw === 'string' ? Number(raw) : Number.NaN;
|
|
1520
|
+
// A NAMED container port. Resolving it needs the destination pod's own
|
|
1521
|
+
// container spec, which this check never has.
|
|
1522
|
+
if (!Number.isInteger(port))
|
|
1523
|
+
return 'unreadable';
|
|
1524
|
+
ranges.push({
|
|
1525
|
+
...(protocol !== undefined ? { protocol } : {}),
|
|
1526
|
+
start: port,
|
|
1527
|
+
...(typeof endPort === 'number' ? { end: endPort } : {}),
|
|
1528
|
+
});
|
|
1529
|
+
}
|
|
1530
|
+
return ranges;
|
|
1531
|
+
}
|
|
1532
|
+
function readCorePeer(peer) {
|
|
1533
|
+
if (!isRecord(peer))
|
|
1534
|
+
return undefined;
|
|
1535
|
+
const { ipBlock, podSelector, namespaceSelector } = peer;
|
|
1536
|
+
if (ipBlock !== undefined) {
|
|
1537
|
+
if (!isRecord(ipBlock))
|
|
1538
|
+
return undefined;
|
|
1539
|
+
const cidr = parseCidr(ipBlock.cidr);
|
|
1540
|
+
if (cidr === undefined)
|
|
1541
|
+
return undefined;
|
|
1542
|
+
const rawExcept = readList(ipBlock.except);
|
|
1543
|
+
if (rawExcept === 'unreadable')
|
|
1544
|
+
return undefined;
|
|
1545
|
+
const except = [];
|
|
1546
|
+
for (const entry of rawExcept ?? []) {
|
|
1547
|
+
const parsed = parseCidr(entry);
|
|
1548
|
+
if (parsed === undefined)
|
|
1549
|
+
return undefined;
|
|
1550
|
+
except.push(parsed);
|
|
1551
|
+
}
|
|
1552
|
+
return { kind: 'cidr', cidr, except, text: JSON.stringify(ipBlock) };
|
|
1553
|
+
}
|
|
1554
|
+
if (podSelector === undefined && namespaceSelector === undefined) {
|
|
1555
|
+
// A peer naming none of the three constrains nothing. The API server
|
|
1556
|
+
// rejects it on admission, so an object carrying one did not come from
|
|
1557
|
+
// there and is not read as anything.
|
|
1558
|
+
return undefined;
|
|
1559
|
+
}
|
|
1560
|
+
if (!selectorIsReadable(podSelector) || !selectorIsReadable(namespaceSelector))
|
|
1561
|
+
return undefined;
|
|
1562
|
+
return {
|
|
1563
|
+
kind: 'selector',
|
|
1564
|
+
...(namespaceSelector !== undefined ? { namespaceSelector } : {}),
|
|
1565
|
+
...(podSelector !== undefined ? { podSelector } : {}),
|
|
1566
|
+
text: JSON.stringify(peer),
|
|
1567
|
+
};
|
|
1568
|
+
}
|
|
1569
|
+
/**
|
|
1570
|
+
* The allowance a translated policy expresses. See {@link EgressAllowance}.
|
|
1571
|
+
*/
|
|
1572
|
+
export function egressAllowance(translated) {
|
|
1573
|
+
const spec = translated.manifest.spec;
|
|
1574
|
+
// This module built the manifest a line ago, so `spec.egress` is always a
|
|
1575
|
+
// list here; the narrowing exists so the allowance is derived from the
|
|
1576
|
+
// object itself rather than from a second statement of what each kind
|
|
1577
|
+
// permits, and an impossible shape yields an allowance that permits
|
|
1578
|
+
// nothing rather than one that permits anything.
|
|
1579
|
+
const emitted = readList(spec.egress);
|
|
1580
|
+
const rules = emitted === undefined || emitted === 'unreadable' ? [] : emitted;
|
|
1581
|
+
if (translated.kind === 'CiliumNetworkPolicy') {
|
|
1582
|
+
const fqdns = [];
|
|
1583
|
+
// The DNS-visibility rule is the one entry among `rules` whose
|
|
1584
|
+
// `toEndpoints` names the cluster resolver — narrowed or not, it always
|
|
1585
|
+
// reuses `CILIUM_DNS_VISIBILITY_RULE.toEndpoints` verbatim (see
|
|
1586
|
+
// `narrowedDnsVisibilityRule` and `buildCiliumNetworkPolicy`), so a deep
|
|
1587
|
+
// equality check finds it regardless of which branch built this rule.
|
|
1588
|
+
const dnsVisibilityRule = rules.find((rule) => isRecord(rule) &&
|
|
1589
|
+
isDeepStrictEqual(rule.toEndpoints, CILIUM_DNS_VISIBILITY_RULE.toEndpoints));
|
|
1590
|
+
for (const rule of rules) {
|
|
1591
|
+
if (!isRecord(rule))
|
|
1592
|
+
continue;
|
|
1593
|
+
const hostNames = readList(rule.toFQDNs);
|
|
1594
|
+
if (hostNames === undefined || hostNames === 'unreadable')
|
|
1595
|
+
continue;
|
|
1596
|
+
// This module built `rule` a few lines above (see the comment at the
|
|
1597
|
+
// top of this function), so its `toPorts` is always in the shape
|
|
1598
|
+
// `readCiliumRulePorts` reads — 'unreadable' cannot happen for a
|
|
1599
|
+
// manifest this file emitted, and 'all' is the safe fallback if it
|
|
1600
|
+
// somehow did, since that is what an absent `toPorts` also means.
|
|
1601
|
+
const portsRead = readCiliumRulePorts(rule);
|
|
1602
|
+
const ports = portsRead.ok ? portsRead.ports : 'all';
|
|
1603
|
+
for (const entry of hostNames) {
|
|
1604
|
+
if (isRecord(entry) && typeof entry.matchName === 'string') {
|
|
1605
|
+
fqdns.push({ host: entry.matchName, ports });
|
|
1606
|
+
}
|
|
1607
|
+
}
|
|
1608
|
+
}
|
|
1609
|
+
// This module built `dnsVisibilityRule` above (when it exists at all —
|
|
1610
|
+
// every Cilium translation this function reads carries one), so
|
|
1611
|
+
// `readCiliumRuleDnsNames` failing to read it, or reading a wildcard,
|
|
1612
|
+
// cannot mean anything other than "not narrowed": 'all' is the safe
|
|
1613
|
+
// fallback, the same reasoning `readCiliumRulePorts`'s own fallback above
|
|
1614
|
+
// already relies on.
|
|
1615
|
+
const dnsNamesRead = dnsVisibilityRule === undefined ? undefined : readCiliumRuleDnsNames(dnsVisibilityRule);
|
|
1616
|
+
const dnsNarrowedTo = dnsNamesRead?.ok === true && dnsNamesRead.names !== 'all' ? dnsNamesRead.names : 'all';
|
|
1617
|
+
return {
|
|
1618
|
+
// The core-shaped reading of CILIUM_DNS_VISIBILITY_RULE — the one
|
|
1619
|
+
// place in this module where one resource's rule is restated in the
|
|
1620
|
+
// other's vocabulary, so that a core policy allowing exactly cluster
|
|
1621
|
+
// DNS is not reported as widening a translation that already allows
|
|
1622
|
+
// it. Nothing else about a Cilium translation is restated: an
|
|
1623
|
+
// allowlist of names has no core spelling at all.
|
|
1624
|
+
destinations: [{ peer: RESOLVER_PEER, ports: [{ start: 53 }] }],
|
|
1625
|
+
fqdns,
|
|
1626
|
+
dnsNarrowedTo,
|
|
1627
|
+
permitsEverything: false,
|
|
1628
|
+
permitsNothing: false,
|
|
1629
|
+
policyKind: translated.policyKind,
|
|
1630
|
+
};
|
|
1631
|
+
}
|
|
1632
|
+
const destinations = [];
|
|
1633
|
+
let permitsEverything = false;
|
|
1634
|
+
for (const rule of rules) {
|
|
1635
|
+
if (!isRecord(rule))
|
|
1636
|
+
continue;
|
|
1637
|
+
const ports = readPortRanges(rule.ports, 'TCP');
|
|
1638
|
+
if (ports === 'unreadable')
|
|
1639
|
+
continue;
|
|
1640
|
+
const to = readList(rule.to);
|
|
1641
|
+
if (to === 'unreadable')
|
|
1642
|
+
continue;
|
|
1643
|
+
if (to === undefined || to.length === 0) {
|
|
1644
|
+
destinations.push({ peer: { kind: 'everything' }, ports });
|
|
1645
|
+
if (ports === 'all')
|
|
1646
|
+
permitsEverything = true;
|
|
1647
|
+
continue;
|
|
1648
|
+
}
|
|
1649
|
+
for (const peer of to) {
|
|
1650
|
+
const read = readCorePeer(peer);
|
|
1651
|
+
if (read !== undefined)
|
|
1652
|
+
destinations.push({ peer: read, ports });
|
|
1653
|
+
}
|
|
1654
|
+
}
|
|
1655
|
+
return {
|
|
1656
|
+
destinations,
|
|
1657
|
+
fqdns: [],
|
|
1658
|
+
// A core `NetworkPolicy` translation never narrows DNS — it has no L7
|
|
1659
|
+
// concept to narrow with — so the DNS-widening check in
|
|
1660
|
+
// `coreEgressRuleVerdict`/`ciliumEgressRuleVerdict` never fires against
|
|
1661
|
+
// this allowance.
|
|
1662
|
+
dnsNarrowedTo: 'all',
|
|
1663
|
+
permitsEverything,
|
|
1664
|
+
permitsNothing: rules.length === 0,
|
|
1665
|
+
policyKind: translated.policyKind,
|
|
1666
|
+
};
|
|
1667
|
+
}
|
|
1668
|
+
// ---------------------------------------------------------------------------
|
|
1669
|
+
// Is one policy's rule inside the translation
|
|
1670
|
+
// ---------------------------------------------------------------------------
|
|
1671
|
+
function protocolCovers(allowed, wanted) {
|
|
1672
|
+
// `undefined` on the allowed side is EVERY protocol; on the wanted side it
|
|
1673
|
+
// is also every protocol, which only an every-protocol allowance covers.
|
|
1674
|
+
return allowed === undefined || allowed === wanted;
|
|
1675
|
+
}
|
|
1676
|
+
function portRangeCovers(allowed, wanted) {
|
|
1677
|
+
if (!protocolCovers(allowed.protocol, wanted.protocol))
|
|
1678
|
+
return false;
|
|
1679
|
+
if (allowed.start === undefined)
|
|
1680
|
+
return true;
|
|
1681
|
+
if (wanted.start === undefined)
|
|
1682
|
+
return false;
|
|
1683
|
+
const allowedEnd = allowed.end ?? allowed.start;
|
|
1684
|
+
const wantedEnd = wanted.end ?? wanted.start;
|
|
1685
|
+
return wanted.start >= allowed.start && wantedEnd <= allowedEnd;
|
|
1686
|
+
}
|
|
1687
|
+
function portsCover(allowed, wanted) {
|
|
1688
|
+
if (allowed === 'all')
|
|
1689
|
+
return true;
|
|
1690
|
+
return allowed.some((range) => portRangeCovers(range, wanted));
|
|
1691
|
+
}
|
|
1692
|
+
/** Every constraint `outer` places, `inner` places too — so `inner` selects a subset. */
|
|
1693
|
+
function selectorIsNarrower(outer, inner) {
|
|
1694
|
+
if (outer === undefined)
|
|
1695
|
+
return true;
|
|
1696
|
+
if (!isRecord(outer))
|
|
1697
|
+
return false;
|
|
1698
|
+
const outerLabels = outer.matchLabels;
|
|
1699
|
+
const outerExpressions = readList(outer.matchExpressions);
|
|
1700
|
+
if (outerExpressions === 'unreadable')
|
|
1701
|
+
return false;
|
|
1702
|
+
if (outerLabels === undefined && (outerExpressions ?? []).length === 0) {
|
|
1703
|
+
// An EMPTY selector is "everything of this kind", which every selector
|
|
1704
|
+
// of that kind is inside.
|
|
1705
|
+
return true;
|
|
1706
|
+
}
|
|
1707
|
+
if (inner === undefined || !isRecord(inner))
|
|
1708
|
+
return false;
|
|
1709
|
+
if (outerLabels !== undefined) {
|
|
1710
|
+
if (!isRecord(outerLabels))
|
|
1711
|
+
return false;
|
|
1712
|
+
const innerLabels = inner.matchLabels;
|
|
1713
|
+
if (!isRecord(innerLabels))
|
|
1714
|
+
return false;
|
|
1715
|
+
for (const [key, value] of Object.entries(outerLabels)) {
|
|
1716
|
+
if (!Object.hasOwn(innerLabels, key) || innerLabels[key] !== value)
|
|
1717
|
+
return false;
|
|
1718
|
+
}
|
|
1719
|
+
}
|
|
1720
|
+
const innerExpressions = readList(inner.matchExpressions);
|
|
1721
|
+
if (innerExpressions === 'unreadable')
|
|
1722
|
+
return false;
|
|
1723
|
+
for (const expression of outerExpressions ?? []) {
|
|
1724
|
+
if (!(innerExpressions ?? []).some((candidate) => isDeepStrictEqual(candidate, expression))) {
|
|
1725
|
+
return false;
|
|
1726
|
+
}
|
|
1727
|
+
}
|
|
1728
|
+
return true;
|
|
1729
|
+
}
|
|
1730
|
+
/** Is every address `wanted` admits also admitted by `allowed`. */
|
|
1731
|
+
function peerIsWithin(allowed, wanted) {
|
|
1732
|
+
if (allowed.kind === 'everything')
|
|
1733
|
+
return true;
|
|
1734
|
+
if (wanted.kind === 'everything')
|
|
1735
|
+
return false;
|
|
1736
|
+
if (allowed.kind === 'cidr') {
|
|
1737
|
+
if (wanted.kind !== 'cidr')
|
|
1738
|
+
return false;
|
|
1739
|
+
if (!cidrContains(allowed.cidr, wanted.cidr))
|
|
1740
|
+
return false;
|
|
1741
|
+
// Every hole the allowance carves out of its block has to be a hole in
|
|
1742
|
+
// this peer too, or this peer reaches an address the translation does
|
|
1743
|
+
// not allow. Coverage by a UNION of the peer's own `except` entries is
|
|
1744
|
+
// not attempted: one entry has to contain it.
|
|
1745
|
+
for (const hole of allowed.except) {
|
|
1746
|
+
if (!cidrsOverlap(hole, wanted.cidr))
|
|
1747
|
+
continue;
|
|
1748
|
+
if (!wanted.except.some((own) => cidrContains(own, hole)))
|
|
1749
|
+
return false;
|
|
1750
|
+
}
|
|
1751
|
+
return true;
|
|
1752
|
+
}
|
|
1753
|
+
if (wanted.kind !== 'selector')
|
|
1754
|
+
return false;
|
|
1755
|
+
// A namespaceSelector the allowance omits means "this namespace"; a peer
|
|
1756
|
+
// that names namespaces reaches further than that.
|
|
1757
|
+
if (allowed.namespaceSelector === undefined && wanted.namespaceSelector !== undefined) {
|
|
1758
|
+
return false;
|
|
1759
|
+
}
|
|
1760
|
+
return (selectorIsNarrower(allowed.namespaceSelector, wanted.namespaceSelector) &&
|
|
1761
|
+
selectorIsNarrower(allowed.podSelector, wanted.podSelector));
|
|
1762
|
+
}
|
|
1763
|
+
function destinationIsAllowed(allowance, peer, ports) {
|
|
1764
|
+
const wanted = ports === 'all' ? [{}] : ports;
|
|
1765
|
+
return wanted.every((range) => allowance.destinations.some((destination) => peerIsWithin(destination.peer, peer) && portsCover(destination.ports, range)));
|
|
1766
|
+
}
|
|
1767
|
+
/** Is every port `wanted` reaches on `host` also allowed by some `fqdns` entry naming it. */
|
|
1768
|
+
function fqdnIsAllowed(allowance, host, ports) {
|
|
1769
|
+
const wanted = ports === 'all' ? [{}] : ports;
|
|
1770
|
+
return wanted.every((range) => allowance.fqdns.some((entry) => entry.host === host && portsCover(entry.ports, range)));
|
|
1771
|
+
}
|
|
1772
|
+
/** Does `ports` (as a CANDIDATE rule's own port list) reach TCP or UDP 53 at all. */
|
|
1773
|
+
function coversDnsPort(ports) {
|
|
1774
|
+
if (ports === 'all')
|
|
1775
|
+
return true;
|
|
1776
|
+
return ports.some((range) => {
|
|
1777
|
+
if (range.protocol !== undefined && range.protocol !== 'UDP' && range.protocol !== 'TCP') {
|
|
1778
|
+
return false;
|
|
1779
|
+
}
|
|
1780
|
+
if (range.start === undefined)
|
|
1781
|
+
return true;
|
|
1782
|
+
return range.start <= 53 && (range.end ?? range.start) >= 53;
|
|
1783
|
+
});
|
|
1784
|
+
}
|
|
1785
|
+
/**
|
|
1786
|
+
* Does a candidate rule's `peer`+`ports` reach the cluster resolver on the DNS
|
|
1787
|
+
* port at all — the precondition for the DNS-widening check both
|
|
1788
|
+
* {@link coreEgressRuleVerdict} and {@link ciliumEgressRuleVerdict} apply
|
|
1789
|
+
* before falling back to the ordinary peer/port `destinationIsAllowed` check.
|
|
1790
|
+
*/
|
|
1791
|
+
function reachesResolverAtDnsPort(peer, ports) {
|
|
1792
|
+
return peerIsWithin(peer, RESOLVER_PEER) && coversDnsPort(ports);
|
|
1793
|
+
}
|
|
1794
|
+
function describePeer(peer) {
|
|
1795
|
+
if (peer.kind === 'everything')
|
|
1796
|
+
return 'every destination';
|
|
1797
|
+
return peer.text;
|
|
1798
|
+
}
|
|
1799
|
+
function describePorts(ports) {
|
|
1800
|
+
if (ports === 'all')
|
|
1801
|
+
return 'every port';
|
|
1802
|
+
return ports
|
|
1803
|
+
.map((range) => range.start === undefined
|
|
1804
|
+
? `every ${range.protocol ?? ''} port`.trim()
|
|
1805
|
+
: `${range.protocol ?? 'any'} ${range.start}${range.end !== undefined ? `-${range.end}` : ''}`)
|
|
1806
|
+
.join(', ');
|
|
1807
|
+
}
|
|
1808
|
+
function coreEgressRuleVerdict(rule, allowance) {
|
|
1809
|
+
// Under a translation that permits nothing, the rule's own shape does not
|
|
1810
|
+
// matter and is not read: ANY egress rule on a policy selecting this pod
|
|
1811
|
+
// lets something out.
|
|
1812
|
+
if (allowance.permitsNothing) {
|
|
1813
|
+
return {
|
|
1814
|
+
beyond: true,
|
|
1815
|
+
detail: 'an egress rule, where the configured translation permits no egress at all',
|
|
1816
|
+
};
|
|
1817
|
+
}
|
|
1818
|
+
if (!isRecord(rule))
|
|
1819
|
+
return {
|
|
1820
|
+
beyond: 'unknown',
|
|
1821
|
+
detail: 'an egress rule that is not an object',
|
|
1822
|
+
};
|
|
1823
|
+
const ports = readPortRanges(rule.ports, 'TCP');
|
|
1824
|
+
if (ports === 'unreadable') {
|
|
1825
|
+
return {
|
|
1826
|
+
beyond: 'unknown',
|
|
1827
|
+
detail: 'a ports entry this check cannot read',
|
|
1828
|
+
};
|
|
1829
|
+
}
|
|
1830
|
+
const to = readList(rule.to);
|
|
1831
|
+
if (to === 'unreadable') {
|
|
1832
|
+
return { beyond: 'unknown', detail: "a 'to' that is not a list of peers" };
|
|
1833
|
+
}
|
|
1834
|
+
if (to === undefined || to.length === 0) {
|
|
1835
|
+
// No `to` on an egress rule means EVERY destination.
|
|
1836
|
+
return allowance.permitsEverything
|
|
1837
|
+
? { beyond: false }
|
|
1838
|
+
: {
|
|
1839
|
+
beyond: true,
|
|
1840
|
+
detail: `no 'to' peers, so every destination is reachable on ${describePorts(ports)}`,
|
|
1841
|
+
};
|
|
1842
|
+
}
|
|
1843
|
+
for (const peer of to) {
|
|
1844
|
+
const read = readCorePeer(peer);
|
|
1845
|
+
if (read === undefined) {
|
|
1846
|
+
return {
|
|
1847
|
+
beyond: 'unknown',
|
|
1848
|
+
detail: `a 'to' peer this check cannot read (${JSON.stringify(peer)})`,
|
|
1849
|
+
};
|
|
1850
|
+
}
|
|
1851
|
+
// A plain `NetworkPolicy` has no L7 concept, so it cannot express the DNS
|
|
1852
|
+
// restriction `ciliumNarrowing.dnsNames` narrows to — a core rule
|
|
1853
|
+
// reaching the resolver on the DNS port always resolves every name, and
|
|
1854
|
+
// that is wider than a narrowed translation whatever its own peer/port
|
|
1855
|
+
// shape says. This has to be decided BEFORE `destinationIsAllowed`
|
|
1856
|
+
// below: that check only reasons about reachability, and our own
|
|
1857
|
+
// translation's `destinations` entry for the resolver is peer/port-only
|
|
1858
|
+
// too, so a plain kube-dns rule would otherwise read as `within` a
|
|
1859
|
+
// translation it actually resolves every name for.
|
|
1860
|
+
if (allowance.dnsNarrowedTo !== 'all' && reachesResolverAtDnsPort(read, ports)) {
|
|
1861
|
+
return {
|
|
1862
|
+
beyond: true,
|
|
1863
|
+
detail: `a 'to' peer ${describePeer(read)} on ${describePorts(ports)} reaching the cluster resolver's DNS port with no DNS-name restriction — a plain NetworkPolicy cannot narrow lookups the way the configured translation's ciliumNarrowing.dnsNames does, so this rule resolves every name the narrowed policy does not`,
|
|
1864
|
+
};
|
|
1865
|
+
}
|
|
1866
|
+
if (!destinationIsAllowed(allowance, read, ports)) {
|
|
1867
|
+
const wideOpen = corePeerIsWideOpen(peer) === true;
|
|
1868
|
+
return {
|
|
1869
|
+
beyond: true,
|
|
1870
|
+
detail: `${wideOpen ? 'a wide-open ' : 'a '}'to' peer ${describePeer(read)} on ${describePorts(ports)}, which a '${allowance.policyKind}' translation does not allow`,
|
|
1871
|
+
};
|
|
1872
|
+
}
|
|
1873
|
+
}
|
|
1874
|
+
return { beyond: false };
|
|
1875
|
+
}
|
|
1876
|
+
/** Every `to…` field the Cilium CRD declares. A rule naming none of them is port-only. */
|
|
1877
|
+
const CILIUM_DESTINATION_FIELDS = [
|
|
1878
|
+
'toEndpoints',
|
|
1879
|
+
'toEntities',
|
|
1880
|
+
'toCIDR',
|
|
1881
|
+
'toCIDRSet',
|
|
1882
|
+
'toFQDNs',
|
|
1883
|
+
'toServices',
|
|
1884
|
+
'toGroups',
|
|
1885
|
+
'toNodes',
|
|
1886
|
+
];
|
|
1887
|
+
/**
|
|
1888
|
+
* The label set a Cilium endpoint selector names, read back as the core
|
|
1889
|
+
* `namespaceSelector`/`podSelector` pair it is equivalent to, so the one
|
|
1890
|
+
* `peerIsWithin` serves both resource kinds. `undefined` when the selector
|
|
1891
|
+
* uses anything this reading cannot map — a label source that is not a pod
|
|
1892
|
+
* label, or a match expression.
|
|
1893
|
+
*/
|
|
1894
|
+
function ciliumEndpointPeer(selector) {
|
|
1895
|
+
if (!isRecord(selector))
|
|
1896
|
+
return undefined;
|
|
1897
|
+
if (selector.matchExpressions !== undefined)
|
|
1898
|
+
return undefined;
|
|
1899
|
+
const matchLabels = selector.matchLabels;
|
|
1900
|
+
if (!isRecord(matchLabels))
|
|
1901
|
+
return undefined;
|
|
1902
|
+
const podLabels = {};
|
|
1903
|
+
let namespace;
|
|
1904
|
+
for (const [rawKey, value] of Object.entries(matchLabels)) {
|
|
1905
|
+
if (typeof value !== 'string')
|
|
1906
|
+
return undefined;
|
|
1907
|
+
const key = ciliumSelectorKey(rawKey);
|
|
1908
|
+
if (key === undefined)
|
|
1909
|
+
return undefined;
|
|
1910
|
+
if (key === 'io.kubernetes.pod.namespace') {
|
|
1911
|
+
namespace = value;
|
|
1912
|
+
continue;
|
|
1913
|
+
}
|
|
1914
|
+
podLabels[key] = value;
|
|
1915
|
+
}
|
|
1916
|
+
return {
|
|
1917
|
+
kind: 'selector',
|
|
1918
|
+
...(namespace !== undefined
|
|
1919
|
+
? {
|
|
1920
|
+
namespaceSelector: {
|
|
1921
|
+
matchLabels: { 'kubernetes.io/metadata.name': namespace },
|
|
1922
|
+
},
|
|
1923
|
+
}
|
|
1924
|
+
: {}),
|
|
1925
|
+
podSelector: { matchLabels: podLabels },
|
|
1926
|
+
text: JSON.stringify(selector),
|
|
1927
|
+
};
|
|
1928
|
+
}
|
|
1929
|
+
function readCiliumRulePorts(rule) {
|
|
1930
|
+
// A Cilium rule carries its ports one level deeper, and a port entry with
|
|
1931
|
+
// no protocol means ANY rather than TCP.
|
|
1932
|
+
const toPorts = readList(rule.toPorts);
|
|
1933
|
+
if (toPorts === 'unreadable') {
|
|
1934
|
+
return { ok: false, detail: 'a toPorts that is not a list' };
|
|
1935
|
+
}
|
|
1936
|
+
const portEntries = [];
|
|
1937
|
+
for (const entry of toPorts ?? []) {
|
|
1938
|
+
if (!isRecord(entry)) {
|
|
1939
|
+
return { ok: false, detail: 'a toPorts entry that is not an object' };
|
|
1940
|
+
}
|
|
1941
|
+
const list = readList(entry.ports);
|
|
1942
|
+
if (list === 'unreadable') {
|
|
1943
|
+
return { ok: false, detail: 'a toPorts ports field that is not a list' };
|
|
1944
|
+
}
|
|
1945
|
+
// An entry with no `ports` at all bounds nothing, so the rule reaches
|
|
1946
|
+
// every port — exactly what an absent `toPorts` means.
|
|
1947
|
+
if (list === undefined || list.length === 0) {
|
|
1948
|
+
portEntries.length = 0;
|
|
1949
|
+
break;
|
|
1950
|
+
}
|
|
1951
|
+
portEntries.push(...list);
|
|
1952
|
+
}
|
|
1953
|
+
const ports = readPortRanges(portEntries, undefined);
|
|
1954
|
+
if (ports === 'unreadable') {
|
|
1955
|
+
return { ok: false, detail: 'a toPorts entry this check cannot read' };
|
|
1956
|
+
}
|
|
1957
|
+
return { ok: true, ports };
|
|
1958
|
+
}
|
|
1959
|
+
/**
|
|
1960
|
+
* A Cilium rule's `toPorts[].rules.dns`, read into the exact-name list it
|
|
1961
|
+
* restricts lookups to, or `'all'` when the rule does not restrict DNS at
|
|
1962
|
+
* all. Shared by {@link egressAllowance} (reading OUR OWN narrowed
|
|
1963
|
+
* DNS-visibility rule into {@link EgressAllowance.dnsNarrowedTo}) and
|
|
1964
|
+
* {@link ciliumEgressRuleVerdict} (deciding whether a CANDIDATE rule's own
|
|
1965
|
+
* restriction is narrow enough to not widen it) — one reading of "what names
|
|
1966
|
+
* does this Cilium rule let resolve", not two that could disagree.
|
|
1967
|
+
*
|
|
1968
|
+
* A `toPorts` entry with no `rules` at all, or a `rules.dns` entry carrying
|
|
1969
|
+
* `matchPattern` rather than `matchName`, both read as `'all'`: an absent L7
|
|
1970
|
+
* restriction resolves every name by definition, and this check does not
|
|
1971
|
+
* attempt to decide whether some wildcard pattern is a subset of an exact
|
|
1972
|
+
* name list — `'all'` is the conservative (never under-counts a widening)
|
|
1973
|
+
* answer for a shape it cannot reduce further.
|
|
1974
|
+
*/
|
|
1975
|
+
function readCiliumRuleDnsNames(rule) {
|
|
1976
|
+
const toPorts = readList(rule.toPorts);
|
|
1977
|
+
if (toPorts === 'unreadable')
|
|
1978
|
+
return { ok: false };
|
|
1979
|
+
const names = [];
|
|
1980
|
+
for (const entry of toPorts ?? []) {
|
|
1981
|
+
if (!isRecord(entry))
|
|
1982
|
+
return { ok: false };
|
|
1983
|
+
if (entry.rules === undefined)
|
|
1984
|
+
return { ok: true, names: 'all' };
|
|
1985
|
+
if (!isRecord(entry.rules))
|
|
1986
|
+
return { ok: false };
|
|
1987
|
+
const dns = readList(entry.rules.dns);
|
|
1988
|
+
if (dns === 'unreadable')
|
|
1989
|
+
return { ok: false };
|
|
1990
|
+
if (dns === undefined)
|
|
1991
|
+
return { ok: true, names: 'all' };
|
|
1992
|
+
for (const item of dns) {
|
|
1993
|
+
if (!isRecord(item))
|
|
1994
|
+
return { ok: false };
|
|
1995
|
+
if (typeof item.matchName === 'string') {
|
|
1996
|
+
names.push(item.matchName);
|
|
1997
|
+
continue;
|
|
1998
|
+
}
|
|
1999
|
+
// `matchPattern` (Cilium's glob syntax) or anything else this reading
|
|
2000
|
+
// does not recognise — both are read as unrestricted rather than
|
|
2001
|
+
// guessed at, per the doc comment above.
|
|
2002
|
+
return { ok: true, names: 'all' };
|
|
2003
|
+
}
|
|
2004
|
+
}
|
|
2005
|
+
return { ok: true, names };
|
|
2006
|
+
}
|
|
2007
|
+
function ciliumEgressRuleVerdict(rule, allowance) {
|
|
2008
|
+
if (allowance.permitsNothing) {
|
|
2009
|
+
return {
|
|
2010
|
+
beyond: true,
|
|
2011
|
+
detail: 'an egress rule, where the configured translation permits no egress at all',
|
|
2012
|
+
};
|
|
2013
|
+
}
|
|
2014
|
+
if (!isRecord(rule))
|
|
2015
|
+
return {
|
|
2016
|
+
beyond: 'unknown',
|
|
2017
|
+
detail: 'an egress rule that is not an object',
|
|
2018
|
+
};
|
|
2019
|
+
const portsRead = readCiliumRulePorts(rule);
|
|
2020
|
+
if (!portsRead.ok) {
|
|
2021
|
+
return { beyond: 'unknown', detail: portsRead.detail };
|
|
2022
|
+
}
|
|
2023
|
+
const ports = portsRead.ports;
|
|
2024
|
+
const fields = new Map();
|
|
2025
|
+
for (const field of CILIUM_DESTINATION_FIELDS) {
|
|
2026
|
+
const list = readList(rule[field]);
|
|
2027
|
+
if (list === 'unreadable') {
|
|
2028
|
+
return {
|
|
2029
|
+
beyond: 'unknown',
|
|
2030
|
+
detail: `a ${field} that is not a list of peers`,
|
|
2031
|
+
};
|
|
2032
|
+
}
|
|
2033
|
+
if (list !== undefined && list.length > 0)
|
|
2034
|
+
fields.set(field, list);
|
|
2035
|
+
}
|
|
2036
|
+
if (fields.size === 0) {
|
|
2037
|
+
return allowance.permitsEverything
|
|
2038
|
+
? { beyond: false }
|
|
2039
|
+
: {
|
|
2040
|
+
beyond: true,
|
|
2041
|
+
detail: `a port-only egress rule, so every destination is reachable on ${describePorts(ports)}`,
|
|
2042
|
+
};
|
|
2043
|
+
}
|
|
2044
|
+
for (const entity of fields.get('toEntities') ?? []) {
|
|
2045
|
+
if (typeof entity !== 'string') {
|
|
2046
|
+
return {
|
|
2047
|
+
beyond: 'unknown',
|
|
2048
|
+
detail: 'a toEntities entry that is not an entity name',
|
|
2049
|
+
};
|
|
2050
|
+
}
|
|
2051
|
+
if (!allowance.permitsEverything) {
|
|
2052
|
+
return {
|
|
2053
|
+
beyond: true,
|
|
2054
|
+
detail: `toEntities '${entity}', which a '${allowance.policyKind}' translation does not allow`,
|
|
2055
|
+
};
|
|
2056
|
+
}
|
|
2057
|
+
}
|
|
2058
|
+
for (const field of ['toCIDR', 'toCIDRSet']) {
|
|
2059
|
+
for (const entry of fields.get(field) ?? []) {
|
|
2060
|
+
const peer = field === 'toCIDR'
|
|
2061
|
+
? readCorePeer({ ipBlock: { cidr: entry } })
|
|
2062
|
+
: isRecord(entry)
|
|
2063
|
+
? readCorePeer({
|
|
2064
|
+
ipBlock: { cidr: entry.cidr, except: entry.except },
|
|
2065
|
+
})
|
|
2066
|
+
: undefined;
|
|
2067
|
+
if (peer === undefined) {
|
|
2068
|
+
return {
|
|
2069
|
+
beyond: 'unknown',
|
|
2070
|
+
detail: `a ${field} entry this check cannot read`,
|
|
2071
|
+
};
|
|
2072
|
+
}
|
|
2073
|
+
if (!destinationIsAllowed(allowance, peer, ports)) {
|
|
2074
|
+
return {
|
|
2075
|
+
beyond: true,
|
|
2076
|
+
detail: `${field} ${describePeer(peer)} on ${describePorts(ports)}, which a '${allowance.policyKind}' translation does not allow`,
|
|
2077
|
+
};
|
|
2078
|
+
}
|
|
2079
|
+
}
|
|
2080
|
+
}
|
|
2081
|
+
for (const entry of fields.get('toFQDNs') ?? []) {
|
|
2082
|
+
if (!isRecord(entry)) {
|
|
2083
|
+
return {
|
|
2084
|
+
beyond: 'unknown',
|
|
2085
|
+
detail: 'a toFQDNs entry that is not an object',
|
|
2086
|
+
};
|
|
2087
|
+
}
|
|
2088
|
+
const matchName = entry.matchName;
|
|
2089
|
+
if (typeof matchName !== 'string' || !fqdnIsAllowed(allowance, matchName, ports)) {
|
|
2090
|
+
return {
|
|
2091
|
+
beyond: true,
|
|
2092
|
+
detail: `toFQDNs ${JSON.stringify(entry)} on ${describePorts(ports)}, which a '${allowance.policyKind}' translation does not allow`,
|
|
2093
|
+
};
|
|
2094
|
+
}
|
|
2095
|
+
}
|
|
2096
|
+
for (const selector of fields.get('toEndpoints') ?? []) {
|
|
2097
|
+
const peer = ciliumEndpointPeer(selector);
|
|
2098
|
+
if (peer === undefined) {
|
|
2099
|
+
return {
|
|
2100
|
+
beyond: 'unknown',
|
|
2101
|
+
detail: 'a toEndpoints entry this check cannot read as a selector',
|
|
2102
|
+
};
|
|
2103
|
+
}
|
|
2104
|
+
// See the matching comment in `coreEgressRuleVerdict`: reachability alone
|
|
2105
|
+
// cannot tell a plain kube-dns rule from a narrowed one, so this has to
|
|
2106
|
+
// run before `destinationIsAllowed` below. Unlike a core rule, a Cilium
|
|
2107
|
+
// one CAN narrow DNS on its own `toPorts.rules.dns` — read it and accept
|
|
2108
|
+
// the rule only when what it names is a subset of what our own
|
|
2109
|
+
// translation narrows to.
|
|
2110
|
+
if (allowance.dnsNarrowedTo !== 'all' && reachesResolverAtDnsPort(peer, ports)) {
|
|
2111
|
+
const narrowedTo = allowance.dnsNarrowedTo;
|
|
2112
|
+
const candidate = readCiliumRuleDnsNames(rule);
|
|
2113
|
+
const isSubset = candidate.ok &&
|
|
2114
|
+
candidate.names !== 'all' &&
|
|
2115
|
+
candidate.names.every((n) => narrowedTo.includes(n));
|
|
2116
|
+
if (!isSubset) {
|
|
2117
|
+
return {
|
|
2118
|
+
beyond: true,
|
|
2119
|
+
detail: `toEndpoints ${describePeer(peer)} on ${describePorts(ports)} reaching the cluster resolver's DNS port with ${candidate.ok && candidate.names !== 'all' ? 'a rules.dns list this check cannot confirm is a subset of' : 'no rules.dns restriction at least as narrow as'} the configured translation's ciliumNarrowing.dnsNames`,
|
|
2120
|
+
};
|
|
2121
|
+
}
|
|
2122
|
+
continue;
|
|
2123
|
+
}
|
|
2124
|
+
if (!destinationIsAllowed(allowance, peer, ports)) {
|
|
2125
|
+
return {
|
|
2126
|
+
beyond: true,
|
|
2127
|
+
detail: `toEndpoints ${describePeer(peer)} on ${describePorts(ports)}, which a '${allowance.policyKind}' translation does not allow`,
|
|
2128
|
+
};
|
|
2129
|
+
}
|
|
2130
|
+
}
|
|
2131
|
+
for (const field of ['toServices', 'toGroups', 'toNodes']) {
|
|
2132
|
+
if (fields.has(field)) {
|
|
2133
|
+
return {
|
|
2134
|
+
beyond: 'unknown',
|
|
2135
|
+
detail: `a ${field} rule, whose destinations this check cannot enumerate`,
|
|
2136
|
+
};
|
|
2137
|
+
}
|
|
2138
|
+
}
|
|
2139
|
+
return { beyond: false };
|
|
2140
|
+
}
|
|
2141
|
+
function unreadableEgressPolicy(kind, name, detail) {
|
|
2142
|
+
return {
|
|
2143
|
+
kind,
|
|
2144
|
+
name,
|
|
2145
|
+
selects: 'unknown',
|
|
2146
|
+
enforcesEgress: false,
|
|
2147
|
+
rules: [],
|
|
2148
|
+
unreadable: detail,
|
|
2149
|
+
};
|
|
2150
|
+
}
|
|
2151
|
+
/** Every core `NetworkPolicy` in the list, reduced to {@link EgressPolicyDocument}. */
|
|
2152
|
+
export function readCoreEgressPolicies(items, target, allowance) {
|
|
2153
|
+
return items.map((item, index) => {
|
|
2154
|
+
const name = policyName(item, index);
|
|
2155
|
+
if (!isRecord(item)) {
|
|
2156
|
+
return unreadableEgressPolicy('NetworkPolicy', name, 'a list entry that is not a policy object');
|
|
2157
|
+
}
|
|
2158
|
+
const spec = item.spec;
|
|
2159
|
+
if (!isRecord(spec)) {
|
|
2160
|
+
return unreadableEgressPolicy('NetworkPolicy', name, 'a spec that is not an object');
|
|
2161
|
+
}
|
|
2162
|
+
const policyTypes = readList(spec.policyTypes);
|
|
2163
|
+
if (policyTypes === 'unreadable') {
|
|
2164
|
+
return unreadableEgressPolicy('NetworkPolicy', name, 'a spec.policyTypes that is not a list');
|
|
2165
|
+
}
|
|
2166
|
+
const rules = readList(spec.egress);
|
|
2167
|
+
if (rules === 'unreadable') {
|
|
2168
|
+
return unreadableEgressPolicy('NetworkPolicy', name, 'a spec.egress that is not a list of rules');
|
|
2169
|
+
}
|
|
2170
|
+
// An ABSENT `policyTypes` is defaulted by the API server from the blocks
|
|
2171
|
+
// the object carries: Egress appears exactly when `spec.egress` does.
|
|
2172
|
+
// This is the opposite of the ingress default, where Egress's absence is
|
|
2173
|
+
// the thing that has to be spelled out.
|
|
2174
|
+
const enforcesEgress = policyTypes === undefined ? rules !== undefined : policyTypes.includes('Egress');
|
|
2175
|
+
return {
|
|
2176
|
+
kind: 'NetworkPolicy',
|
|
2177
|
+
name,
|
|
2178
|
+
selects: matchesLabelSelector(spec.podSelector, target.podLabels),
|
|
2179
|
+
enforcesEgress,
|
|
2180
|
+
// An `egress` block under a `policyTypes` that leaves Egress out is
|
|
2181
|
+
// ignored by the API server itself: it neither bounds nor widens.
|
|
2182
|
+
rules: enforcesEgress
|
|
2183
|
+
? (rules ?? []).map((rule) => coreEgressRuleVerdict(rule, allowance))
|
|
2184
|
+
: [],
|
|
2185
|
+
};
|
|
2186
|
+
});
|
|
2187
|
+
}
|
|
2188
|
+
/** Every `CiliumNetworkPolicy` in the list, reduced the same way. */
|
|
2189
|
+
export function readCiliumEgressPolicies(items, target, allowance) {
|
|
2190
|
+
const identity = ciliumIdentityLabels(target.podLabels, target.namespace);
|
|
2191
|
+
const documents = [];
|
|
2192
|
+
for (const [index, item] of items.entries()) {
|
|
2193
|
+
const name = policyName(item, index);
|
|
2194
|
+
if (!isRecord(item)) {
|
|
2195
|
+
documents.push(unreadableEgressPolicy('CiliumNetworkPolicy', name, 'a list entry that is not a policy object'));
|
|
2196
|
+
continue;
|
|
2197
|
+
}
|
|
2198
|
+
// The CRD carries EITHER one `spec` or a `specs` list, and a rule in
|
|
2199
|
+
// either enforces. Reading only `spec` would miss a whole policy.
|
|
2200
|
+
const specs = [];
|
|
2201
|
+
if (item.spec !== undefined && item.spec !== null)
|
|
2202
|
+
specs.push(item.spec);
|
|
2203
|
+
const more = readList(item.specs);
|
|
2204
|
+
if (more === 'unreadable') {
|
|
2205
|
+
documents.push(unreadableEgressPolicy('CiliumNetworkPolicy', name, 'a specs that is not a list of rule specs'));
|
|
2206
|
+
continue;
|
|
2207
|
+
}
|
|
2208
|
+
for (const spec of more ?? [])
|
|
2209
|
+
specs.push(spec);
|
|
2210
|
+
if (specs.length === 0) {
|
|
2211
|
+
documents.push(unreadableEgressPolicy('CiliumNetworkPolicy', name, 'neither a spec nor a specs list'));
|
|
2212
|
+
continue;
|
|
2213
|
+
}
|
|
2214
|
+
for (const spec of specs) {
|
|
2215
|
+
const document = readCiliumEgressRuleSpec(spec, name, identity, allowance);
|
|
2216
|
+
if (document !== undefined)
|
|
2217
|
+
documents.push(document);
|
|
2218
|
+
}
|
|
2219
|
+
}
|
|
2220
|
+
return documents;
|
|
2221
|
+
}
|
|
2222
|
+
/** One `spec`/`specs` entry. `undefined` when it is node-scoped — see below. */
|
|
2223
|
+
function readCiliumEgressRuleSpec(spec, name, identity, allowance) {
|
|
2224
|
+
const unreadable = (detail) => unreadableEgressPolicy('CiliumNetworkPolicy', name, detail);
|
|
2225
|
+
if (!isRecord(spec))
|
|
2226
|
+
return unreadable('a rule spec that is not an object');
|
|
2227
|
+
// A node-scoped rule selects nodes, never pods.
|
|
2228
|
+
if (spec.nodeSelector !== undefined)
|
|
2229
|
+
return undefined;
|
|
2230
|
+
const rules = readList(spec.egress);
|
|
2231
|
+
if (rules === 'unreadable')
|
|
2232
|
+
return unreadable('a spec.egress that is not a list of rules');
|
|
2233
|
+
const egressDeny = readList(spec.egressDeny);
|
|
2234
|
+
if (egressDeny === 'unreadable')
|
|
2235
|
+
return unreadable('a spec.egressDeny that is not a list of rules');
|
|
2236
|
+
let enforcesEgress = rules !== undefined || egressDeny !== undefined;
|
|
2237
|
+
const enableDefaultDeny = spec.enableDefaultDeny;
|
|
2238
|
+
if (enableDefaultDeny !== undefined) {
|
|
2239
|
+
if (!isRecord(enableDefaultDeny))
|
|
2240
|
+
return unreadable('an enableDefaultDeny that is not an object');
|
|
2241
|
+
const forEgress = enableDefaultDeny.egress;
|
|
2242
|
+
if (forEgress !== undefined && typeof forEgress !== 'boolean') {
|
|
2243
|
+
return unreadable('an enableDefaultDeny.egress that is not a boolean');
|
|
2244
|
+
}
|
|
2245
|
+
if (forEgress === false)
|
|
2246
|
+
enforcesEgress = false;
|
|
2247
|
+
}
|
|
2248
|
+
return {
|
|
2249
|
+
kind: 'CiliumNetworkPolicy',
|
|
2250
|
+
name,
|
|
2251
|
+
selects: matchesLabelSelector(spec.endpointSelector, identity, ciliumSelectorKey),
|
|
2252
|
+
enforcesEgress,
|
|
2253
|
+
// `egressDeny` rules are not read: a deny rule can only narrow what
|
|
2254
|
+
// leaves the pod, and this check refuses widening. Allow rules are read
|
|
2255
|
+
// whatever `enableDefaultDeny` says, because what a rule admits it
|
|
2256
|
+
// admits — it simply may not be the policy that default-denies.
|
|
2257
|
+
rules: (rules ?? []).map((rule) => ciliumEgressRuleVerdict(rule, allowance)),
|
|
2258
|
+
};
|
|
2259
|
+
}
|
|
2260
|
+
/**
|
|
2261
|
+
* The union rule, applied. Pure — no I/O, no client, no clock — so every
|
|
2262
|
+
* shape that has to be refused can be asserted one per test.
|
|
2263
|
+
*
|
|
2264
|
+
* Kubernetes UNIONS every policy selecting a pod: traffic leaves if ANY
|
|
2265
|
+
* selecting policy allows it. So one policy allowing more than the
|
|
2266
|
+
* translation is the finding however many narrower ones sit beside it, and a
|
|
2267
|
+
* pod no policy default-denies has no egress boundary at all whatever the
|
|
2268
|
+
* named object says.
|
|
2269
|
+
*/
|
|
2270
|
+
export function decideEgressUnion(documents, allowance) {
|
|
2271
|
+
const examined = [];
|
|
2272
|
+
let enforcing = 0;
|
|
2273
|
+
let widening;
|
|
2274
|
+
let undecided;
|
|
2275
|
+
for (const document of documents) {
|
|
2276
|
+
const base = { kind: document.kind, name: document.name };
|
|
2277
|
+
if (document.unreadable !== undefined) {
|
|
2278
|
+
const entry = {
|
|
2279
|
+
...base,
|
|
2280
|
+
verdict: 'not-evaluable',
|
|
2281
|
+
detail: document.unreadable,
|
|
2282
|
+
};
|
|
2283
|
+
examined.push(entry);
|
|
2284
|
+
undecided ??= entry;
|
|
2285
|
+
continue;
|
|
2286
|
+
}
|
|
2287
|
+
if (document.selects === 'no') {
|
|
2288
|
+
examined.push({ ...base, verdict: 'does-not-select' });
|
|
2289
|
+
continue;
|
|
2290
|
+
}
|
|
2291
|
+
if (document.selects === 'unknown') {
|
|
2292
|
+
const entry = {
|
|
2293
|
+
...base,
|
|
2294
|
+
verdict: 'not-evaluable',
|
|
2295
|
+
detail: 'its selector uses something this check cannot evaluate against pod labels',
|
|
2296
|
+
};
|
|
2297
|
+
examined.push(entry);
|
|
2298
|
+
undecided ??= entry;
|
|
2299
|
+
continue;
|
|
2300
|
+
}
|
|
2301
|
+
const beyondRule = document.rules.find((rule) => rule.beyond === true);
|
|
2302
|
+
if (beyondRule !== undefined) {
|
|
2303
|
+
const entry = {
|
|
2304
|
+
...base,
|
|
2305
|
+
verdict: 'widens-egress',
|
|
2306
|
+
...(beyondRule.detail !== undefined ? { detail: beyondRule.detail } : {}),
|
|
2307
|
+
};
|
|
2308
|
+
examined.push(entry);
|
|
2309
|
+
widening ??= entry;
|
|
2310
|
+
continue;
|
|
2311
|
+
}
|
|
2312
|
+
const unknownRule = document.rules.find((rule) => rule.beyond === 'unknown');
|
|
2313
|
+
if (unknownRule !== undefined) {
|
|
2314
|
+
const entry = {
|
|
2315
|
+
...base,
|
|
2316
|
+
verdict: 'not-evaluable',
|
|
2317
|
+
...(unknownRule.detail !== undefined ? { detail: unknownRule.detail } : {}),
|
|
2318
|
+
};
|
|
2319
|
+
examined.push(entry);
|
|
2320
|
+
undecided ??= entry;
|
|
2321
|
+
continue;
|
|
2322
|
+
}
|
|
2323
|
+
if (!document.enforcesEgress) {
|
|
2324
|
+
examined.push({
|
|
2325
|
+
...base,
|
|
2326
|
+
verdict: 'not-egress-scoped',
|
|
2327
|
+
detail: 'it selects the pod but default-denies nothing on egress',
|
|
2328
|
+
});
|
|
2329
|
+
continue;
|
|
2330
|
+
}
|
|
2331
|
+
examined.push({ ...base, verdict: 'within' });
|
|
2332
|
+
enforcing += 1;
|
|
2333
|
+
}
|
|
2334
|
+
if (widening !== undefined) {
|
|
2335
|
+
return {
|
|
2336
|
+
examined,
|
|
2337
|
+
refusal: {
|
|
2338
|
+
kind: 'policy-widens-egress',
|
|
2339
|
+
summary: `${widening.kind}/${widening.name} selects this pod and allows ${widening.detail ?? 'egress the configured translation does not'}.`,
|
|
2340
|
+
},
|
|
2341
|
+
};
|
|
2342
|
+
}
|
|
2343
|
+
if (undecided !== undefined) {
|
|
2344
|
+
return {
|
|
2345
|
+
examined,
|
|
2346
|
+
refusal: {
|
|
2347
|
+
kind: 'not-evaluable',
|
|
2348
|
+
summary: `${undecided.kind}/${undecided.name} contains ${undecided.detail ?? 'something this check cannot evaluate'}, so what this pod may reach cannot be decided from the cluster's own objects.`,
|
|
2349
|
+
},
|
|
2350
|
+
};
|
|
2351
|
+
}
|
|
2352
|
+
// `allow-all` asks for no boundary, so a pod nothing default-denies is
|
|
2353
|
+
// exactly what it configured; every other kind needs a policy that
|
|
2354
|
+
// actually puts this pod in egress default-deny, or the translation is a
|
|
2355
|
+
// manifest nobody enforces.
|
|
2356
|
+
if (enforcing === 0 && !allowance.permitsEverything) {
|
|
2357
|
+
return {
|
|
2358
|
+
examined,
|
|
2359
|
+
refusal: {
|
|
2360
|
+
kind: 'no-enforcing-policy',
|
|
2361
|
+
summary: `no applied policy puts this pod in egress default-deny, so a '${allowance.policyKind}' translation bounds nothing it sends.`,
|
|
2362
|
+
},
|
|
2363
|
+
};
|
|
2364
|
+
}
|
|
2365
|
+
return { examined };
|
|
2366
|
+
}
|
|
2367
|
+
/**
|
|
2368
|
+
* The named refusal. Distinct from {@link KubernetesEgressPolicyMismatchError}
|
|
2369
|
+
* — which is about the ONE named object drifting from the translation — and
|
|
2370
|
+
* from `KubernetesIngressPolicyError`, which is about the agent port being
|
|
2371
|
+
* reachable. An operator debugging a release that ships all three tells them
|
|
2372
|
+
* apart by class and by the first clause of the message.
|
|
2373
|
+
*
|
|
2374
|
+
* It carries the pod's labels and EVERY policy examined, with a verdict each,
|
|
2375
|
+
* because that list is the operator's whole debugging session: "why does my
|
|
2376
|
+
* policy not count?" is answered by the line saying it did not select these
|
|
2377
|
+
* labels.
|
|
2378
|
+
*/
|
|
2379
|
+
export class KubernetesEgressPolicyUnionError extends Error {
|
|
2380
|
+
refusal;
|
|
2381
|
+
subject;
|
|
2382
|
+
podLabels;
|
|
2383
|
+
policyKind;
|
|
2384
|
+
examined;
|
|
2385
|
+
unread;
|
|
2386
|
+
name = 'KubernetesEgressPolicyUnionError';
|
|
2387
|
+
constructor(refusal, subject, podLabels, policyKind, examined, summary,
|
|
2388
|
+
/** Empty on every decision made from policies that WERE read. */
|
|
2389
|
+
unread = []) {
|
|
2390
|
+
super(`kubernetes: refusing ${subject} — ${summary} config.egress.policy is '${policyKind}' and the pod's labels are ${formatLabels(podLabels)}. ${formatExaminedEgress(examined, unread)} Kubernetes UNIONS every policy selecting a pod, so what leaves this pod is everything ANY of them allows — a second policy widens egress however exactly the named object matches. ${formatEgressRemedy(refusal, unread)}`);
|
|
2391
|
+
this.refusal = refusal;
|
|
2392
|
+
this.subject = subject;
|
|
2393
|
+
this.podLabels = podLabels;
|
|
2394
|
+
this.policyKind = policyKind;
|
|
2395
|
+
this.examined = examined;
|
|
2396
|
+
this.unread = unread;
|
|
2397
|
+
}
|
|
2398
|
+
}
|
|
2399
|
+
function formatExaminedEgress(examined, unread) {
|
|
2400
|
+
const lines = examined.map((entry) => {
|
|
2401
|
+
const detail = entry.detail !== undefined ? ` (${entry.detail})` : '';
|
|
2402
|
+
return `${entry.kind}/${entry.name}: ${entry.verdict}${detail}`;
|
|
2403
|
+
});
|
|
2404
|
+
const read = lines.length === 0
|
|
2405
|
+
? 'No policy was read from the collections this check could enumerate.'
|
|
2406
|
+
: `Policies examined: ${lines.join('; ')}.`;
|
|
2407
|
+
if (unread.length === 0)
|
|
2408
|
+
return read;
|
|
2409
|
+
const missing = unread
|
|
2410
|
+
.map((source) => `${source.resource} at ${source.path} (${source.why}: ${source.reason})`)
|
|
2411
|
+
.join('; ');
|
|
2412
|
+
return `${read} NOT read, so nothing below is a statement about it: ${missing}.`;
|
|
2413
|
+
}
|
|
2414
|
+
function formatEgressRemedy(refusal, unread) {
|
|
2415
|
+
if (refusal === 'not-evaluable') {
|
|
2416
|
+
const forbidden = unread.some((source) => source.why === 'forbidden');
|
|
2417
|
+
return `${forbidden ? "Grant this backend's ServiceAccount 'list' on that resource, " : 'Fix or remove the policy named above, '}or set egress.verify: 'named-object-only' to check only the one named object, as every release before this one did.`;
|
|
2418
|
+
}
|
|
2419
|
+
if (refusal === 'no-enforcing-policy') {
|
|
2420
|
+
return "Apply a NetworkPolicy whose podSelector matches the labels above and whose policyTypes includes 'Egress' (packages/sandbox/k8s/manifests/networkpolicy.yaml is this repo's baseline), or set egress.verify: 'named-object-only' if the boundary lives somewhere a namespaced Role cannot read.";
|
|
2421
|
+
}
|
|
2422
|
+
return "Narrow or delete the policy named above so that nothing selecting these pods allows more than config.egress does, or set egress.verify: 'named-object-only' to go back to checking only the named object.";
|
|
2423
|
+
}
|
|
2424
|
+
// ---------------------------------------------------------------------------
|
|
2425
|
+
// The I/O half
|
|
2426
|
+
// ---------------------------------------------------------------------------
|
|
2427
|
+
/**
|
|
2428
|
+
* Verify-not-trust, widened from one object to the union: list the
|
|
2429
|
+
* namespace's policies, evaluate every one that selects this pod against the
|
|
2430
|
+
* configured translation, and refuse unless nothing lets out more than
|
|
2431
|
+
* `config.egress` says.
|
|
2432
|
+
*
|
|
2433
|
+
* Runs beside the ingress check on every create path — before the POST for a
|
|
2434
|
+
* directly created Sandbox, where the labels are known and a refusal leaves
|
|
2435
|
+
* nothing behind, and after the bind for a claimed one, where the pool's own
|
|
2436
|
+
* template decides the labels and a refusal releases the claim through the
|
|
2437
|
+
* acquire path's cleanup.
|
|
2438
|
+
*/
|
|
2439
|
+
export async function verifyEgressPolicyUnion(client, translated, target, signal) {
|
|
2440
|
+
const allowance = egressAllowance(translated);
|
|
2441
|
+
const documents = [];
|
|
2442
|
+
try {
|
|
2443
|
+
documents.push(...readCoreEgressPolicies(await listPolicies(client, networkPolicyCollectionPath(target.namespace), 'networkpolicies', signal), target, allowance));
|
|
2444
|
+
if (target.engine === 'cilium') {
|
|
2445
|
+
documents.push(...readCiliumEgressPolicies(await listPolicies(client, ciliumNetworkPolicyCollectionPath(target.namespace), 'ciliumnetworkpolicies', signal), target, allowance));
|
|
2446
|
+
}
|
|
2447
|
+
}
|
|
2448
|
+
catch (err) {
|
|
2449
|
+
if (!(err instanceof UnreadPolicyCollection))
|
|
2450
|
+
throw err;
|
|
2451
|
+
throw new KubernetesEgressPolicyUnionError('not-evaluable', target.subject, target.podLabels, allowance.policyKind, decideEgressUnion(documents, allowance).examined, err.summary, [err.source]);
|
|
2452
|
+
}
|
|
2453
|
+
const decision = decideEgressUnion(documents, allowance);
|
|
2454
|
+
if (decision.refusal === undefined)
|
|
2455
|
+
return;
|
|
2456
|
+
throw new KubernetesEgressPolicyUnionError(decision.refusal.kind, target.subject, target.podLabels, allowance.policyKind, decision.examined, decision.refusal.summary);
|
|
313
2457
|
}
|
|
314
2458
|
//# sourceMappingURL=egress-policy.js.map
|