---
title: "Open a container registry firewall for a pipeline agent only right before the push"
url: "https://www.dariuszparys.com/notes/open-registry-firewall-right-before-the-push"
author: "Dariusz Parys"
published: 2026-11-23T00:00:00.000Z
description: "A hosted CI agent allowed its public IP in a container registry firewall at the start of the job, and the push several minutes later failed with 403, most likely because the agent then sent requests from a different IP."
tags:
  - "Azure"
  - "CI/CD"
  - "Security"
---

# Open a container registry firewall for a pipeline agent only right before the push

## Context

A registry with a deny-by-default firewall needs the hosted CI agent's public IP allowed before the agent can push to it. The pipeline did this at the start of the job:

1. looked up its public IP through an IP echo service
2. added the IP as a firewall rule
3. confirmed with a registry login that the rule worked

Then came several minutes of image builds and vulnerability scans, none of which needed the registry. The first push request failed with **403 Forbidden**. Nothing had changed the registry in the meantime, so the agent most likely sent the push from a different public IP than the one it had reported earlier.

## Insight

Keep the window between adding the rule and pushing as short as possible, and make the push step say which IP it actually used:

- add the firewall rule directly before the push steps, after builds and scans
- log the agent's current IP next to the allowed one in each push step, and warn if they differ
- retry a failed push a couple of times
- if it's still unreliable, use an agent with a fixed outgoing IP, or one that reaches the registry through a private endpoint, so no IP rules are needed

## Snippet

```bash
for attempt in 1 2 3; do
  current_ip="$(curl -fsS --max-time 10 https://api.ipify.org || echo unknown)"
  echo "push ${attempt}/3: agent IP now ${current_ip}, allowed ${AGENT_IP:-unknown}"
  docker push "$IMAGE" && exit 0
  sleep 15
done
exit 1
```
