mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-05 21:02:34 +01:00
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work > - Confinement providers protect agent runs with default-deny network policies > - Kubernetes environments currently apply only provider-level, namespace-wide egress allowances > - Tasks that legitimately need GitHub or package registries therefore cannot request narrow access, while network failures do not explain the governing policy or how to request a grant > - This pull request adds issue-scoped egress grants that become workload-owned, run-label-selected policies and carries the effective grant through lease audit metadata > - The benefit is that internet-dependent work can run without enabling broad egress for every concurrent task, and denied requests point operators to the exact grant path ## Linked Issues or Issue Description No public issue exists. Related but distinct: Refs #9944, which adds a provider-wide open-internet posture; this PR keeps provider defaults narrow and adds per-task grants. **Problem / motivation** Kubernetes sandbox egress is configured at the provider/tenant level. A task that needs to clone from GitHub or install from PyPI cannot request those destinations without changing the policy for every run in the tenant namespace. DNS/connectivity failures also surface as generic tool errors with no policy name or remediation path. **Proposed solution** Accept `executionWorkspaceSettings.networkEgress.allowFqdns` and `allowCidrs`, forward the setting through heartbeat environment acquisition, and create a workload-owned NetworkPolicy or CiliumNetworkPolicy selected by `paperclip.io/run-id`. Record the effective grant in lease activity/metadata, expose policy context through `PAPERCLIP_NETWORK_EGRESS_*`, and append the grant path to likely policy-related stderr failures. **Alternatives considered** A provider-wide open-internet switch is broader than required and is already covered by #9944. Mutating the existing namespace policy would leak each task's destinations to other concurrent runs. Standard Kubernetes NetworkPolicy cannot enforce FQDNs exactly, so standard mode uses the existing hardened public-IPv4 TCP 80/443 fallback only for the selected run; Cilium mode remains exact. **Roadmap alignment** This extends the existing cloud/sandbox agent roadmap capability with task-level control-plane policy and does not duplicate a planned roadmap item. ## What Changed - Added validated `networkEgress` grants to issue execution workspace settings and forwarded them through environment lease acquisition. - Added workload-owned, run-label-scoped NetworkPolicy/CiliumNetworkPolicy resources for task FQDN/CIDR grants. - Added lease audit metadata, sandbox policy environment variables, and actionable network-denial stderr guidance. - Added focused parser, manifest, policy creation, and denial-message tests plus Kubernetes provider documentation. ## Verification - `pnpm -C packages/shared exec vitest run src/validators/issue.test.ts` — 27 passed. - `pnpm -C packages/plugins/sandbox-providers/kubernetes test -- --run test/unit/network-policy.test.ts test/unit/cilium-network-policy.test.ts test/unit/scoped-network-egress.test.ts` — 21 passed. - `pnpm -C server exec vitest run src/__tests__/execution-workspace-policy.test.ts` — 15 passed. - `pnpm exec vitest run server/src/__tests__/heartbeat-plugin-environment.test.ts server/src/__tests__/environment-runtime.test.ts` — 26 passed. - `pnpm --dir packages/db build && pnpm --dir packages/shared build && pnpm --dir packages/plugins/sdk build` — passed, including migration safety checks. - `pnpm --dir packages/plugins/sandbox-providers/kubernetes typecheck && pnpm --dir server typecheck` — passed after refreshing the worktree's frozen offline dependencies. - End-to-end cluster validation of the `build-cython-ext` benchmark remains for CI/maintainer Kubernetes infrastructure; the focused tests assert `github.com` and `pypi.org` produce a policy selected only by the granted run. ## Risks - Standard NetworkPolicy cannot express FQDNs, so an FQDN grant allows hardened public IPv4 TCP 80/443 for that run; use Cilium mode for exact hostname enforcement. - The new field is additive and absent by default, so existing runs keep the current provider-level policy. - Workload owner references garbage-collect scoped policies with the Job/Sandbox; a cluster/controller that ignores owner references could temporarily strand a policy that still selects no future run ID. > For core feature work, check [`ROADMAP.md`](ROADMAP.md) first and discuss it in `#dev` before opening the PR. Feature PRs that overlap with planned core work may need to be redirected — check the roadmap first. See `CONTRIBUTING.md`. ## Model Used OpenAI Codex, exact model ID `gpt-5.6-sol`, high reasoning mode, tool use and code execution. The runtime did not expose a context-window size. ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [x] All Paperclip CI gates are green - [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge --------- Co-authored-by: Paperclip <noreply@paperclip.ing>
141 lines
5.2 KiB
TypeScript
141 lines
5.2 KiB
TypeScript
export interface BuildNetworkPolicyInput {
|
|
namespace: string;
|
|
paperclipServerNamespace: string;
|
|
egressAllowCidrs: string[];
|
|
/**
|
|
* Adapter-configured FQDNs (e.g. `api.anthropic.com`). Standard
|
|
* NetworkPolicy cannot express FQDNs natively — only Cilium can.
|
|
* When this list is non-empty AND no explicit `egressAllowCidrs`
|
|
* was provided, the standard NetworkPolicy falls back to "public
|
|
* IPv4 except RFC1918/link-local/loopback/multicast" so the
|
|
* configured FQDNs at least become reachable. This is broader
|
|
* than the operator probably wants — switch to `egressMode:
|
|
* "cilium"` for exact FQDN allow-listing in production.
|
|
*/
|
|
egressAllowFqdns?: string[];
|
|
name?: string;
|
|
podSelector?: Record<string, string>;
|
|
includeBaseRules?: boolean;
|
|
ownerReferences?: Record<string, unknown>[];
|
|
}
|
|
|
|
/**
|
|
* IPv4 ranges to carve out of `0.0.0.0/0` when we apply the
|
|
* public-internet fallback. Keeps agent pods from reaching cluster
|
|
* internals (RFC1918), node link-local + AWS IMDS (169.254.0.0/16),
|
|
* cluster loopback (127.0.0.0/8), CGNAT (100.64.0.0/10), this-network
|
|
* (0.0.0.0/8), and multicast (224.0.0.0/4).
|
|
*/
|
|
const PRIVATE_AND_LINK_LOCAL_EXCEPT_CIDRS = [
|
|
"0.0.0.0/8",
|
|
"10.0.0.0/8",
|
|
"100.64.0.0/10",
|
|
"127.0.0.0/8",
|
|
"169.254.0.0/16",
|
|
"172.16.0.0/12",
|
|
"192.168.0.0/16",
|
|
"224.0.0.0/4",
|
|
];
|
|
|
|
// Design note: the deny-all baseline blocks all ingress to agent pods.
|
|
// Paperclip-server does NOT push to agent pods — the agent shim makes
|
|
// outbound calls to paperclip-server via the egress allow-list (port 3100).
|
|
// This pull/callback model means no ingress rule is needed. If a future
|
|
// feature requires server→agent push (e.g. forced shutdown, live exec),
|
|
// add a targeted ingress rule here scoped to the paperclip-server pod
|
|
// selector.
|
|
export function buildNetworkPolicyManifests(input: BuildNetworkPolicyInput): Record<string, unknown>[] {
|
|
const denyAll = {
|
|
apiVersion: "networking.k8s.io/v1",
|
|
kind: "NetworkPolicy",
|
|
metadata: {
|
|
name: "paperclip-deny-all",
|
|
namespace: input.namespace,
|
|
labels: { "paperclip.io/managed-by": "paperclip-k8s-plugin" },
|
|
},
|
|
spec: {
|
|
podSelector: {},
|
|
policyTypes: ["Ingress", "Egress"],
|
|
},
|
|
};
|
|
|
|
const egressAllow: Record<string, unknown> = {
|
|
apiVersion: "networking.k8s.io/v1",
|
|
kind: "NetworkPolicy",
|
|
metadata: {
|
|
namespace: input.namespace,
|
|
labels: { "paperclip.io/managed-by": "paperclip-k8s-plugin" },
|
|
},
|
|
spec: {
|
|
podSelector: { matchLabels: input.podSelector ?? { "paperclip.io/role": "agent" } },
|
|
policyTypes: ["Egress"],
|
|
egress: [
|
|
...(input.includeBaseRules === false ? [] : [{
|
|
to: [
|
|
{
|
|
namespaceSelector: { matchLabels: { "kubernetes.io/metadata.name": "kube-system" } },
|
|
podSelector: { matchLabels: { "k8s-app": "kube-dns" } },
|
|
},
|
|
],
|
|
ports: [
|
|
{ protocol: "UDP", port: 53 },
|
|
{ protocol: "TCP", port: 53 },
|
|
],
|
|
}]),
|
|
...(input.includeBaseRules === false ? [] : [{
|
|
to: [
|
|
{
|
|
namespaceSelector: { matchLabels: { "kubernetes.io/metadata.name": input.paperclipServerNamespace } },
|
|
podSelector: { matchLabels: { app: "paperclip-server" } },
|
|
},
|
|
],
|
|
ports: [{ protocol: "TCP", port: 3100 }],
|
|
}]),
|
|
// NOTE: operator-supplied CIDRs are intentionally NOT port-scoped —
|
|
// operators may need them for non-HTTP services (e.g. private VCS
|
|
// mirrors, S3 endpoints, internal artifact registries). Operators
|
|
// should keep CIDRs as specific as possible. For HTTP/HTTPS-only
|
|
// LLM endpoints, the public-IPv4 fallback below is port-scoped
|
|
// (TCP 80/443).
|
|
...input.egressAllowCidrs.map((cidr) => ({
|
|
to: [{ ipBlock: { cidr } }],
|
|
})),
|
|
// Standard-NetworkPolicy fallback for FQDN-based egress. If the
|
|
// adapter requires FQDNs (e.g. api.anthropic.com) and the
|
|
// operator didn't supply explicit CIDRs, allow public IPv4 with
|
|
// private/link-local/loopback/multicast carved out. This makes
|
|
// the default `egressMode: "standard"` config functional out of
|
|
// the box for cloud LLM APIs without inadvertently exposing
|
|
// cluster internals or link-local metadata endpoints. Operators
|
|
// who want exact FQDN enforcement should use `egressMode: "cilium"`.
|
|
...(input.egressAllowCidrs.length === 0 &&
|
|
(input.egressAllowFqdns?.length ?? 0) > 0
|
|
? [
|
|
{
|
|
to: [
|
|
{
|
|
ipBlock: {
|
|
cidr: "0.0.0.0/0",
|
|
except: PRIVATE_AND_LINK_LOCAL_EXCEPT_CIDRS,
|
|
},
|
|
},
|
|
],
|
|
ports: [
|
|
{ protocol: "TCP", port: 443 },
|
|
{ protocol: "TCP", port: 80 },
|
|
],
|
|
},
|
|
]
|
|
: []),
|
|
],
|
|
},
|
|
};
|
|
|
|
(egressAllow.metadata as Record<string, unknown>).name = input.name ?? "paperclip-egress-allow";
|
|
if (input.ownerReferences) {
|
|
(egressAllow.metadata as Record<string, unknown>).ownerReferences = input.ownerReferences;
|
|
}
|
|
|
|
return [denyAll, egressAllow];
|
|
}
|