-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsanitychecks.go
159 lines (132 loc) · 4.29 KB
/
sanitychecks.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
Copyright 2017 Tuenti Technologies S.L. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"fmt"
"log"
"net"
"time"
"github.com/achanda/go-sysctl"
"k8s.io/client-go/pkg/api/v1"
)
const (
addressesExpiration = 5 * time.Second
ephemeralPortsRangeSysKey = "net.ipv4.ip_local_port_range"
nonLocalBindSysKey = "net.ipv4.ip_nonlocal_bind"
)
type ServiceValidator interface {
ValidateService(*v1.Service) error
}
var (
sanityChecks []ServiceValidator
)
func ValidateService(s *v1.Service) error {
for _, check := range sanityChecks {
if err := check.ValidateService(s); err != nil {
return err
}
}
return nil
}
type EphemeralPortsRange struct {
check bool
LowPort int32
HighPort int32
}
// Provide the range as a String
func (r EphemeralPortsRange) String() string {
return fmt.Sprintf("%d->%d", r.LowPort, r.HighPort)
}
// Return error if any of the ports is in the ephemeral ports range, otherwise return nil (also if the check is disabled)
func (r EphemeralPortsRange) ValidateService(s *v1.Service) error {
// Check is disabled
if !r.check {
return nil
}
for _, port := range s.Spec.Ports {
// Port found in range
if (port.Port >= r.LowPort) && (port.Port <= r.HighPort) {
return fmt.Errorf("service %s in %s Service Port %d is in the ephemeral ports range (%s), skipping it. Please check your configuration!", s.Name, s.Namespace, port.Port, r)
}
}
// None of the ports in range
return nil
}
// Retrieve the data from sysctl and return the values, disable the check if unsuccessful and log
func initEphemeralPortsRangeCheck() *EphemeralPortsRange {
var l, h int32
r, err := sysctl.Get(ephemeralPortsRangeSysKey)
if err != nil {
log.Printf("Error reading %s from sysctl: %s, skipping ephemeral ports range checks", ephemeralPortsRangeSysKey, err)
return &EphemeralPortsRange{check: false, LowPort: 0, HighPort: 0}
}
fmt.Sscanf(r, "%d %d", &l, &h)
return &EphemeralPortsRange{check: true, LowPort: l, HighPort: h}
}
func init() {
sanityChecks = append(sanityChecks, initEphemeralPortsRangeCheck())
}
type AddressForLoadBalancerIP struct {
checkLocalBind bool
interfaceAddresses []net.Addr
addressesTime time.Time
}
func (a *AddressForLoadBalancerIP) addresses() ([]net.Addr, error) {
if a.addressesTime.IsZero() || time.Since(a.addressesTime) > addressesExpiration {
addresses, err := net.InterfaceAddrs()
if err != nil {
return nil, err
}
a.addressesTime = time.Now()
a.interfaceAddresses = addresses
}
return a.interfaceAddresses, nil
}
func (a AddressForLoadBalancerIP) ValidateService(s *v1.Service) error {
if s.Spec.Type != v1.ServiceTypeLoadBalancer || s.Spec.LoadBalancerIP == "" {
return nil
}
ip := net.ParseIP(s.Spec.LoadBalancerIP)
if ip == nil {
return fmt.Errorf("couldn't parse IP '%s' for service %s in %s",
s.Spec.LoadBalancerIP, s.Name, s.Namespace)
}
if a.checkLocalBind {
addrs, err := a.addresses()
if err != nil {
log.Printf("Error obtaining local interface addresses: %s", err)
return nil
}
for _, addr := range addrs {
switch addr := addr.(type) {
case *net.IPNet:
if addr.IP.Equal(ip) {
return nil
}
}
}
return fmt.Errorf("service %s in %s cannot be bound to address %s defined in load balancer IP, skipping it. Please check your configuration!", s.Name, s.Namespace, s.Spec.LoadBalancerIP)
}
return nil
}
func initAddressForLoadBalancerIPCheck() *AddressForLoadBalancerIP {
nonLocalBind, err := sysctl.Get(nonLocalBindSysKey)
if err != nil {
log.Printf("Error reading %s from sysctl: %s, skipping load balancer IP checks", nonLocalBindSysKey, err)
return &AddressForLoadBalancerIP{checkLocalBind: false}
}
return &AddressForLoadBalancerIP{checkLocalBind: nonLocalBind == "0"}
}
func init() {
sanityChecks = append(sanityChecks, initAddressForLoadBalancerIPCheck())
}