How I Isolated an AI Coding Agent from My Homelab Infrastructure
Giving an AI coding agent access to your infrastructure repository can be incredibly useful. It can inspect configurations, troubleshoot services, modify code, and help maintain the entire setup. But there's an importan
Giving an AI coding agent access to your infrastructure repository can be incredibly useful. It can inspect configurations, troubleshoot services, modify code, and help maintain the entire setup.
But there's an important question: how much access should it actually have?
A lot of people give AI coding agents shell access, SSH keys, and access to environment files, then rely on system prompts to tell them what not to do.
I wasn't comfortable with that approach.
System prompts are instructions, not security boundaries. An internet-connected agent can encounter malicious instructions hidden in a web page, log file, or package README. It can also generate an incorrect command, misunderstand a task, or accidentally expose sensitive information.
When I introduced an AI agent to my homelab in Part 2, I wanted to approach this differently.
My starting assumption was simple: the agent will eventually make a mistake, and I need to make sure that mistake cannot cause serious damage.
This article covers the four security layers I implemented to isolate the agent from the rest of my infrastructure.
Defining the Threat Model
Before configuring firewalls or changing permissions, I identified three things I wanted to protect against.
1. Access to the rest of my home network
If the control VM running the AI agent gets compromised, I don't want it to become a stepping stone to my main PC, home router, or other devices connected to Wi-Fi.
2. Unauthorized access to sensitive data and infrastructure
A prompt injection or an incorrect command should not be enough to access .vault_pass, terraform.tfvars, SSH keys, or destroy the 500 GB of family photos stored in Immich and passwords stored in Vaultwarden.
3. Difficult recovery
If I ever suspect the control VM has been compromised, I want to be able to discard it and rebuild a clean environment in minutes, rather than spending an entire weekend figuring out what happened.
These requirements became the foundation of my security design, documented in ADR-008.
The Four-Layer Security Architecture
I separated the environment into a disposable control VM, a human-operated management account, and the Proxmox infrastructure itself.
Here's how the pieces fit together:
Main Host PC
(Blocks inbound connections from VMnet8)
โ
โ Unidirectional SSH
โผ
Disposable Control VM: haytham-ctl
(VMware NAT ยท No shared folders or USB)
โ
โโโ homelab-agent (AI)
โ โโโ No sudo
โ โโโ No SSH keys
โ โโโ No secret access
โ โโโ Kernel UID firewall
โ โ DROP 192.168.0.0/16
โ โโโ 3 read-only wrappers
โ โโโ plan
โ โโโ check
โ โโโ health
โ
โโโ homelab-ops (Human Gate)
โโโ Read-only Infisical identity
โโโ RAM-only secrets (/dev/shm)
โโโ Human-approved operations
โโโ ops-tf-apply
โโโ ops-ansible-apply
โ
โผ
Proxmox VE
(.2 ยท Hardened RBAC)
โ
โโโ Zone 1 (.10)
โ CT 110 Infisical
โ protection=1
โ No Terraform ACL
โ
โโโ Zone 2 (.20 to .21)
โ Vaultwarden & Immich
โ protection=1
โ No destructive access
โ
โโโ Zones 3 to 4
Rebuildable infrastructure
and dashboards
The architecture has four layers. Each one addresses a different part of the threat model.
Layer 1: Isolating the Control VM and Blocking LAN Access
The first layer is the machine running the AI agent.
My control node, haytham-ctl, runs Ubuntu 24.04 inside a local VMware VM. From the beginning, I treat this machine as untrusted and disposable.
I don't want a compromised control VM to have an easy path back to my main workstation or the rest of the home network.
I implemented several restrictions.
VMware isolation
The VM uses VMware NAT through VMnet8.
I also disabled:
- Shared Folders
- Drag and Drop
- Copy and Paste
- USB auto-connect
These settings reduce the number of ways data can move between the host and guest environments.
Host firewall
I added a firewall script on my main PC that blocks inbound connections initiated from the VMnet8 subnet.
The idea is to prevent the control VM from initiating connections into the host machine.
Restricted SSH access
I connect from my desktop terminal into haytham-ctl, rather than allowing the control VM to initiate SSH connections back to my desktop.
The SSH configuration includes:
AllowUsers farhan
PasswordAuthentication no
AllowAgentForwarding no
AllowTcpForwarding no
X11Forwarding no
I also disabled legacy TTY injection through:
dev.tty.legacy_tiocsti = 0
This helps prevent terminal input injection through the legacy TIOCSTI mechanism.
Default-deny outbound firewall
Inside the VM, UFW follows a default-deny approach for outbound traffic.
The control VM can only reach the specific destinations required for normal operations:
- Proxmox
.2for SSH and API access. - Infisical
.10:8080. - Active services in Zones 2 to 4.
- Public HTTPS and DNS.
The home router (.1), main PC, reserved address ranges, and other LAN clients are explicitly blocked.
Kernel-level UID firewall
UFW alone wasn't enough for my intended isolation.
I wanted a restriction that specifically applies to the AI agent, regardless of which application or command it uses.
So I added a persistent netfilter rule through haytham-agent-egress.service.
The rule matches the Linux UID of homelab-agent and drops all traffic destined for:
192.168.0.0/16
This means that even if the agent tries something like:
curl http://192.168.100.10:8080
or:
nc 192.168.100.2 8006
the kernel rejects the traffic.
This is an additional restriction on top of UFW, specifically tied to the agent's operating-system identity.
The important distinction is that the restriction doesn't depend on whether the agent understands or follows its instructions. The operating system enforces it.
Layer 2: Separating AI Permissions from Human Operations
Network isolation protects the rest of my LAN, but it doesn't solve everything.
The agent still needs to work with the infrastructure repository. It needs to edit Terraform files, update Ansible playbooks, inspect documentation, and run checks.
At the same time, I don't want it to have unrestricted access to deployment commands or secrets.
Inside haytham-ctl, I use two separate Linux accounts:
-
homelab-agent: Used by the AI agent for development and inspection. -
homelab-ops: Used by me for privileged infrastructure operations.
Both accounts work with the shared Git repository:
/srv/haytham-homelab
The repository uses the homelab-ops:homelab-repo group with permissions set to 2775.
Here's how I divide the responsibilities:
| Task | Executed As | Mechanism |
|---|---|---|
| Edit Terraform, Ansible, KB, and scripts | homelab-agent |
Normal Git workspace |
| Terraform plan | homelab-agent |
ops-tf-plan |
| Ansible dry-run | homelab-agent |
ops-ansible-check |
| Live health checks | homelab-agent |
ops-health-check |
| Terraform apply | Human (homelab-ops) |
Interactive TTY and APPLY confirmation |
| Ansible apply | Human (homelab-ops) |
Interactive TTY and YES confirmation |
| Secret rotation | Human | Infisical Web UI with MFA and audit log |
| Disaster recovery unlock | Human | Break-glass SSH |
The agent has access to only three predefined, root-owned wrapper commands through a scoped sudoers configuration:
ops-tf-planops-ansible-checkops-health-check
These allow the agent to perform most of the preparation work without giving it unrestricted shell access.
For example, it can modify Terraform configurations, run a plan, inspect the proposed changes, and check whether services are healthy.
But the final terraform apply or ansible-playbook operation remains under human control.
I have to switch to homelab-ops, review the changes, and explicitly type APPLY or YES before the operation proceeds.
This keeps the daily workflow relatively convenient while making sure that infrastructure changes with real consequences require human approval.
Layer 3: Keeping Secrets Outside the Agent's Reach
One thing I wanted to avoid was storing infrastructure credentials directly on the control VM.
Files like .vault_pass, plaintext terraform.tfvars, and SSH private keys should never be accessible to the AI agent.
Instead, I keep infrastructure secrets in Infisical, running in Zone 1:
CT 110
192.168.100.10:8080
This instance is provisioned separately from Terraform and protected at the hypervisor level.
The human-operated account, homelab-ops, holds a read-only Infisical Machine Identity called homelab-ops-ro.
Its credentials are stored here:
/var/lib/haytham-ops/infisical-ops.env
The file has 0600 permissions and belongs to the human-operated account. The AI agent cannot read it.
When an ops-* wrapper runs, it retrieves the required secrets from Infisical.
Instead of writing them to persistent storage, the wrapper loads them into temporary locations:
- RAM-backed files under
/dev/shm. - Ephemeral
TF_VAR_*environment variables for Terraform.
The wrapper also starts a short-lived ssh-agent when needed.
After the operation finishes, an EXIT trap shreds the temporary files.
The Machine Identity itself is read-only, meaning even homelab-ops cannot modify or delete the secrets stored in Infisical through that identity.
This gives me a useful separation:
- The AI agent can work with infrastructure code.
- The human-operated account can retrieve the credentials required for operations.
- The secrets remain outside the agent's accessible environment.
The important part is that the agent doesn't need to know the actual credentials to help prepare infrastructure changes.
Layer 4: Protecting Critical Data at the Hypervisor Level
The previous layers restrict what the agent can do inside the control VM.
But what if someone manages to compromise homelab-ops itself?
I wanted another boundary that doesn't depend entirely on the security of the control VM.
That's where Proxmox RBAC comes in.
Scoped Proxmox Permissions
I configured harden-pve-rbac.sh to remove the global PVEVMAdmin permission from / for the Terraform API identity:
terraform@pve
Instead of granting broad permissions across the entire Proxmox environment, I separated the infrastructure into two resource pools.
Ephemeral pool
/pool/homelab-ephemeral
Terraform gets the permissions needed for the full lifecycle of resources in this pool.
This covers Zones 3 and 4, including network infrastructure and dashboards that can be rebuilt if necessary.
Protected pool
/pool/homelab-protected
This contains Zone 2 services:
- Vaultwarden
- Immich
Terraform only receives a custom non-destructive role called HomelabProtectedOps.
In addition, every Zone 1 and Zone 2 container has:
protection=1
This provides another layer of protection against accidental destructive operations at the hypervisor level.
The distinction is important: not every container has the same recovery cost. Network services and dashboards are relatively easy to rebuild. Family photos and password vaults are not.
Three Separate SSH Keys
I also separated SSH access into three different keys.
| Key | Location | Purpose |
|---|---|---|
id_ed25519 |
Main PC only | Break-glass access and backup pulling |
id_ed25519_haytham_ctl |
Main PC | SSH and SCP into the control VM |
id_ed25519_control |
Control VM | Revocable infrastructure access |
The first key never gets copied to haytham-ctl. It is reserved for direct access to Proxmox .2, Infisical .10, and pulling backups.
The second key is only used to connect from my desktop to the control VM.
The third key is generated inside the control VM. It is not stored in Zone 1 and is automatically rotated whenever I rebuild the control environment.
This way, compromising the control VM doesn't automatically expose the SSH credentials I use for direct recovery.
Rebuilding the Control VM in Under 10 Minutes
The final piece is recovery.
I don't want to spend hours cleaning up a potentially compromised machine. I'd rather discard it and start from a known-good state.
Before storing any operational state, I take a clean VMware snapshot called:
clean-base
The control-node state is then packaged into an encrypted archive using age through:
ops-bundle pack
The archive contains:
terraform.tfstate- Non-secret
terraform.tfvars infisical-ops.env
The revocable SSH private key, id_ed25519_control, is deliberately excluded.
If I suspect the control VM has been compromised, the recovery procedure is:
- Revert the VM to the
clean-basesnapshot. - Revoke the old SSH key and Infisical identity.
- Run
ops-bundle unpackto restore the required operational state. - Generate a fresh SSH keypair automatically.
The goal is to get back to a clean control environment in under ten minutes.
This doesn't mean the entire Proxmox infrastructure can be recovered in ten minutes. It means the management environment can be replaced quickly without rebuilding everything it manages.
Testing the Security Boundaries
I didn't want to assume that the configuration was correct just because the firewall rules and permissions looked right.
A security boundary needs to be tested from the perspective of someone trying to cross it.
So I added a set of negative security tests that must fail whenever I build or rebuild the control VM.
Here are some examples.
1. Testing Secret and Privilege Restrictions
As homelab-agent, attempts to access human-only files or execute privileged operations must fail:
# Secret access must fail
sudo -u homelab-agent \
cat /var/lib/haytham-ops/infisical-ops.env
sudo -u homelab-agent \
cat /var/lib/haytham-ops/.ssh/id_ed25519_control
# Privileged operations must fail
sudo -u homelab-agent \
sudo -u homelab-ops \
/usr/local/libexec/haytham-ops/ops-tf-apply
sudo -u homelab-agent \
sudo -u homelab-ops \
/usr/local/libexec/haytham-ops/ops-ansible-check "zone3_infra.yml; id"
2. Testing the Kernel-Level Network Block
The agent must not be able to reach the protected LAN endpoints:
sudo -u homelab-agent \
curl -m 2 http://192.168.100.10:8080/api/status
sudo -u homelab-agent \
nc -zw2 192.168.100.2 8006
Both attempts should be rejected by the kernel firewall.
3. Testing SSH Restrictions
From my main PC, I also test that the control VM rejects unauthorized SSH access and forwarding:
# Root login must fail
ssh root@<HAYTHAM_CTL_IP>
# Human operations account must not be directly accessible
ssh homelab-ops@<HAYTHAM_CTL_IP>
# Agent forwarding must fail
ssh -A haytham-ctl "ssh-add -l"
# TCP forwarding must fail
ssh -L 9999:192.168.100.10:8080 haytham-ctl
These tests cover different boundaries: operating-system permissions, secret isolation, network access, and SSH configuration.
My rule is simple: all forbidden operations must fail, while the three approved read-only wrappers must continue to work.
Only then do I consider the control VM ready for use.
What's Coming in Part 4
With the infrastructure automated, documented, and protected, the next step was simply to use it.
And that's where some interesting problems started showing up.
In Part 4, I'll share what happened after my family started using the homelab regularly, including:
- Debugging a video transcoding process that got stuck for 3.5 hours.
- Setting up multi-account Immich and Tailscale for both our phones.
- Building a Windows backup-pull script that doesn't depend on cloud storage and includes a live progress bar.
- Testing the backup process with an actual Vaultwarden restore drill.
Because having a server that runs successfully is one thing. Making sure it works reliably for the people who actually depend on it is another.
Full GitHub repository: zero-trust-homelab
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes โ full credit and traffic to the original publisher.