/
config.go
234 lines (213 loc) · 9.24 KB
/
config.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// Copyright Istio Authors
//
// 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 config
import (
"encoding/json"
"fmt"
"net"
"net/netip"
"os/user"
"strconv"
"strings"
"time"
"github.com/miekg/dns"
"istio.io/istio/pkg/env"
"istio.io/istio/pkg/log"
netutil "istio.io/istio/pkg/util/net"
"istio.io/istio/tools/istio-iptables/pkg/constants"
)
func DefaultConfig() *Config {
return &Config{
RestoreFormat: true,
ProxyPort: "15001",
InboundCapturePort: "15006",
InboundTunnelPort: "15008",
InboundTProxyMark: "1337",
InboundTProxyRouteTable: "133",
IptablesProbePort: constants.DefaultIptablesProbePortUint,
ProbeTimeout: constants.DefaultProbeTimeout,
OwnerGroupsInclude: constants.OwnerGroupsInclude.DefaultValue,
OwnerGroupsExclude: constants.OwnerGroupsExclude.DefaultValue,
HostIPv4LoopbackCidr: constants.HostIPv4LoopbackCidr.DefaultValue,
}
}
// Command line options
// nolint: maligned
type Config struct {
ProxyPort string `json:"PROXY_PORT"`
InboundCapturePort string `json:"INBOUND_CAPTURE_PORT"`
InboundTunnelPort string `json:"INBOUND_TUNNEL_PORT"`
ProxyUID string `json:"PROXY_UID"`
ProxyGID string `json:"PROXY_GID"`
InboundInterceptionMode string `json:"INBOUND_INTERCEPTION_MODE"`
InboundTProxyMark string `json:"INBOUND_TPROXY_MARK"`
InboundTProxyRouteTable string `json:"INBOUND_TPROXY_ROUTE_TABLE"`
InboundPortsInclude string `json:"INBOUND_PORTS_INCLUDE"`
InboundPortsExclude string `json:"INBOUND_PORTS_EXCLUDE"`
OwnerGroupsInclude string `json:"OUTBOUND_OWNER_GROUPS_INCLUDE"`
OwnerGroupsExclude string `json:"OUTBOUND_OWNER_GROUPS_EXCLUDE"`
OutboundPortsInclude string `json:"OUTBOUND_PORTS_INCLUDE"`
OutboundPortsExclude string `json:"OUTBOUND_PORTS_EXCLUDE"`
OutboundIPRangesInclude string `json:"OUTBOUND_IPRANGES_INCLUDE"`
OutboundIPRangesExclude string `json:"OUTBOUND_IPRANGES_EXCLUDE"`
KubeVirtInterfaces string `json:"KUBE_VIRT_INTERFACES"`
ExcludeInterfaces string `json:"EXCLUDE_INTERFACES"`
IptablesProbePort uint16 `json:"IPTABLES_PROBE_PORT"`
ProbeTimeout time.Duration `json:"PROBE_TIMEOUT"`
DryRun bool `json:"DRY_RUN"`
RestoreFormat bool `json:"RESTORE_FORMAT"`
SkipRuleApply bool `json:"SKIP_RULE_APPLY"`
RunValidation bool `json:"RUN_VALIDATION"`
RedirectDNS bool `json:"REDIRECT_DNS"`
DropInvalid bool `json:"DROP_INVALID"`
CaptureAllDNS bool `json:"CAPTURE_ALL_DNS"`
EnableIPv6 bool `json:"ENABLE_INBOUND_IPV6"`
DNSServersV4 []string `json:"DNS_SERVERS_V4"`
DNSServersV6 []string `json:"DNS_SERVERS_V6"`
NetworkNamespace string `json:"NETWORK_NAMESPACE"`
CNIMode bool `json:"CNI_MODE"`
TraceLogging bool `json:"IPTABLES_TRACE_LOGGING"`
DualStack bool `json:"DUAL_STACK"`
HostIP netip.Addr `json:"HOST_IP"`
HostIPv4LoopbackCidr string `json:"HOST_IPV4_LOOPBACK_CIDR"`
}
func (c *Config) String() string {
output, err := json.MarshalIndent(c, "", "\t")
if err != nil {
log.Errorf("Unable to marshal config object: %v", err)
}
return string(output)
}
func (c *Config) Print() {
var b strings.Builder
b.WriteString(fmt.Sprintf("PROXY_PORT=%s\n", c.ProxyPort))
b.WriteString(fmt.Sprintf("PROXY_INBOUND_CAPTURE_PORT=%s\n", c.InboundCapturePort))
b.WriteString(fmt.Sprintf("PROXY_TUNNEL_PORT=%s\n", c.InboundTunnelPort))
b.WriteString(fmt.Sprintf("PROXY_UID=%s\n", c.ProxyUID))
b.WriteString(fmt.Sprintf("PROXY_GID=%s\n", c.ProxyGID))
b.WriteString(fmt.Sprintf("INBOUND_INTERCEPTION_MODE=%s\n", c.InboundInterceptionMode))
b.WriteString(fmt.Sprintf("INBOUND_TPROXY_MARK=%s\n", c.InboundTProxyMark))
b.WriteString(fmt.Sprintf("INBOUND_TPROXY_ROUTE_TABLE=%s\n", c.InboundTProxyRouteTable))
b.WriteString(fmt.Sprintf("INBOUND_PORTS_INCLUDE=%s\n", c.InboundPortsInclude))
b.WriteString(fmt.Sprintf("INBOUND_PORTS_EXCLUDE=%s\n", c.InboundPortsExclude))
b.WriteString(fmt.Sprintf("OUTBOUND_OWNER_GROUPS_INCLUDE=%s\n", c.OwnerGroupsInclude))
b.WriteString(fmt.Sprintf("OUTBOUND_OWNER_GROUPS_EXCLUDE=%s\n", c.OwnerGroupsExclude))
b.WriteString(fmt.Sprintf("OUTBOUND_IP_RANGES_INCLUDE=%s\n", c.OutboundIPRangesInclude))
b.WriteString(fmt.Sprintf("OUTBOUND_IP_RANGES_EXCLUDE=%s\n", c.OutboundIPRangesExclude))
b.WriteString(fmt.Sprintf("OUTBOUND_PORTS_INCLUDE=%s\n", c.OutboundPortsInclude))
b.WriteString(fmt.Sprintf("OUTBOUND_PORTS_EXCLUDE=%s\n", c.OutboundPortsExclude))
b.WriteString(fmt.Sprintf("KUBE_VIRT_INTERFACES=%s\n", c.KubeVirtInterfaces))
// TODO consider renaming this env var to ENABLE_IPV6 - nothing about it is specific to "INBOUND"
b.WriteString(fmt.Sprintf("ENABLE_INBOUND_IPV6=%t\n", c.EnableIPv6))
// TODO remove this flag - "dual stack" should just mean
// - supports IPv6
// - supports pods with more than one podIP
// The former already has a flag, the latter is something we should do by default and is a bug where we do not
b.WriteString(fmt.Sprintf("DUAL_STACK=%t\n", c.DualStack))
b.WriteString(fmt.Sprintf("DNS_CAPTURE=%t\n", c.RedirectDNS))
b.WriteString(fmt.Sprintf("DROP_INVALID=%t\n", c.DropInvalid))
b.WriteString(fmt.Sprintf("CAPTURE_ALL_DNS=%t\n", c.CaptureAllDNS))
b.WriteString(fmt.Sprintf("DNS_SERVERS=%s,%s\n", c.DNSServersV4, c.DNSServersV6))
b.WriteString(fmt.Sprintf("NETWORK_NAMESPACE=%s\n", c.NetworkNamespace))
b.WriteString(fmt.Sprintf("CNI_MODE=%s\n", strconv.FormatBool(c.CNIMode)))
b.WriteString(fmt.Sprintf("EXCLUDE_INTERFACES=%s\n", c.ExcludeInterfaces))
log.Infof("Istio iptables variables:\n%s", b.String())
}
func (c *Config) Validate() error {
if err := ValidateOwnerGroups(c.OwnerGroupsInclude, c.OwnerGroupsExclude); err != nil {
return err
}
return ValidateIPv4LoopbackCidr(c.HostIPv4LoopbackCidr)
}
var envoyUserVar = env.Register(constants.EnvoyUser, "istio-proxy", "Envoy proxy username")
func (c *Config) FillConfigFromEnvironment() error {
// Fill in env-var only options
c.OwnerGroupsInclude = constants.OwnerGroupsInclude.Get()
c.OwnerGroupsExclude = constants.OwnerGroupsExclude.Get()
c.HostIPv4LoopbackCidr = constants.HostIPv4LoopbackCidr.Get()
// TODO: Make this more configurable, maybe with an allowlist of users to be captured for output instead of a denylist.
if c.ProxyUID == "" {
usr, err := user.Lookup(envoyUserVar.Get())
var userID string
// Default to the UID of ENVOY_USER
if err != nil {
userID = constants.DefaultProxyUID
} else {
userID = usr.Uid
}
c.ProxyUID = userID
}
// For TPROXY as its uid and gid are same.
if c.ProxyGID == "" {
c.ProxyGID = c.ProxyUID
}
// Detect whether IPv6 is enabled by checking if the pod's IP address is IPv4 or IPv6.
// TODO remove this check, it will break with more than one pod IP
hostIP, isIPv6, err := getLocalIP(c.DualStack)
if err != nil {
return err
}
c.HostIP = hostIP
c.EnableIPv6 = isIPv6
// Lookup DNS nameservers. We only do this if DNS is enabled in case of some obscure theoretical
// case where reading /etc/resolv.conf could fail.
// If capture all DNS option is enabled, we don't need to read from the dns resolve conf. All
// traffic to port 53 will be captured.
if c.RedirectDNS && !c.CaptureAllDNS {
dnsConfig, err := dns.ClientConfigFromFile("/etc/resolv.conf")
if err != nil {
return fmt.Errorf("failed to load /etc/resolv.conf: %v", err)
}
c.DNSServersV4, c.DNSServersV6 = netutil.IPsSplitV4V6(dnsConfig.Servers)
}
return nil
}
// mock net.InterfaceAddrs to make its unit test become available
var (
LocalIPAddrs = net.InterfaceAddrs
)
// getLocalIP returns one of the local IP address and it should support IPv6 or not
func getLocalIP(dualStack bool) (netip.Addr, bool, error) {
var isIPv6 bool
var ipAddrs []netip.Addr
addrs, err := LocalIPAddrs()
if err != nil {
return netip.Addr{}, isIPv6, err
}
for _, a := range addrs {
if ipnet, ok := a.(*net.IPNet); ok {
ip := ipnet.IP
ipAddr, ok := netip.AddrFromSlice(ip)
if !ok {
continue
}
// unwrap the IPv4-mapped IPv6 address
unwrapAddr := ipAddr.Unmap()
if !unwrapAddr.IsLoopback() && !unwrapAddr.IsLinkLocalUnicast() && !unwrapAddr.IsLinkLocalMulticast() {
isIPv6 = unwrapAddr.Is6()
ipAddrs = append(ipAddrs, unwrapAddr)
if !dualStack {
return unwrapAddr, isIPv6, nil
}
if isIPv6 {
break
}
}
}
}
if len(ipAddrs) > 0 {
return ipAddrs[0], isIPv6, nil
}
return netip.Addr{}, isIPv6, fmt.Errorf("no valid local IP address found")
}