【问题标题】:Why is Jenkins on the docker host responding to HTTP requests from inside containers?为什么 docker 主机上的 Jenkins 会响应来自容器内部的 HTTP 请求?
【发布时间】:2017-05-30 13:46:41
【问题描述】:

我在安装了 Jenkins 和 Docker 的机器上遇到了一些相当奇怪的行为。为清楚起见,Jenkins 不是作为 Docker 容器运行,而是在 jenkins 用户下运行。

在容器中运行 curl 时,我得到 403:

root@ada71c8116bf:/# curl -I www.google.co.uk
HTTP/1.1 403 Forbidden
Date: Tue, 30 May 2017 13:41:07 GMT
X-Content-Type-Options: nosniff
Set-Cookie: JSESSIONID.f1223778=36hjq9sozhveoe1bfsss1dnq;Path=/;HttpOnly
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Content-Type: text/html;charset=UTF-8
X-Hudson: 1.395
X-Jenkins: 2.46.3
X-Jenkins-Session: 2836b130
X-You-Are-Authenticated-As: anonymous
X-You-Are-In-Group-Disabled: JENKINS-39402: use -Dhudson.security.AccessDeniedException2.REPORT_GROUP_HEADERS=true or use /whoAmI to diagnose
X-Required-Permission: hudson.model.Hudson.Read
X-Permission-Implied-By: hudson.security.Permission.GenericRead
X-Permission-Implied-By: hudson.model.Hudson.Administer
Content-Length: 793
Server: Jetty(9.2.z-SNAPSHOT)

在主机上的容器之外,我得到了预期的响应:

$ curl -I www.google.co.uk
HTTP/1.1 200 OK
Date: Tue, 30 May 2017 13:40:17 GMT
Expires: -1
Cache-Control: private, max-age=0
Content-Type: text/html; charset=ISO-8859-1
P3P: CP="This is not a P3P policy! See https://www.google.com/support/accounts/answer/151657?hl=en for more info."
Server: gws
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
Set-Cookie: NID=104=mMKjBy002X3N_SkhkD_8xuAwpFuw03CFi0iOJjNX81FUHfMT6qTq95LcgRwdhrV_GZoUF9LQ1B9qAQPriN9Er3Bu2JWoqPgvt16TduuVj5QsNs9GiJTQBtaSXWic7G9E; expires=Wed, 29-Nov-2017 13:40:17 GMT; path=/; domain=.google.co.uk; HttpOnly
Transfer-Encoding: chunked
Accept-Ranges: none
Vary: Accept-Encoding

Jenkins 显然是罪魁祸首,但我不知道为什么它会拦截离开容器的 HTTP 流量。 ping Google 工作正常,发送 HTTPS 请求也是如此。没有其他机器有这个问题(大概是因为他们没有安装 Jenkins)。那么,这里发生了什么?如何让 Jenkins 停止拦截来自 Docker 容器的 HTTP?

更新

关闭 Jenkins 的“防止跨站点请求伪造攻击”选项会导致 Jenkins 不再返回 403。相反,Jenkins 使用仪表板页面(即默认页面)响应来自容器内的任何 HTTP 请求。

另外值得注意的是 DNS 工作正常;主机名被解析为正确的 IP 地址。

我要离开 Wireshark。

【问题讨论】:

    标签: http docker jenkins url-routing intercept


    【解决方案1】:

    通过使用 Wireshark,我发现某些东西正在将 HTTP 流量重定向到主机上的端口 8090。幸运的谷歌让我检查了主机的 IP 表 (iptables -t nat -L -n),果然有规则将所有端口 80 流量从任何地方重定向到主机的端口 8090。为了 Jenkins 用户的利益,有人明确设置了此重定向。

    解决方案是更改 IP 表以不重定向来自 docker 子网的流量。

    之前的表格:

    $ sudo iptables -t nat -L -n 
    Chain PREROUTING (policy ACCEPT)
    target     prot opt source               destination              
    REDIRECT   tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:80 redir ports 8090
    DOCKER     all  --  0.0.0.0/0            0.0.0.0/0            ADDRTYPE match dst-type LOCAL
    
    Chain INPUT (policy ACCEPT)
    target     prot opt source               destination         
    
    Chain OUTPUT (policy ACCEPT)
    target     prot opt source               destination         
    REDIRECT   tcp  --  0.0.0.0/0            127.0.0.1            tcp dpt:80 redir ports 8090
    DOCKER     all  --  0.0.0.0/0           !127.0.0.0/8          ADDRTYPE match dst-type LOCAL
    
    Chain POSTROUTING (policy ACCEPT)
    target     prot opt source               destination         
    MASQUERADE  all  --  172.17.0.0/16        0.0.0.0/0           
    
    Chain DOCKER (2 references)
    target     prot opt source               destination         
    RETURN     all  --  0.0.0.0/0            0.0.0.0/0   
    

    要修改的命令:

    $ sudo iptables -t nat -R PREROUTING 1 ! -s 172.17.0.0/16 -p tcp -m tcp --dport 80 -j REDIRECT --to-ports 8090
    $ sudo iptables -t nat -R OUTPUT 1 ! -s 172.17.0.0/16 -d 127.0.0.1/32 -p tcp -m tcp --dport 80 -j REDIRECT --to-ports 8090
    

    生成的 IP 表:

    $ sudo iptables -t nat -L -n
    Chain PREROUTING (policy ACCEPT)
    target     prot opt source               destination         
    REDIRECT   tcp  -- !172.17.0.0/16        0.0.0.0/0            tcp dpt:80 redir ports 8090
    DOCKER     all  --  0.0.0.0/0            0.0.0.0/0            ADDRTYPE match dst-type LOCAL
    
    Chain INPUT (policy ACCEPT)
    target     prot opt source               destination         
    
    Chain OUTPUT (policy ACCEPT)
    target     prot opt source               destination         
    REDIRECT   tcp  -- !172.17.0.0/16        127.0.0.1            tcp dpt:80 redir ports 8090
    DOCKER     all  --  0.0.0.0/0           !127.0.0.0/8          ADDRTYPE match dst-type LOCAL
    
    Chain POSTROUTING (policy ACCEPT)
    target     prot opt source               destination         
    MASQUERADE  all  --  172.17.0.0/16        0.0.0.0/0           
    
    Chain DOCKER (2 references)
    target     prot opt source               destination         
    RETURN     all  --  0.0.0.0/0            0.0.0.0/0    
    

    【讨论】:

    • 你在这里没有得到太多回应,所以我想说你的回答已经解决了我从今天到你发布它的那一刻之间的类似问题。谢谢!
    猜你喜欢
    • 2017-05-01
    • 2022-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多