Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to create rules when container not started #41

Open
rgomezceis opened this issue Dec 27, 2022 · 26 comments
Open

Unable to create rules when container not started #41

rgomezceis opened this issue Dec 27, 2022 · 26 comments
Labels
bug Something isn't working question Further information is requested

Comments

@rgomezceis
Copy link

rgomezceis commented Dec 27, 2022

Using this rule:

- network: default
  container: test_container

If the container 'test_container' isn't running whalewall can't create all the rules for this service.
That's correct but what happen when you have multiple stacks (docker compose files) and do a server reboot. Whalewall won't create the rules for some services because some containers isn't running (all the stacks are deployed at the same time)

So we can control the start order for services on the same docker compose file with 'depends_on' label, but not the services in other compose files. We could control this with a health check instead.

Is possible to create the rules only when the container is in healthy state? We can do a health check to check if the dependant container has already started and then create the whalewall rules when the service has the healthy label. This can be configurable with a label like 'whalewall.healthy: true' if true create the rules when container is in healthy state, and if isn't set create the rules when container starts.

Or do 3-5 retries until the container starts?

I don't know how to solve this problem, because if i use static ip for containers i need to set rules in both containers.

  • loki container: 172.27.0.2
  • promtail container: 172.27.0.3
  • Only outgoing traffic from promtail to loki:3100

Loki:

whalewall.enabled: true

Promtail:

      whalewall.enabled: true
      whalewall.rules: |
        output:
          # push to loki
          - ip: "172.27.0.2"
            proto: tcp
            port: 3100

Dec 27 11:17:11 xxxxxxx kernel: [ 5433.189077] whalewall-loki-16c68c683925 drop: IN=xxxxxxxx OUT=xxxxxxxx PHYSIN=xxxxxxx PHYSOUT=xxxxxxxx MAC=xxxxxxxxxxxxxx SRC=172.27.0.2 DST=172.27.0.3 LEN=60 TOS=0x00 PREC=0x00 TTL=64 ID=0 DF PROTO=TCP SPT=3100 DPT=47836 WINDOW=65160 RES=0x00 ACK SYN URGP=0

nft chain:

        chain whalewall-loki-16c68c683925 {
                counter packets 630 bytes 37800 log prefix "whalewall-loki-16c68c683925 drop: " level info drop
        }

        chain whalewall-promtail-4f3d6d98bee7 {
                ip saddr 172.27.0.3 ip daddr 172.27.0.2 tcp dport 3100 ct state established,related,new counter packets 32 bytes 1920 accept
                ip saddr 172.27.0.2 ip daddr 172.27.0.3 tcp sport 3100 ct state established,related counter packets 0 bytes 0 accept 
                counter packets 33 bytes 1980 log prefix "whalewall-promtail-4f3d6d98bee7 drop: " level info drop
        }

Rule "ip saddr 172.27.0.2 ip daddr 172.27.0.3 tcp sport 3100 ct state established,related counter packets 0 bytes 0 accept" not working because is blocking by "log prefix "whalewall-loki-16c68c683925 drop:" first.

Traffic will be blocked in the opposite side (idk the reason of this traffic because it's only promtail -> loki). But when using container label, everything works:

      whalewall.enabled: true
      whalewall.rules: |
        output:
          # push to loki
          - network: logging_network
            container: loki
            proto: tcp
            port: 3100

nft chain:

    chain whalewall-loki-16c68c683925 {
            ip saddr 172.27.0.2 ip daddr 172.27.0.3 tcp sport 3100 ct state established,related counter packets 0 bytes 0 accept
            counter packets 698 bytes 41880 log prefix "whalewall-loki-16c68c683925 drop: " level info drop
    }

    chain whalewall-promtail-24f8a974fb5d {
            ip saddr 172.27.0.3 ip daddr 172.27.0.2 tcp dport 3100 ct state established,related,new counter packets 0 bytes 0 accept
            counter packets 3 bytes 180 log prefix "whalewall-promtail-24f8a974fb5d drop: " level info drop
    }
@capnspacehook
Copy link
Owner

I think it's possible to optionally wait until containers are in a healthy state to create rules for them, but I don't see how that'd solve your issue. How can you guarantee the order containers are started across multiple Docker Compose files by setting health checks? I don't understand that.

The reason you couldn't create container -> container rules by specifying the container's static IP is because the way whalewall creates nftables rules. Packets entering the 'whalewall' chain jump to the appropriate container chain based on the source IP. For established inbound traffic for an outbound rule the packets will originate from the container specified in the outbound rule, hence requiring nftables rules to be created in both container's chains.

@rgomezceis
Copy link
Author

Because I can do a health check using curl command to check if the other container is started, if success container will be in healthy state and then whalewall will create the rules.
This will avoid the problem if a dependant container started later than the container which has the whalewall rules.

@capnspacehook
Copy link
Owner

capnspacehook commented Dec 27, 2022

Right, I understand how health checks work, I just don't see how setting a health check on a container will affect the order in which containers in different docker compose files will be started. How are you starting/stopping the different stacks? For each compose file, 'docker compose up $file'?

If a container specifies a rule mentioning a container that is not yet started, whalewall will continue handling container start events for up to 10 seconds. Basically it'll wait for the mentioned container to hopefully start. If the container doesn't start within 10 seconds, whalewall will return an error. This can be changed with the '-t' flag, you may be able to solve your problem by specifying a longer timeout.

I realize from the description of the '-t' flag that it affects waiting for containers isn't obvious, I think I should either update the description or add another flag only for container waiting timeouts.

@rgomezceis
Copy link
Author

rgomezceis commented Dec 28, 2022

We use portainer and when you reboot the server the stacks starts without an order.

When you use -t flag whalewall doesn't create db files.

version: "3.8"

services:
  whalewall:
    container_name: whalewall
    image: ghcr.io/capnspacehook/whalewall
    restart: unless-stopped
    cap_add:
      - NET_ADMIN
    command: '-t 45s'
    network_mode: host
    volumes:
      - whalewall_data:/data
      - /var/run/docker.sock:/var/run/docker.sock:ro

volumes:
  whalewall_data:

Using binary works well, but after reboot and executing whalewall in cron job (whalewall -t 30s) I have this error:

{"level":"error","time":"2022-12-28T10:03:43.043860222Z","msg":"error creating rules","container.id":"d44de8d777aa","container.name":"grafana","error":"error creating output rules: error handling required container \"metrics_prometheus\": error creating output rules: error handling required container \"metrics_cadvisor\": error creating output rules: error handling required container \"openproject_db\": error creating output rules: container \"metrics_prometheus\" not found","stacktrace":"github.com/capnspacehook/whalewall.(*RuleManager).createRules\n\tgithub.com/capnspacehook/whalewall/create.go:64\ngithub.com/capnspacehook/whalewall.(*RuleManager).Start.func1\n\tgithub.com/capnspacehook/whalewall/manager.go:112"}

@capnspacehook
Copy link
Owner

Ah, because the location of the database is specified in the CMD in the Dockerfile, you overriding the command arguments causes -d /data to be removed and the database to be created somewhere else: https://github.com/capnspacehook/whalewall/blob/master/Dockerfile#L26. I may want to add environmental variables as a way to configure whalewall itself, so defaults can be set in the Dockerfile and gracefully overridden by users if they want.

As for your original issue, I've been thinking about it a bit, and I think I may have come up with a solution. Try pulling the latest Docker image and omitting the command line in your docker-compose.yml, increasing the timeout shouldn't be needed. I had to add to the DB schema, and I don't have migrations so I'd recommend running whalewall -clear in the old container to remove old firewall rules, then pulling the new container and running it with a new volume so the database is recreated fresh.

If that works I'll create a release soon.

@capnspacehook capnspacehook added bug Something isn't working question Further information is requested labels Apr 4, 2023
@rgomezceis
Copy link
Author

rgomezceis commented Apr 12, 2023

Hello, thanks for your reply.

Now, whalewall only applies the rules when you restart the whalewall container, not when you update the labels of the containers that have rules.

Also when you restart the machine, no rule is applied.

Rules are deleted as you can see with "nft list table ip filter" command after machine restart.
You have to run "whalewall -clear" and then "whalewall" to recreate the rules.

It seems that the database is not working properly.

@capnspacehook
Copy link
Owner

Hmm that's weird, if anything I'd think that functionality would be working better now, not worse... Are you running whalewall as a container or a binary, and what arguments are you passing it?

@rgomezceis
Copy link
Author

I've tried both and the same behavior. No arguments, just running whalewall container or "./whalewall" if using the binary.

Can you test it by restarting the computer? There is no rule after rebooting the computer and whalewall does not create them until you run './whalewall -clear' and then './whalewall'.

Also when you add whalewall labels to a container it does not create the rules until you restart whalewall.

@capnspacehook
Copy link
Owner

I will do some in depth testing, but note that whalewall has never acted on container label change events, whalewall only responds to container 'start' or 'die' events. If you want to change the rules of a container you'll have to update the labels and restart it.

@capnspacehook capnspacehook removed the question Further information is requested label Apr 13, 2023
@capnspacehook
Copy link
Owner

Also just to confirm, if you didn't already can you confirm that the previous version of whalewall you were using didn't have this bug? If so can you post the output of 'whalewall -version' for both the working old version and the buggy new version? Thanks

@rgomezceis
Copy link
Author

I will do some in depth testing, but note that whalewall has never acted on container label change events, whalewall only responds to container 'start' or 'die' events. If you want to change the rules of a container you'll have to update the labels and restart it.

I know but when I update the labels and restart the containers whalewall doesn't update the rules (no log about rules in whalewall container), it only does if I restart the whalewall container.

@capnspacehook
Copy link
Owner

Oh, I think I know what's wrong then, thanks for clarifying

@rgomezceis
Copy link
Author

Also just to confirm, if you didn't already can you confirm that the previous version of whalewall you were using didn't have this bug? If so can you post the output of 'whalewall -version' for both the working old version and the buggy new version? Thanks

No. In v0.2.3 version when you restart host machine, whalewall applies the rules using the database. In the newer version, whalewall doesn't apply any rule.

Buggy build:

Whalewall devel

Build Information:

Go version:   go1.20.3
-buildmode:   pie
Compiler:     gc
-trimpath:    true
CGO_ENABLED:  0
GOARCH:       amd64
GOOS:         linux
GOAMD64:      v1
Commit:       5c67e57f18ed5d6dcf392ea34250b26e3f77cdb3
Commit Time:  2023-04-05T23:37:13Z

Dependencies:

Name                                           Version                             Hash
github.com/docker/distribution                 v2.8.1+incompatible                 h1:Q50tZOPR6T/hjNsyc9g8/syEs6bk8XXApsHjKukMl68=
github.com/docker/docker                       v20.10.22+incompatible              h1:6jX4yB+NtcbldT90k7vBSaWJDB3i+zkVJT9BEK8kQkk=
github.com/docker/go-connections               v0.4.0                              h1:El9xVISelRB7BuFusrZozjnkIM5YnzCViNKohAFqRJQ=
github.com/docker/go-units                     v0.4.0                              h1:3uh0PgVws3nIA0Q+MwDC8yjEPf9zjRfZZWXZYDct3Tw=
github.com/dustin/go-humanize                  v1.0.0                              h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
github.com/gogo/protobuf                       v1.3.2                              h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
github.com/google/nftables                     v0.0.0-20221015190445-4f5cd5826fbd  h1:EWP0uTXu3ItJlSimbOvSFVejL5/Bc8eMui48EESr9R4=
github.com/google/uuid                         v1.3.0                              h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/josharian/native                    v1.0.0                              h1:Ts/E8zCSEsG17dUqv7joXJFybuMLjQfWE04tsBODTxk=
github.com/landlock-lsm/go-landlock            v0.0.0-20230212201647-821adaecc1a5  h1:8BDph6n7ZDRI7ppV9jLD4vFc4rjpu2CxY9anN2++FM4=
github.com/mattn/go-isatty                     v0.0.16                             h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ=
github.com/mdlayher/netlink                    v1.6.2                              h1:D2zGSkvYsJ6NreeED3JiVTu1lj2sIYATqSaZlhPzUgQ=
github.com/mdlayher/socket                     v0.2.3                              h1:XZA2X2TjdOwNoNPVPclRCURoX/hokBY8nkTmRZFEheM=
github.com/opencontainers/go-digest            v1.0.0                              h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
github.com/opencontainers/image-spec           v1.0.2                              h1:9yCKha/T5XdGtO0q9Q9a6T5NUCsTn/DrBg0D7ufOcFM=
github.com/pkg/errors                          v0.9.1                              h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/remyoudompheng/bigfft               v0.0.0-20230129092748-24d4a6f8daec  h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
github.com/sirupsen/logrus                     v1.8.1                              h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
go.uber.org/atomic                             v1.7.0                              h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
go.uber.org/multierr                           v1.6.0                              h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4=
go.uber.org/zap                                v1.24.0                             h1:FiJd5l1UOLj0wCgbSE0rwwXHzEdAZS6hiiSnxJN/D60=
go4.org/netipx                                 v0.0.0-20220812043211-3cc044ffd68d  h1:ggxwEf5eu0l8v+87VhX1czFh8zJul3hK16Gmruxn7hw=
golang.org/x/exp                               v0.0.0-20220827204233-334a2380cb91  h1:tnebWN09GYg9OLPss1KXj8txwZc6X6uMr6VFdcGNbHw=
golang.org/x/net                               v0.7.0                              h1:rJrUqqhjsgNp7KqAIc25s9pZnjU7TUcSY7HcVZjdn1g=
golang.org/x/sync                              v0.1.0                              h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sys                               v0.7.0                              h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=
golang.org/x/time                              v0.0.0-20200630173020-3af7569d3a1e  h1:EHBhcS0mlXEAVwNyO2dLfjToGsyY4j24pTs2ScHnX7s=
gopkg.in/yaml.v3                               v3.0.1                              h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gvisor.dev/gvisor                              v0.0.0-20221108212141-79965837f088  h1:S2KFXSdEiuahdJyngV98RrTVQnjWEdh3VtWqAtbv6AU=
kernel.org/pub/linux/libs/security/libcap/psx  v1.2.66                             h1:ikIhPzfkSSAEwBOU+2DWhoF+xnGUhvlMTfQjBVhvzQY=
modernc.org/libc                               v1.22.3                             h1:D/g6O5ftAfavceqlLOFwaZuA5KYafKwmr30A6iSqoyY=
modernc.org/mathutil                           v1.5.0                              h1:rV0Ko/6SfM+8G+yKiyI830l3Wuz1zRutdslNoQ0kfiQ=
modernc.org/memory                             v1.5.0                              h1:N+/8c5rE6EqugZwHii4IFsaJ7MUhoWX07J5tC/iI5Ds=
modernc.org/sqlite                             v1.21.1                             h1:GyDFqNnESLOhwwDRaHGdp2jKLDzpyT/rNLglX3ZkMSU=

@capnspacehook
Copy link
Owner

I think I finally found the source of the regression, can you please test again? Specifically, whalewall should modify firewall rules for containers when they are created or stopped whether it is running or not. The builds you were testing with had a bug where Docker events were ignored, so whalewall only modified firewall rules when it started up.

If that's fixed, let me know if your original issue is solved as well, I think it should be. If you still have issues, I'd run whalewall -clear to remove whalewall-created firewall rules, then delete whalewall's database files. Should be 3 files prefixed with db.sqlite in either the directory whalewall was run in, or the directory you specified with the -d flag.

@rgomezceis
Copy link
Author

rgomezceis commented Apr 18, 2023

Now working well, but rules still not working after a machine reboot.
I think whalewall is not using the database properly.

Here whalewall log before and after reboot:

2023-04-18T05:35:32.968011923Z INF undefined | msg=applied landlock rules 
2023-04-18T05:35:32.968505273Z INF undefined | msg=applied seccomp filters syscalls.allowed=48 
2023-04-18T05:35:32.96852473Z INF undefined | msg=starting whalewall version=devel commit=d36566f6cf5a906b00faa24849ec1719ba7a3f36 
2023-04-18T05:35:56.028746215Z INF undefined | msg=creating rules container.id=a0cb791dc22c container.name=wiki_db 
2023-04-18T05:35:56.345249172Z INF undefined | msg=creating rules container.id=fb50bd80a212 container.name=wiki 
2023-04-18T05:36:32.483556137Z INF undefined | msg=shutting down 
2023-04-18T05:37:04.695132263Z INF undefined | msg=applied landlock rules 
2023-04-18T05:37:04.695703794Z INF undefined | msg=applied seccomp filters syscalls.allowed=48 
2023-04-18T05:37:04.695718779Z INF undefined | msg=starting whalewall version=devel commit=d36566f6cf5a906b00faa24849ec1719ba7a3f36

nft table after reboot:

table ip filter {
        map whalewall-container-addrs {
                type ipv4_addr : verdict
        }

        chain DOCKER {
                iifname != "br-8a1207fb6cae" oifname "br-8a1207fb6cae" meta l4proto tcp ip daddr 172.18.0.2 tcp dport 9000 counter packets 13 bytes 610 accept
                iifname != "br-498c5f61477f" oifname "br-498c5f61477f" meta l4proto tcp ip daddr 172.19.0.3 tcp dport 443 counter packets 0 bytes 0 accept
                iifname != "br-498c5f61477f" oifname "br-498c5f61477f" meta l4proto tcp ip daddr 172.19.0.3 tcp dport 80 counter packets 0 bytes 0 accept
        }

        chain DOCKER-ISOLATION-STAGE-1 {
                iifname "docker0" oifname != "docker0" counter packets 0 bytes 0 jump DOCKER-ISOLATION-STAGE-2
                iifname "br-e4726b4d1a2d" oifname != "br-e4726b4d1a2d" counter packets 157 bytes 13188 jump DOCKER-ISOLATION-STAGE-2
                iifname "br-8a1207fb6cae" oifname != "br-8a1207fb6cae" counter packets 633 bytes 850954 jump DOCKER-ISOLATION-STAGE-2
                iifname "br-6281c5fa18c5" oifname != "br-6281c5fa18c5" counter packets 0 bytes 0 jump DOCKER-ISOLATION-STAGE-2
                iifname "br-498c5f61477f" oifname != "br-498c5f61477f" counter packets 29 bytes 4812 jump DOCKER-ISOLATION-STAGE-2
                counter packets 5108 bytes 2287968 return
        }

        chain DOCKER-ISOLATION-STAGE-2 {
                oifname "docker0" counter packets 0 bytes 0 drop
                oifname "br-e4726b4d1a2d" counter packets 0 bytes 0 drop
                oifname "br-8a1207fb6cae" counter packets 0 bytes 0 drop
                oifname "br-6281c5fa18c5" counter packets 0 bytes 0 drop
                oifname "br-498c5f61477f" counter packets 0 bytes 0 drop
                counter packets 819 bytes 868954 return
        }

        chain FORWARD {
                type filter hook forward priority filter; policy drop;
                counter packets 5108 bytes 2287968 jump DOCKER-USER
                counter packets 5108 bytes 2287968 jump DOCKER-ISOLATION-STAGE-1
                oifname "docker0" ct state related,established counter packets 0 bytes 0 accept
                oifname "docker0" counter packets 0 bytes 0 jump DOCKER
                iifname "docker0" oifname != "docker0" counter packets 0 bytes 0 accept
                iifname "docker0" oifname "docker0" counter packets 0 bytes 0 accept
                oifname "br-e4726b4d1a2d" ct state related,established counter packets 1189 bytes 246566 accept
                oifname "br-e4726b4d1a2d" counter packets 7 bytes 420 jump DOCKER
                iifname "br-e4726b4d1a2d" oifname != "br-e4726b4d1a2d" counter packets 157 bytes 13188 accept
                iifname "br-e4726b4d1a2d" oifname "br-e4726b4d1a2d" counter packets 7 bytes 420 accept
                oifname "br-8a1207fb6cae" ct state related,established counter packets 2973 bytes 1136514 accept
                oifname "br-8a1207fb6cae" counter packets 62 bytes 3550 jump DOCKER
                iifname "br-8a1207fb6cae" oifname != "br-8a1207fb6cae" counter packets 633 bytes 850954 accept
                iifname "br-8a1207fb6cae" oifname "br-8a1207fb6cae" counter packets 49 bytes 2940 accept
                oifname "br-6281c5fa18c5" ct state related,established counter packets 14 bytes 1155 accept
                oifname "br-6281c5fa18c5" counter packets 2 bytes 120 jump DOCKER
                iifname "br-6281c5fa18c5" oifname != "br-6281c5fa18c5" counter packets 0 bytes 0 accept
                iifname "br-6281c5fa18c5" oifname "br-6281c5fa18c5" counter packets 2 bytes 120 accept
                oifname "br-498c5f61477f" ct state related,established counter packets 42 bytes 30689 accept
                oifname "br-498c5f61477f" counter packets 0 bytes 0 jump DOCKER
                iifname "br-498c5f61477f" oifname != "br-498c5f61477f" counter packets 29 bytes 4812 accept
                iifname "br-498c5f61477f" oifname "br-498c5f61477f" counter packets 0 bytes 0 accept
        }

        chain DOCKER-USER {
                counter packets 5108 bytes 2287968 jump whalewall
                counter packets 5108 bytes 2287968 return
        }

        chain whalewall {
                ip saddr vmap @whalewall-container-addrs
                ip daddr vmap @whalewall-container-addrs
        }

        chain INPUT {
                type filter hook input priority filter; policy accept;
                counter packets 565 bytes 41798 jump whalewall
        }

        chain OUTPUT {
                type filter hook output priority filter; policy accept;
                counter packets 901 bytes 181592 jump whalewall
        }
}

As you can see all rules are flushed after reboot and whalewall is not creating the existent rules using the database.
I'm using latest docker image with no command options because -d arg is specified in the DockerFile.

So i need to restart the containers two times:

2023-04-18T06:01:32.142113758Z INF undefined | msg=starting whalewall version=devel commit=d36566f6cf5a906b00faa24849ec1719ba7a3f36 
2023-04-18T06:05:50.243257335Z INF undefined | msg=deleting rules container.id=e07638f57f7e container.name=wiki_db 
2023-04-18T06:05:50.519018905Z INF undefined | msg=creating rules container.id=e07638f57f7e container.name=wiki_db 
2023-04-18T06:05:50.571306889Z ERR undefined | msg=error creating rules container.id=e07638f57f7e container.name=wiki_db error=error adding container addr to database: constraint failed: UNIQUE constraint failed: addrs.addr (1555) stacktrace=github.com/capnspacehook/whalewall.(*RuleManager).createRules
	github.com/capnspacehook/whalewall/create.go:63
github.com/capnspacehook/whalewall.(*RuleManager).Start.func1
	github.com/capnspacehook/whalewall/manager.go:118 
2023-04-18T06:05:55.849669739Z INF undefined | msg=deleting rules container.id=e560a3e249a5 container.name=wiki 
2023-04-18T06:05:56.256665527Z INF undefined | msg=creating rules container.id=e560a3e249a5 container.name=wiki 
2023-04-18T06:06:33.468951609Z INF undefined | msg=creating rules container.id=e07638f57f7e container.name=wiki_db 
2023-04-18T06:06:37.418661316Z INF undefined | msg=deleting rules container.id=e560a3e249a5 container.name=wiki 
2023-04-18T06:06:37.83025247Z INF undefined | msg=creating rules container.id=e560a3e249a5 container.name=wiki

@capnspacehook
Copy link
Owner

capnspacehook commented Apr 18, 2023

Hmm that's very weird, haven't noticed any behavior like that before... Can you share a minimal version of your docker compose file that still causes this issue, and test again with debug logging enabled? If you set the command of the whalewall container to '-d /new-data -debug', it will both ensure you're using a new database with the new schema and enable debug logs.

@rgomezceis
Copy link
Author

rgomezceis commented Apr 18, 2023

My docker compose:

version: "3.8"
services:
  whalewall:
    container_name: whalewall
    image: ghcr.io/capnspacehook/whalewall:latest
    restart: unless-stopped
    privileged: true
    command:
      - "-debug"
      - "-d"
      - "/data"
    network_mode: host
    volumes:
      - whalewall_data:/data
      - /var/run/docker.sock:/var/run/docker.sock:ro

volumes:
  whalewall_data:
    name: whalewall_data
    driver: local
    driver_opts:
      o: bind
      device: /mnt/docker/whalewall/volumes/whalewall_data
      type: none

whalewall log before reboot:

2023-04-18T17:39:36.11982076Z INF undefined | msg=applied landlock rules 
2023-04-18T17:39:36.120974528Z INF undefined | msg=applied seccomp filters syscalls.allowed=48 
2023-04-18T17:39:36.120993133Z INF undefined | msg=starting whalewall version=devel commit=d36566f6cf5a906b00faa24849ec1719ba7a3f36 
2023-04-18T17:39:36.125314289Z DBG undefined | msg=creating chain chain.name=INPUT 
2023-04-18T17:39:36.125372008Z DBG undefined | msg=creating chain chain.name=OUTPUT 
2023-04-18T17:40:08.711817004Z INF undefined | msg=creating rules container.id=9c20208483af container.name=wiki_db 
2023-04-18T17:40:08.788182422Z DBG undefined | msg=adding to database container.id=9c20208483af container.name=wiki_db 
2023-04-18T17:40:09.060967133Z INF undefined | msg=creating rules container.id=b332c42f4347 container.name=wiki 
2023-04-18T17:40:09.065707809Z DBG undefined | msg=creating rule container.id=b332c42f4347 container.name=wiki rule=[object Object] 
2023-04-18T17:40:09.11459109Z DBG undefined | msg=adding to database container.id=b332c42f4347 container.name=wiki

nft table before reboot:

table ip filter {
        map whalewall-container-addrs {
                type ipv4_addr : verdict
                elements = { 172.19.0.4 : jump whalewall-wiki-b332c42f4347, 172.20.0.2 : jump whalewall-wiki_db-9c20208483af,
                             172.20.0.3 : jump whalewall-wiki-b332c42f4347 }
        }

        chain DOCKER {
                iifname != "br-8a1207fb6cae" oifname "br-8a1207fb6cae" meta l4proto tcp ip daddr 172.18.0.2 tcp dport 9000 counter packets 12 bytes 558 accept
                iifname != "br-498c5f61477f" oifname "br-498c5f61477f" meta l4proto tcp ip daddr 172.19.0.2 tcp dport 443 counter packets 0 bytes 0 accept
                iifname != "br-498c5f61477f" oifname "br-498c5f61477f" meta l4proto tcp ip daddr 172.19.0.2 tcp dport 80 counter packets 0 bytes 0 accept
        }

        chain DOCKER-ISOLATION-STAGE-1 {
                iifname "docker0" oifname != "docker0" counter packets 0 bytes 0 jump DOCKER-ISOLATION-STAGE-2
                iifname "br-e4726b4d1a2d" oifname != "br-e4726b4d1a2d" counter packets 0 bytes 0 jump DOCKER-ISOLATION-STAGE-2
                iifname "br-8a1207fb6cae" oifname != "br-8a1207fb6cae" counter packets 784 bytes 1123412 jump DOCKER-ISOLATION-STAGE-2
                iifname "br-6281c5fa18c5" oifname != "br-6281c5fa18c5" counter packets 0 bytes 0 jump DOCKER-ISOLATION-STAGE-2
                iifname "br-498c5f61477f" oifname != "br-498c5f61477f" counter packets 30 bytes 4864 jump DOCKER-ISOLATION-STAGE-2
                counter packets 4148 bytes 2595535 return
        }

        chain DOCKER-ISOLATION-STAGE-2 {
                oifname "docker0" counter packets 0 bytes 0 drop
                oifname "br-e4726b4d1a2d" counter packets 0 bytes 0 drop
                oifname "br-8a1207fb6cae" counter packets 0 bytes 0 drop
                oifname "br-6281c5fa18c5" counter packets 0 bytes 0 drop
                oifname "br-498c5f61477f" counter packets 0 bytes 0 drop
                counter packets 814 bytes 1128276 return
        }

        chain FORWARD {
                type filter hook forward priority filter; policy drop;
                counter packets 5111 bytes 2738502 jump DOCKER-USER
                counter packets 4148 bytes 2595535 jump DOCKER-ISOLATION-STAGE-1
                oifname "docker0" ct state related,established counter packets 0 bytes 0 accept
                oifname "docker0" counter packets 0 bytes 0 jump DOCKER
                iifname "docker0" oifname != "docker0" counter packets 0 bytes 0 accept
                iifname "docker0" oifname "docker0" counter packets 0 bytes 0 accept
                oifname "br-e4726b4d1a2d" ct state related,established counter packets 1030 bytes 233502 accept
                oifname "br-e4726b4d1a2d" counter packets 7 bytes 420 jump DOCKER
                iifname "br-e4726b4d1a2d" oifname != "br-e4726b4d1a2d" counter packets 0 bytes 0 accept
                iifname "br-e4726b4d1a2d" oifname "br-e4726b4d1a2d" counter packets 7 bytes 420 accept
                oifname "br-8a1207fb6cae" ct state related,established counter packets 2184 bytes 1198263 accept
                oifname "br-8a1207fb6cae" counter packets 51 bytes 2898 jump DOCKER
                iifname "br-8a1207fb6cae" oifname != "br-8a1207fb6cae" counter packets 784 bytes 1123412 accept
                iifname "br-8a1207fb6cae" oifname "br-8a1207fb6cae" counter packets 39 bytes 2340 accept
                oifname "br-6281c5fa18c5" ct state related,established counter packets 14 bytes 1155 accept
                oifname "br-6281c5fa18c5" counter packets 2 bytes 120 jump DOCKER
                iifname "br-6281c5fa18c5" oifname != "br-6281c5fa18c5" counter packets 0 bytes 0 accept
                iifname "br-6281c5fa18c5" oifname "br-6281c5fa18c5" counter packets 2 bytes 120 accept
                oifname "br-498c5f61477f" ct state related,established counter packets 46 bytes 30901 accept
                oifname "br-498c5f61477f" counter packets 0 bytes 0 jump DOCKER
                iifname "br-498c5f61477f" oifname != "br-498c5f61477f" counter packets 30 bytes 4864 accept
                iifname "br-498c5f61477f" oifname "br-498c5f61477f" counter packets 0 bytes 0 accept
        }

        chain DOCKER-USER {
                counter packets 2375 bytes 1260429 jump whalewall
                counter packets 4148 bytes 2595535 return
        }

        chain whalewall {
                ip saddr vmap @whalewall-container-addrs
                ip daddr vmap @whalewall-container-addrs
        }

        chain INPUT {
                type filter hook input priority filter; policy accept;
                counter packets 45 bytes 3432 jump whalewall
        }

        chain OUTPUT {
                type filter hook output priority filter; policy accept;
                counter packets 28 bytes 3024 jump whalewall
        }

        chain whalewall-wiki_db-9c20208483af {
                ip saddr 172.20.0.2 ip daddr 172.20.0.3 tcp sport 5432 ct state established,related counter packets 367 bytes 94004 accept
                counter packets 0 bytes 0 log prefix "whalewall-wiki_db-9c20208483af drop: " level info drop
        }

        chain whalewall-wiki-b332c42f4347 {
                ip saddr 172.20.0.3 ip daddr 172.20.0.2 tcp dport 5432 ct state established,related,new counter packets 586 bytes 48363 accept
                counter packets 10 bytes 600 log prefix "whalewall-wiki-b332c42f4347 drop: " level info drop
        }
}

Now after reboot, the nft table:

table ip filter {
        map whalewall-container-addrs {
                type ipv4_addr : verdict
        }

        chain DOCKER {
                iifname != "br-8a1207fb6cae" oifname "br-8a1207fb6cae" meta l4proto tcp ip daddr 172.18.0.2 tcp dport 9000 counter packets 0 bytes 0 accept
                iifname != "br-498c5f61477f" oifname "br-498c5f61477f" meta l4proto tcp ip daddr 172.19.0.3 tcp dport 443 counter packets 0 bytes 0 accept
                iifname != "br-498c5f61477f" oifname "br-498c5f61477f" meta l4proto tcp ip daddr 172.19.0.3 tcp dport 80 counter packets 0 bytes 0 accept
        }

        chain DOCKER-ISOLATION-STAGE-1 {
                iifname "docker0" oifname != "docker0" counter packets 0 bytes 0 jump DOCKER-ISOLATION-STAGE-2
                iifname "br-e4726b4d1a2d" oifname != "br-e4726b4d1a2d" counter packets 0 bytes 0 jump DOCKER-ISOLATION-STAGE-2
                iifname "br-8a1207fb6cae" oifname != "br-8a1207fb6cae" counter packets 9 bytes 1109 jump DOCKER-ISOLATION-STAGE-2
                iifname "br-6281c5fa18c5" oifname != "br-6281c5fa18c5" counter packets 0 bytes 0 jump DOCKER-ISOLATION-STAGE-2
                iifname "br-498c5f61477f" oifname != "br-498c5f61477f" counter packets 29 bytes 4812 jump DOCKER-ISOLATION-STAGE-2
                counter packets 1183 bytes 381676 return
        }

        chain DOCKER-ISOLATION-STAGE-2 {
                oifname "docker0" counter packets 0 bytes 0 drop
                oifname "br-e4726b4d1a2d" counter packets 0 bytes 0 drop
                oifname "br-8a1207fb6cae" counter packets 0 bytes 0 drop
                oifname "br-6281c5fa18c5" counter packets 0 bytes 0 drop
                oifname "br-498c5f61477f" counter packets 0 bytes 0 drop
                counter packets 38 bytes 5921 return
        }

        chain FORWARD {
                type filter hook forward priority filter; policy drop;
                counter packets 1183 bytes 381676 jump DOCKER-USER
                counter packets 1183 bytes 381676 jump DOCKER-ISOLATION-STAGE-1
                oifname "docker0" ct state related,established counter packets 0 bytes 0 accept
                oifname "docker0" counter packets 0 bytes 0 jump DOCKER
                iifname "docker0" oifname != "docker0" counter packets 0 bytes 0 accept
                iifname "docker0" oifname "docker0" counter packets 0 bytes 0 accept
                oifname "br-e4726b4d1a2d" ct state related,established counter packets 980 bytes 230208 accept
                oifname "br-e4726b4d1a2d" counter packets 7 bytes 420 jump DOCKER
                iifname "br-e4726b4d1a2d" oifname != "br-e4726b4d1a2d" counter packets 0 bytes 0 accept
                iifname "br-e4726b4d1a2d" oifname "br-e4726b4d1a2d" counter packets 7 bytes 420 accept
                oifname "br-8a1207fb6cae" ct state related,established counter packets 95 bytes 112889 accept
                oifname "br-8a1207fb6cae" counter packets 2 bytes 120 jump DOCKER
                iifname "br-8a1207fb6cae" oifname != "br-8a1207fb6cae" counter packets 9 bytes 1109 accept
                iifname "br-8a1207fb6cae" oifname "br-8a1207fb6cae" counter packets 2 bytes 120 accept
                oifname "br-6281c5fa18c5" ct state related,established counter packets 14 bytes 1155 accept
                oifname "br-6281c5fa18c5" counter packets 2 bytes 120 jump DOCKER
                iifname "br-6281c5fa18c5" oifname != "br-6281c5fa18c5" counter packets 0 bytes 0 accept
                iifname "br-6281c5fa18c5" oifname "br-6281c5fa18c5" counter packets 2 bytes 120 accept
                oifname "br-498c5f61477f" ct state related,established counter packets 45 bytes 30843 accept
                oifname "br-498c5f61477f" counter packets 0 bytes 0 jump DOCKER
                iifname "br-498c5f61477f" oifname != "br-498c5f61477f" counter packets 29 bytes 4812 accept
                iifname "br-498c5f61477f" oifname "br-498c5f61477f" counter packets 0 bytes 0 accept
        }

        chain DOCKER-USER {
                counter packets 1183 bytes 381676 jump whalewall
                counter packets 1183 bytes 381676 return
        }

        chain whalewall {
                ip saddr vmap @whalewall-container-addrs
                ip daddr vmap @whalewall-container-addrs
        }

        chain INPUT {
                type filter hook input priority filter; policy accept;
                counter packets 50 bytes 6866 jump whalewall
        }

        chain OUTPUT {
                type filter hook output priority filter; policy accept;
                counter packets 43 bytes 8787 jump whalewall
        }
}

And whalewall log (including log entries before reboot):

2023-04-18T17:39:36.11982076Z INF undefined | msg=applied landlock rules 
2023-04-18T17:39:36.120974528Z INF undefined | msg=applied seccomp filters syscalls.allowed=48 
2023-04-18T17:39:36.120993133Z INF undefined | msg=starting whalewall version=devel commit=d36566f6cf5a906b00faa24849ec1719ba7a3f36 
2023-04-18T17:39:36.125314289Z DBG undefined | msg=creating chain chain.name=INPUT 
2023-04-18T17:39:36.125372008Z DBG undefined | msg=creating chain chain.name=OUTPUT 
2023-04-18T17:40:08.711817004Z INF undefined | msg=creating rules container.id=9c20208483af container.name=wiki_db 
2023-04-18T17:40:08.788182422Z DBG undefined | msg=adding to database container.id=9c20208483af container.name=wiki_db 
2023-04-18T17:40:09.060967133Z INF undefined | msg=creating rules container.id=b332c42f4347 container.name=wiki 
2023-04-18T17:40:09.065707809Z DBG undefined | msg=creating rule container.id=b332c42f4347 container.name=wiki rule=[object Object] 
2023-04-18T17:40:09.11459109Z DBG undefined | msg=adding to database container.id=b332c42f4347 container.name=wiki 
2023-04-18T17:42:44.255708663Z INF undefined | msg=shutting down 
2023-04-18T17:43:16.206933344Z INF undefined | msg=applied landlock rules 
2023-04-18T17:43:16.207532818Z INF undefined | msg=applied seccomp filters syscalls.allowed=48 
2023-04-18T17:43:16.207546234Z INF undefined | msg=starting whalewall version=devel commit=d36566f6cf5a906b00faa24849ec1719ba7a3f36 
2023-04-18T17:43:17.824009057Z DBG undefined | msg=creating chain chain.name=INPUT 
2023-04-18T17:43:17.82407979Z DBG undefined | msg=creating chain chain.name=OUTPUT

@capnspacehook
Copy link
Owner

Thanks, but to reproduce your issue I need the docker compose details for your 'wiki' and 'wiki_db' services. Redact any personal info if it exists in the config as long as it doesn't change the behavior with whalewall.

@rgomezceis
Copy link
Author

rgomezceis commented Apr 18, 2023

Here you go (you can ommit traefik labels and just publish port 3000 tcp but u should use mapped_ports config from whalewall):

version: "3.8"

services:
  db:
    container_name: wiki_db
    image: postgres:14.5-alpine
    restart: unless-stopped
    environment:
      POSTGRES_DB: wiki
      POSTGRES_USER: wikijs
      POSTGRES_PASSWORD: somepassword
    networks:
      wiki_network:
    volumes:
      - wiki_db_data:/var/lib/postgresql/data
    labels:
      # WHALEWALL
      whalewall.enabled: true

  wiki:
    container_name: wiki
    image: ghcr.io/dti-ceis/wiki:latest
    restart: unless-stopped
    depends_on:
      - db
    environment:
      DB_TYPE: postgres
      DB_HOST: db
      DB_PORT: 5432
      DB_NAME: wiki
      DB_USER: wikijs
      DB_PASS: somepassword
    networks:
      wiki_network:
      public:
    volumes:
      - wiki_data:/wiki/data/content
    labels:
      # WHALEWALL
      whalewall.enabled: true
      whalewall.rules: |
        output:
          # Permitir a la base de datos
          - network: wiki_network
            container: wiki_db
            proto: tcp
            port: 5432
      # TRAEFIK
      traefik.enable: true
      traefik.http.routers.wiki.rule: "Host(`wikidev.iamruben.local`)"
      traefik.http.routers.wiki.entrypoints: "web,websecure"
      traefik.http.routers.wiki.tls: true
      traefik.http.services.wiki.loadbalancer.server.port: 3000
      traefik.http.routers.wiki.service: "wiki"

networks:
  wiki_network:
    external: true
  public:
    external: true

volumes:
  wiki_data:
    name: wiki_data
    driver: local
    driver_opts:
      o: bind
      device: /mnt/docker/wiki/volumes/wiki_data
      type: none
  wiki_db_data:
    name: wiki_db_data
    driver: local
    driver_opts:
      o: bind
      device: /mnt/docker/wiki/volumes/wiki_db_data
      type: none

@capnspacehook
Copy link
Owner

capnspacehook commented Apr 18, 2023

Hmm, maybe part of the problem is that I've never tested with external networks, didn't know they existed before now. How did you create the wiki_network and public networks? Are external networks Docker networks not created by Docker Compose?

@rgomezceis
Copy link
Author

From command line or portainer.

docker network create -d bridge wiki_network
docker network create -d bridge public

@capnspacehook
Copy link
Owner

Thanks for troubleshooting for so long with me, I may have finally fixed everything. Pull the latest image and try again please, whalewall should recreate rules properly even after a reboot. If you're interested as to what the problem was, I detailed it in this commit message: 6515da8

@rgomezceis
Copy link
Author

rgomezceis commented Apr 19, 2023

Thanks for fix that. Now seems to be working, but when using traefik container as a reverse proxy and reboot the machine, whalewall fails to create some rules and I need to restart traefik container to recreate the rules.

Seems whalewall detects traefik container as new but it isn't.

Compose file of traefik:

version: "3.8"

services:
  traefik:
    container_name: traefik
    image: traefik:latest
    restart: unless-stopped
    environment:
      - TRAEFIK_ENTRYPOINTS_WEB_ADDRESS=:80
      - TRAEFIK_ENTRYPOINTS_WEBSECURE_ADDRESS=:443
      - TRAEFIK_ENTRYPOINTS_WEB_HTTP_REDIRECTIONS_ENTRYPOINT_TO=websecure
      - TRAEFIK_ENTRYPOINTS_WEB_HTTP_REDIRECTIONS_ENTRYPOINT_SCHEME=https
      - TRAEFIK_ENTRYPOINTS_WEB_HTTP_REDIRECTIONS_ENTRYPOINT_PERMANENT=true
      - TRAEFIK_PROVIDERS_DOCKER=true
      - TRAEFIK_PROVIDERS_DOCKER_EXPOSEDBYDEFAULT=false
      - TRAEFIK_PROVIDERS_DOCKER_NETWORK=public
      - TRAEFIK_API_DASHBOARD=true
      - TRAEFIK_LOG_LEVEL=info
      - TZ=Europe/Madrid
    ports:
      - target: 80
        published: 80
        protocol: tcp
        mode: host
      - target: 443
        published: 443
        protocol: tcp
        mode: host
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock:ro"
    networks:
      public:
    labels:
      #WHALEWALL
      whalewall.enabled: true
      whalewall.rules: |
        mapped_ports:
          external:
            allow: true
        output:
          - network: public
            container: portainer
            proto: tcp
            port: 9000
          - network: public
            container: wiki
            proto: tcp
            port: 3000
          # Permite el trafico HTTP
          - log_prefix: "http"
            proto: tcp
            port: 80
          # Permite el trafico HTTPS
          - log_prefix: "https"
            proto: tcp
            port: 443
      # TRAEFIK
      traefik.enable: true
      traefik.http.routers.traefik.entrypoints: "web,websecure"
      traefik.http.routers.traefik.tls: true
      traefik.http.routers.traefik.rule: "Host(`traefikdev.iamruben.local`)"
      traefik.http.routers.traefik.service: "api@internal"
      traefik.http.services.traefik.loadbalancer.server.port: 8080

networks:
  public:
    external: true

Before restart traefik container:

2023-04-19T06:15:30.2662584Z INF undefined | msg=applied landlock rules 
2023-04-19T06:15:30.266763006Z INF undefined | msg=applied seccomp filters syscalls.allowed=48 
2023-04-19T06:15:30.266776769Z INF undefined | msg=starting whalewall version=devel commit=6515da855aa3529560e877af3e7b151fa048096f 
2023-04-19T06:15:31.925579204Z INF undefined | msg=creating rules container.id=1989b04f3b1a container.name=wiki_db container.is_new=false 
2023-04-19T06:15:31.992739438Z INF undefined | msg=creating rules container.id=f0c817abe650 container.name=wiki container.is_new=false 
2023-04-19T06:15:32.040634863Z INF undefined | msg=creating rules container.id=f5dce7cca5b7 container.name=portainer_agent container.is_new=false 
2023-04-19T06:15:32.042937391Z INF undefined | msg=creating rules container.id=ba534fb07e8e container.name=traefik container.is_new=true 
2023-04-19T06:15:32.066852959Z ERR undefined | msg=error creating output rules container.id=ba534fb07e8e container.name=traefik error=conn.Receive: netlink receive: no such file or directory stacktrace=github.com/capnspacehook/whalewall.(*RuleManager).createContainerRules
	github.com/capnspacehook/whalewall/create.go:244
github.com/capnspacehook/whalewall.(*RuleManager).createRules
	github.com/capnspacehook/whalewall/create.go:62
github.com/capnspacehook/whalewall.(*RuleManager).Start.func1
	github.com/capnspacehook/whalewall/manager.go:123 
2023-04-19T06:15:32.067495761Z ERR undefined | msg=error creating rules container.id=ba534fb07e8e container.name=traefik error=error adding container addr to database: constraint failed: UNIQUE constraint failed: addrs.addr (1555) stacktrace=github.com/capnspacehook/whalewall.(*RuleManager).createRules
	github.com/capnspacehook/whalewall/create.go:63
github.com/capnspacehook/whalewall.(*RuleManager).Start.func1
	github.com/capnspacehook/whalewall/manager.go:123 
2023-04-19T06:15:32.067519563Z INF undefined | msg=creating rules container.id=77f5c1fab2b0 container.name=portainer container.is_new=false 

After restart traefik container (rules created and working properly):

2023-04-19T06:16:45.758924262Z INF undefined | msg=not deleting container that isn't in database container.id=ba534fb07e8e 
2023-04-19T06:16:46.088931279Z INF undefined | msg=creating rules container.id=ba534fb07e8e container.name=traefik container.is_new=true 
2023-04-19T06:16:46.109140661Z ERR undefined | msg=error creating rules container.id=ba534fb07e8e container.name=traefik error=error adding container addr to database: constraint failed: UNIQUE constraint failed: addrs.addr (1555) stacktrace=github.com/capnspacehook/whalewall.(*RuleManager).createRules
	github.com/capnspacehook/whalewall/create.go:63
github.com/capnspacehook/whalewall.(*RuleManager).Start.func1
	github.com/capnspacehook/whalewall/manager.go:123

I don't know what is the meaning of this error conn.Receive: netlink receive: no such file or directory

Also, I think -clear flag is not working properly (not flushing nft table).

Thanks in advance.

@capnspacehook
Copy link
Owner

It looks like adding the traefik container to the database fails because one of its IP addresses is already in the database somehow. That's why whalewall always thinks it's new, because it was never added to the database. That's also why traefik's rules are cleared, because whalewall doesn't remember it as it isn't in the database.

Can you send the full docker compose file with all containers that use whalewall?

Good to know the previous issues are solved at least.

@capnspacehook capnspacehook added the question Further information is requested label May 14, 2023
@rgomezceis
Copy link
Author

rgomezceis commented May 19, 2023

Try to restart traefik container and reboot host machine to reproduce the error.
When host machine starts whalewall throws this error when creating traefik rules:

INF | msg=creating rules container.id=54ff108fcdab container.name=traefik container.is_new=false 
ERR | msg=error creating output rules container.id=54ff108fcdab container.name=traefik error=conn.Receive: netlink receive: no such file or directory stacktrace=github.com/capnspacehook/whalewall.(*RuleManager).createContainerRules
	github.com/capnspacehook/whalewall/create.go:288
github.com/capnspacehook/whalewall.(*RuleManager).createRules
	github.com/capnspacehook/whalewall/create.go:62
github.com/capnspacehook/whalewall.(*RuleManager).Start.func1
	github.com/capnspacehook/whalewall/manager.go:118 

Workaround is restart traefik container to recreate the rules as a "new" container:

INF | msg=deleting rules container.id=54ff108fcdab container.name=traefik 
INF | msg=creating rules container.id=54ff108fcdab container.name=traefik container.is_new=true

Below the environment details:

  1. Create networks using:
docker network create -d bridge public
docker network create -d bridge wiki_network
  1. Traefik stack:
version: "3.8"

services:
  traefik:
    container_name: traefik
    image: traefik:latest
    restart: unless-stopped
    environment:
      - TRAEFIK_ENTRYPOINTS_WEB_ADDRESS=:80
      - TRAEFIK_ENTRYPOINTS_WEBSECURE_ADDRESS=:443
      - TRAEFIK_ENTRYPOINTS_WEB_HTTP_REDIRECTIONS_ENTRYPOINT_TO=websecure
      - TRAEFIK_ENTRYPOINTS_WEB_HTTP_REDIRECTIONS_ENTRYPOINT_SCHEME=https
      - TRAEFIK_ENTRYPOINTS_WEB_HTTP_REDIRECTIONS_ENTRYPOINT_PERMANENT=true
      - TRAEFIK_PROVIDERS_DOCKER=true
      - TRAEFIK_PROVIDERS_DOCKER_EXPOSEDBYDEFAULT=false
      - TRAEFIK_PROVIDERS_DOCKER_NETWORK=public
      - TRAEFIK_API_DASHBOARD=true
      - TRAEFIK_LOG_LEVEL=info
      - TZ=Europe/Madrid
    ports:
      - target: 80
        published: 80
        protocol: tcp
        mode: host
      - target: 443
        published: 443
        protocol: tcp
        mode: host
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock:ro"
    networks:
      public:
    labels:
      # WHALEWALL
      whalewall.enabled: true
      whalewall.rules: |
        mapped_ports:
          external:
            allow: true
        output:
          - network: public
            container: wiki
            proto: tcp
            port: 3000
          - log_prefix: "http"
            proto: tcp
            port: 80
          - log_prefix: "https"
            proto: tcp
            port: 443
      # TRAEFIK
      traefik.enable: true
      traefik.http.routers.traefik.entrypoints: "web,websecure"
      traefik.http.routers.traefik.tls: true
      traefik.http.routers.traefik.rule: "Host(`traefik.iamruben.local`)"
      traefik.http.routers.traefik.service: "api@internal"
      traefik.http.services.traefik.loadbalancer.server.port: 8080

networks:
  public:
    external: true
  1. Wiki stack:
version: "3.8"

services:
  db:
    container_name: wiki_db
    image: postgres:14.5-alpine
    restart: unless-stopped
    environment:
      POSTGRES_DB: wiki
      POSTGRES_USER: wikijs
      POSTGRES_PASSWORD: somepassword
    networks:
      wiki_network:
    volumes:
      - wiki_db_data:/var/lib/postgresql/data
    labels:
      # WHALEWALL
      whalewall.enabled: true

  wiki:
    container_name: wiki
    image: requarks/wiki
    restart: unless-stopped
    depends_on:
      - db
    environment:
      DB_TYPE: postgres
      DB_HOST: db
      DB_PORT: 5432
      DB_NAME: wiki
      DB_USER: wikijs
      DB_PASS: somepassword
    networks:
      wiki_network:
      public:
    volumes:
      - wiki_data:/wiki/data/content
    labels:
      # WHALEWALL
      whalewall.enabled: true
      whalewall.rules: |
         output:
          - network: wiki_network
            container: wiki_db
            proto: tcp
            port: 5432
      # TRAEFIK
      traefik.enable: true
      traefik.http.routers.wiki.rule: "Host(`wiki.iamruben.local`)"
      traefik.http.routers.wiki.entrypoints: "web,websecure"
      traefik.http.routers.wiki.tls: true
      traefik.http.services.wiki.loadbalancer.server.port: 3000
      traefik.http.routers.wiki.service: "wiki"

networks:
  wiki_network:
    external: true
  public:
    external: true

volumes:
  wiki_data:
  wiki_db_data:

@capnspacehook
Copy link
Owner

I tried to reproduce with your exact Compose configs and did find and fix a bug related to what you mentioned, but I'm not sure if it's the exact issue your described. Can you test again please? Note that you'll have to update the whalewall configs slightly since I've added and renamed port related fields to support multiple source and destination ports per rule, see the README for details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants