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

Remove localhost calls from external names #7092

Merged
merged 1 commit into from
Apr 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ jobs:

strategy:
matrix:
k8s: [v1.16.15, v1.17.17, v1.18.19, v1.19.11, v1.20.7, v1.21.0]
k8s: [v1.16.15, v1.17.17, v1.18.15, v1.19.7, v1.20.2]

steps:

Expand Down
6 changes: 6 additions & 0 deletions internal/ingress/controller/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ func getEndpoints(s *corev1.Service, port *corev1.ServicePort, proto corev1.Prot

// ExternalName services
if s.Spec.Type == corev1.ServiceTypeExternalName {
if ip := net.ParseIP(s.Spec.ExternalName); s.Spec.ExternalName == "localhost" ||
rikatz marked this conversation as resolved.
Show resolved Hide resolved
(ip != nil && ip.IsLoopback()) {
klog.Errorf("Invalid attempt to use localhost name %s in %q", s.Spec.ExternalName, svcKey)
return upsServers
}

klog.V(3).Infof("Ingress using Service %q of type ExternalName.", svcKey)
targetPort := port.TargetPort.IntValue()
// if the externalName is not an IP address we need to validate is a valid FQDN
Expand Down
48 changes: 48 additions & 0 deletions internal/ingress/controller/endpoints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,54 @@ func TestGetEndpoints(t *testing.T) {
},
[]ingress.Endpoint{},
},
{
"a service type ServiceTypeExternalName service with localhost in name should return 0 endpoint",
&corev1.Service{
Spec: corev1.ServiceSpec{
Type: corev1.ServiceTypeExternalName,
ExternalName: "localhost",
Ports: []corev1.ServicePort{
{
Name: "default",
TargetPort: intstr.FromInt(443),
},
},
},
},
&corev1.ServicePort{
Name: "default",
TargetPort: intstr.FromInt(80),
},
corev1.ProtocolTCP,
func(string) (*corev1.Endpoints, error) {
return &corev1.Endpoints{}, nil
},
[]ingress.Endpoint{},
},
{
"a service type ServiceTypeExternalName service with 127.0.0.1 in name should return 0 endpoint",
&corev1.Service{
Spec: corev1.ServiceSpec{
Type: corev1.ServiceTypeExternalName,
ExternalName: "127.0.0.1",
Ports: []corev1.ServicePort{
{
Name: "default",
TargetPort: intstr.FromInt(443),
},
},
},
},
&corev1.ServicePort{
Name: "default",
TargetPort: intstr.FromInt(80),
},
corev1.ProtocolTCP,
func(string) (*corev1.Endpoints, error) {
return &corev1.Endpoints{}, nil
},
[]ingress.Endpoint{},
},
{
"a service type ServiceTypeExternalName with a valid port should return one endpoint",
&corev1.Service{
Expand Down
8 changes: 8 additions & 0 deletions rootfs/etc/nginx/lua/balancer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ local IMPLEMENTATIONS = {
ewma = ewma,
}

local PROHIBITED_LOCALHOST_PORT = configuration.prohibited_localhost_port or '10246'
local PROHIBITED_PEER_PATTERN = "^127.*:" .. PROHIBITED_LOCALHOST_PORT .. "$"

local _M = {}
local balancers = {}
local backends_with_external_name = {}
Expand Down Expand Up @@ -317,6 +320,11 @@ function _M.balance()
return
end

if peer:match(PROHIBITED_PEER_PATTERN) then
ngx.log(ngx.ERR, "attempted to proxy to self, balancer: ", balancer.name, ", peer: ", peer)
return
end

ngx_balancer.set_more_tries(1)

local ok, err = ngx_balancer.set_current_peer(peer)
Expand Down
8 changes: 8 additions & 0 deletions rootfs/etc/nginx/lua/tcp_udp_balancer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ local IMPLEMENTATIONS = {
round_robin = round_robin
}

local PROHIBITED_LOCALHOST_PORT = configuration.prohibited_localhost_port or '10246'
local PROHIBITED_PEER_PATTERN = "^127.*:" .. PROHIBITED_LOCALHOST_PORT .. "$"

local _M = {}
local balancers = {}
local backends_with_external_name = {}
Expand Down Expand Up @@ -181,6 +184,11 @@ function _M.balance()
return
end

if peer:match(PROHIBITED_PEER_PATTERN) then
ngx.log(ngx.ERR, "attempted to proxy to self, balancer: ", balancer.name, ", peer: ", peer)
return
end

ngx_balancer.set_more_tries(1)

local ok, err = ngx_balancer.set_current_peer(peer)
Expand Down
3 changes: 3 additions & 0 deletions rootfs/etc/nginx/template/nginx.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ http {
error("require failed: " .. tostring(res))
else
configuration = res
configuration.prohibited_localhost_port = '{{ .StatusPort }}'
end

ok, res = pcall(require, "balancer")
Expand Down Expand Up @@ -713,6 +714,8 @@ stream {
error("require failed: " .. tostring(res))
else
tcp_udp_configuration = res
tcp_udp_configuration.prohibited_localhost_port = '{{ .StatusPort }}'

end

ok, res = pcall(require, "tcp_udp_balancer")
Expand Down