ufw-docker icon indicating copy to clipboard operation
ufw-docker copied to clipboard

service allow from ip

Open mbarbeaux opened this issue 5 years ago • 19 comments

Hi,

Thanks for this amazing project. It works real good in swarm mode ! I was wondering if it is possible to restrain opening a service port for only a custom IP ?

eg something like sudo ufw-docker service allow my-service from 123.45.67.89 1234/tcp ?

Moreover, is it possible to open multiple ports at the same for a service ? Something like sudo ufw-docker service allow my-service 1234:1238/tcp ?

mbarbeaux avatar Jan 10 '19 20:01 mbarbeaux

Thanks for using this small project.

About the second question, check this issue #5

Yes, I think it's possible to use more complicated rules for services. I will consider how to implement this feature.

chaifeng avatar Jan 13 '19 09:01 chaifeng

Thanks for this tool. It would be great if you could post some examples on how to use this tool to limit ports per specific IP addresses as it is very common when you need to limit certain ports to certain IP addresses - to allow access from VPN or between specific instances, etc.

I tried using synxtax ufw allow route from IP_ADDRESS/32 to any port 1234 However firewall with config specified in this tool is rejecting such rule and it only works if I allow any IP address to this port.

darklow avatar Apr 03 '19 15:04 darklow

I've recently encountered the same need. Forwarding ports to a service but only from my IP address (remote debugging).

I think the syntax should align with ufw:

# ufw
$ ufw route allow proto tcp from ${CUSTOM-IP-ADDR} to ${DOCKER-CONTAINER-IP} port ${PORT}

# ufw-docker
$ ufw-docker allow from ${CUSTOM-IP-ADDR} ${CONTAINER-NAME} ${PORT}/${PROTO}

riker09 avatar Apr 08 '19 13:04 riker09

I agree that this will be useful. In my case, I have:

  • A database service running in Docker, exposing its port to the localhost (127.0.0.1:3306->3306/tcp)
  • Another Docker container, from another docker-compose project, that should access this database via 172.17.0.1:3306.

At the moment, a connection from the host to 127.0.0.1:3306 is working, but not from another Docker container to 172.17.0.1:3306. It gets blocked by ufw:

May 23 12:03:20 hostname kernel: [30312236.628116] [UFW BLOCK] IN=br-… OUT= PHYSIN=veth… MAC=… SRC=172.20.0.3 DST=172.17.0.1 LEN=60 TOS=0x00 PREC=0x00 TTL=64 ID=45020 DF PROTO=TCP SPT=48138 DPT=3306 WINDOW=29200 RES=0x00 SYN URGP=0

slhck avatar May 23 '19 11:05 slhck

The corresponding line is this:

https://github.com/chaifeng/ufw-docker/blob/master/ufw-docker#L98

Would it be enough to add a new parameter that specifies the from IP address?

slhck avatar May 23 '19 12:05 slhck

@slhck I tried doing it manually with the following command

sudo ufw route allow proto tcp from 192.168.0.0/24 to any port 443

It still seems to be dropping the connections from the whitelisted IP range.

coltonrb avatar Jul 03 '19 16:07 coltonrb

Is the container’s port 443? We must user container ports for the ufw command, not host ports.

Chai Feng


From: Colton B [email protected] Sent: Wednesday, July 3, 2019 18:46 To: chaifeng/ufw-docker Cc: Chai Feng; Comment Subject: Re: [chaifeng/ufw-docker] service allow from ip (#6)

@slhckhttps://github.com/slhck I tried doing it manually with the following command

sudo ufw route allow proto tcp from 192.168.0.0/24 to any port 443

It still seems to be dropping the connections from the whitelisted IP range.

— You are receiving this because you commented. Reply to this email directly, view it on GitHubhttps://github.com/chaifeng/ufw-docker/issues/6?email_source=notifications&email_token=AAAZ24YOUFGTLBWFZBAH7XLP5TJU5A5CNFSM4GPJGMZ2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODZFBB2Q#issuecomment-508170474, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AAAZ245UB5FEFGLZDNOMRDTP5TJU5ANCNFSM4GPJGMZQ.

chaifeng avatar Jul 06 '19 04:07 chaifeng

Yes, it was the container's port (actually, it was both the container's port and the host's port, just to eliminate that variable).

coltonrb avatar Jul 07 '19 23:07 coltonrb

While the swarm agent works perfectly and thank you for writing this, any updates on whether this functionality has been added?

MuzammilM avatar Oct 07 '19 11:10 MuzammilM

Also looking for this functionality. I don't care about per-container support - just predictably deny all to 443/80 + allow 1 IP to 443/80.

dm17 avatar Oct 07 '19 22:10 dm17

Hi guys,

I will begin implementing this feature.

The ufw-docker sub-commands will be like:

ufw-docker service allow from ${ip_address} to \
    service:${swarm_service_name} port ${service_port}

ufw-docker service allow proto tcp from ${ip_address} to \
    service:${swarm_service_name} port ${service_port}

ufw-docker service allow proto tcp from ${ip_address} to \
    service:all port ${service_port}

Thanks for your feedback!

chaifeng avatar Oct 10 '19 05:10 chaifeng

That's great, thanks!

Just curious though, this will not just be for Swarm services, right? In practice one would probably specify the name of a container or particular docker compose service.

slhck avatar Oct 10 '19 05:10 slhck

Yes,

That's great, thanks!

Just curious though, this will not just be for Swarm services, right? In practice one would probably specify the name of a container or particular docker compose service.

Ya, hopefully

chaifeng avatar Oct 10 '19 06:10 chaifeng

Also looking for this functionality. I don't care about per-container support - just predictably deny all to 443/80 + allow 1 IP to 443/80.

OK, I think this functionality works fine... I was just using the wrong UFW commands. One must deny all, and then use this to allow per IP per port: ufw route allow proto tcp from the.ip.addr.0 to any port 443

dm17 avatar Oct 11 '19 16:10 dm17

Has this feature been implemented yet?

ocafebabe avatar Aug 25 '20 23:08 ocafebabe

For people that have tried this before docker version 19.03.4 here some tests. I think the docker team did some changes on the FORWARD chain on docker 19.03.4 (released in 2019-10-17).

I have a nginx container : "docker run --name nginx -p 8080:80 -d nginx" I want only the 192.168.0.0/24 subnet to be able to access nginx server on 10.0.0.2:8080

  • Host IP : 10.0.0.2
  • Container IP : 172.16.0.2
  • Client Subnet : 192.168.0.0/24
  • UFW command : "ufw route allow proto tcp from 192.168.0.0/24 to 172.16.0.2 port 80".
  1. On docker 19.03.3 => any ip can access the nginx server (with or wihout the UFW command)
test@test:~$ sudo iptables -L FORWARD
Chain FORWARD (policy DROP)
target     prot opt source               destination
DOCKER-ISOLATION-STAGE-1  all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED
DOCKER     all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED
DOCKER     all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere
DOCKER-USER  all  --  anywhere             anywhere
ufw-before-logging-forward  all  --  anywhere             anywhere
ufw-before-forward  all  --  anywhere             anywhere
ufw-after-forward  all  --  anywhere             anywhere
ufw-after-logging-forward  all  --  anywhere             anywhere
ufw-reject-forward  all  --  anywhere             anywhere
ufw-track-forward  all  --  anywhere             anywhere
  1. On docker 19.03.4 (and onwards) => only 192.168.0.0/24 subnet can access nginx server on 10.0.0.2:8080
test@test:~$ sudo iptables -L FORWARD
Chain FORWARD (policy DROP)
target     prot opt source               destination
DOCKER-USER  all  --  anywhere             anywhere
DOCKER-ISOLATION-STAGE-1  all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED
DOCKER     all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED
DOCKER     all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED
DOCKER     all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere
ufw-before-logging-forward  all  --  anywhere             anywhere
ufw-before-forward  all  --  anywhere             anywhere
ufw-after-forward  all  --  anywhere             anywhere
ufw-after-logging-forward  all  --  anywhere             anywhere
ufw-reject-forward  all  --  anywhere             anywhere
ufw-track-forward  all  --  anywhere             anywhere

You can see that in v19.03.4 the DOCKER-USER chain is on top of DOCKER-ISOLATION-STAGE-1 chain. I could assume this is the reason why the traffic is not filtered in v19.03.3 and in 19.03.4 it is filtered as expected.

mlollo avatar Dec 22 '20 20:12 mlollo

Have this option been implemented? Need to expose the container, but limit the source addresses that could access them.

arbaldin avatar Jul 28 '23 20:07 arbaldin

Don't think this options is implemented. I tried this command but it does not work: sudo ufw allow from x.x.x.x to any port 8080 proto tcp Does anyone have any suggestion?

sravzpublic avatar Jul 29 '23 18:07 sravzpublic

I got it working to expose a container only to a whitelisted ip with the normal installation of ufw-docker and then execute the commands on the host

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' docker_container_name

Now we got the IP Adress from the Docker Container.

ufw route allow proto tcp from white_listed_ip to docker_container_ip port container_port

After that you can access the docker container from the whitelisted ip

mxwmnn avatar Aug 24 '23 11:08 mxwmnn